Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which openMP pragma do I use for a blocking for loop?

Tags:

c++

openmp

My algorithm (solving Poisson's equation) is completely parallelizable--provided that all the threads sync at the end of each iteration.

Function f, fNext;
init(f);
#pragma omp parallel
for(int step=0; step<maxITER; step++) {
#pragma omp for
   for(int i=0; i<N; i++) {
      for(int j=0; j<N; j++) {
         fNext(i,j) = someOperator( f(i,j) );
      }
   }
   f = fNext;
}//Threads must synchronize here

Does #pragma omp for ensure thread synchronization before continuing to the next iteration?

like image 959
Nick Vence Avatar asked Dec 31 '25 18:12

Nick Vence


1 Answers

Yes. From the OpenMP Spec (eg, v 3.1, but this has been in since the beginning), under "worksharing constructs:"

There is an implicit barrier at the end of a loop construct unless a nowait clause is specified.

That is, at the end of the for loop, unless you do something like #pragma omp for nowait, there is an implied barrier so that no thread will execute f=fNext until all threads are done the for loop.

like image 77
Jonathan Dursi Avatar answered Jan 02 '26 10:01

Jonathan Dursi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!