1

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.

0

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

As I am new to OpenMP I did not know about avoiding nested OpenMP sections. Thanks for great explanation!
Maybe you could take a look at this tutorial...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.