reduction operation for the sum of std::vectors

General OpenMP discussion

reduction operation for the sum of std::vectors

Postby jack » Mon May 13, 2013 1:33 pm

Is it possible to declare a reduction operation for sum of std::vectors in C++? if so, could you help me writing it?
I would like to parallelize the following simple code:

vector<float> A;

vector< vector <float> > O;
for(int i=0; i < N; ++i) O[i].resize(3);

for (int i=0; i < N; ++i){
/* calculate for each i, some observables O_0(i), O_1(i) and O_2(i) */

for (int q=0; q < 3; ++q)

At the end of the iteration, A is a vector of 3 components A[k] each of which contains the sum of N contributions given by all the O[i][k].
If A weren't a 3-component object, but just a 1-component object,A, I could have used a reduction operation writing at the beginning of external for the following line:

pragma omp parallel for private (i,q) reduction (+:A)

Is there a way to do something similar in the case of a 3-component vector apart form a critical section?
I cannot define three scalar variables and solve the problem this way, the reason being that the number 3 serves just as an example and I would like to use the above code for a generic n-vector component.
Posts: 1
Joined: Mon May 13, 2013 4:05 am

Re: reduction operation for the sum of std::vectors

Postby MarkB » Tue May 14, 2013 3:20 am

Hi there,

There is currently no way to do this with a reduction clause in OpenMP. The two main options are to use critical (or atomic, which may be more efficient), or to expand A into a vector of vectors of size the number of threads: each thread accumulates partial sums into its own element, indexed using omp_get_thread_num(), and then these are added together at the end (this operation can be parallelised across different components of the vector). Which is more efficient will depend on a number of factors including the cost of evaluating the Os, the value of N, the number of components in the vector and the number of threads.

OpenMP 4.0 will support user-defined reduction operators, so you will be able to define what reduction means for an n-vector and use it in a reduction clause. Working implementations are likely 6-12 months away, though.

Hope that helps,
Posts: 746
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 1 guest