根据条件选择OpenMP编译指示 [英] Choose OpenMP pragma according to condition

查看:126
本文介绍了根据条件选择OpenMP编译指示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要优化的代码,该代码应在各种线程数量下运行.在有一个for循环中使用不同的调度技术运行了一些测试后,我得出的结论是,最合适的方法是当我只有一个线程并以其他方式进行引导时执行动态调度.在openMP中甚至有可能吗?

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(.....){
  ...
}

如果有人可以帮助我,我将不胜感激.另一种解决方案是编写两次for循环并使用if条件.但我想避免这种情况.

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.

推荐答案

可能的解决方案是将循环复制到if语句中,然后将循环主体提取"到函数中,以避免破坏

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(....);
}

这篇关于根据条件选择OpenMP编译指示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆