What I basically want to do is this:
int main() { const int n = 100; #pragma omp parallel for for (int i=0; i<n; i++) { int thread_ID = omp_get_thread_num(); printf("%d) work 1 %d\n", thread_ID, i); #pragma omp barrier printf(" %d) work 2 %d\n", thread_ID, i); #pragma omp barrier } return 0; }
except we cannot put a barrier into a parallel for
in OpenMP; it just cannot be done. However, it's possible to explicitly do the separation between the threads in order to make it work:
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main()
{
const int n = 100;
int nthreads;
#pragma omp parallel
{
// get the number of threads
#pragma omp single
{
nthreads = omp_get_num_threads();
}
int thread_ID = omp_get_thread_num();
// calculate which threads have to do one more iteration
int one_more = thread_ID<(n%nthreads);
int step = n/nthreads;
int start, end;
if(one_more){
start = step*thread_ID + thread_ID;
end = start + step + 1;
}
else{
start = step*thread_ID + n%nthreads;
end = start + step;
}
// the real work is here
for (int i=start; i<start+step+1; i++)
{
if(i<end)
printf("%d) work 1 %d\n", thread_ID, i);
#pragma omp barrier
if(i<end)
printf(" %d) work 2 %d\n", thread_ID, i);
#pragma omp barrier
}
}
return 0;
}
However, I find this a bit dirty and I would have preferred something cleaner. Does somebody have a better idea?
barrier
is used to ensure that the threads have all reached the same point in the code, but if that point is within a loop, threads may reach that point any number of times (including zero), and not necessarily the same number of times in all threads. Is the intent supposed to be that at any given time, work1 isn't being done at the same time as work2? It sounds like you just need a suitable mutex solution. Perhaps a bit more explanation of your motivation (use case) might help? \$\endgroup\$barrier
meant "all thread that did not terminate must reach this before continuing" and if it was allowed to put the barrier in the parallel loop obviously \$\endgroup\$