# OpenMP workaround for barrier in loop

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++)
{
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;
#pragma omp parallel
{
// get the number of threads
#pragma omp single
{
}

// calculate which threads have to do one more iteration
int start, end;

if(one_more){
end = start + step + 1;
}
else{
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?

• What would the first program mean, if it meant something? 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? – Toby Speight Apr 5 '17 at 12:23
• It would mean something if a 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 – yakoudbz Apr 5 '17 at 13:25

You want to synchronize two parts of your iteration and parallelize the each one of them independently. A good option is splitting the code in two:

int main()
{
const int n = 100;

#pragma omp parallel
{
#pragma omp for
for (int i=0; i<n; i++)
{