Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

allocatable result on (sub)module function

zp3
Beginner
757 Views

Hi,

Afaik, since F2003 it should be possible to assign an allocated function result to a deferred lhs. I've tried that on a module function lying in a submodule and it doesn't seem to work. My code looks like:

        !(Interface in module file)
        module function gst_il2p(lm,descn,sb) result(ind)
            implicit none
            integer(GST_IK), intent(in) :: lm(:,:), descn(4), sb
            integer(GST_IK), allocatable :: ind(:)
        end function gst_il2p

        program test_gst
            use gst
            implicit none
            integer(GST_IK), allocatable :: ind(:) 
            ....
            ind=gst_il2p(lm,int([0,50,200,4],GST_IK),1_GST_IK)
            write(*,*) ind(1)
            ....
        end program test_gst

When writing ind(1) I get an access violation, because ind isn't allocated. Nevertheless, the program works perfectly when substituting allocatable with pointer:

        !(Interface in module file)
        module function gst_il2p(lm,descn,sb) result(ind)
            implicit none
            integer(GST_IK), intent(in) :: lm(:,:), descn(4), sb
            integer(GST_IK), pointer :: ind(:)
        end function gst_il2p

        program test_gst
            use gst
            implicit none
            integer(GST_IK), pointer :: ind(:) 
            ....
            ind=>gst_il2p(lm,int([0,50,200,4],GST_IK),1_GST_IK)
            write(*,*) ind(1)
            ....
        end program test_gst

Do I think something wrong here? I should admit that I'm using ifort 16 beta because of the submodule support,..

And an other question, is there generally any difference in the code execution using allocatable or pointer results?

 

Thanks for advice,

zp3

0 Kudos
1 Solution
Steven_L_Intel1
Employee
757 Views

You need to use -standard-semantics to get the F2003 automatic reallocation feature (or -assume realloc_lhs for just that part.) We're considering changing the default for this in a future release.

View solution in original post

0 Kudos
5 Replies
Steven_L_Intel1
Employee
758 Views

You need to use -standard-semantics to get the F2003 automatic reallocation feature (or -assume realloc_lhs for just that part.) We're considering changing the default for this in a future release.

0 Kudos
zp3
Beginner
757 Views

Thank you Dr. Fortran!

Already tested, now it works like a charm!

Considering speed, are there any perfomance differences/losses compared to an analogous subroutine solution?

0 Kudos
Steven_L_Intel1
Employee
757 Views

With the function result and assignment you would have an additional allocate/deallocate/copy. I wouldn't worry about this unless performance measurement shows it to be a problem.

You don't show the actual function call - I'm concerned that the pointer version would end up with a pointer to no-longer-defined storage.

0 Kudos
zp3
Beginner
757 Views

I won't bother you with my ignorance, I'm very new to fortran...

Maybe it will, I don't know how fortran handles the function return. From my opinion the pointer assignment (=>) should assign the address of the storage allocated in the function, to the pointer value defined in the caller; So no copy (beside the copy of the address itself) should be required - the storage should be deallocable in the caller.

The relevant code snipplet is quite simple:

    module procedure gst_il2p
        implicit none
        ... 
        allocate(ind(size(lm,2)))
        ...
    end procedure gst_il2p

I could also use an automatic array in this case, but then i would have to use a intrinsic function in the interface part and i by myself consider that a bit unclean programming. And I believe anyway that there aren't any big performance improvements...

Because of the fact that the returning array size could get quite big (1e7+), I'll probably consider to change the procedure to a subroutine.

0 Kudos
Steven_L_Intel1
Employee
757 Views

Ok - I took another look. In your pointer version, the function result is a pointer that you allocate in the function. In that case this works fine - ind is now associated with the data that was allocated in the function. The pointer result disappears at the end of the statement but by then you don't need it. You do need to make sure that the storage gets deallocated when you don't need it anymore.

0 Kudos
Reply