OpenMP: conditional use of #pragma

╄→гoц情女王★ 提交于 2019-12-18 19:15:26

问题


I'm using OpenMP to improve my program efficiency on loops.

But recently I discovered that on small loops the use of this library decreased performances and that using the normal way was better.

In fact, I'd like to use openMP only if a condition is satisfied, my code is

#pragma omp parallel for
 for (unsigned i = 0; i < size; ++i)
   do_some_stuff ();

But what I want to do is to disable the #pragma if size is small enough i.e.:

if (size > OMP_MIN_VALUE)
  #pragma omp parallel for
for (unsigned i = 0; i < size; ++i)
do_some_stuff ();

But does not work, the better way is to write the loop twice but I don't want to do that way...

if (size > OMP_MIN_VALUE)
{
  #pragma omp parallel for
  for (unsigned i = 0; i < size; ++i)
    do_some_stuff ();
}
else
{
  for (unsigned i = 0; i < size; ++i)
    do_some_stuff ();
}

What is the better way to do that?


回答1:


I think you should be able to achieve the effect you're looking for by using the optional schedule clause on your parallel for directive:

#pragma omp parallel for schedule(static, OMP_MIN_VALUE)
 for (unsigned i = 0; i < size; ++i)
   do_some_stuff ();

You might want to play around with different kinds of scheduling though and different chunk sizes to see what suits your library routines best.



来源:https://stackoverflow.com/questions/7413331/openmp-conditional-use-of-pragma

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