Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an `omp parallel for` with synchronization (`barrier`) of all threads in the middle with OpenMP

Tags:

openmp

I have two functions, do_step_one(i) and do_step_two(i), for i from 0 to N-1.

Currently, I have this (sequential) code:

for(unsigned int i=0; i<N; i++) {
     do_step_one(i);
}

for(unsigned int i=0; i<N; i++) {
     do_step_two(i);
}

Each call of do_step_one() and do_step2() can be done in any order and in parallel, but any do_step_two() needs the end of all the do_step_one() to start (it use do_step_one() results).

I tried the following :

#omp parallel for
for(unsigned int i=0; i<N; i++) {
    do_step_one(i);

#omp barrier

    do_step_two(i);
}

But gcc complains

convolve_slices.c:21: warning: barrier region may not be closely nested inside of work-sharing, critical, ordered, master or explicit task region.

What do I misunderstand? How to solve that issue?

like image 923
Guillaume Bouchard Avatar asked Nov 10 '09 18:11

Guillaume Bouchard


People also ask

Is there an implicit barrier after #pragma OMP parallel region?

Yes, "There is an implicit barrier at the end of the parallel construct."

How does OMP parallel work?

When run, an OpenMP program will use one thread (in the sequential sections), and several threads (in the parallel sections). There is one thread that runs from the beginning to the end, and it's called the master thread. The parallel sections of the program will cause additional threads to fork.

What does #pragma OMP parallel for do?

#pragma omp parallel spawns a group of threads, while #pragma omp for divides loop iterations between the spawned threads. You can do both things at once with the fused #pragma omp parallel for directive.


1 Answers

Just a side note, if you want to make sure the threads are not recreated, separate the declaration of parallel and declaration of for:

#pragma omp parallel
{
  #pragma omp for
  for(unsigned int i=0; i<N; i++){
    do_step_one(i);
  }
  //implicit barrier here
  #pragma omp for
  for(unsigned int i=0; i<N; i++){
    do_step_two(i);
  }
}
like image 68
Jason Avatar answered Sep 26 '22 01:09

Jason