Allocatable arrays in MODULE vs. passing them as arguments ?

Allocatable arrays in MODULE vs. passing them as arguments ?

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'
integer(4) :: nx, ny, nz
COMMON /dimensionsCommon/ nx, ny, nz

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

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

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?

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Retired 12/31/2016

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?

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.

Retired 12/31/2016

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?



No - that's quite simple.

Retired 12/31/2016

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

Leave a Comment

Please sign in to add a comment. Not a member? Join today