allocate array

allocate array

In a parallel region, there are several ALLOCATE statements, the questions is do we need to enclosethese statementswith OMP Critical directives explicitly,without the Critical directives, arethe statementsexecuted in a critical session?

I have seen some strange errors with the ALLOCATE function, sometimes the functions return the error of #41, insufficient virtual memory, though the allocated space is not big.

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

If you're allocating a shared data region, and can't do it outside the parallel region, I suppose (if OpenMP) you would use omp single. I'd be concerned about critical being slow. But I'm probably not answering your question.
If allocating a private data region, I would think you could let each thread do its own allocation.

Thanks for your reply. I am allocating a private data region, in particular, the data region for allocatable arrays is allocated in the routines that are called by the statements within the parallel region, they are temporary arrays in the routines. I agree with you that each thread does its own allocation, and each thread should have its own copies of these allocatalbe variables, while thread data sharing detection monitor detects the variables being accessed by multiple threads. I understand that these messages do not indicate errors, but sometimes the program has trouble allocating data space for these allocatable arrays due to insufficient virtual memory problem.

If the allocation (or other) is once-only you can also use something like this

if(.not. allocated(YourArray)) then
!$omp critical(critical_allocate_YourArray)
if(.not. allocated(YourArray)) then
allocate(YourArray(YourSize(s)))
endif
!$omp end critical(critical_allocate_YourArray)
do while((.not. allocated(YourArray))
call sleepqq(0)
end do
endif

The above avoids a barrier when it is not needed.

Jim Dempsey

www.quickthreadprogramming.com

Because these routines are called N times, where N is the number of Do loop iterations, these arrays will be allocated and deallocated N times, I would like to avoid using Critical directive. Since they are just local arrays to the routines, each thread should execute its own copies of the routines and do its own allocation. The program should work just fine without defining these allocate statements in critical region, why the programs always crash when trying to allocate the arrays?

When the arrays are large (.gt. 100KB), you should not allocate them off the stack. Stack space is a limited resource on some operating systems. While you can specify largerstack size, the heap or static arrays are more suitable alternatives. Note, the sum total of static data is limited to 2GB.

You have a valid concern.

For shared arrays, consider making the large shared array SAVE'd (and allocate it once). The code I provided earlier would perform the allocation once, enter critical section once by the allocating threadand potentially enter the critical section once by the other threads. Subsequent to the first call the critical section is bypassed.

As (?) TimP pointed out, you could "lift" the allocation outside the parallel region then pass in the array by reference on the CALL's issued within the parallel region.

How do you multi-thread and not allocate large private arrays N times when calling a subroutine N times?

A way to do this is to use(static) thread private allocatable arrays

IOW each thread has its own thread private array descriptor that they allocate from the heap once.

Jim Dempsey

www.quickthreadprogramming.com

Leave a Comment

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