0

My question is simple, yet I'm not able to find the words to search the answer.

If I do this:

const unsigned N = ... ;
#pragma omp parallel
{
   unsigned i;
   #pragma omp for
   for(i=0; i<N; i++) // first loop
      <1st stuffs>

   <stuffs with other omp for...>

   #pragma omp for
   for(i=0; i<N; i++) // second loop
      <3rd stuffs>
}

Are the first and second loop guaranteed to be separated the same way among threads ?

If not, is there another possibility than defining the chunk size explicitly or using the schedule clause?

yakoudbz
  • 903
  • 1
  • 6
  • 14

2 Answers2

3

If certain conditions are met, then yes (OpenMP specification, Section 2.7.1 Loop Construct):

A compliant implementation of the static schedule must ensure that the same assignment of logical iteration numbers to threads will be used in two loop regions if the following conditions are satisfied: 1) both loop regions have the same number of loop iterations, 2) both loop regions have the same value of chunk_size specified, or both loop regions have no chunk_size specified, 3) both loop regions bind to the same parallel region, and 4) neither loop is associated with a SIMD construct.

The code in your example satisfies all four conditions, but you must explicitly specify static scheduling as the OpenMP specification leaves the default loop scheduling kind (i.e. the kind when no schedule clause is present) implementation-specific.

The following code is an example that is fully compliant:

#pragma omp parallel
{
   #pragma omp for schedule(static)
   for(i=0; i<N; i++) // first loop
      <1st stuffs>

   <other stuff>

   #pragma omp for schedule(static)
   for(i=0; i<N; i++) // second loop
      <3rd stuffs>
}
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
1

According to this answer https://stackoverflow.com/a/10852852/7024081 the threads will indeed be assigned the same iteration ranges in both loops assuming # threads and N are unchanged and that the scheduling is set to static.

If you can't set schedule to static I'm not sure what will happen as the default scheduling will be dependent on implementation: https://computing.llnl.gov/tutorials/openMP/#DO

Community
  • 1
  • 1
Joakim
  • 126
  • 1
  • 11