Parallelizing a for loop using openmp & replacing push_back

前端 未结 3 1437
深忆病人
深忆病人 2020-12-19 11:18

I\'d like to parallelize the following piece of code but am new to openmp and creating parallel code.

std::vector good_matches;
for (int i = 0;         


        
3条回答
  •  渐次进展
    2020-12-19 11:54

    I showed how to do this here c-openmp-parallel-for-loop-alternatives-to-stdvector

    Make private versions of the std::vector and fill the shared std::vector in a critical section like this:

    std::vector good_matches;
    #pragma omp parallel
    {
        std::vector good_matches_private;
        #pragma omp for nowait
        for (int i = 0; i < descriptors_A.rows; i++) {
           if (matches_RM[i].distance < 3 * min_dist) {
              good_matches_private.push_back(matches_RM[i]);
           }
        }
        #pragma omp critical
        good_matches.insert(good_matches.end(), good_matches_private.begin(), good_matches_private.end());
    }
    

提交回复
热议问题