Reduction trees: coordinate the work and specify the numerical answer
Design a parallel sum with correct participation, edge identities, cross-block combination, and an explicit floating-point accuracy contract.
On this page
The question for today
What you’ll understand
- Count addition work and tree depth for a power-of-two reduction.
- Explain how reassociation changes floating-point results.
- Choose identities, masks, accumulator types, and cross-block combination rules deliberately.
A sum looks like the simplest tensor operation until thousands of threads contribute partial results. Correctness includes which values participate, when partials are visible, and what numerical variation is acceptable. A balanced tree reduces the dependency depth, but floating-point addition does not behave like exact real arithmetic under arbitrary reassociation. Make both the execution tree and the accuracy contract explicit.
A tree reduces depth without removing additions
For n values where n is a power of two, a balanced binary tree performs n−1 additions across log₂n stages. At each stage, independent pairs can add concurrently. That gives a shorter dependency chain than a sequential loop, but the actual schedule includes loads, communication, barriers, and available execution resources. A GPU implementation often reduces within thread groups and then combines their partials. Independent CUDA blocks cannot assume an ordinary block barrier synchronizes the entire grid; a separate kernel, appropriate atomic protocol, or supported cooperative mechanism is needed.
A non-power-of-two length requires an edge design. For summation, masked lanes can contribute zero if the load itself is safely masked; for a maximum reduction, the identity differs. Empty input needs a defined API result or rejection. Warp shuffle instructions also have participation and active-mask requirements in the targeted CUDA interface. Borrowing an old warp-synchronous code fragment without checking current execution and synchronization rules can introduce races. Identify which lane holds a valid partial and who is allowed to read it at each stage.
Reassociation changes floating-point rounding
A floating-point sum rounds intermediate results. Different grouping can preserve or lose small contributions near a large value. A balanced tree often improves a worst-case error bound relative to a long sequential chain, but it does not guarantee the smallest actual error for every input. Cancellation can make relative error against the final sum very large even when absolute error is modest compared with the inputs. Compare absolute and relative tolerances with a meaningful scale, and distinguish repeatable scheduling from accuracy relative to the desired mathematical result.
A wider accumulator, pairwise summation, or a compensated method can improve accuracy at different computational costs. Promoting accumulation does not undo rounding already present in the input representation. Atomic additions can make contribution order schedule-dependent, and a deterministic reduction path can still differ from another implementation's deterministic path. Document whether the requirement is numerical tolerance, reproducibility within a configuration, or bitwise identity across devices. Those are different promises, particularly when compiler transformations or fast-math modes alter the arithmetic.
Validate adversarial sums as well as throughput
Use positive values with an analytical sum, mixed signs with cancellation, repeated small increments near a large value, zeros, tails, and the defined nonfinite cases. Validate every output across many rows, not only one scalar average. A hierarchical reduction also needs its intermediate-storage lifetime checked across streams or launches. Performance comparisons must use the same accumulator policy and include the actual combination stage. A kernel reporting only its fast first stage while omitting the final reduction is not measuring the requested operation.
The probability lab provides an analytically normalized positive reference family. Reset, set p=0.50 and n=10, predict mean five and total probability one, then Play or Step. Separately Reset, set p=0.50 and n=20, predict mean ten and the same total probability, then Play or Step. These exact binomial bars are not a GPU reduction or a rounding-order simulator. Their normalization is a useful invariant for positive test data, while the cancellation example must be analyzed separately. Passing a positive-sum invariant alone cannot establish accuracy for difficult mixed-sign tensors.
Work it through
A small input changes under reassociation
Consider IEEE binary32 round-to-nearest, ties-to-even inputs [2²⁴,1,−2²⁴,1], with every listed value exactly representable. Compare a left-to-right sum with grouping (first+third)+(second+fourth).
In left-to-right order, 2²⁴+1 lies halfway between adjacent binary32 values at that magnitude and rounds to 2²⁴. Adding −2²⁴ gives zero, then adding the last one gives one.
With the alternative grouping, the large terms cancel exactly to zero and the two small terms add exactly to two. The final result is two.
The exact real sum is two. The difference comes from intermediate rounding and grouping, not an out-of-bounds access; the reordered calculation also illustrates why execution-order promises must be stated separately from algebraic identities.
The two legal arithmetic groupings produce one and two under the stated rounding model. A parallel implementation needs an explicit accuracy and reproducibility target.
The living notebook
Explore the model
Interactive lab · probability
An analytical positive-sum invariant
Reset, set p=0.50 and n=10, predict mean 5 and total probability 1, then Play or Step. Separately Reset, set p=0.50 and n=20, predict mean 10 and total probability 1, then Play or Step. The exact binomial model does not implement floating-point reduction order, cancellation, or a GPU kernel.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA known positive-sum invariant is useful test evidence but does not cover mixed-sign numerical sensitivity.
Pause & reason
Check your understanding
Your turn
Try it yourself
A complete balanced sum tree has 1,024 leaves. Find additions and dependency stages. If an input has 1,000 values, what must a padded implementation establish about its extra lanes?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use the power-of-two tree formulas, then choose the summation identity and safe masking.
Compare with the solution
The 1,024-leaf tree has 1,023 additions and ten stages. For 1,000 values padded to that shape, the extra twenty-four lanes must contribute zero without reading invalid addresses, and the synchronization/participation protocol must remain valid. Zero padding preserves the real sum but does not make all floating-point grouping effects disappear.
Keep the idea
Take it with you
- Parallel depth, total arithmetic work, and communication are different costs.
- Floating-point reassociation requires an explicit accuracy contract.
- Masks, identities, and cross-block visibility are part of reduction correctness.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.