Unit 3 of 11 · about 4 hours · derive

Reduction trees, work efficiency and floating-point order

Reduction is where parallelism meets floating-point reality. The fastest tree also sums in a different order, and that order is part of the result.

You'll be able to

  • Derive the work , depth and idle-thread fraction of a tree reduction.
  • Predict how tree order changes a float32 sum and bound the change with a float64 reference.
  • Trace the interleaved-to-sequential addressing fixes and state what each one removes.

Covers

Reduction trees, operation count and work efficiencyFloating-point non-associativity and reduction tolerances
1

Read

In this order, only the parts listed

0 of 3 done

  1. Reduction trees and a simple kernelProgramming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12

    Chapter 10, Sections 10.1 to 10.3 (printed pp. 211 to 218): reduction trees, operation count and the first shared-memory kernel

  2. Seven-kernel optimization sequenceOptimizing Parallel Reduction in CUDA

    Slide 2 (intent: 7 versions), slides 7 to 28 from interleaved addressing through complete unrolling, and the slide 34 performance table for 4M elements

    The GB/s values are 2008-era hardware. Trace the code changes and treat the table as history, not targets.

  3. Order-dependence scriptsGPU MODE - Reductions and Scans

    Lecture 9 code in lecture_009/: nondeterminism.py (left-to-right 0.0 versus right-to-left 1e-19 on the same list) and accuracy.py (fp32 versus fp16 absorption at 1000.0 plus 1e-3)

    Run the scripts to feel order effects. Save the lecture slides for later.

2

Focus on

  • A tree does adds in steps. Step does adds, so half the threads idle after the first step.
  • Interleaved addressing diverges warps and pays for modulo; the strided-index form removes divergence but bank-conflicts; sequential addressing is conflict-free.
  • Challenge 4's reference accumulates in float64, so an fp32 tree passes at 1e-5 on friendly magnitudes. That proves nothing about ill-conditioned inputs.
  • The deck uses int throughout, where every tree order is exact. Its sequence needs an FP re-derivation before you copy it into a float kernel.
Common mistakes · 3
  • Treating a Challenge 4 pass as an order certificate. The float64 reference plus 1e-5 tolerance absorbs tree-order error on friendly magnitudes, so test cancellation inputs separately.
  • Copying the deck's unrolled last warp. It relies on volatile shared memory and warps running in lockstep; from Volta on, threads of a warp are scheduled independently, so finish with __shfl_down_sync or put __syncwarp() between the steps.
  • Reading idle threads as inefficiency. Halving active threads each step is still work-efficient at adds; a scheme that keeps every thread busy each step, as Kogge-Stone does for scan, does about .
3

Try it yourself

Closed-book, before checking anything

By hand, reduce [6, 2, 7, 4, 5, 8, 3, 1] with a sequential-addressing tree to get 36 with 7 adds in 3 steps. Then show in float32 that while .
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.For 1024 elements, how many adds and how many steps does a tree reduction take?Answer

    1023 adds in 10 steps. Each step halves the partials until one remains.

  2. 2.Why is the deck's first kernel slow, in two causes?Answer

    tid % (2*s) diverges warps and integer modulo is slow. Later kernels replace it with a strided index and a halving loop.

  3. 3.Challenge 4 compares against torch.sum(input.double()).float(). What does that reference hide?Answer

    Summation order error. Any fp32 tree order lands within 1e-5 on the test magnitudes, so order bugs on ill-conditioned inputs escape.

  4. 4.Why do and differ in float32?Answer

    Near , float32 values are 8 apart, so rounds back to and the second order returns 0, while the first cancels exactly and then adds 1.

5

Practice

  • LeetGPU challenge 4: Reduction

    Challenge 4: solve(input, output, N) fp32 sum checked against torch.sum(input.double()).float() at absolute and relative tolerance 1e-5, with functional cases for , and a 15M-element input, and a performance case of

    Run it on LeetGPU or locally: build your solve with nvcc -shared -Xcompiler -fPIC, load it with ctypes, and check it against reference_impl on the cases from generate_functional_test and generate_performance_test in the challenge's challenge.py (public in AlphaGPU/leetgpu-challenges). That file imports core.challenge_base, which the repository does not ship; a two-line stub class with device = "cuda" is enough. Pass all functional tests, then add your own cancellation test in the style against a float64 reference. The public tests do not check order.

You're done when

You hand-trace the 8-element tree, explain what each deck stage removes, and your kernel passes Challenge 4 plus your own cancellation test.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 8 more in the section
  • Programming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12Wen-mei Hwu, David Kirk and Izzat El Hajj · primary

    Read thread coarsening in Chapter 6, then Chapters 9 (histogram), 10 (reduction), 11 (prefix sum) and 12 (merge, optional). Work the exercises that test work, depth, contention and boundary handling.

  • Optimizing Parallel Reduction in CUDANVIDIA · required

    Implement the optimization sequence from interleaved addressing through sequential addressing, first-add-during-load, unrolling and warp-level completion. Correct outdated assumptions and retain the stage where each optimization stops helping.

  • GPU MODE - Reductions and ScansGPU MODE · support

    Use Lectures 9, 20, 21 and 24 after the PMPP implementations. Reproduce one faster scan or reduction idea and show the exact shape and device regime where it changes the bottleneck.

Also in this section

  • AI Systems Performance Engineering - Chapters 11 and 12Chris Fregly (O'Reilly) · required

    Read Chapter 11 on streams, events, inter-kernel pipelining and stream-ordered allocation, and Chapter 12 on CUDA graphs and dynamic scheduling. Skip device-initiated launch beyond the vocabulary.

  • Efficient Matrix Transpose in CUDA C/C++NVIDIA · required

    Rebuild copy, naive transpose, coalesced transpose and padded shared-memory variants. Update deprecated details for the installed toolkit and compare requested transactions, bank behavior and achieved bandwidth on odd as well as friendly shapes.

  • Single-pass Parallel Prefix Scan with Decoupled Look-backNVIDIA Research · depth

    Read after two-pass scans pass. Derive the state and progress argument, then implement or inspect a bounded version only if the available hardware and time support meaningful comparison.

  • Online Normalizer Calculation for SoftmaxOnline Softmax Authors · required

    Derive the running maximum and normalizer recurrence, test extreme and all-masked rows, and compare materialized, two-pass and online implementations before using the recurrence inside attention.

  • LeetGPU - CUDA Primitive ChallengesAlphaGPU · required

    Solve challenge 3 Matrix Transpose, 4 Reduction, 5 Softmax, 13 Histogramming and 16 Prefix Sum in CUDA. Add 70 Segmented Prefix Sum or 72 Stream Compaction only after the required set. Keep local source, hidden-shape assumptions, hardware, raw timings and correctness evidence outside the leaderboard.

  • MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7MIT · support

    Use Lecture 6 for host-device copies and streams, and Lectures 7 and 9 for map, stencil, reduce, compaction, scan, segmented scan, histogram and merge. Do Lab 7 (Run-Length Compression, with its scan and shuffle warm-ups) built for your own GPU.

  • GPU Microbenchmark and Kernel Suite - CUDA CoreRoadmap-Final · support

    Complete the CUDA vector map, reduction, transpose and stable softmax requirements plus seeded access, synchronization, mask, stream and numerical failures. Preserve library and framework baselines.

  • CUDA C++ Programming Guide (v13.4.2)NVIDIA · reference

    Use Section 2.3.4 for coalesced access, the transpose example and bank-conflict padding; Section 2.5 for streams, events, synchronization and the stream-capture intro; and Section 4.2 for graph creation, update and capture constraints. This is the API reference beside the book chapters.