C ++ OpenMP: schedule tasks of very unequal size

I need to sort a set of vectors with OpenMP in C ++. I really think this should be a common problem; however, I did not find anything about it.

Of course, I could just use

  #pragma omp parallel for schedule(dynamic,1)
    for(int i =0; i<narrays; ++i)
      sortSerial(array[i]);

and leave planning at runtime.

However, one array may take longer than all the others together. In fact, I may well have only one array. Or maybe I have twenty thousand one hundred elements, plus two with a million each. In this case, if I have 8 cores, I would assign five thousand partitions per thread for the first four threads, and then two threads in each partition with the last four. So that the total time is constant.

Then I could use nested parallelism. But if I do it

  #pragma omp parallel for schedule(dynamic,1)
    for(int i =0; i<narrays; ++i)
      sortParallel(array[i]);

sortParallel , OMP ; parallelism, , . , , sortParallel .

, , , , , .

, , VisualStudio 2013, , , OpenMP 2.0 - , , .

. . - , ?

, , ( ) , total/threads. , . , ; (, ) . , [i] , ,

void sort(int i,int threads){
    if(i<nbigintervals) //sort one big interval
       sortParallel(bigInterval[i],threads);  //spawns threads threads
    else                //sort all the others
       #pragma omp parallel for schedule(dynamic,1) num_threads(threads)  //nested parallel section, assigning the remaining vectors in dynamic fashion.
           for(int j=0;j<nsmallintervals;++j)
              sortSerial(smallInterval[j]);
}

int main() {
//....
//assign vectors of intervals
//balance threads

omp_set_nested(true);
#pragma omp parallel for schedule(static,1) num_threads(nbigintervals +1)
   for(int i=0; i<=nbigintervals; ++i) //notice the less _or equal_, which adds one iteration (and makes their number match the number of threads requested)
      sort(i,threads[i]);
 //....
 }

, , , . / / ?

+4

Source: https://habr.com/ru/post/1611117/


All Articles