In the code I am attempting to port to OpenMP, I have a parallelized loop nested in an outer loop. Depending on the iteration of the outer loop, I would like a particular array to be either shared
or reduction(+)
. Is there a way to do this in Fortran?
Here's a mockup of what I want:
do i = 1, 2
!$omp if(i.eq.1) parallel do reduction(+:foo)
!$omp if(i.eq.2) parallel do shared(foo)
do j = 1,j_max
work on foo
enddo
!$omp end parallel
enddo
The discussion in openMP conditional pragma "if else" suggests that scheduling cannot be modified during execution. Is that also the case for shared/private/reduction/etc.?
One obvious course of action is to create foo_1 (reduction:+) and foo_2 (shared), copy foo_1 to foo_2 after the first iteration on i, and then have if statements within the loop over j to refer to the proper array. But that's not terribly elegant. I'm hoping there's a better/cleverer/cleaner way to do this.
Edit: for the unimaginative, here's the pseudocode version of my alternative
do i = 1, 2
!$omp parallel do reduction(+:foo_1), shared(foo_2)
do j = 1,j_max
if( i .eq. 1 ) then
work on foo_1
else
work on foo_2
endif
enddo
!$omp end parallel
foo_2 = foo_1
enddo