c openmp parallel for inside a parallel region

こ雲淡風輕ζ 提交于 2019-12-11 06:34:34

问题


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 ?


回答1:


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.




回答2:


Yes, and the right way to do it is the one you have chosen: the second for-loop will be split by each 4 threads so that 8 threads may execute the inner-most loop concurrently.



来源:https://stackoverflow.com/questions/15830526/c-openmp-parallel-for-inside-a-parallel-region

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!