std::accumulate and std::reduce

The std::accumulate algorithm cannot be parallelized as it requires to be executed in order of the elements, which is not possible to parallelize. Instead, a new algorithm called std::reduce has been added, which works just like std::accumulate with the exception that it is executed un-ordered.

With commutative operations their result is the same, as the order of accumulation doesn't matter.

In other words, given a range of integers:

auto c = std::vector<int>{1, 2, 3, 4, 5}; 

Accumulating them by addition or multiplication:

auto sum = std::accumulate(c.begin(), c.end(), 0,   [](int a, int b) { return a + b; } 
); auto product = std::accumulate(c.begin(), c.end(), 1,   [](int a, int b) { return a * b; } );

Would ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.