---
title: The roofline model
heading: Arithmetic intensity, memory-bound and compute-bound kernels, ceilings and the ridge point
eyebrow: S4 · Lesson 31 · The roofline model
xp: 50
---

::: hook
A machine can do 1.5 trillion floating-point operations a second, and its memory delivers 100 billion bytes a second. Which limit does a given program hit? The roofline model answers with one picture. Each kernel has an arithmetic intensity, the operations it does per byte it moves from memory, and its attainable performance is the lower of two roofs: the compute peak, or the memory bandwidth times its intensity. The picture tells a programmer whether to optimize arithmetic or data movement, and tells an architect which kernels a machine is built for.
:::

::: play
- Attainable performance against arithmetic intensity on log scales: the sloped roof is bandwidth × intensity, the flat roof the compute peak. They meet at the **ridge point**, peak/bandwidth.
- The markers are typical kernels: DAXPY ($y = ax + y$, 1/12 flop per byte), sparse matrix-vector product (about 0.25), a stencil (about 0.5) and a blocked matrix multiply (several to tens).
- Lower ceilings (dashed) show the peak without SIMD or without FMA: a kernel above the ridge that doesn't vectorize hits those instead.
:::
:::

```sim plot
view: graph
x: I = 0.01..100 flop/B log
y: P = 1..10000 GFLOPS log
slider: F = 100..5000 = 1536 GFLOPS log
slider: B = 20..2000 = 100 GB/s log
plot: (F + B*I - abs(F - B*I))/2 | samples: 400 | label: attainable, GFLOPS
plot: (F/8 + B*I - abs(F/8 - B*I))/2 | samples: 400 | dashed | color: ch2 | label: without SIMD (1/8)
plot: (F/16 + B*I - abs(F/16 - B*I))/2 | samples: 400 | dashed | color: ch3 | label: without SIMD or FMA (1/16)
marker: x = 1/12 | label: DAXPY
marker: x = 0.25 | label: SpMV
marker: x = 0.5 | label: stencil
marker: x = 16 | label: blocked matrix multiply
readout: ridge point, flop/B = F/B
```

::: predict
## A machine peaks at 1536 GFLOPS with 100 GB/s of memory bandwidth. DAXPY does 2 operations per 24 bytes moved. What fraction of peak can it reach?

- [ ] About 50%
- [ ] About 10%
- [x] Under 1%
- [ ] 100% with enough SIMD

::: reveal
Under 1%: its intensity is 2/24 = 1/12 flop per byte, so 100 GB/s × 1/12 = 8.3 GFLOPS, 0.54% of the peak. No amount of vectorization helps, because the memory can't deliver the data faster. Only reusing data (raising the intensity, as blocking does) or a machine with more bandwidth would.
:::
:::

::: nameit
## The model

$$\text{attainable} = \min(P_\text{peak},\ B \times I), \qquad I = \frac{\text{operations}}{\text{bytes moved from memory}}$$

- **Ridge point** $I^* = P_\text{peak}/B$: kernels with $I < I^*$ are **memory-bound**, those above are **compute-bound**.
- The intensity counts bytes that actually move from DRAM (or from whichever level the roof represents), so caching and blocking raise it.
- A kernel's run time is at least $\max\left(\frac{\text{ops}}{P_\text{peak}}, \frac{\text{bytes}}{B}\right)$.

## Ceilings

Real code rarely reaches the roofs. **Compute ceilings** below the peak: no SIMD (divide by the lane count), no FMA (divide by 2), poor instruction mix. **Bandwidth ceilings** below the peak bandwidth: no prefetching, poor locality, NUMA effects. Separate roofs for L1, L2 and DRAM bandwidth give a hierarchical roofline.

## Typical intensities (double precision)

| Kernel | flop/byte |
|---|---|
| DAXPY | 2 / 24 = 0.083 |
| sparse matrix-vector | about 0.25 |
| 7-point stencil | about 0.5 |
| FFT | about 1 to 2 |
| matrix multiply blocked by B | about B/8 |
:::

::: example
## On the 1536 GFLOPS, 100 GB/s machine, where's the ridge, and what do DAXPY and a matrix multiply blocked by 32 and by 128 reach?

::: solution
1. **Ridge.**
   $$1536/100 = 15.4\ \text{flop/byte}$$
2. **DAXPY.** It does 2 flops per 24 bytes:
   $$100/12 = 8.3\ \text{GFLOPS}$$
3. **Matrix multiply intensity.** Blocking by B moves about $2N^3/B$ doubles for $2N^3$ operations, so
   $$I \approx B/8$$
4. **B = 32.** I = 4 flop/byte, so it's memory-bound:
   $$100 \times 4 = 400\ \text{GFLOPS}$$
5. **B = 128.** I = 16 flop/byte, above the ridge, so it's compute-bound at 1536 GFLOPS.
6. **Cache size.** Larger blocks need larger caches, which is why the last level of blocking targets the L2 or L3.

**Answer:** the ridge is at 15.4 flop/byte; DAXPY reaches 8.3 GFLOPS, and the matrix multiply 400 GFLOPS blocked by 32 and the full 1536 GFLOPS blocked by 128.
:::
:::

::: example
## A stencil updates $10^9$ points, each needing 8 operations and 16 bytes of DRAM traffic. How long does it take on that machine?

::: solution
1. **Intensity.**
   $$\begin{aligned} I &= 8/16 \\ &= 0.5\ \text{flop/byte} \end{aligned}$$
   below the ridge, so the attainable rate is $100 \times 0.5 = 50$ GFLOPS.
2. **Time.**
   $$\begin{aligned} t &= \frac{8 \times 10^9}{50 \times 10^9}\ \text{s} \\ &= 0.16\ \text{s} \end{aligned}$$
   the same as the time to move 16 GB at 100 GB/s.
3. **Compare with the arithmetic.** The arithmetic alone would take 5 ms; all the rest is waiting on memory.

**Answer:** the stencil takes 0.16 s.
:::
:::

::: example
## A machine peaks at 2 TFLOP/s with 200 GB/s of memory bandwidth. A kernel performs 4 flops per byte moved. Where does it sit, and what if blocking raises its intensity to 16?

::: solution
1. **Ridge.**
   $$2000/200 = 10\ \text{flop/byte}$$
2. **At an intensity of 4.** The kernel is memory-bound:
   $$200 \times 4 = 800\ \text{GFLOP/s}$$
   which is 40% of peak.
3. **At an intensity of 16.** It's past the ridge and can reach the full 2 TFLOP/s.
4. **What helped.** Optimizing its arithmetic would have done nothing at 4; reducing its traffic did everything.

**Answer:** at 4 flop/byte the kernel is memory-bound at 800 GFLOP/s (40% of peak); at 16 it reaches the full 2 TFLOP/s.
:::
:::

::: check
given: F = 500 | 1500 | 4000; B = 50 | 200 | 1000; I = 0.1 | 1 | 10
answer: (F + B*I - abs(F - B*I))/2
unit: GFLOPS
tolerance: 0.5%

## A machine peaks at {F} GFLOPS with {B} GB/s. What does a kernel of intensity {I} flop/byte attain?

::: right
Right. min(peak, B·I) = {answer} GFLOPS.
:::

::: misses
- [any] Not yet. The lower of the two roofs: the compute peak and bandwidth × intensity.
:::
:::

::: check
given: F = 800 | 2500 | 20000; B = 60 | 400 | 1500
answer: F/B
tolerance: 0.5%

## A machine peaks at {F} GFLOPS with {B} GB/s of memory bandwidth. Where's its ridge point?

::: right
Right. {answer} flop/byte.
:::

::: misses
- [B/F] Intensity is flops per byte: peak over bandwidth.
- [any] Not yet. Where B·I meets the peak.
:::
:::

::: check
given: o = 2 | 3 | 8; r = 2 | 3; w = 1 | 2; e = 4 | 8
answer: o/((r + w)*e)
tolerance: 0.5%

## A loop does {o} operations per element, reading {r} arrays and writing {w}, with {e}-byte elements and no reuse. What's its arithmetic intensity?

::: right
Right. {answer} flop/byte.
:::

::: misses
- [o/(r*e)] Writes move bytes too.
- [any] Not yet. Operations over bytes moved per element.
:::
:::

::: check
given: N = 1 | 10; o = 4 | 20; b = 8 | 32; F = 1000 | 3000; B = 100 | 500
answer: (N*o/F + N*b/B + abs(N*o/F - N*b/B))/2
unit: s
tolerance: 0.5%

## A kernel processes {N} billion elements, each {o} operations and {b} bytes of DRAM traffic, on a {F} GFLOPS, {B} GB/s machine. What's its minimum run time?

::: right
Right. The larger of compute and transfer time: {answer} s.
:::

::: misses
- [any] Not yet. Both the arithmetic and the data movement take time: max(ops/peak, bytes/bandwidth).
:::
:::

::: check
given: F = 1000 | 2500; B = 100 | 250; I = 0.25 | 2 | 50
answer: 100*(F + B*I - abs(F - B*I))/2/F
unit: %
tolerance: 0.5%

## On a {F} GFLOPS, {B} GB/s machine, what fraction of peak can a kernel of intensity {I} reach?

::: right
Right. {answer}%.
:::

::: misses
- [any] Not yet. Attainable over peak.
:::
:::

::: check
given: T = 16 | 64 | 128; e = 4 | 8
answer: T/e
tolerance: 0.5%

## A matrix multiply is blocked with {T} × {T} tiles of {e}-byte numbers. About what intensity does it reach, at $2N^3$ operations for $2N^3/T$ elements moved?

::: right
Right. $\frac{2N^3}{(2N^3/T)e} = T/e$ = {answer} flop/byte.
:::

::: misses
- [T] Convert elements to bytes.
- [any] Not yet. Operations over bytes.
:::
:::

::: check
kind: parts
given: P = 500 | 2000; B = 50 | 200; I = 0.25 | 2 | 16; O = 10 | 100

## A machine peaks at {P} GFLOP/s with {B} GB/s. A kernel of {O} G flops has an intensity of {I} flop/byte.

::: part
answer: P/B
unit: flop/byte
tolerance: 0.5%

### Where is the ridge point?

::: misses
- [any] $P/B$.
:::
:::

::: part
answer: B*I - (B*I - P)*step(B*I - P)
unit: GFLOP/s
tolerance: 0.5%

### What rate can the kernel reach?

::: misses
- [any] $\min(P, BI)$.
:::
:::

::: part
answer: O/(B*I - (B*I - P)*step(B*I - P))
unit: s
tolerance: 0.5%

### What is its shortest run time?

::: misses
- [any] Flops over the attainable rate.
:::
:::
:::

::: check
kind: derivation
vars: P = 100..20000, B = 10..2000, I = 0.01..100

## A machine peaks at P with bandwidth B; a kernel has intensity I.

::: step
answer: P/B

### What is the ridge intensity?
:::

::: step
answer: B*I - (B*I - P)*step(B*I - P)

### What rate does the kernel reach?
:::

::: step
answer: (B*I - (B*I - P)*step(B*I - P))/P

### What fraction of the peak is that?
:::

::: right
Right. Below the ridge, only bandwidth and intensity matter; above it, only the compute peak.
:::
:::

::: explain
## Why does adding SIMD units or cores not speed up a memory-bound kernel?

::: model
A memory-bound kernel spends its time waiting for data, not computing: at its arithmetic intensity, the memory system delivers bytes more slowly than the processor could consume the operations they feed. Its attainable performance is the bandwidth times its intensity, a number that doesn't include the compute peak at all. Wider SIMD units or more cores raise the flat roof of the roofline, but the kernel sits on the sloped roof, under the ridge point, so its performance doesn't move. What helps is raising its intensity, by reusing each byte more through blocking, fusing loops or using smaller data types, or giving the machine more bandwidth.
:::
:::

::: practice

::: check
given: F = 700 | 3500; B = 80 | 900; I = 0.2 | 5
answer: (F + B*I - abs(F - B*I))/2
unit: GFLOPS
tolerance: 0.5%

## Attainable: {F} GFLOPS, {B} GB/s, intensity {I}?

::: misses
- [any] min(F, B·I).
:::
:::

::: check
given: F = 1200 | 9000; B = 100 | 3000
answer: F/B
tolerance: 0.5%

## Ridge point: {F} GFLOPS, {B} GB/s?

::: misses
- [any] F/B.
:::
:::

::: check
given: o = 2 | 6; r = 1 | 3; w = 1; e = 4 | 8
answer: o/((r + w)*e)
tolerance: 0.5%

## Intensity: {o} ops, {r} reads, {w} write of {e}-byte elements?

::: misses
- [any] Ops over bytes.
:::
:::
:::
