OpenMP在for()循环中面临的情况 [英] Situations faced in OpenMP on for() loops
本文介绍了OpenMP在for()循环中面临的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用OpenMP为此,我不信任我的答案以及。真的需要你的帮助。我一直在想知道哪种方法(串行或并行)在这里运行速度更快。我的 #pragma
命令(设置为注释)如下所示。
I'm using OpenMP for this and I'm not confident of my answer as well. Really need your help in this. I've been wondering which method (serial or parallel) is faster in run speed in this. My #pragma
commands (set into comments) are shown below.
Triangle Triangle::t_ID_lookup(Triangle a[], int ID, int n)
{
Triangle res; int i;
//#pragma omp for schedule(static) ordered
for(i=0; i<n; i++)
{
if(ID==a[i].t_ID)
{
//#pragma omp ordered
return (res=a[i]); // <-changed into "res = a[i]" instead of "return(...)"
}
}
return res;
}
推荐答案
- 它取决于
n
。如果n
很小,那么OMP线程所需的开销会使OMP版本变慢。这可以通过添加如果
子句:#pragma omp parallel if(n> YourThreshhold)
- 如果所有
a [i] .t_ID
不是唯一的,那么当使用OMP
。 - 如果您的函数中有更多的功能,而不仅仅是单个比较,请考虑添加一个共享标记变量,比较
if(found)continue;
可以在循环开始时添加。 - c>已订购,因此如果这是您问题的症结所在,请忽略所有上述内容,并考虑此< a> answer。
- 个人资料。
- 如果您仍然想要一个理论答案,那么随机查找将是O(n),平均值为n / 2,而OMP版本将是一个常数n / k,其中k是不包括开销的线程/核心数。
- It depends on
n
. Ifn
is small, then the overhead required for the OMP threads makes the OMP version slower. This can be overcome by adding anif
clause:#pragma omp parallel if (n > YourThreshhold)
- If all
a[i].t_ID
are not unique, then you may receive different results from the same data when usingOMP
. - If you have more in your function than just a single comparison, consider adding a shared flag variable that would indicate that it was found so that a comparison
if(found) continue;
can be added at the beginning of the loop. - I have no experience with
ordered
, so if that was the crux of your question, ignore all the above and consider this answer. - Profile. In the end, there is no better answer.
- If you still want a theoretical answer, then a random lookup would be O(n) with a mean of n/2 while the OMP version would be a constant n/k where k is the number of threads/cores not including overhead.
循环,请参阅 Z Boson 对另一个问题的回答。
For an alternative way of writing your loop, see Z Boson's answer to a different question.
这篇关于OpenMP在for()循环中面临的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文