OpenMP有序并行化

时间:2011-08-08 15:54:52

标签: c++ openmp parallel-processing

我正在尝试并行化以下函数(伪代码):

vector<int32> out;
for (int32 i = 0; i < 10; ++i)
{
    int32 result = multiplyStuffByTwo(i);

    // Push to results
    out.push_back(result);
}

当我现在并行化for循环并将push_back部分定义为关键路径时,我遇到的问题是(当然)结果的顺序并不总是正确的。如何使线程运行在for循环的最后一行以正确的顺序执行代码?谢谢!

2 个答案:

答案 0 :(得分:8)

您可以通过调用out.resize()设置out-vector的大小,然后按索引设置值,而不是通过push_back()

设置

的伪代码:

vector<int32> out; out.resize(10);
for (int32 i = 0; i < 10; ++i)
{
   int32 result = multiplyStuffByTwo(i);

   // set the result
   out[i] = result;
}

但是,我建议使用“经典”数组。它们要快得多,而且管理起来也不是很难

答案 1 :(得分:1)

vector<int32> out;

#pragma omp parallel for ordered 
for (int32 i = 0; i < 10; ++i)
{
    int32 result = multiplyStuffByTwo(i); // this will be run in parallel

    #pragma omp ordered
    // Push to results
    out.push_back(result); // this will be run sequential
}

这可能会有所帮助:

http://openmp.org/mp-documents/omp-hands-on-SC08.pdf