Unit 4 of 10 · about 4 hours · derive, measure
Global memory, caches, transactions and coalescing
Threads that are adjacent in a tensor need not request adjacent bytes. The address pattern of each instruction determines coalescing; caches then determine how much of that traffic reaches device memory.
You'll be able to
- Derive the aligned memory sectors touched by each warp instruction, including offsets, strides and access widths.
- Distinguish useful bytes, requested sectors, cache hits and traffic reaching device memory.
- Measure paired access patterns and explain discrepancies between sector predictions and elapsed time.
Covers
Read
In this order, only the parts listed
0 of 4 done
- PMPP, 4th edition: coalescing and memory latencyProgramming Massively Parallel Processors, 4th edition - Chapters 2 to 6
Section 6.1, Figures 6.1 to 6.4; Section 6.2 through Figure 6.8
Stop before the toy channel assignment in Figure 6.9. Use CUDA's sector model below rather than treating a DRAM burst as the transaction unit of every GPU.
- CUDA C++ Best Practices Guide 13.4: aligned, offset and strided accessesCUDA C++ Best Practices Guide
Sections 10.2.1.1 to 10.2.1.4
- NVIDIA: vectorized memory access, August 4, 2025NVIDIA CUDA Pro Tips: grid-stride loops and vectorized memory access
Opening scalar copy kernel and SASS, then the vector-type and alignment discussion through the aligned
int2pointer-offset exampleStop before the structure example and full vector-copy kernels. Their remainder handler relies on a thread whose index can lie outside the launched grid, so it can skip the tail. Treat performance improvements as hypotheses, not guarantees.
Focus on
- For compute capability 6.0 and later, count distinct aligned 32-byte sectors touched by the active lanes of one instruction. Permuting the same addresses does not change that count.
- Count every byte of a wide access, including sector crossings. A wider instruction may reduce instruction count without reducing useful bytes or aggregate sectors for the same work.
- L1 is local to an SM and L2 is shared across SMs. Requested sectors are not measured DRAM bytes; cache reuse can make the timing penalty smaller than a per-warp sector ratio.
- A base allocation's alignment does not guarantee an offset view's alignment. Validate the address and tail before using
int2orint4loads.
Common mistakes · 3
- Calling a 128-byte cache line one 32-byte sector. TensorTonic's configurable line model and the CUDA sector model use different transaction units.
- Counting only starting addresses for vector accesses. A load can touch several sectors.
- Calling a copy memory-bound from its source alone. Small arrays can be launch-limited, and cache-resident arrays have a different traffic boundary.
Try it yourself
Closed-book, before checking anything
kernellab, compare bounds-safe offset and stride copy variants with an independent reference and event timings; do not convert those sector ratios directly into speedups.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.If lanes reverse the order of 32 adjacent four-byte words at an aligned base, does the sector count change?Answer
No. The same four sectors are touched; coalescing depends on the address set, not lane order.
2.Why can an offset copy lose less time than its five-versus-four sector ratio suggests?Answer
Adjacent warps can reuse overlapping sectors in cache. The per-warp request count is not the total traffic reaching DRAM.
3.A warp loads one consecutive 16-byte object per lane from a 32-byte-aligned base. How many sectors cover its byte range?Answer
Sixteen sectors cover 512 bytes. Wider per-lane loads do not make that byte range fit in four sectors.
Practice
- TensorTonic: Global-Memory Coalescing Counter
Implement
coalescing_analysis; work the adjacent-word, separated-line and line-crossing public casesThis free problem counts each requested byte once even where lanes overlap, with a default 128-byte line. Solve its stated model, then repeat your own cases with
cache_line_bytes=32for the CUDA sector analysis. Its line-crossing case is an abstract byte interval, not permission to issue a misaligned typed CUDA load. The section mini-project covers the paired GPU copies and timings.
You're done when
Your counter handles overlaps and crossings, the three sector predictions agree with your derivation, and your paired-copy report separates requested sectors from measured time and any unavailable DRAM counters.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 7 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.
- 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.
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.
- 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.