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

Allocatable arrays in MODULE vs. passing them as arguments ?

jirina
New Contributor I
882 Views

I would like to ask a question about allocatable arrays. What I currently have in my application is that 3D arrays are allocated in the main program and they are passed to subroutines as arguments. A simplified example would be:

! contents of dimensions.inc'
integer(4) :: nx, ny, nz
COMMON /dimensionsCommon/ nx, ny, nz

! main program
program Test
include 'dimensions.inc'
real(8), dimension(:,:,:), allocatable :: Array
allocate( Array(ny,ny,nz), STAT=allocationStatus )
call initializeArray( Array )
! ...
end

! another file
subroutine initializeArray( Array )
include 'dimensions.inc'
real(8) :: Array(nx,ny,nz)
! ...
return
end

My application is far more complicated - tens of 3D arrays are passed this way to subroutines that pass them to other subroutines and functions. I am considering having the allocatable arrays in modules and using the command use instead of passing the arrays as arguments, but I would like to ask first of all whether such a change (in a huge code) makes any sense. Does any of the approaches have a significant impact on the performance and the application speed? It is possible to say which approach is the best from the performance point of view?

0 Kudos
6 Replies
Steven_L_Intel1
Employee
882 Views

For performance, putting the variables in a module is slightly better because there is non-zero time taken in pushing the arguments onto the stack. Do whatever makes the most sense for your application. In general, though, avoid global variables as they inhibit parallelization.

0 Kudos
jirina
New Contributor I
882 Views

Thank you for you clear answer and let me ask one more question - I use OpenMP paralellization a lot, so did you mean by your last statement that if I have allocatable arrays in a module and work with these arrays inside a parallel block then this block would not be run parallel? Put another way - if I change the example from above and the allocatable array Array is in a module that is used by a subroutine (instead of passing Array as an argument), and if the subroutine contains a parallel region that works with Array, would that be a problem?

0 Kudos
Steven_L_Intel1
Employee
882 Views

It's really more a problem when you're using global variables to keep track of work done in multiple threads. For array operations that are parallelized, it wouldn't be an issue.

0 Kudos
nvaneck
New Contributor I
882 Views

ISteve, I understand the savings from not pushing arguments on the stack, but is there a hit from addressing the module variables in a different way, in space or time?

 

 

0 Kudos
Steven_L_Intel1
Employee
882 Views

No - that's quite simple.

0 Kudos
jimdempseyatthecove
Honored Contributor III
882 Views

When an array in a module is dimensioned using parameters as opposed to an array coming in as an argument with unknown (to the compiler) dimensions, then the space/time access to the module array can be smaller/faster (at least no worse). When the module array is allocatable then you would see no/little difference (other than the time for pushing the address of the array descriptor onto stack or into register). In the case of the allocatable array in module, the address of the descriptor can be compiled in as a base address (immediate value), thus potentially a) eliminating a load of the array descriptor address from stack, or b) releives requirement for array descriptor address brom being held/cached in register. Note, simple test programs will likely not show a difference.

Jim Dempsey

0 Kudos
Reply