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

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

I currently 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 to do_step_one() and do_step2() can be executed in any order and in parallel, but for any do_step_two() , the end of all do_step_one() required to run (it uses the results of do_step_one() ).

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: the area of โ€‹โ€‹the barrier cannot be closely nested inside the area of โ€‹โ€‹shared, critical, ordered, main or explicit task.

What am I misunderstanding? How to solve this problem?

+6
openmp
source share
2 answers

Just pay attention, if you want to make sure that the threads are not recreated, separate the concurrency declaration and declare 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); } } 
+11
source share

One problem that I see with this code is that the code does not meet the specification :)

If you need to complete all of do_step_one (), you will need the following:

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

The result of this will be parallelism of the first for, and then a parallelism of the second for.

+4
source share

All Articles