简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM