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

what's the advantages of fortran pointer over allocatable

Xj_Kong
Novice
1,516 Views

Recently, I am studying the efficiency difference in using pointer and allocatable variables. 

I have tested the character type, but I could not prove that the allocatable is more efficient than the pointer as per http://software.intel.com/en-us/forums/topic/276711

What I could get is opposite. Has anybody experienced the same?

0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,516 Views

I very much doubt you'd find POINTER has an advantage over ALLOCATABLE. ALLOCATABLE has the advantage that the compiler knows the data is contiguous, allowing it to skip making temporary copies of the data.  ALLOCATABLE also makes it very difficult if not impossible to leak memory. Use ALLOCATABLE unless you must use pointer assignment.

0 Kudos
Xj_Kong
Novice
1,516 Views

It would be nice if more examples can be proving...

0 Kudos
Xj_Kong
Novice
1,516 Views

I doubt myself also. But I tested the example on two machines, one desktop and one laptop and I got the same results.

Please help with further testing.

Thanks, Kong

0 Kudos
Andrew_Smith
Valued Contributor I
1,516 Views

My results in win32 release mode are:

Allocatable string Running Time was:  0.1133607     [Min]

Pointer string Running Time was:  5.6420636E-02 [Min]

I re-implemented the reallocate function as a subroutine and got: Allocatable string Running Time was:  5.7720371E-02 [Min].

[fortran]

subroutine reallocate(P,N)

CHARACTER(len=:),allocatable,intent(inout)::P

INTEGER,INTENT(IN)::N

CHARACTER(len=:),allocatable::R

! reallocate=repeat(' ',N) ! should avoid repeat function which consumes too much RAM

allocate(character(n)::R); R(:n)=' ';

IF(ALLOCATED(P))then

   NOLD=len(P)

   R(1:MIN(NOLD,N))=P(1:MIN(NOLD,N))

   DEALLOCATE(P)

end if

call move_alloc(R, P)

END subroutine[/fortran]

0 Kudos
IanH
Honored Contributor III
1,516 Views

Without measuring to confirm, I would have though the IO in the code would have dominated over any memory management strategy.

0 Kudos
Xj_Kong
Novice
1,516 Views

Thanks a lot. So the finding is that the function is much less effecient than the subroutine. Unfortunately, for years, I have converted subroutines into functions to simulate c style...

0 Kudos
Xj_Kong
Novice
1,516 Views

I agree with IanH, but could we have some substantially better alternatives?

0 Kudos
Reply