Amos Chew
Amos Chew

Reputation: 137

Situations faced in OpenMP on for() loops

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

Upvotes: 1

Views: 125

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

  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.

Upvotes: 2

Related Questions