omp reduction on vector of cv::Mat or cv::Mat in general

前端 未结 1 621
名媛妹妹
名媛妹妹 2021-01-22 07:34
//In other words, this equilavent to cv::Mat1f mat(5,n)
//i.e. a matrix 5xn
std::vector mat(5,cv::Mat1f::zeros(1,n));
std::vector indexes(m         


        
相关标签:
1条回答
  • 2021-01-22 08:19

    Types such as cv::Mat1f, that use references instead of copying, are indeed dangerous in this context. You make a clear explicit solution by splitting the parallel region and the for loop.

    #pragma omp declare reduction(vec_mat1f_plus : std::vector<cv::Mat1f> : \
                std::transform(omp_out.begin(), omp_out.end(), omp_in.begin(), omp_out.begin(), std::plus<cv::Mat1f>()));
    // initializer not necessary if you initialize explicitly
    
    std::vector<cv::Mat1f> mat;
    #pragma omp parallel reduction(vec_mat1f_plus : mat)
    {
      mat = std::vector<cv::Mat1f>(5);
      for (auto& elem : mat) {
        elem = cv:Mat1f::zeros(1, n);
      }
      #pragma omp for
      for(size_t i=0; i<m; i++){
        mat[indexes[m]] += 1;
      }
    }
    

    I haven't tested whether std::plus<cv::Mat1f> works, but it looks good.

    Your approach with vectMat will also work if you provide an operator= that deep-copies the underlying Mat with clone(), and keep the initializer.

    0 讨论(0)
提交回复
热议问题