Unit 6 of 11 · about 4 hours · implement, measure

Histograms, atomic contention and privatization

Histograms are the first primitive where threads collide on the same addresses. Contention, not traffic, sets the ceiling, and privatization is the general escape.

You'll be able to

  • Predict atomic contention from the bin-count distribution before timing.
  • Implement privatized histograms with a correct merge and overflow policy.
  • Measure private versus global atomics across uniform and skewed inputs and explain the crossover.

Covers

Histogram atomics, contention and collision distributionsPrivatization, aggregation and merge costs
1

Read

In this order, only the parts listed

0 of 3 done

  1. Atomics and privatizationProgramming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12

    Chapter 9, Sections 9.2 to 9.4 (printed pp. 194 to 202): the basic histogram kernel, atomic latency and throughput, and privatization into per-block copies

  2. Coarsening, aggregation and exercisesProgramming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12

    Sections 9.5 and 9.6 (coarsening and aggregation under heavy contention) with Exercises 1 to 6 (printed p. 209), especially Exercise 6 on 524288 elements over 128 bins with 1024 threads

  3. Histogram patternMIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7

    Lecture 9, Pattern #6: Histogram (slides 29 to 42): partition-and-increment sketch with atomics, privatization, coarsening and aggregation

2

Focus on

  • One global atomic per update serializes on hot bins. With uniform input over bins each bin takes about atomics; with skewed input one bin takes nearly N.
  • Privatization replicates bins per block in shared memory, which is fast and block-scoped, then merges at a cost of blocks times bins adds. Coarsening into fewer blocks cuts the merge.
  • Work PMPP Exercise 6 by hand before coding: it quantifies basic versus privatized versus coarsened update counts on the same input.
Common mistakes · 3
  • Merging privatized int32 bins into int32 global bins can overflow while per-block counts look fine. Accumulate the merge in 64-bit or document the length limit.
  • Benchmarking only uniform input hides contention. One hot bin serializes global atomics on one address (27.4 against 17.8 ms for 50M updates in the run above), so always include a skewed case.
  • Skipping __syncthreads() between zeroing, updating and merging races within the block, since later phases read data other threads wrote.
3

Try it yourself

Closed-book, before checking anything

Implement global-atomic and privatized int32 histograms. On uniform 1M-element input over 256 bins and on input that falls entirely in one bin, both match torch.bincount and the privatized version is faster. On an RTX 3050 6 GB, 50M updates took 17.8 ms with global atomics over 256 uniform bins and 27.4 ms into one bin, against 1.3 ms privatized in both cases; your times will differ, so compare the ratios on your own GPU. State the int32 bin overflow limit (2147483647) and your merge policy above it.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.With 50M uniform values over 256 bins, how many atomics hit each bin?Answer

    About 195312 each. Every bin is equally hot, so global atomics still serialize in groups.

  2. 2.All 50M values fall in one bin with global atomics. What happens, and what fixes it?Answer

    Updates serialize fully on one address. Per-block private bins plus a merge remove the serialization at a merge cost of blocks times bins.

  3. 3.Why do the private copies live in shared memory?Answer

    Lower latency, contention scoped to the block, and no global traffic until the merge. Global privatization would keep the contention domain wide.

  4. 4.With 128 blocks and 1024 bins, what does the merge cost, and how does coarsening change it?Answer

    131072 adds. Coarsening into fewer blocks cuts the merge proportionally, at the usual occupancy cost.

5

Practice

  • LeetGPU challenge 13: Histogramming

    Challenge 13: solve(input, histogram, N, num_bins) over int32, with functional cases from over 3 bins (expecting [2, 2, 1]) through 10000 over 128 bins, and a performance case of with 256 bins

    Run it on LeetGPU or locally with the harness from the transpose unit. Then add an ungraded skewed-input timing that compares global against privatized atomics.

You're done when

Both variants match torch.bincount on uniform and skewed inputs, and you predict the faster one from the bin distribution before timing.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 9 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.

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

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