Modern C++ › C++17
Parallel Algorithms
Execution policies for standard algorithms.
Explanation
C++17 lets most <algorithm> and <numeric> functions run in parallel by adding an execution policy as the first argument (header <execution>):
| Policy | Meaning |
|---|---|
std::execution::seq |
sequential, like before |
std::execution::par |
may use multiple threads |
std::execution::par_unseq |
threads and SIMD vectorization (strictest requirements) |
std::execution::unseq (C++20) |
vectorization only |
std::sort(std::execution::par, v.begin(), v.end());
The library adds new algorithms designed for parallelism: std::reduce (like accumulate, but may combine in any order), std::transform_reduce (map then reduce), and inclusive_scan/exclusive_scan.
Your code must be safe to run concurrently. Element access functions (lambdas) must not race, and with par_unseq must not lock or allocate. With par, an exception thrown from a user function calls std::terminate.
Implementation support varies: MSVC and GCC (with Intel TBB installed, linking -ltbb) support it, libc++ is partial. The speed-up appears only for large inputs (roughly 10,000+ elements) of real work.
Example
#include <algorithm>
#include <chrono>
#include <execution>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<double> v(5'000'000);
std::iota(v.begin(), v.end(), 1.0); // 1, 2, 3, ...
// parallel transform: each element is independent
std::transform(std::execution::par, v.begin(), v.end(), v.begin(),
[](double x) { return x * 0.5; });
// parallel reduce: order of combination is unspecified -> operation must be associative
double total = std::reduce(std::execution::par, v.begin(), v.end(), 0.0);
std::cout << "sum ~ " << static_cast<long long>(total) << "\n";
// map + reduce in one pass: sum of squares
double sq = std::transform_reduce(std::execution::par, v.begin(), v.end(), 0.0,
std::plus<>{}, [](double x) { return x * x; });
std::cout << "sum of squares > 0: " << (sq > 0) << "\n";
std::sort(std::execution::par, v.begin(), v.end(), std::greater<>{});
std::cout << "max " << v.front() << "\n";
long long evens = std::count_if(std::execution::par_unseq, v.begin(), v.end(),
[](double x) { return static_cast<long long>(x) % 2 == 0; });
std::cout << "evens > 0: " << (evens > 0) << "\n";
return 0;
}
Common pitfalls
- Data races in your lambda:
std::for_each(par, ..., [&](auto x){ total += x; })races ontotal. Usereduce/transform_reduceor an atomic. std::reduceon non-associative or non-commutative operations (floating-point addition is only approximately associative) gives results that vary between runs;accumulateis the deterministic one.- Parallelism has overhead; small inputs, or cheap operations, get slower.
par_unseqforbids locking and memory allocation inside the callbacks.- Memory-bound work (a plain copy) rarely scales across cores.
- Forgetting to link TBB on GCC/Clang gives a link error (or silently sequential execution on some libraries).
Legacy vs modern
C++98 algorithms are strictly sequential; to parallelize you used OpenMP pragmas, Intel TBB, PPL (parallel_for on Windows), or hand-written threads. C++17 standardizes this for the standard algorithms; C++20 adds ranges (which don’t take execution policies yet). See also Threads, Mutexes and Atomics for explicit threading.
Exercises
1. Why is std::reduce not a drop-in replacement for std::accumulate with floating-point values?
reduce may combine elements in any order and in parallel, so rounding differs from the strictly left-to-right order of accumulate; results can vary between runs.