fun.sys / experiments / transform_reduce

std::transform_reduce

Change every element, then combine the results into one value: a map and a reduce in one call, from C++17's <numeric>. What makes it different from std::accumulate is a promise you make to it: the combining op does not care about order or grouping. Break that promise and you get whatever answer the library's grouping gives you. This page is a game about keeping it.


  

Facts about the standard are checked against cppreference's transform_reduce page. Every output on this page was printed by a real program, compiled with (-std=c++20 -O2) against for the parallel policies, on a -thread desktop, and none was typed in by hand. Every snippet was also compiled and run with on the same standard library as a cross-check. The reduction trees are traced from that library too (see part 3).

1The game: twelve levels

Build levels: pick the init, the reduce op and the transform until the call prints the target. Predict levels: say what a call prints before you see it. Fix levels: a call is broken; choose the repair. Fewer tries earn more stars. Your stars are saved in this browser.


          

          
read more ↓

2The overloads

Six overloads in three shapes, each with and without an execution policy. Pick one; the picture shows the transform running on each element, then the reduce combining the results in the grouping GCC's library actually uses.


      

          

          

3Who groups it?

std::accumulate and std::inner_product are specified to work strictly in order: ((init + a) + b) + c, a left fold. std::transform_reduce is not. cppreference describes it as a pool: put init and every transformed element in, then "picks any two of them and applies reduce", until one is left. Any grouping, any order. Its notes are blunt: "The result is non-deterministic if the reduce is not associative or not commutative (such as floating-point addition)."

To see the grouping a library really uses, run it on strings: the transform turns each number into its label, and the reduce writes "(" + a + "+" + b + ")". The output is the tree. The trees below are those outputs.

numbers 1…8, init 0, with
and the op

          

          

A million numbers, five runs each

Each program below was run five times. Integers with std::plus agree every time, because integer addition is associative and commutative. Minus doesn't, and neither does floating-point addition. The exact sum of ten million 0.1f values is 1000000; nobody here gets it, and the parallel run gets a different wrong answer each time.

show the programs

4init picks the type

The return type is the type of init, and libstdc++ keeps every partial result in a variable of that type. So the init is not just a starting value; it is a type declaration. The same five doubles, four inits:


      

The exact sum is 1000000004.6. 0 and 0LL lose the fractions at every step, not once at the end (GCC pairs them up: 0.25 + 0.5 → 0, 1.75 + 2.1 → 3, see the tree in part 3), which is why they print …03 and not …04. 0.0f keeps fractions but has only 24 bits of precision, so a billion swallows them. Write 0.0 for doubles, 0LL or std::int64_t{0} when an int sum could pass two billion, std::size_t{0} for sizes.

5Uses

Anything that is "transform each element, then combine with an associative, commutative op". All of these are ready for a policy argument.

There is no std::ranges::transform_reduce to pass a whole range to; C++23 added std::ranges::fold_left, which is an in-order fold like accumulate, not a reduce. (From memory, not measured.)

6Fine print


        

          

The game's answers are never written into the page. A level is judged against what g++ printed for that exact call. The picture beside a result is drawn by the page from the traced tree shape, and a test checks that its last node matches the real output for every combination in every level. Every snippet here is the body of main(); open "show the whole program" for the includes.

← experiments · fun.sys

show the whole program around a snippet