c在并行区域内openmp并行 [英] c openmp parallel for inside a parallel region
问题描述
#pramga omp parallel num_threads(4)
{
//在4个线程上执行某些操作
#pragma omp parallel for num_threads(2)
for(int i = 0; i <2; i ++){
//在8个线程中执行总计
}
$ b 那么,是否有办法将每个(4)运行的线程拆分两个(新的)线程,所以在for循环内更多(8)个线程正在运行?
解决方案 href =http://docs.oracle.com/cd/E19205-01/819-5270/aewbc/index.html =nofollow noreferrer>嵌套并行,一个并行部分在另一个内部 - 被大多数当前支持OpenMP的编译器支持,但通常默认情况下是关闭的。您需要将 OMP_NESTED
环境变量设置为 TRUE
,或者在程序中调用 omp_set_nested(1)
。请参阅这个答案。
回答您的后续问题在注释中提出问题,在OpenMP并行for循环结束时不需要屏障;除非您使用 nowait
子句,在for循环结束时已经有一个显式的同步屏障。而且你不能在for循环里有一个障碍。如果循环迭代没有被线程均分,会发生什么?你最终会遇到一些线程卡住在其他线程无法访问的障碍。
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 explicit barrier for synchronization at the end of your for loops. 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.
这篇关于c在并行区域内openmp并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!