- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for the hint with the compile option. Now i can see some warnings. This should help to avoid bottlenecks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page