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

Effect of temp variables in subroutine on speed and memory

Wee_Beng_T_
Beginner
685 Views

Hi,

I've quite a few subroutines for my cfd code. In most subroutines, I need them to accept input variables such as arrays. One e.g. is

subroutine area(xy,ab,cd,ans)

real(8), intent(in) :: xy(size_x,size_y),ab(size_x,size_y),cd(size_x,size_y)

real(8), intent(out) :: ans(size_x,size_y)

ans=ab*cd+xy

end subroutine area

Another way to do this is:

subroutine area

ans=ab*cd+xy

end subroutine area

where ans,ab,cd,xy are global variables.

If I do it by the 2nd method, will I save speed and memory? I've quite a few of the 1sttypeof subroutines andI wonder if I am wasting memory and slowing the code down. Of cos, for the 1st type, I could input different variables such as xy2,xy3 and it makes my code more compact.

Thank you.

 

0 Kudos
5 Replies
TimP
Honored Contributor III
685 Views
You aren't being specific enough; I might guess that "global variable" meant in a COMMON block, but you didn't show it. Nor did you indicate whether you got any information from the opt_report (by default, vectorization is reported).
In certain cases, where you pass an array section as an assumed size argument, there is more overhead going on, either to make a local copy at the call site, or for calculations at the top of the subroutine, associated with the size of the array.
Module variables ought to do as well as COMMON variables, but I don't think most people would call them "global."
0 Kudos
Wee_Beng_T_
Beginner
685 Views

Sorry about that. By global variables, I mean I have a global.f90 which has:

module global_data

implicit none

save

real(8) :: ab(size_x,size_y) etc..

real(8), allocatable:: xy(:,:) etc...

end module global_data

So if I'm going by the 1st mtd, I will be wasting memory and slowing things down. Is that what you mean?

You also mention "pass an array section as an assumed size argument, there is more overhead going on". So is there a better way? And at the same time enable me to input different variables.

If not, should I try to minimize the use of the 1st mtd and and use the 2nd mtd whenever possible?

Btw, I've not used -opt-report before. I just tried it a while ago. Don't really understand but what does e.g. 5/10 (50%) mean? Just to add that I'm using -O3 and -ip or -ipo for my compilation.

Thank you very much.

0 Kudos
TimP
Honored Contributor III
685 Views
You could use ifort -check arg_temp_created to get diagnostics for cases where there is a temporary copy of an argument. If that doesn't happen, you won't lose much, in most cases, by passing the arrays as arguments. However, if you have already declared them as module arrays, that seems the logical way to make them visible in the subroutine.
0 Kudos
Wee_Beng_T_
Beginner
685 Views

Hi,

so you are saying that by passing arrays as argument as in the 1st mtd, it doesn't really make much difference?

I haven't also posted in a CFD forum and one user said that:

It doesn't take up any extra memory since the arrays are not been reallocated/duplicated. Basically the first method simply passes the memory address of where the arrays are stored and then performs the calculation on that area of memory (just as if the arrays were global).

Is he/she correct?

Thank you.

0 Kudos
Steven_L_Intel1
Employee
685 Views
That is generally correct. The exception is when you pass a non-contiguous array slice or pointer to a procedure for which there is no explicit interface saying that it accepts an assumed-shape array (dimension(:)). In the exception case, the switch Tim mentioned can alert you when the compiler has to make a copy.
0 Kudos
Reply