Unit 3 of 10 · about 4 hours · derive, measure
Occupancy limits, registers, spills and launch resources
Registers and shared memory can save traffic while reducing residency. Occupancy is a resource bound to reconcile with measurements, not an optimization score.
You'll be able to
- Derive resident-block limits from threads, warps, blocks, registers and shared memory.
- Validate a compiled kernel's launch resources with device properties, compiler reports and the occupancy API.
- Sweep block size and resource use, keeping correctness and explaining why higher occupancy need not reduce time.
Covers
Read
In this order, only the parts listed
0 of 3 done
- PMPP, 4th edition: resource partitioningProgramming Massively Parallel Processors, 4th edition - Chapters 2 to 6
Section 4.7
- CUDA C++ Best Practices Guide 13.4: occupancy and local memoryCUDA C++ Best Practices Guide
Sections 10.2.4, 10.2.7.1, 11.1 to 11.4
Do not adopt Section 11.1's assertion that low occupancy always degrades performance. Section 11.3 itself explains that sufficient instruction-level parallelism can cover latency at low occupancy. Skip historical calculator screenshots.
- CUDA Programming Guide 13.4.2: launch and memory resource limitsCUDA Programming Guide
Sections 2.3.3.2.2 to 2.3.3.4 and 2.3.7
Focus on
- Calculate whole resident blocks, then resident warps divided by the SM's warp maximum. Include every resource limit and allocation granularity; a continuous bytes-per-thread estimate is not enough.
- Read
numRegsand static shared bytes from compiled-kernel attributes. Add dynamic shared bytes supplied at launch; compare the estimate withcudaOccupancyMaxActiveBlocksPerMultiprocessor. - Local memory is thread-private device memory, not an on-chip scratchpad. A local array is not necessarily a spill; inspect the compiler's spill report and final code.
- Compare event-timed, correctness-checked variants with warmup and interleaved repeated trials. If counters are permitted, inspect Nsight Compute's Occupancy section; otherwise report theoretical occupancy and timings separately.
Common mistakes · 3
- Using
sharedMemPerBlockorregsPerBlockas the SM capacity. Per-block legality and per-SM residency are separate checks. - Forcing a register cap and assuming the reduced register count is a free improvement. Spills or recomputation can erase the residency gain.
- Equating theoretical occupancy with achieved occupancy or the fraction of cycles doing useful work. A small grid or stalled warps can leave the machine underused.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why is TensorTonic's first example limited to four blocks rather than eight?Answer
Each block uses 8192 registers in the stated model. The 32768-register budget allows four blocks even though thread and warp budgets allow eight.
2.Can a declared local array be implemented in registers?Answer
Yes, if the compiler can scalarize its accesses and resources permit. A source declaration does not prove a particular final placement.
3.Why might allocating unused dynamic shared memory change time?Answer
It changes residency and possibly the L1/shared-memory carveout without changing useful arithmetic. Those effects can change latency hiding and caching, so this is not a perfectly isolated occupancy-only control.
Practice
- TensorTonic: GPU Occupancy Calculator
Implement
gpu_occupancy; work its three public cases: register-limited, shared-memory-limited and oversized blockThis free problem deliberately omits hardware allocation rounding beyond whole warps. Solve that stated model, not an undocumented approximation to an actual SM. The section mini-project covers the compiled-kernel sweep, spill diagnosis and per-block launch validation; compare the real kernel with the CUDA occupancy API.
You're done when
Your calculator matches the public examples, and your real-kernel sweep identifies each limiting resource, reconciles API versus hand estimates and retains timings without treating occupancy as speed.
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 2 to 6Wen-mei Hwu, David Kirk and Izzat El Hajj · primary
Read Chapters 2 to 6: data-parallel kernels, multidimensional grids, compute architecture and scheduling, memory architecture and data locality, and performance considerations. Work the chapter exercises on warps, occupancy, memory and coalescing, and predict three altered launch or address patterns before running them.
- CUDA Programming GuideNVIDIA · required
Read the Basics and Programming Model chapters plus the sections on memory, synchronization, hardware implementation and compute capabilities used by your kernels. Record the toolkit documentation version alongside every claim.
- CUDA C++ Best Practices GuideNVIDIA · required
Study Assess, Parallelize, Optimize, Deploy; Getting the Right Answer; memory optimization; execution configuration; instruction optimization; and deployment compatibility. Convert recommendations into hypotheses, not universal rules.
Also in this section
- MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 2 to 5 and Labs 2 and 3MIT · required
Use Lecture 2 (Throughput Processors), Lectures 3 and 4 (Memory, Parts 1 and 2) and Lecture 5 with its continuation (scratchpads, bank conflicts, register shuffles, overlap) for why the hardware looks as it does. Do Lab 2 (Massively-Parallel Mandelbrot, with its FMA-latency and warp-scheduler probes) and Lab 3 (Wave Simulation, with its coalesced-load and memory-latency probes) from the public lab repositories, built with nvcc for your own GPU instead of the course service. The labs were written for an RTX 4000 Ada, so recompute any SM count or bandwidth the questions assume.
- Parallel Thread Execution ISANVIDIA · reference
Read the PTX purpose, machine model, state spaces, memory consistency and target directives. Compile two tiny kernels, map source to PTX and SASS, and explain what PTX does and does not guarantee across devices.
- GPU MODE Foundations LecturesGPU MODE · support
Use Lectures 2, 3, 4, 5, 8, 37 and 41 for PMPP review, CUDA setup, architecture, optimization checklists, SASS and documentation reading. Reproduce only the examples that test a prediction from this stage.
- TensorTonic Language Modeling from Scratch - GPU analysis problemsTensorTonic · support
Solve GPU Occupancy Calculator, Shared-Memory Bank Conflict Analyzer and Global-Memory Coalescing Counter after the matching units, and check each answer against your own derivation.
- AI Performance Engineering - GPU FundamentalsWafer · reference
A curated index. Use Section 1 only to find a CUDA, PTX or architecture source the other entries lack.
- NVIDIA CUDA Pro Tips: grid-stride loops and vectorized memory accessNVIDIA · required
Read the grid-stride SAXPY loop and its scalability and debugging discussion (April 22, 2013), plus the scalar-copy SASS and vector-type alignment discussion in Increase Performance with Vectorized Memory Access (August 4, 2025). Skip the latter article's full vector-copy kernels: their tail-thread condition can omit remainders. These fill the pool's grid-stride and vector-width coverage gaps.
- CUDA Binary Utilities 13.4NVIDIA · reference
Sections 1.1 to 1.2, 2.1.1 to 2.1.3, 3.1.1 and 3.1.4 for cubin extraction, resource reports and line-correlated SASS. The existing compiler and PTX references lack a worked binary-tool workflow.
- PyTorch custom C++/CUDA operators and CUDA semanticsPyTorch · required
Custom C++ and CUDA Operators: build system, backend registration, Testing an operator and mutable schema; CUDA semantics 2.14: CUDA streams. These fill the native tensor-boundary gap in the CUDA-only pool. Stop before fake-kernel and autograd implementation; harden the tutorial's CUDA example for empty inputs, same-device checks, device guards, indexing width and launch errors.