Situations faced in OpenMP on for() loops

╄→гoц情女王★ 提交于 2019-12-13 02:06:59

问题


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:


  1. It depends on n. If n is small, then the overhead required for the OMP threads makes the OMP version slower. This can be overcome by adding an if clause: #pragma omp parallel if (n > YourThreshhold)
  2. If all a[i].t_ID are not unique, then you may receive different results from the same data when using OMP.
  3. 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.
  4. I have no experience with ordered, so if that was the crux of your question, ignore all the above and consider this answer.
  5. Profile. In the end, there is no better answer.
  6. 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

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