Finfa811
Finfa811

Reputation: 638

Parallelize two for loops through c++ list/iterator using OpenMP

I would like to parallelize the following structure using OpenMP:

for (auto it = data.begin(); it != data.end(); ++it)
{
   for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
   {
       // code here (it2->process)
   }
}

I have tried different approaches but I could not make it work properly. Could you help me?.

I am using gcc 5.3.0, OpenMP 4.0. The solutions I've tried so far with no success are:

Solution 1:

#pragma omp parallel
#pragma omp single
{
   for (auto it = data.begin(); it != data.end(); ++it)
   {
   #pragma omp task firstprivate(it)
       for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
       {
          // code here (it2->process)
       }
   #pragma omp taskwait
   }
}

Solution 2:

#pragma omp parallel
{
   for (auto it = data.begin(); it != data.end(); ++it)
   {
      #pragma omp single nowait
      {
          for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
          {
             // code here (it2->process)
          }
      }
   }
}

Upvotes: 1

Views: 660

Answers (1)

Radek
Radek

Reputation: 518

I think you should not call omp taskwait.

#pragma omp parallel
{
    #pragma omp single
    {
        for (auto it = data.begin(); it != data.end(); ++it)
        {
        #pragma omp task firstprivate(it)
            for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
            {
            // code here (it2->process)
            }
        }
     }
 }

Upvotes: 1

Related Questions