GPU Parallel Programming: SIMT Execution, Warps, Shared SRAM, and the Roofline Model
Whereas a CPU dedicates vast die area to deep out-of-order branch predictors and giant L3 caches so a few sequential threads suffer minimal latency, a Graphics Processing Unit (GPU) dedicates its silicon to hundreds of Streaming Multiprocessors (SMs / Compute Units) running tens of thousands of lightweight threads in lockstep **Single Instruction, Multiple Threads (SIMT)** groups called **Warps**. Mastering warp divergence, coalesced HBM transactions, on-chip Shared SRAM tiling, and Samuel Williams’s Roofline Model unlocks 10×–50× faster custom AI and scientific kernels.
Key Highlights
- GPUs hide 400-cycle HBM memory latency NOT with giant caches alone, but via zero-overhead hardware context switching across dozens of resident 32-thread Warps per Streaming Multiprocessor.
- Memory Coalescing is rule #1 of GPU bandwidth: when 32 threads in a warp read 32 adjacent 4-byte floats, hardware merges them into a single 128-byte HBM sector burst; strided random reads waste up to 96% of memory bandwidth.
- Tiled Matrix Multiplication (`GEMM`) and `FlashAttention` stage blocks of matrices from slow HBM into fast on-SM Shared Memory (`SRAM`) and Tensor Core registers, reusing each fetched byte dozens of times.
- The **Roofline Model** plots Kernel Throughput (FLOPs/sec) against **Arithmetic Intensity** (`FLOPs / HBM Byte Moved`), immediately diagnosing whether a kernel is Memory-Bandwidth-Bound or Compute-Bound.
Asynchronous Tensor Memory Accelerators (TMA) and CUDA Graphs
Modern Hopper and Blackwell GPUs add dedicated hardware Tensor Memory Accelerators (TMA) and warp-specialized producer–consumer pipelines (used in FlashAttention-3): dedicated producer warps DMA-prefetch the next HBM matrix tile asynchronously into Shared SRAM while consumer warps keep Tensor Cores 100% busy on the current tile.
- Never call `.item()`, `.cpu()`, or `torch.cuda.synchronize()` inside a training or inference loop—blocking the host CPU stalls the asynchronous GPU command queue completely!
- Overlap data transfers (`cudaMemcpyAsync` on pinned `pin_memory=True` host pages) on a separate CUDA Stream concurrently with compute kernels.
- Profile with NVIDIA Nsight Compute / PyTorch Profiler before guessing optimizations—check whether SM Compute or HBM DRAM throughput is near ceiling.
References & Sources
- Kirk, David B.; Hwu, Wen-mei W.; El Hajj, Izzat — Programming Massively Parallel Processors: A Hands-on Approach (4th ed.), Morgan Kaufmann.
- Williams, Samuel; Waterman, Andrew; Patterson, David (2009) — Roofline: An Insightful Visual Performance Model for Multicore Architectures, Communications of the ACM.
- Tillet, Philippe; Kung, H. T.; Cox, David (2019) — Triton: An Intermediate Language and Compiler for Tiled Neural Network Computations, ACM MAPL.