繁体   English   中英

OpenMP有序并行化

[英]OpenMP Ordered Parallelization

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

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循环的最后一行以正确的顺序执行代码? 谢谢!

你可以通过调用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;
}

但是,我建议使用“经典”数组。 他们的速度要快得多,也不是很难管理

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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