Search code examples
cmultithreadingfor-loopopenmp

c openmp parallel for inside a parallel region


my question is like this one. but i'd like to do something different...

for instance, inside my parallel region i'd like to run my code on 4 threads. when each thread enters the for loop, i'd like to run my code on 8 threads. something like

#pramga omp parallel num_threads(4)
{
    //do something on 4 threads
    #pragma omp parallel for num_threads(2)
    for(int i=0;i<2;i++){
        //do something on 8 threads in total
    }
}

so, is there a way to "split" each (4) running threads into two (new) threads so inside the for loop more (8) threads are running ?


Solution

  • What you have here - nested parallelism, with one parallel section inside another - is supported by most current OpenMP-enabled compilers, but is normally turned off by default. You'll need to set the OMP_NESTED environment variable to TRUE, or in your program call omp_set_nested(1). See, eg, this answer.

    To answer your followup question in comments, you don't need a barrier at the end of OpenMP parallel for loops; unless you use the nowait clause, there already is an implicit barrier for synchronization at the end of your for loop. And you can't have a barrier inside the for loop; what happens if the loop iterations aren't evenly divided by threads? You'd end up with some threads being "stuck" waiting at a barrier none of the other threads would get to.