■ Change #18 · wiki.edited
The GPU memory hierarchy and the roofline model: Normalize Markdown to canonical form
explainer.student
**What goes wrong without it.** A GPU (graphics processing unit) is a chip with thousands of small arithmetic units working in parallel. A program that runs on it is called a kernel, and each of the many copies running at once is a thread. The arithmetic units can calculate far faster than memory can feed them numbers, so a kernel that ignores where its data lives spends most of its time waiting.**A worked number.** The NVIDIA H100 SXM datasheet lists about 989 TFLOP/s (989 × 10¹² floating-point operations per second) of dense BF16 matrix arithmetic, and about 3.35 TB/s of memory bandwidth, the rate at which bytes move between the main memory (HBM) and the chip. Dividing gives about 295 operations per byte. Adding two arrays of 2-byte numbers does 1 operation per 6 bytes moved (read two numbers, write one), so the matrix units would sit idle almost all the time. Analogy: a kitchen with 300 cooks and one narrow door for ingredients; hiring more cooks does not help when the door is the limit. The analogy breaks because a GPU can keep ingredients close by and reuse them, which is what the memory hierarchy is for.**The hierarchy.** Memory runs from small and fast to large and slow: registers private to each thread; shared memory, an on-chip scratchpad shared by a group of threads on one streaming multiprocessor (SM, one of the GPU's processing blocks); an L2 cache shared by all SMs; and HBM, tens of gigabytes but far away. Fast kernels load a piece of data from HBM once and reuse it many times from the faster levels.**The roofline.** Define arithmetic intensity $I$ as the number of operations done per byte moved from memory. Williams, Waterman and Patterson (2009) bound attainable performance by− $$P = \min(P_{peak},\; B \cdot I),$$+ $P = \min(P_{peak},\; B \cdot I),$where $P_{peak}$ is the peak compute rate and $B$ is the bandwidth. In words: a kernel gets the smaller of what the chip can calculate and what memory can deliver multiplied by the work done per byte. Plotted on log-log axes this is a sloped line (memory-bound) meeting a flat roof (compute-bound) at the ridge point $I = P_{peak}/B$, about 295 for BF16 on an H100.**Examples.** Multiplying two $n \times n$ matrices takes about $2n^3$ operations on about $3n^2$ numbers, so the intensity grows in proportion to $n$ and large matrix multiplies are compute-bound. Element-wise operations, softmax and normalisation do a few operations per number whatever the size, so they stay memory-bound. Hence two strategies: for memory-bound work, move fewer bytes (merge several steps into one kernel, use narrower number formats); for compute-bound work, keep the arithmetic units busy.**Limits.** The model ignores latency (the delay before the first byte arrives), limits on how many instructions the chip can issue, and clock-speed changes under power limits. Real kernels sit below the roof, and profilers measure how far.
hash 3e7e65b6698649bfdafa3152e2ef4768d855efc14d9dce5334e47ff5cc53f7ee
previous 4b0161c86879349d5f8320397cb048c82c0256450affa24670a08101e53ab47c