简体   繁体   中英

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 ?

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.

是的,正确的方法是您选择的方法:第二个 for 循环将被每 4 个线程拆分,以便 8 个线程可以同时执行最内部的循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM