Reputation: 10795
I have a Fortran code. Code contains two loops. I want to parallelize ONLY inner loop. Here is my code:
!$OMP PARALLEL PRIVATE(i,j)
do i=1, N
!$OMP PARALLEL DO
do j=1, M
! do some calculations
end do
!$OMP END PARALLEL DO
end do
!$OMP END PARALLEL
Is parallelization correct? I am not sure, whether it is needed to put !$OMP PARALLEL PRIVATE(i,j)
at the beginning? Should I omit it and declare just PRIVATE(i)
before the second loop? Anyways, I am a little bit confused, please explain what is correct behaviour.
Upvotes: 1
Views: 753
Reputation: 18098
Why is i
private? Isn't it required for all threads? It mustn't change during the whole of the inner loop (since it is the loop counter of the outer one). If it is declared private, it is undefined at the beginning of the parallel section unless firstprivate
is used.
OpenMP in Fortran notices that j
is the loop counter of the parallelized loop, so it is implicitly private. So there is no need to declare it explicitly.
The next thing is that you should avoid nested OpenMP section (i.e. a second !$OMP PARALLEL
)
Because I like to be explicit, I would write it as
!$OMP PARALLEL PRIVATE(j) SHARED(i)
do i=1, N
!$OMP DO
do j=1, M
! do some calculations
end do
!$OMP END DO
end do
!$OMP END PARALLEL
Upvotes: 1