使用OpenMP通过c ++ list / iterator并行化两个for循环

时间:2016-02-01 09:49:18

标签: c++ list parallel-processing openmp

我想使用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)
   }
}

我尝试了不同的方法,但我无法使其正常工作。你能救我吗?。

我正在使用gcc 5.3.0,OpenMP 4.0。到目前为止我尝试过的解决方案没有成功:

解决方案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
   }
}

解决方案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)
          }
      }
   }
}

1 个答案:

答案 0 :(得分:1)

我认为你不应该致电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)
            }
        }
     }
 }