rajaditya_m - 1 year ago 159

C++ Question

I have a STL Vector containing some Matrices such as this

std::vector foovec;

There are approximately 3000 entries in this vector that I want to add up and form a new Eigen::Matrix.

The naive C++ code to do this would be :

`for(int i = 0; i <foovec.size();i++) {`

result += foovec[i];

}

However I want to parallelize this operation using OPENMP. I know I cant use the reduction pragma since this is a non-scalar type. If anyone can tell me a good way to parallelize this reduction without any write conflicts, that would be great.

Answer Source

You can do this

```
//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];
```