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 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?

7 帖子 / 0 全新
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项

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.

Steve - Intel Developer Support

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.

Steve - Intel Developer Support

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.

Steve - Intel Developer Support

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

www.quickthreadprogramming.com

发表评论

登录添加评论。还不是成员?立即加入