PDA

View Full Version : Sharing variables in OpenMP




Spanky Deluxe
Aug 7, 2007, 03:52 AM
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.:

#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?



Spanky Deluxe
Aug 7, 2007, 06:38 AM
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:

#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. :)