greywolf82
greywolf82

Reputation: 22173

Optimize loop with openmp

I've got the following loop:

    while (a != b) {
#pragma omp parallel
        {
#pragma omp for
            // first for 
#pragma omp for
            // second for
        }
    }

In this way the team is created at each loop. Is it possible to rearrange the code in order to have a single team? "a" variable is accessed with omp atomic inside the loop and "b" is a constant.

Upvotes: 0

Views: 718

Answers (1)

Massimiliano
Massimiliano

Reputation: 8032

The only thing that comes to my mind is something like this:

#pragma omp parallel
{
  while (a != b) {
  #pragma omp barrier 
  // This barrier ensures that threads 
  // wait each other after evaluating the condition
  // in the while loop
  #pragma omp for
  // first for (implicit barrier)
  #pragma omp for
  // second for (implicit barrier)
  // The second implicit barrier ensures that every 
  // thread will have the same view of a
  } // while
} // omp parallel

In this way each thread will evaluate the condition, but every evaluation will be consistent with the others. If you really want a single thread to evaluate the condition, then you should think of transforming your worksharing constructs into task constructs.

Upvotes: 2

Related Questions