forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing allocatable arrays

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Emanuele



Joined: 21 Oct 2009
Posts: 77
Location: Bologna (Italy)

PostPosted: Sat Nov 15, 2014 4:10 pm    Post subject: Passing allocatable arrays Reply with quote

In a program I have an allocatable array but I would like to allocate it in a subroutine. I tried by using a module and by an interface statement, but the compiler always returns an error ("a dummy argument cannot be allocatable").

This is a minimal code to reproduce my problem:


Code:
subroutine alloca(aaa)
    real, allocatable, intent(inout):: aaa(:,:)
   
    allocate(aaa(5,5))
    aaa(1,1) = 4.5
end subroutine alloca

program prova
   
    interface
        subroutine alloca(aaa)
        real, allocatable, intent(inout):: aaa(:,:)
        end subroutine alloca
    end interface

    real, allocatable:: bbb(:,:)
    call alloca(bbb)

    write(*,*) bbb(1,1)
end program prova


Where am I wrong?
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Sat Nov 15, 2014 4:49 pm    Post subject: Reply with quote

You are not doing anything wrong (considering that this is the year 2014), but allocatable subprogram arguments were not allowed in Fortran 95. They were added in TR 15581 by the Fortran Standards committee (around 1999), and many compiler vendors implemented the TR because customers asked for this feature. Absoft added this capability rather late, and FTN95 has not yet done so. The contents of the TR were merged into F2003 when that appeared. The TR is available at ftp://ftp.nag.co.uk/sc22wg5/N1351-N1400/N1379.pdf .

If you really need the feature, you will have to use a different compiler, or see if you can adapt your code so that it does not use this feature. Silverfrost/Salford are a small company, and probably do not have the personnel to implement all the new features in Fortran 2003 and 2008. However, they have a great product in FTN95 and their compiler is among the best for development and debugging. Your request for TR 15581 features will be noticed, and compared against other requests to assign priority. Perhaps they will comment about this.


Last edited by mecej4 on Sat Nov 15, 2014 5:24 pm; edited 3 times in total
Back to top
View user's profile Send private message
Emanuele



Joined: 21 Oct 2009
Posts: 77
Location: Bologna (Italy)

PostPosted: Sat Nov 15, 2014 5:15 pm    Post subject: Reply with quote

Thank you very much. Your answer is a great comfort to me! Well, I will try to work around…
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Sat Nov 15, 2014 10:04 pm    Post subject: Re: Reply with quote

You can use dummy arrays with the POINTER attribute to workaround this issue in Fortran 95.
_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Sun Nov 16, 2014 1:38 am    Post subject: Reply with quote

You can also place the allocatable arrays in a module and then manage them via subroutines that USE the module.
This provides a similar functionality for most types of uses, although there is less flexibility around using them as subroutine arguments if you choose to do a resize (deallocate then allocate).

John
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Sun Nov 16, 2014 3:31 am    Post subject: Reply with quote

Emanuele, there is one point which perhaps should be clarified. The issues discussed in this thread come into play only if the called subroutine needs to change the allocation status of one or more of its dummy arguments, i.e., to allocate an unallocated argument, to deallocate a previously allocated argument, or to reallocate with a different size.

If none of these features are needed, there is no need to declare the dummy argument as allocatable; the caller can allocate the array and call the subroutine in the same way that it would if the array had been statically allocated (i.e, with an explicit dimension declaration) by the caller or in another subprogram higher up in the call chain. The dummy argument can be declared to be either assumed size or assumed shape, as desired. In this case, FTN95 is perfectly adequate.
Back to top
View user's profile Send private message
Emanuele



Joined: 21 Oct 2009
Posts: 77
Location: Bologna (Italy)

PostPosted: Mon Nov 17, 2014 11:46 am    Post subject: Reply with quote

Thanks to everybody for suggestions.

Mecej, no doubt that FTN95 is a great product and I am not thinking of changing compiler.
Regarding your final clarification, you are right, changing the allocation status in the subroutine is not unavoidable, but it would make my life really easier!
Back to top
View user's profile Send private message
Emanuele



Joined: 21 Oct 2009
Posts: 77
Location: Bologna (Italy)

PostPosted: Mon Nov 17, 2014 12:36 pm    Post subject: Reply with quote

So, if I understand correctly, this is a possible work around following David’s suggestion:

Code:
program prova
    real, allocatable, target:: aaa(:,:)
    real, pointer:: pa(:,:)

    interface
        subroutine alloca(pa)
            real, pointer::pa(:,:)
        end subroutine alloca
    end interface

    pa=>aaa
    call alloca(pa)

    write(*,*) pa(1,1)
end program prova


subroutine alloca(pa)
    real, pointer::pa(:,:)

    allocate(pa(5,5))
    pa(1,1) = 4.5
end subroutine alloca


Instead, this is another possible solution following John’s suggestion:

Code:
module mio
    real, allocatable:: aaa(:,:)
   
end module

program prova
    use mio
   
    call alloca()

    write(*,*) aaa(1,1)
end program prova

subroutine alloca()
    use mio

    allocate(aaa(5,5))
    aaa(1,1) = 4.5
end subroutine alloca


In any case, implementation of TR would make the code more simple and flexible.
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Mon Nov 17, 2014 8:54 pm    Post subject: Reply with quote

This is the easiest use of POINTER that I had in mind. Just change allocatable to pointer and remove the intents (Intent isn't allowed on a pointer dummy argument in Fortran 95).

Code:

subroutine alloca(aaa)
    real, pointer :: aaa(:,:)
   
    allocate(aaa(5,5))
    aaa(1,1) = 4.5
end subroutine alloca

program prova
   
    interface
        subroutine alloca(aaa)
        real, pointer:: aaa(:,:)
        end subroutine alloca
    end interface

    real, pointer :: bbb(:,:)
    call alloca(bbb)

    write(*,*) bbb(1,1)
end program prova


It is essentially the same code as your first example.
_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
Emanuele



Joined: 21 Oct 2009
Posts: 77
Location: Bologna (Italy)

PostPosted: Mon Nov 17, 2014 10:23 pm    Post subject: Reply with quote

Ok, thanks. I am not very familiar with pointers!
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Wed Mar 09, 2016 5:12 am    Post subject: Reply with quote

Paul,

Has there been any review of TR-15581: Enhanced Data Type Facilities ?
Was this supplement ever considered to be incorporated into Silverfrost FTN95 ?
I have been reviewing an easy way of resizing multiple arrays or allocating arrays where their size is not initially known and this approach can assist.

John
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Wed Mar 09, 2016 8:44 am    Post subject: Reply with quote

No. This is on the wish list but the current port to 64 bits has priority.
Back to top
View user's profile Send private message AIM Address
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Wed Sep 13, 2017 4:58 am    Post subject: Reply with quote

Paul,

Has there been any further review of TR 15581:2001, especially Allocatable Arrays as Dummy Arguments.
This would be an extension to FTN95 which would improve functionality, but would require some work on the meaning of dummy arguments.

John
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Wed Sep 13, 2017 7:30 am    Post subject: Reply with quote

John

No there has been no work on this. But now that 64 bit FTN95 is beginning to settle down there is a possibility that we may be able to move it up the wish list.
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group