The Roofline Model
A program can be limited by how fast the chip does arithmetic or by how fast memory feeds it data. Its arithmetic intensity, FLOPs per byte moved, decides which: attainable speed is , a slanted roof that flattens at the ridge point.
What the symbols mean
- Arithmetic intensity
Floating-point operations done per byte moved between memory and the chip.
What the symbols mean
- Attainable performance
The best FLOPs per second the program can reach.
- Peak compute
The most FLOPs per second the chip can do.
- Memory bandwidth
Bytes per second memory can deliver.
What the symbols mean
- Ridge point
The intensity where the two limits meet: below it a program is memory-bound, above it compute-bound.
Explanation
The roofline model is simply a way to tell whether a program is held back by the processor's arithmetic or by memory. Every calculation needs data, and memory can only deliver so many bytes per second. The key number is , the arithmetic intensity: how many floating-point operations (FLOPs, additions and multiplications on decimal numbers) the program does for each byte it brings in. Then says: you get the smaller of two limits, the chip's top arithmetic speed , or the bandwidth times how much work you do per byte. That's the whole model, two straight lines, and it's surprisingly powerful.
Deriving it. A program does FLOPs and moves bytes. The arithmetic alone takes at least seconds; the data alone takes at least . Even if they overlap perfectly, the run takes at least the larger of the two:
Performance is , so
Plot against and you get a slanted line rising with slope , then a flat ceiling at : a roofline. The corner where they meet is the ridge point, .
Memory-bound versus compute-bound. Take a chip with GFLOP/s and GB/s. The ridge is at FLOPs per byte. A program with sits on the slanted part: it's memory-bound, and only more bandwidth, or doing more work per byte, makes it faster. A faster processor won't help at all. A program with sits on the flat part: it's compute-bound. There's a graph further down with this exact roofline.
Vector addition versus matrix multiplication. Vector addition with 8-byte numbers does 1 FLOP per element but moves 24 bytes (read , read , write ): . On our chip that's GFLOP/s, about half a percent of peak. Nothing about the code is wrong; the operation just doesn't do enough work per byte.
Matrix multiplication of two matrices is the opposite. Each of the entries of the product is a dot product of length : multiplications and additions, about FLOPs, so about in all. But the three matrices hold only numbers, bytes. If each number crosses from memory only once (which takes careful use of the cache), then
which grows with . At , , far right of the ridge, so matrix multiplication can run at full peak. Each number fetched gets reused about times. That's why so much of scientific computing and machine learning is arranged as matrix multiplications.
A common mistake. It's tempting to count the bytes the code mentions rather than the bytes that actually cross from memory. A value reused from the cache doesn't cost bandwidth again, so the same arithmetic can have very different intensities depending on how the loops are ordered; that's the whole point of blocking (the last practice problem). The other slip is assuming a faster processor speeds up everything. For a memory-bound kernel, more GFLOP/s raises the flat roof, which the kernel never reaches.
See it
Worked examples
A chip has a peak of GFLOP/s and GB/s of memory bandwidth. Find its ridge point, then the attainable performance of (a) vector addition on 8-byte numbers, and (b) multiplying two matrices of 8-byte numbers, assuming each matrix crosses from memory once.
- 1
Ridge point: FLOPs per byte. The "per second" and the "giga" cancel.
A dot product of two long vectors of 8-byte numbers runs on the same chip. What is its attainable performance, and what memory bandwidth would it need to reach GFLOP/s?
- 1
Work per element: one multiplication and one addition into the running sum, so 2 FLOPs.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A processor has a peak of GFLOP/s and GB/s of memory bandwidth. Where is its ridge point, in FLOPs per byte?
A processor has a peak of GFLOP/s and GB/s of memory bandwidth. What is the attainable performance of a kernel with intensity FLOPs per byte, in GFLOP/s?
The operation runs on 4-byte numbers (2 FLOPs per element; read , read , write ). On a chip with GFLOP/s peak and GB/s bandwidth, what is its attainable performance, in GFLOP/s? Give 3 decimal places.
A machine has GFLOP/s peak and GB/s bandwidth. A matrix multiply of 8-byte matrices has intensity when each matrix crosses from memory once. What is the smallest for which it's compute-bound?
A kernel performs FLOPs and moves bytes on a chip with GFLOP/s peak and GB/s bandwidth. What is the shortest possible run time, in ms?
A kernel has intensity FLOPs per byte on a chip whose ridge point is . Which change would speed it up?
A program spends of its time in a memory-bound kernel (intensity FLOPs per byte) and in compute-bound code. The chip has a GFLOP/s peak, and its memory bandwidth is upgraded from to GB/s. What is the overall speedup? Give 3 decimal places.
The hardest one here. Get it, and you've got this lesson.
A chip has a GFLOP/s peak, GB/s bandwidth and a KiB (-byte) cache. A blocked matrix multiply works on tiles of 8-byte numbers: it needs three tiles in the cache at once, and for each tile product ( FLOPs) it loads two new tiles from memory. Using the largest power-of-two whose three tiles fit in the cache, what performance can it attain, in GFLOP/s?
Where it's used
The roofline is how performance engineers decide what to optimize on CPUs and GPUs: profile a kernel, compute its intensity, and see whether it's under the slanted roof (fix the memory access) or the flat one (fix the arithmetic). It explains why AI chips advertise huge FLOP/s but are often limited by memory bandwidth when generating text one word at a time, and why libraries for matrix multiplication spend so much effort on blocking for the cache.