Silverfrost Forums

Welcome to our forums

Passing allocatable arrays

15 Nov 2014 15:10GMT #15075

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:

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?

15 Nov 2014 15:49GMT (Edited: 15 Nov 2014 16:24GMT) #15076

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.

15 Nov 2014 16:15GMT #15077

Thank you very much. Your answer is a great comfort to me! Well, I will try to work around…

15 Nov 2014 21:04GMT #15078

You can use dummy arrays with the POINTER attribute to workaround this issue in Fortran 95.

16 Nov 2014 00:38GMT #15079

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

16 Nov 2014 02:31GMT #15080

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.

17 Nov 2014 10:46GMT #15086

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!

17 Nov 2014 11:36GMT #15087

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

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:

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.

17 Nov 2014 19:54GMT #15091

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).

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.

17 Nov 2014 21:23GMT #15092

Ok, thanks. I am not very familiar with pointers!

9 Mar 2016 04:12GMT #17294

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

9 Mar 2016 07:44GMT #17295

No. This is on the wish list but the current port to 64 bits has priority.

13 Sep 2017 03:58GMT #20225

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

13 Sep 2017 06:30GMT #20227

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.

Please login to reply.