CUDA Data Movement and Parallel Primitives

Transpose, reduction, scan, histogram and softmax expose the recurring constraints behind larger kernels: memory layout, synchronization, work efficiency, atomics, numerical order and asynchronous lifetime.

Section 4 of 10 · 11 units · about 46 hours

0/11

By the end you can Implement and diagnose CUDA transpose, reduction, scan or histogram, and online softmax across adversarial tails and values, then prove a measured data-movement or synchronization improvement with profiler evidence and a retained losing regime.

Units

Build it

Mini project · about 12 to 16 hours

CUDA maps, reductions, transpose and overlap with async proof

Fill kernellab with maps, reductions, transpose, scans, histograms, softmax, overlapped copies and captured graphs. Each kernel is checked against an independent reference with adversarial sizes kept. Overlap, graphs and defect regressions carry into later sections.

  1. 1Build the LeetGPU harness in tests/leetgpu/: compile each solve, check it against reference_impl with the public generators, and verify the path on one challenge before timing anything.
  2. 2Write scalar, vectorized and coarsened maps with grid-stride loops. Check every output against an independent CPU reference over odd and empty sizes, and keep the comparison.
  3. 3Write shuffle and multi-pass reductions with identity-padded tails and a zero-length path. Match a float64 reference on your own inputs, sweep the coarsening factor with a retained loser, and pass LeetGPU challenge 4.
  4. 4Write copy, naive and padded tiled transpose and time each with events. Diagnose bank behavior with an enumerator, keep the padding fix, and pass LeetGPU challenge 3.
  5. 5Implement block scans with add-back plus scan-plus-scatter compaction, privatized histograms and online softmax against torch.cumsum, torch.bincount and a max-trick reference, following MIT Lab 7. Cover masked rows with a documented policy, and pass LeetGPU challenges 5, 13, 16 and 72.
  6. 6Build a multi-chunk copy plus kernel pipeline on separate streams with event chaining, and show overlap on an nsys timeline. Capture a kernel chain with CUDA graphs, demonstrate update and re-instantiation, and replay identically to eager.
  7. 7Inject five defects, such as a missing barrier, an out-of-bounds tail and a stream race. Locate each with compute-sanitizer or a timeline, repair it without a global synchronization, and keep it as a regression.
What you hand in
  • The LeetGPU harness with reference_impl checks for challenges 3, 4, 5, 13, 16 and 72.
  • Maps, reductions, transpose, scans, histograms, compaction and softmax with independent references and mask tests.
  • The overlap timeline with event math, graph capture with update evidence, and defect regressions with tool logs.
How to check it
How it's graded

Oracle agreement

Pass: Each kernel matches its independent reference on random and adversarial inputs including empty sizes, and injected omissions fail.

Great: The map keeps a fused against unfused comparison showing the last-bit gap.

Performance with loser

Pass: Comparisons use interleaved event trials with raw samples, and one losing factor or shape is retained with an explanation.

Great: Block counts and residency are derived before the sweep.

Async proof

Pass: The nsys timeline shows overlap with event math below the sequential sum, graphs replay identically with update shown, and each defect names the locating tool line.

Great: Buffer lifetimes are drawn with stream dependencies, and no repair hides behind a global barrier.

Resources

  • Programming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12Wen-mei Hwu, David Kirk and Izzat El Hajj

    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.

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

    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

    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

    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

    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

    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

    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.

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

    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

    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.

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

    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.

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

    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.