OpenMP:有条件使用#pragma [英] OpenMP: conditional use of #pragma

查看:166
本文介绍了OpenMP:有条件使用#pragma的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenMP来提高我的程序在循环中的效率。

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.

事实上,我想使用openMP只有条件满足,我的代码是

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

但是我想做的是禁用#pragma if 足够小,即:

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

更好的方法是什么?

推荐答案

我想你应该能够通过使用可选的 parallel的

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.

这篇关于OpenMP:有条件使用#pragma的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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