Choose OpenMP pragma according to condition

不羁岁月 提交于 2019-12-10 17:38:14

问题


I have a code that I want to optimise that should run in a variaty of threads ammount. After running some tests using different scheduling techniques in a for loop that I have, I came to the conclusion that what suits best is to perform a dynamic scheduling when I have only one thread and guided otherwise. Is that even possible in openMP?

To be more precise I want to be able to do something like the following:

if(omp_get_max_threads()>1)
#pragma omp parallel for .... scheduling(guided)
else
#pragma omp parallel for .... scheduling(dynamic)
for(.....){
  ...
}

If anyone can help me I would appreciate it. The other solution would be to write two times the for loop and use an if condition. But I want to avoid that if it is possible.


回答1:


Possible solution is to copy the loop into an if statement and to "extract" loop body into function to avoid breaking DRY principle. Then there will be only one place where you have to change this code if you need to change it in the future:

void foo(....)
{
   ...
}

if(omp_get_max_threads()>1)
{
    #pragma omp parallel for .... scheduling(guided)
    for (.....)
        foo(....);
}
else
{
    #pragma omp parallel for .... scheduling(dynamic)
    for (.....)
        foo(....);
}


来源:https://stackoverflow.com/questions/9366466/choose-openmp-pragma-according-to-condition

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