Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Spanky Deluxe

macrumors 603
Original poster
Mar 17, 2005
5,293
1,839
London, UK
This should be an easy one for anyone that's used OpenMP before!

How do I share a variable in a parallel loop.

I.e.:

Code:
#pragma omp parallel for
  for(i=0;i<n1;i++)
  {
    for(j=0;j<n2;j++)
    {
      indigo=array1[i]*array2[j];
      sausage=sqrt(indigo);
      array3[i][j]=sausage;
    }
  }

The problem is that sausage and indigo need to be independent for each thread. Now I guess I could use the thread number and the total number of threads to somehow create an array for each indigo and sausage but I'm sure there's something built into openmp where you can just say "give each thread their own version of indigo and sausage".

Any ideas?
 
Right, I think I worked this out so I thought I'd share for anyone else interested, you simply use private, i.e. the code would become:

Code:
#pragma omp parallel for private(indigo, sausage)
  for(i=0;i<n1;i++)
  {
    for(j=0;j<n2;j++)
    {
      indigo=array1[i]*array2[j];
      sausage=sqrt(indigo);
      array3[i][j]=sausage;
    }
  }

Seems to work just fine. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.