Zolo
Zolo

Reputation: 27

nested c++ for loop parallelized with openmp

I would like to set the diagonals of a matrix using openmp.

this a part of the plan:

for(int i=0; i<II; i++)
{
//calculate JJ
#pragma omp parallel for private(j)
  for(j=0; j<JJ; j++)
  {
    for(k=0; k<JJ; k++)
     {
       for(l=0; l<JJ; l++)
        {
          //calculate A
           for(m=0; m<JJ; m++)
           {
            if(j==l && k==m)
           {
            //calculate B
            // calculate c=A-B
       add C to matrix(diagonal, diagonal);
           }
         }
        }
diagonal++;
   }
  }
}

How can you parallelise this with openmp? Would it be possible to parallelise only the inner loops? when I run this it gives me the wrong result.

Thanks

Upvotes: 1

Views: 257

Answers (1)

simpel01
simpel01

Reputation: 1782

You have a loop-carried dependency given by the:

diagonal++;

statement. Therefore you can't parallelize this with OpenMP. You need to find a way to break that dependency (maybe running that statement within an #pragma omp ordered directive)

Upvotes: 2

Related Questions