Abbreviation omp on the vector cv :: Mat or cv :: Mat in general

//In other words, this equilavent to cv::Mat1f mat(5,n)
//i.e. a matrix 5xn
std::vector<cv::Mat1f> mat(5,cv::Mat1f::zeros(1,n));
std::vector<float> indexes(m);
// fill indexes
// m >> nThreads (from hundreds to thousands)
for(size_t i=0; i<m; i++){
  mat[indexes[m]] += 1;
}

The expected result is to increase each element of each row by one. This is an example of a toy, the actual amount is much more complicated. I tried to parallelize it with:

#pragma omp declare reduction(vec_float_plus : std::vector<cv::Mat1f> : \
            std::transform(omp_out.begin(), omp_out.end(), omp_in.begin(), omp_out.begin(), std::plus<cv::Mat1f>())) \
            initializer(omp_priv=omp_orig);

#pragma omp parallel for reduction(vec_float_plus : mat)
for(size_t i=0; i<m; i++){
    mat[indexes[m]] += 1;
}       

But this fails because each element of each row is accidentally initialized. How can i solve this?

So, I found out that the problem is with this . Therefore, I have to initialize matwith:

std::vector<cv::Mat1f> mat(5);
for(size_t i=0; i<mat.size(); i++)
  mat[i] = cv::Mat1f::zeros(1,n);

But then this will create problems with omp_priv = omp_orig, as it will consider std::vector<cv::Mat1f> mat(5);, and the values ​​will be undefined. How can i solve this? The only solution that occurred to me was to create a wrapper structure, for example:

class vectMat{
public:
    vectMat(size_t rows, size_t j){
        for(size_t i=0; i<rows; i++)
            mats.push_back(cv::Mat1f::zeros(1,j));
    }
private:
    std::vector<cv::Mat1f> mats;
};

But what should I do to make it work with the rest of the code?

+2
1

, cv::Mat1f, , . , parallel for.

#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;
  }
}

, std::plus<cv::Mat1f>, .

vectMat , operator=, Mat clone() .

+1

Source: https://habr.com/ru/post/1673810/


All Articles