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

Passing a contiguous allocatable array section to a explicit shape array

OP1
New Contributor III
743 Views

Is passing a contiguous array section of an allocatable array to a subroutine where the corresponding dummy argument is an explicit shape array efficient? In other words, is a temporary array created?

Consider the following example:

PROGRAM PROG
INTEGER(4) N,P

REAL(8),ALLOCATABLE :: M(:,:)
! Allocate and initialize M(N,P)
CALL SUB(M(:,1),N)

END PROGRAM PROG

SUBROUTINE SUB(A,SIZE)
INTEGER(4) SIZE
REAL(8) A(SIZE)
! Do some work here...
END SUBROUTINE

Is this efficient? Is the array passed by address or by value in SUB (in the later case a copy of the array section would need to be done I suppose).

How does the compiler recognize that the data (the array section) is contiguous?

Thanks,

Olivier

0 Kudos
3 Replies
Steven_L_Intel1
Employee
743 Views
The compiler does a run-time test and creates the copy only if needed. To test this, add the /check:arg_temp_created option (Run-Time - Check for actual arguments using temporary storage) and see if you get a run-time warning when the call is made.
0 Kudos
jimdempseyatthecove
Honored Contributor III
743 Views

Olivier,

Consider using

CALL SUB(M(:,1), size(M(:,1)))

This reduces potential for error in the event the allocation is changed without changing N.

The is nothing wrong with using the N as you had before...
as long as the N isn't reused with different values for different arrays.

The above syntax should be within a few memory cycles of using N.

Jim Dempsey

0 Kudos
OP1
New Contributor III
743 Views

Thank you Jim and Steve for your help. I found the run-time check solution proposed by Steve a very valuable tool.

Olivier

0 Kudos
Reply