通常对cv :: Mat或cv :: Mat的向量进行omp减少 [英] omp reduction on vector of cv::Mat or cv::Mat in general

查看:116
本文介绍了通常对cv :: Mat或cv :: Mat的向量进行omp减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

预期结果是将每行的每个元素加一.这是一个玩具示例,实际的总和要复杂得多.我尝试将其并行化:

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

但这失败了,因为每一行的每个元素都是随机初始化的.我该如何解决?

因此我发现问题与有关.所以我应该用

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

初始化mat

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

但是这会给omp_priv = omp_orig带来问题,因为它会考虑std::vector<cv::Mat1f> mat(5);并且其值是不确定的.我该如何解决?我想到的唯一解决方案是创建一个包装器结构,例如:

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

但是接下来我应该实现什么才能使其与其余代码一起工作?

解决方案

在这种情况下,使用引用而不是复制的诸如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>是否有效,但它this. So I should initialize mat with:

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 would create problems with omp_priv = omp_orig, since it would consider std::vector<cv::Mat1f> mat(5); and it's values are undefined. How can I solve this? The only solution that came to my mind is to create a wrapper structure, something like:

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 then what should I implement to make it work with the rest of the code?

解决方案

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.

这篇关于通常对cv :: Mat或cv :: Mat的向量进行omp减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆