1

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;
}
Amos Chew
  • 137
  • 3
  • 10

1 Answers1

2
  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.

Community
  • 1
  • 1
Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56
  • woahh...i didn't know abt `if` clause...many thanks for linking to other sources...will take note of them... – Amos Chew Apr 20 '15 at 12:04
  • It's worth pointing out that using a condition in a pragma does not necessarily produce the same code as using no pragma at all. That's because it creates one function which has to plan for the case of `n> YourThreshhold`. When I really want to compare to the case without OpenMP I write a separate function without the pragma. That's duplicate code. I suppose it could be done better using templates. – Z boson Apr 21 '15 at 07:22
  • @Zboson Agreed, especially since OPs implementation is different with and without the pragma (which makes sense, as there is no reason to continue the search once it's found). – Avi Ginsburg Apr 21 '15 at 07:26
  • Step 5 is the most important one. If you haven't profiled then there's nothing to talk about. – jepio Apr 22 '15 at 11:04