Post

std::reduce

std::reduce

std::reduce


Prerequisites


1. What is std::reduce? (C++17)

std::reduce is a C++17 algorithm that computes a single aggregated value from a range, similar to std::accumulate.

However, unlike accumulate, reduce is designed for parallel execution and allows the implementation to reorder operations for better performance.

1
2
3
4
5
6
#include <numeric>
#include <vector>

std::vector<int> v = {1, 2, 3, 4};

int sum = std::reduce(v.begin(), v.end(), 0);
1
10
Parallel Version
1
2
3
4
#include <execution>

int sum = std::reduce(std::execution::par,
                      v.begin(), v.end(), 0);

2. What is difference between std::accumulate and std::reduce?

Unlike std::accumulate:

1
(((0 + 1) + 2) + 3) + 4   // strict order

std::reduce may compute like this:

1
(1 + 2) + (3 + 4)

order is not guaranteed

3. Function Signature

1
2
template<class InputIt, class T>
T reduce(InputIt first, InputIt last, T init);

With Execution Policy

1
2
3
4
template<class ExecutionPolicy, class InputIt, class T>
T reduce(ExecutionPolicy&& policy,
         InputIt first, InputIt last,
         T init);

With Custom Operation

1
2
3
template<class InputIt, class T, class BinaryOp>
T reduce(InputIt first, InputIt last,
         T init, BinaryOp op);

3. Critical Rule: Associativity

The operation used in reduce must be associative.

✔️ Good (associative)
1
a + b + c + d
1
(a + b) + (c + d) == ((a + b) + c) + d
❌ Bad (order-dependent)
1
2
3
4
5
std::reduce(v.begin(), v.end(), 0,
    [](int a, int b)
    {
        return a - b;
    });

result may change depending on execution

Example: Multiplication

1
2
3
4
5
int product = std::reduce(v.begin(), v.end(), 1,
    [](int a, int b)
    {
        return a * b;
    });

Example: Parallel Sum

1
2
3
4
#include <execution>

int sum = std::reduce(std::execution::par,
                      v.begin(), v.end(), 0);

Performance Insight

std::reduce can:

  • split data into chunks
  • process chunks in parallel
  • combine partial results

Conceptually:

1
2
3
Thread 1 → (1 + 2)
Thread 2 → (3 + 4)
Final → combine results
This post is licensed under CC BY 4.0 by the author.