IIntroduction to AI Systems Performance EngineeringTechnical textbook
Chapter 8

Parallel Reduction, Scan, and Histograms

Build hierarchical algorithms for common GPU operations

Learn warp and block collectives, hierarchical reductions, scans, histograms, and the cost of irregularity.

A recurring training example uses a decoder-only model with approximately 1 billion parameters on one server containing eight GPUs. A representative microbatch holds four sequences of 2048 tokens in BF16, and data-parallel workers exchange gradients after backpropagation. One training step repeatedly needs answers such as the largest score in a row, the sum of squared gradients, the position at which a retained token should be written, and the number of tokens assigned to each expert. PyTorch presents those jobs as operations such as max, sum, nonzero, and bincount. A GPU profiler shows that the apparently small operations launch CUDA kernels and sometimes occupy a surprising fraction of the step because millions of threads must exchange or update information.

Chapter 8 studies three reusable patterns behind those kernels. A reduction turns many input values into one result, such as one sum. A prefix scan produces a running result at every input position, which lets a program assign output addresses without a serial counter. A histogram lets many inputs update a smaller collection of counters. The chapter introduces each name after tracing a concrete array through the operation.

NVIDIA Nsight Compute and Nsight Systems provide the empirical evidence used in the chapter. A reduction often appears as a bandwidth-heavy kernel followed by a much smaller second kernel. A contended histogram can show many atomic operations and a large timing change when the input distribution becomes skewed. A scan used for compaction produces a flag array, an offset array, and a final scatter. The observed arrays and timelines make the synchronization rules concrete.

During those experiments, a central processing unit (CPU) launches work on a graphics processing unit (GPU). Large input arrays reside in GPU high-bandwidth memory (HBM) before CUDA threads create smaller partial results on chip. Chapter 9 applies the same hierarchical reasoning to matrix multiplication (GEMM), whose K dimension contains a reduction for every output element.

The implementation proceeds through GPU scopes in increasing size. One CUDA thread first works with values in its registers. The 32 threads in a warp exchange partial results. Warps in one thread block communicate through shared memory and a block barrier. Separate thread blocks usually finish through another kernel launch or a limited number of atomic updates because an ordinary CUDA block barrier cannot synchronize the whole grid. Each lesson states which value exists at each scope and which event makes the value safe to consume.

Prerequisites

  • Chapters 5–7: CUDA execution, memory, and profiling
  • Associative binary operations and identity values
  • Work and dependency-depth analysis

Learning objectives

  • Trace reductions and scans across thread, warp, block, and grid scope
  • Implement stable stream compaction from an exclusive scan
  • Select an atomic or privatized histogram design from the key distribution
Lessons

Study this chapter

  1. 8.1
    Hierarchical reduction

    Trace and implement a hierarchical reduction from thread-local accumulation through grid completion

    Begin lesson →
  2. 8.2
    Prefix scans and compaction

    Compute inclusive and exclusive scans and use an exclusive scan to assign stable output positions

    Begin lesson →
  3. 8.3
    Histograms, atomics, and skew

    Predict atomic contention from a key distribution and choose a privatization scope

    Begin lesson →
Chapter projects

Extended exercises

Complete these projects after the lesson-level practice tasks. Each project combines several mechanisms from the chapter.

01
Check

Reduction ladder

Implement naive, shared-memory, and shuffle-based sum reductions.

Evidence: Correctness across odd sizes plus time and memory-traffic comparison.

02
Practice

Compaction

Implement stable compaction using flags, scan, and scatter.

Evidence: A bandwidth model and comparison to a trusted primitive.

03
Challenge

Skew-resistant histogram

Design a histogram for multiple key distributions.

Evidence: Distribution sweep and explanation of contention and privatization costs.

References

Sources and further reading

The lesson explanations synthesize primary papers and official references. Follow the originals for proofs, exact APIs, architecture details, and version-sensitive behavior.

  1. CUDA C++ Programming Guide
  2. CUDA C++ Best Practices Guide
  3. Nsight Compute Profiling Guide
  4. NVIDIA Nsight Systems User Guide

    Official reference for capturing and reading CPU, CUDA API, GPU workload, and communication timelines.

  5. NVIDIA CUTLASS