4

I want to run a function that contains a for loop (supposed to run in parallel) in a parallel outer loop. So it looks like the following:

void myfunction(){
    ...
    #pragma omp parallel for
    for (int i=0;i<10;i++){
        do something...
    }
}


int main(){
    #pragma omp parallel for
    for(int i=0;i<5;i++){
        myfunction();
    }
}

Given the above code, I want to create 5 parallel threads for the loop in the main() function, and I want each of the 5 threads to create another K threads to run its own parallel for loop.

Although I know how to run nested for loops if the inner loop is not in a separate function, I cannot find a solution for this kind of problem.

In fact, this piece of code is for parallel 5 fold cross-validation, where each fold has a for loop that should be parallelized.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Mansumen
  • 373
  • 2
  • 4
  • 17

1 Answers1

4

You need to enable nested parallelism :

void myfunction(){

    #pragma omp parallel for
    for (int i=0;i<10;i++){
        ...
    }
}


int main(){

    omp_set_nested(1);       // Enable nested parallelism    
    omp_set_num_threads(5); // Use 5 threads for all parallel regions

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

        myfunction();
    }
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117