问题
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;
}
回答1:
- 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.
For an alternative way of writing your loop, see Z Boson's answer to a different question.
来源:https://stackoverflow.com/questions/29735189/situations-faced-in-openmp-on-for-loops