//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;
}
但这会失败,因为每行的每个元素都是随机初始化的。我该如何解决这个问题?
所以我发现问题与this。所以我应该初始化mat
with:
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;
};
但是我应该实现什么才能使其与其余代码一起工作呢?