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

How are arrays passed to subroutines

ingo_berg
Beginner
804 Views
Hi,

I have a question regarding the passing of arrays to subroutines. I have a subroutine like:

pure subroutine GetPertVar(press, phi)

implicit none

real(DP), intent(out) :: press(10,10), sat(phCOUNT)
real(DP), intent(out) :: phi(10)

.....

end souroutine

This function will be called verry often so I'd like to know how the arrays will be handed over to the calling routine.

My guess would be that intent(in) is similar to a const reference in C++ whilst intent(out) requires the copying of an array. I intent(inout) is similar to a reference in C++.

This is my guess is it correct?

Regards,
Ingo
0 Kudos
4 Replies
Steven_L_Intel1
Employee
804 Views
You also have to look at the caller in order to determine how arrays are passed.

Under most circumstances, arrays are passed by reference (address), with no copy being done. This is regardless of INTENT. INTENT is a promise of what the called routine will do.

If the routine declares the array with deferred shape (:), this requires that the caller can see an explicit interface and the array is passed by descriptor - no copy is made.

The case where a copy is made is if the caller cannot see an interface specifying deferred-shape and it cannot prove that the array being passed in is contiguous. The compiler does various compile and run-time checks to avoid making copies, but it does not catch all of the cases, so sometimes a copy is made unnecessarily. You can ask the compiler to alert you of this with /check:arg_temp_created.
0 Kudos
TimP
Honored Contributor III
804 Views
I don't know of any compilers which change the implementation in accordance with intent(). Intel Fortran goes with the majority in passing arguments by reference. As you say, intent(in) is analogous to C or C++ const, in that the compiler should object to any assignment to the array within the subroutine.
Whether there is a copy on the calling side would depend on the code there. If you want to minimize time spent passing arguments, you would want to set up your source for interprocedural optimization, and make it such that copy is not required.
0 Kudos
ingo_berg
Beginner
804 Views
Thank you for the hint with the compile option. Now i can see some warnings. This should help to avoid bottlenecks.
0 Kudos
Intel_C_Intel
Employee
804 Views
Hello. Yes interprocedural optimization (IPO) is good at inlining functions. There are two options: /Qip - interprocedural optimization during compile time for functions within one file. /Qipo - interprocedural optimization during link for functions across many files. The latter option may increase link time and the former option requires that you must put the function to be inlined in the file where it is being called. Sometimes /Qip may be better and sometimes /Qipo may be better. The best is to check both. Regards, Lars Petter Endresen
0 Kudos
Reply