I have a question about Fortran-OpenMP and allocatable arrays. It's simple: Where will the space be allocated? If I have something like
!$omp parallel default(shared) private(arr)
!$omp critical
allocate( arr(BIGNUMBER) )
!$omp end critical
!do calculations with many arr accesses
!$omp critical
deallocate( arr )
!$omp end critical
!$omp end parallel
will the space be allocated on the stack, or the heap? If it's on the heap, is there a difference between the code above and something like this
allocate( arr(BIGNUMBER, nThread) )
!$omp parallel default(shared) private(localArr)
iThread = omp_get_thread_num()
localArr => arr(:, iThread)
!do calculations with many localArr accesses
!$omp end parallel
deallocate( arr )
- In the first code, there are two critical regions. I would assume, that they would slow the execution down and not scale very well. (I'm actually not sure if I could just leave them out, because the allocate is thread-save?) But if the array were allocated on the stack, then it should be faster, because of faster access.
- In the second code I am sure to have the array on the heap, which is slower access. But if the array in the first code is allocated on the heap as well, then I'll save the critical reagions + it's only one allocate/deallocate. Should be faster?
- Does the size of the array play any roll in this?
- If it were to be allocated on the heap, is there a way to force an allocation on the stack?
The short question is basically: Which would seem to be the optimal solution for the problem?
localArr => arr...but this meansarris shared among the threads. I think you don't want that. The pointerlocalArrwill have a copy for each thread, but the contents pointed to will be shared. Changes in one thread to the contents of the array will be reflected in the other threads. - Jason