Unit 5 of 11 · about 5 hours · derive, implement

Kogge-Stone, Brent-Kung and segmented scans

Scan turns per-element predicates into global write positions. Compaction, sort and run-length coding all stand on it.

You'll be able to

  • Derive Kogge-Stone versus Brent-Kung work and step counts from the algorithm structure.
  • Implement exclusive scan for arbitrary lengths with correct identity handling.
  • Explain segmented-scan head-flag semantics and the decoupled look-back tile states.

Covers

Kogge-Stone scan and work-depth trade-offsBrent-Kung scan and reduced workSegmented scan and boundary semantics
1

Read

In this order, only the parts listed

0 of 5 done

  1. Kogge-Stone, work efficiency and Brent-KungProgramming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12

    Chapter 11, Sections 11.2 to 11.4 (printed pp. 238 to 250): Kogge-Stone, the versus work argument, and the Brent-Kung up-sweep and down-sweep with its count

  2. Segmented and single-pass scansProgramming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12

    Section 11.6 (printed p. 253): three-kernel hierarchical scan for long inputs; Section 11.7 (printed p. 256): single-pass motivation with its atomic, fence and barrier deadlock risk

    Read Section 11.7 for the argument only. Do not implement the domino scheme here.

  3. Decoupled look-back protocolSingle-pass Parallel Prefix Scan with Decoupled Look-back

    Sections 4.1 and 4.2: tile states X (invalid), A (aggregate ready) and P (prefix ready), with the look-back, fence and constant-bound properties

    Read for the state machine after two-pass scans pass. Implement a bounded version only if hardware and time allow a fair comparison.

  4. Scan, segmented scan and global strategiesMIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7

    Lecture 7, Pattern #5: Scan (slides 39 to 67), with scan-and-add versus reduce-then-scan versus decoupled look-back; Lecture 9, Pattern #5.5: Segmented Scan (slides 16 to 28), with head flags and the quicksort use case

  5. Associative scan implementationMIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7

    Lab 7, Part 1 (scan.cu, launch_scan): an inclusive scan templated over an associative operator, tested with SumOp and DebugRangeConcatOp, no external libraries such as CUB, benchmarked at length with throughput defined as twice the bytes over time

    Build for your own GPU with nvcc -arch=native. Retarget the course goal of 144 GB/s (40 percent of the RTX 4000 Ada 360 GB/s peak) to 40 percent of your measured copy bandwidth.

2

Focus on

  • Kogge-Stone does adds (20481 for ) in steps, while Brent-Kung does (4083) in about twice the steps. That is step efficiency versus work efficiency.
  • Exclusive scan seeds with the identity (0 for addition) and each output covers a strict prefix. Inclusive versus exclusive confusion shifts every scatter address by one.
  • Segmented scan restarts at head flags. Decoupled look-back tracks three states per tile: X invalid, A aggregate ready, P prefix ready.
Common mistakes · 3
  • Reusing an inclusive scan where an exclusive one is needed shifts every scatter address by one, corrupting output without crashing.
  • Testing only powers of two hides the block-sums tail. Test lengths , plus 1 and 0.
  • Validating a float kernel against a double scan proves nothing about the kernel's rounding. Test the kernel's own dtype and operator.
3

Try it yourself

Closed-book, before checking anything

Hand-run Kogge-Stone exclusive scan on [4, 6, 7, 1, 2, 8, 5, 2] to get [0, 4, 10, 17, 18, 20, 28, 33], then implement block scan plus block-sums scan plus add-back for arbitrary lengths and match torch.cumsum on odd sizes.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.For length 2048, how many adds do Kogge-Stone and Brent-Kung take, against what sequential baseline?Answer

    20481 and 4083 against a 2047-add sequential scan. Kogge-Stone is step-efficient, Brent-Kung is work-efficient.

  2. 2.When does Kogge-Stone win despite doing more work?Answer

    At small lengths where everything fits on chip and step count dominates. At large lengths DRAM passes dominate and work efficiency wins.

  3. 3.In decoupled look-back, what does a block do when the predecessor state is A versus P?Answer

    On A it adds the aggregate and keeps looking back; on P it adds the inclusive prefix and stops.

  4. 4.Segmented inclusive scan of [3, 1, 7, 0, 4, 1, 6, 3] with head flags [1, 0, 0, 1, 0, 0, 1, 0] gives what?Answer

    [3, 4, 11, 0, 4, 5, 6, 9]. Segments starting at positions 0, 3 and 6 restart from their own heads.

5

Practice

  • MIT Lab 7 Part 1: associative scan

    Part 1 launch_scan in scan.cu: pass the SumOp and DebugRangeConcatOp tests without CUB, then benchmark at length against the retargeted bandwidth goal

    Build with nvcc -arch=native and run in your own harness; the course runner needs an MIT token. Recompute every SM count, clock and bandwidth the questions assume.

  • LeetGPU challenge 16: Prefix Sum

    Challenge 16: solve(input, output, N) inclusive float scan at tolerance 0.01, with functional cases for a single element, an 8-element power of two and a 2025-element random input, and a performance case of

    Run it on LeetGPU or locally with the harness from the transpose unit.

  • LeetGPU challenge 70: Segmented Exclusive Prefix Sum

    Challenge 70: solve(values, flags, output, N) with flags[0] set and the first output of every segment 0.0, and a performance case of

    Attempt only after the required challenges 3, 4, 5, 13 and 16 pass.

You're done when

Your scan matches torch.cumsum on adversarial sizes, you derive both work counts from scratch, and Lab 7 Part 1 meets the retargeted bandwidth.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 8 more in the section

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.

  • 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.

  • 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.

  • 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.

  • 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.