简体   繁体   English

c openmp parallel 用于并行区域内

[英]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.例如,在我的并行区域内,我想在 4 个线程上运行我的代码。 when each thread enters the for loop, i'd like to run my code on 8 threads.当每个线程进入 for 循环时,我想在 8 个线程上运行我的代码。 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 ?那么,有没有办法将每个(4)正在运行的线程“拆分”为两个(新)线程,以便在 for 循环内有更多(8)线程正在运行?

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.您在此处拥有的内容 - 嵌套 parallelism ,一个并行部分在另一个中 - 由大多数当前启用 OpenMP 的编译器支持,但默认情况下通常是关闭的。 You'll need to set the OMP_NESTED environment variable to TRUE , or in your program call omp_set_nested(1) .您需要将OMP_NESTED环境变量设置为TRUE ,或者在您的程序中调用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;要在评论中回答您的后续问题,您不需要在 OpenMP 并行 for 循环末尾设置障碍; unless you use the nowait clause , there already is an implicit barrier for synchronization at the end of your for loop.除非你使用nowait子句,否则在你的for循环结束时已经有一个隐式的同步障碍。 And you can't have a barrier inside the for loop;你不能有内部的for循环的障碍; 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 个线程可以同时执行最内部的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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