CUDA Memory Hierarchy & Access Patterns
Coalesced global memory access, tiling with shared memory, bank conflicts, warp shuffle intrinsics, and asynchronous memory prefetching (TMA). The techniques behind FlashAttention and high-performance GEMM.
Overview
Why memory access decides kernel speed
Memory access patterns are the single most impactful factor in GPU kernel performance. A kernel with poor HBM access patterns can reach only 10% of hardware peak bandwidth, while a well-optimized one reaches 90%+.
In a well-written kernel, data takes one path through the memory levels.
- Coalesced loads, issued with cp.async or TMA, bring data from HBM, the GPU's global memory ~500 cycles away, into shared memory.
- Shared memory, ~10-30 cycles away and managed by software, feeds the registers with bank-conflict-free reads.
- Threads in a warp trade values directly between their registers with __shfl_sync, without touching memory at all.
- Results go back to HBM, written once.
Five techniques keep data on that path, and each one targets a single level.
- Coalescing works on global memory and stops wasted cache-line transactions.
- Shared memory tiling works between global and shared memory, and removes repeated HBM reads of reused data.
- Padding works in shared memory and removes bank conflicts.
- Warp shuffles work in registers and replace barrier-heavy shared-memory reductions.
- cp.async and TMA work between global and shared memory, and keep loads from stalling compute.
The rest of this chapter takes them in that order.
Interview questions
- Walk through a tiled matrix multiplication kernel -- how does it improve performance over the naive version?
- Explain FlashAttention and why it dramatically reduces memory usage compared to standard attention.
- How do you identify and fix a shared memory bank conflict?
Sign up free to read the full topic
More AI Infrastructure topics
- AI Accelerator Software Stacks: TPU, Trainium, MTIA & Bring-up
- Bare-metal Provisioning & Fleet Firmware
- Batch GPU Scheduling on Kubernetes: Kueue, Volcano & Gang Scheduling
- CI for the Accelerator Software Stack: Compiler, Kernels & Runtime
- CUDA Kernel Profiling, Occupancy & the Roofline
- Design Data & IP Release Management
- Disaggregated Prefill-Decode Architecture
- Distributed Training: NCCL, Collectives & Parallelism
- DPUs in GPU Clusters: BlueField & DOCA
- EDA in the Cloud: Hybrid Bursting, Storage & IP Security
- EDA Licenses & Compute Farms
- Embedded Linux & BSP Builds at Scale: Yocto, Caches & Compliance
Start free on Cariara · Coding problems · System design studio