Roofline model: compute- vs memory-bound
Compute arithmetic intensity (FLOPs per byte moved) and compare it with the hardware ratio of peak FLOPs to memory bandwidth. Use this to decide whether a kernel is compute-bound or memory-bound.
Builds on: Counting FLOPs of matrix multiplies
What the animation says
Two clocks
This GPU can do three hundred trillion operations per second. Last time, runtime was just work divided by that speed. But every number must first travel from memory to the arithmetic units, at two terabytes per second. So a kernel, one function on the GPU, runs two clocks at once: one for arithmetic, one for delivery. They overlap, so the slower clock sets the runtime. Here's a matrix times a vector, four thousand ninety-six wide. Which bar is longer?
One FLOP per byte
Delivery wins by a mile: about seventeen microseconds, against a tenth of a microsecond of arithmetic. Why? Each matrix entry is two bytes. It arrives, gets one multiply and one add, and is never used again. That's about one FLOP per byte delivered. This ratio is called arithmetic intensity: FLOPs divided by bytes moved. At one FLOP per byte, the arithmetic units sit idle over ninety-nine percent of the time.
The roofline
Plot achievable speed against intensity. On the left, speed is intensity times bandwidth: a rising slope. It can't pass peak compute, so it flattens into a roof at three hundred teraflops. The bend is the ridge point: peak divided by bandwidth. Here, three hundred over two, so one hundred fifty FLOPs per byte. Left of the ridge, a kernel is memory-bound. Right of it, compute-bound. Matrix-vector sits way down at one. Now swap in a GPU with double the FLOP rate, same bandwidth. What happens to matrix-vector?
Reuse climbs the slope
Same speed. The roof rose and the ridge moved right, but matrix-vector stays on the slope. Back to our GPU. To climb, reuse each loaded number: use m rows instead of one vector, so each matrix entry feeds m multiplies. Intensity becomes m times k times n, over the entries of all three matrices. The twos for FLOPs and bytes cancel. When m is much smaller than k and n, intensity is close to m. Sixty-four rows gives about sixty-two. Tap every row that makes it onto the roof.
Read it from the shapes
So you can tell before running anything. Count FLOPs, count bytes, divide. Compare that intensity with the GPU's ridge: peak over bandwidth, in the hundreds for modern GPUs. Below the ridge, time is bytes over bandwidth. Above it, time is FLOPs over peak. A huge FLOP count alone tells you nothing. And this assumes each number moves once. Real kernels move more. The fix is always the same: reuse every loaded number many times.
Work per byte delivered
The roofline rule
Matrix-vector: about 1 FLOP per byte
Grow m and climb onto the roof
P, BW = 300e12, 2e12 # FLOP/s, bytes/s
def roofline(m, k, n, nbytes=2):
F = 2 * m * k * n
byts = nbytes * (m*k + k*n + m*n)
I = F / byts
t_us = max(F / P, byts / BW) * 1e6
kind = 'compute' if I > P / BW else 'memory'
return round(I, 1), kind, round(t_us, 2)
for m in [1, 64, 512, 4096]:
print(m, roofline(m, 4096, 4096))
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses