All courses

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

▶ Watch this lesson Start the whole course

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.

Why does a 300 TFLOP/s GPU crawl on some matmuls?

Last time you estimated runtime as FLOPs ÷ peak FLOP rate. For many real **kernels** (single functions run on the GPU, such as one matmul), that estimate is **over 100× too optimistic**. Every number must also travel from GPU memory to the arithmetic units, and that path is slow compared to the arithmetic. So which limit sets the runtime, the arithmetic or the data movement? And can you tell **before** running the code, from the shapes alone?

Work per byte delivered

Picture a fast assembly line fed by one delivery truck. If each delivered part needs lots of work, the line is the bottleneck. If each part needs only a moment of work, the line sits idle waiting for the truck. A kernel (one GPU function) works the same way. Moving data and doing arithmetic overlap, so runtime is set by whichever is slower. The deciding number is **FLOPs done per byte moved**.

The roofline rule

Arithmetic intensity $I = F/\text{Bytes}$ ($F$ = FLOPs, Bytes = data read + written). $$t \approx \max\left(\tfrac{F}{P},\ \tfrac{\text{Bytes}}{BW}\right)$$ $P$ = peak FLOP/s, $BW$ = memory bandwidth (bytes/s). The attainable rate is $\min(P,\ I\cdot BW)$. Plotted against $I$, it is a sloped line that then flattens into a flat roof at $P$. The **ridge point** is $I^*=P/BW$. If $I<I^*$ the kernel is memory-bound. If $I>I^*$ it is compute-bound. For an $(m\times k)(k\times n)$ matmul with 2-byte numbers: $I=\frac{mkn}{mk+kn+mn}$.

Matrix-vector: about 1 FLOP per byte

GPU: $P=300$ TFLOP/s, $BW=2$ TB/s, so $I^*=150$. Multiply $(1\times4096)(4096\times4096)$ with 2-byte numbers: - $F=2\cdot4096^2\approx33.6$ million FLOPs - Bytes $=2(4096+4096^2+4096)\approx33.6$ MB, so $I\approx1$ - $t_{\text{mem}}=33.6\text{MB}/2\text{TB/s}\approx16.8\,\mu s$ - $t_{\text{comp}}\approx0.11\,\mu s$ $1\ll150$, so it is memory-bound and runs at about 0.7% of peak. Each entry of the big matrix is loaded and then used only once.

Grow m and climb onto the roof

With $k=n=4096$, more rows $m$ reuse each loaded matrix entry $m$ times. Output: $m=1$ gives $I\approx1$ (memory). $m=64$ gives $I\approx62$ (memory). $m=512$ gives $I\approx410$ (compute). $m=4096$ gives $I\approx1365$ (compute). This assumes each number is moved once, which is the ideal case. Real kernels move more.
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

Arithmetic intensity = FLOPs ÷ bytes moved. Compare it with the GPU's ridge point $P/BW$, which is in the hundreds for modern GPUs. Below the ridge, time ≈ bytes ÷ bandwidth, and extra FLOP/s does not help. Above the ridge, time ≈ FLOPs ÷ peak. You raise intensity by reusing each loaded number many times.

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.