0

I have three nested loops. I want to parallelize the middle loop as such:

  do a = 1,amax
    !$omp parallel do private(c)
    do b = 1,bmax
      do c = 1,cmax
        call mysubroutine(b,c)
      end do
    end do
    !$omp end parallel do
  end do

However this creates a problem, in that for every iteration of the a loop, threads are spawned, run through the inner loops, then terminate. I assume this is causing an excessive amount of overhead, as the inner loops do not take long to execute (~ 10^-4 s). So I would like to spawn the threads only once. How can I spawn the threads before starting the a loop while still executing the a loop sequentially? Due to the nature of the code, each iteration of the a loop must be complete before the next can be executed. For example, clearly this would not work:

  !$omp parallel private(c)
  do a = 1,amax
    !$omp do 
    do b = 1,bmax
      do c = 1,cmax
        call mysubroutine(b,c)
      end do
    end do
    !$omp end do
  end do
  !$omp end parallel

because all of the threads will attempt to execute the a loop. Any help appreciated.

2 Answers 2

1

"For example, clearly this would not work"

That is not only not clear, that is completely incorrect. The code you show is exactly what you should do (better with private(a)).

"because all of the threads will attempt to execute the a loop"

Of course they will and they have to! All of them have to execute it if they are supposed to take part in worksharing in the omp do inner loop! If they don't execute it, they simply won't be there to help with inner loop.

A different remark: you may benefit from the collapse(2) clause for the omp do nested loop.

2
  • Thanks, it seems I have a misunderstanding of the way in which OMP is handling the a loop inside the parallel environment. My thought was that every thread would attempt to execute the a loop separately, i.e. that the loop would be duplicated. This is apparently not the case. I still do not understand how it is being handled. My thought is that the a iterator ought to be a public variable, not a private one, since I want the loop to be handled exactly as in the first case, simply without the excessive respawning of threads.
    – Kai
    Sep 2, 2018 at 1:00
  • They are executed separately. But then you have the omp do inside to actually do tho worksharing. Sep 2, 2018 at 7:03
0

A good way to assert that "this is causing an excessive amount of overhead" is to evaluate the scaling using different number of threads.

1s is a long time, respawing threads isn't so costly…

2
  • I had meant to say <<1s. In fact it's something more like 10^-4s. In any case I misunderstood how omp handles non-omp do loops. After eliminating the constant respawning of threads my code is roughly 10% faster
    – Kai
    Sep 2, 2018 at 0:56
  • Any reasonable OpenMP runtime maintains a thread-pool, so the original assertion that threads are re-spawned is wrong anyway! (Go look at the code for the LLVM or GCC OpenMP runtimes if you doubt this, or, on Linux, look at strace and see how many clone calls there are).
    – Jim Cownie
    Sep 3, 2018 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.