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&lt;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 &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;omp.h&gt;


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&lt;(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&lt;start+step+1; i++)
        {
            if(i&lt;end)
                printf("%d) work 1 %d\n", thread_ID, i);
        #pragma omp barrier
            if(i&lt;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?
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&lt;n; i++)
  {
      int thread_ID = omp_get_thread_num();
      printf("%d) work 1 %d\n", thread_ID, i);
  } // barrier (implicit, unless using 'nowait')

  #pragma omp for
  for (int i=0; i&lt;n; i++)
  {
      printf("    %d) work 2 %d\n", thread_ID, i);
  } // barrier
  } // omp parallel
  return 0;
}