Sharing variables in OpenMP

Discussion in 'Mac Programming' started by Spanky Deluxe, Aug 7, 2007.

  1. macrumors demi-god

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #1
    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?
     
  2. thread starter macrumors demi-god

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #2
    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. :)
     

Share This Page