Parallelizing the addition of a vector of matrices to OPENMP
I have an STL vector containing some Matrices such as
std :: vector foovec;
This vector contains about 3000 records that I want to add and form a new Eigen :: Matrix.
Naive C ++ code for this:
for(int i = 0; i <foovec.size();i++) {
result += foovec[i];
}
However, I want to parallelize this operation using OPENMP. I know I cannot use the reduction pragma as it is a non-scalar type. If someone can tell me a good way to parallelize this reduction without write conflicts, that would be great.
+3
source to share
2 answers
You can do it
//std::vector<Eigen::Matrix<double, n, m>> foovec;
#pragma omp parallel
{
Eigen::Matrix<double, n, m> result_private;
#pragma omp for nowait //fill result_private in parallel
for(int i=0; i<foovec.size(); i++) result_private += foovec[i];
#pragma omp critical
result += result_private;
}
For OpenMP> = 4.0 you can also do this
#pragma omp declare reduction (merge : Eigen::Matrix<double, n, m> : omp_out += omp_in)
#pragma omp parallel for reduction(merge: result)
for(int i=0; i<foovec.size(); i++) result += foovec[i];
+3
source to share
For me, using reduction(merge : ...)
didn't help. It had problems with uninitialized matrices (I think).
Following:
#pragma omp declare reduction( \
+: \
Eigen::Matrix3d: \
omp_out= omp_out + omp_in) \
initializer(omp_priv=Eigen::Matrix3d::Zero())
Eigen::Matrix3d sum = Eigen::Matrix3d::Zero();
std::vector<Eigen::Matrix3d> elems;
#pragma omp parallel for reduction(+: sum)
for (size_t i = 0; i < elems.size(); ++i) {
sum += elems[i];
}
0
source to share