|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
Emanuele
Joined: 21 Oct 2009 Posts: 77 Location: Bologna (Italy)
|
Posted: Sat Nov 15, 2014 4:10 pm Post subject: Passing allocatable arrays |
|
|
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 |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1888
|
Posted: Sat Nov 15, 2014 4:49 pm Post subject: |
|
|
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 |
|
|
Emanuele
Joined: 21 Oct 2009 Posts: 77 Location: Bologna (Italy)
|
Posted: Sat Nov 15, 2014 5:15 pm Post subject: |
|
|
Thank you very much. Your answer is a great comfort to me! Well, I will try to work around… |
|
Back to top |
|
|
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Sat Nov 15, 2014 10:04 pm Post subject: Re: |
|
|
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 |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2556 Location: Sydney
|
Posted: Sun Nov 16, 2014 1:38 am Post subject: |
|
|
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 |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1888
|
Posted: Sun Nov 16, 2014 3:31 am Post subject: |
|
|
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 |
|
|
Emanuele
Joined: 21 Oct 2009 Posts: 77 Location: Bologna (Italy)
|
Posted: Mon Nov 17, 2014 11:46 am Post subject: |
|
|
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 |
|
|
Emanuele
Joined: 21 Oct 2009 Posts: 77 Location: Bologna (Italy)
|
Posted: Mon Nov 17, 2014 12:36 pm Post subject: |
|
|
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 |
|
|
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Mon Nov 17, 2014 8:54 pm Post subject: |
|
|
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 |
|
|
Emanuele
Joined: 21 Oct 2009 Posts: 77 Location: Bologna (Italy)
|
Posted: Mon Nov 17, 2014 10:23 pm Post subject: |
|
|
Ok, thanks. I am not very familiar with pointers! |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2556 Location: Sydney
|
Posted: Wed Mar 09, 2016 5:12 am Post subject: |
|
|
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 |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7931 Location: Salford, UK
|
Posted: Wed Mar 09, 2016 8:44 am Post subject: |
|
|
No. This is on the wish list but the current port to 64 bits has priority. |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2556 Location: Sydney
|
Posted: Wed Sep 13, 2017 4:58 am Post subject: |
|
|
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 |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7931 Location: Salford, UK
|
Posted: Wed Sep 13, 2017 7:30 am Post subject: |
|
|
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 |
|
|
|
|
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
|