简体   繁体   English

C++17 for_each 并行

[英]C++17 for_each parallel

I would like to ask how can I parallelize the following loop.我想问一下如何并行化以下循环。 It is currently crashing.它目前正在崩溃。 I tried to search and most answers show that the problem is that I am using std::vector .我尝试搜索,大多数答案表明问题出在我使用的是std::vector I tried to make a fixed-sized std::vector .我试图制作一个固定大小的std::vector But the application still crashes.但是应用程序仍然崩溃。 Could you tell me what is wrong in the following loop?你能告诉我下面的循环有什么问题吗?

    std::vector<int> a(pairsListFlags.size());
    std::generate(a.begin(), a.end(), [n = 0]() mutable { return n++; });
    

    std::for_each(std::execution::par_unseq, std::begin(a), std::end(a), [&](int i) {

        int a = pairsList[i * 2];
        int b = pairsList[i * 2 + 1];
        if (getCollisionOpenNurbs(OBB[a], OBB[b])) {   //Check OBB collision +4-6 ms  
            if (FaceFace(P[a], P[b], Pl[a], Pl[b])) {//Check polygon intersection +20 ms
                pairsListFlags[i] = 1;
                
            }
        }

    });

Your problem is not embarrassingly parallel and so you should not use std::for_each here (at least not without synchronisation mechanisms or atomics which would be inefficient).你的问题不是并行的,所以你不应该在这里使用std::for_each (至少在没有同步机制或效率低下的原子时)。 Instead, you can perform a reduction using std::reduce .相反,您可以使用std::reduce执行归约 Here is an example:这是一个例子:

std::vector<int> a(pairsListFlags.size());
std::generate(a.begin(), a.end(), [n = 0]() mutable { return n++; });
int counter = std::reduce(std::execution::par_unseq, std::begin(a), std::end(a), 0, [&](int i) {
    int a = pairsList[i * 2];
    int b = pairsList[i * 2 + 1];
    if (getCollisionOpenNurbs(OBB[a], OBB[b])) {   //Check OBB collision +4-6 ms  
        if (FaceFace(P[a], P[b], Pl[a], Pl[b])) {  //Check polygon intersection +20 ms
            pairsListFlags[i] = 1;
            return 1;
        }
    }
    return 0;
});

Note that you should be careful about false sharing on pairsListFlags since it can decrease a bit the performance of the resulting code (but have no impact on the result).请注意,您应该小心pairsListFlags上的错误共享,因为它会稍微降低结果代码的性能(但对结果没有影响)。

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

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