开发者

OpenMP Ordered Parallelization

I'm trying to parallelize the fo开发者_JS百科llowing function (pseudocode):

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

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

When I now parallelize the for loop and define the push_back part as a critical path, I'm encountering the problem that (of course) the order of the results in out is not always right. How can I make the threads run execute the code in the right order in the last line of the for loop? Thanks!


You can set the size of the out-vector by calling out.resize() and then set the value by index, not by push_back()

Pseudo-code:

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

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

But, I'd recommend using "classic" arrays. They're much faster and not really harder to manage


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
}

This can be helpful:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜