📚 Encyclopedia

Science & Math · updated 2026-10-26

Parallel reductions and associativity (std::transform_reduce as the case study)

c++reductionassociativityfloating-pointparallel-algorithmstbbnumerical

A reduction folds a sequence into one value with a binary operation. A left fold (((a+b)+c)+d) has one fixed order. A parallel or vectorized reduction is free to group and reorder the elements, so its result only equals the left fold when the operation is associative and commutative. Integer addition and multiplication are; subtraction is not, and floating-point addition is only approximately associative. C++17's std::transform_reduce makes this concrete: the standard says the result is non-deterministic if the operation is not associative or not commutative, and that freedom applies even to the sequential overload.

The rule

Measured behavior (libstdc++ with GCC 13, October 2026)

These came from compiling and running about 200 small programs (g++ -std=c++20 -O2, TBB 2021 for the parallel policies) and tracing the reduction shape by reducing strings with an operation that writes "(a+b)". - Sequential transform_reduce does not fold left. It takes four elements at a time; over 1..8 the shape is ((i+((1+2)+(3+4)))+((5+6)+(7+8))). So reducing 1..8 with minus and init 0 gives 0, while accumulate gives -36. Below four elements it is a left fold, which hides the difference in small tests. - Narrow init types truncate per partial. With an int init and double values, each partial is truncated, not just the final sum. A 0LL init fixes integer overflow that 0 causes. - Mixing a signed init with size_t (for example init -1 while taking a max of string sizes) only goes wrong where -1 meets a raw size_t, so the bug depends on the input size. - Parallel results vary run to run. With std::execution::par on just eight ints, minus gave -36 in 18 runs and 6 in 2 runs out of 20; the string trace showed two different tree shapes. Over a million elements the result differed every run. - Floating-point sums depend on order. Summing ten million 0.1f: accumulate gave 1087937, sequential transform_reduce 975599.1 (its pairwise-ish grouping is actually closer to the true 1,000,000), and the parallel result varied. {1e16, 1, -1e16, 1} in doubles can sum to 0, 1 or 2 depending on grouping (a lone 1 added to 1e16 is rounded away); sequential transform_reduce gave 0. - unseq shape is type-dependent: strings reduced in two interleaved lanes, ints took another code path. - An exception thrown from the operation under an execution policy calls std::terminate. - An operation that modifies the elements it reads is undefined behavior. - clang 18 against the same libstdc++ agreed on everything except the parallel traces.

Floating point in general

Floating-point addition rounds after every step, so (a+b)+c and a+(b+c) can differ, most visibly when magnitudes differ widely (large + small loses the small). Consequences: - A parallel sum is not bit-reproducible across runs or thread counts unless the grouping is fixed. - Grouping in a balanced tree usually has smaller error than a long left fold (error grows roughly with log n instead of n); Kahan or pairwise summation helps when accuracy matters. - Tests of float reductions should compare with a tolerance, never with equality.

Gotchas

See also

Linked from

Sources: justinahampton memory experiments-transform-reduce.md, tldr post #1789, cppreference