---
title: Memory bandwidth and NUMA
heading: Bandwidth per core, misses in flight, non-uniform memory and the energy of moving data
eyebrow: S4 · Lesson 27 · Memory bandwidth and NUMA
xp: 50
---

::: hook
Core counts have grown faster than the pins that feed them, so a modern server's memory bandwidth, divided among its cores, is a few gigabytes a second each. Getting even that needs enough misses in flight to cover the latency, and in a multi-socket machine half the memory sits behind another processor, slower and narrower. Moving the data also costs more energy than computing with it. This lesson puts those limits into numbers: Little's law for memory, the NUMA penalty, and the energy budget of a byte.
:::

::: play
- The curve is the bandwidth a core or chip can sustain with N cache misses in flight, each fetching a 64-byte line from memory L nanoseconds away: $N \times 64/L$, capped by the peak bandwidth.
- Too few outstanding misses leave the memory idle however fast it is; past the knee, more misses only queue.
- The readout is the number of misses in flight needed to reach the peak, from Little's law.
:::

```sim plot
view: graph
x: N = 1..300
y: B = 0..250 GB/s
slider: L = 60..200 = 90 ns
slider: P = 50..400 = 100 GB/s
plot: N*64/L - (N*64/L - P)*step(N*64/L - P) | label: sustained bandwidth
readout: misses in flight for the peak = P*L/64 | digits: 4
```

::: predict
## A chip's memory delivers 100 GB/s with a 90 ns latency, in 64-byte lines. How many misses must be in flight at once to use all of it?

- [ ] About 14
- [x] About 140
- [ ] About 1400
- [ ] One per core

::: reveal
Little's law: data in flight = bandwidth × latency $= 100\text{ GB/s} \times 90\text{ ns} = 9000$ bytes, which is 141 lines of 64 bytes. A core with 10 to 20 miss-status registers can't do that alone; it takes many cores, or prefetchers, all missing at once.
:::
:::

::: nameit
## Bandwidth per core

Peak bandwidth = channels × transfers per second × 8 bytes. A 64-core server with 8 channels of DDR5-4800 has 307 GB/s, under 5 GB/s per core. A program needs about (bytes per instruction) × IPC × clock; data-heavy codes exceed their share and become bandwidth-bound.

## Little's law for memory

$$\text{misses in flight} = \frac{\text{bandwidth} \times \text{latency}}{\text{line size}}$$
Each core can track only as many misses as it has miss-status holding registers, so a single core's bandwidth is at most MSHRs × line ÷ latency.

## NUMA

In a multi-socket machine each socket has its own memory. A local access might take 90 ns and a remote one, crossing the socket link, 140 ns; with a fraction r of accesses remote, the average is $(1 - r)t_\text{local} + rt_\text{remote}$, and the link's bandwidth caps remote traffic. Operating systems allocate pages on the socket of the thread that first touches them (**first touch**) and can migrate them later.

## The energy of data movement

Rough figures for a recent process: a 64-bit floating-point operation costs tens of picojoules; reading 64 bits from a nearby SRAM, about ten; sending a bit a millimeter across the chip, about 0.1 pJ; reading a bit from DDR DRAM, 10 to 20 pJ; from stacked HBM, a few. Fetching an operand from DRAM costs far more energy than using it, which is why caches, blocking and near-memory computing save energy as well as time.
:::

::: example
## A 64-core server has 8 channels of DDR5-4800. What bandwidth does each core get, and is it enough for a core running at 3 GHz and IPC 2 that moves 0.5 bytes per instruction from memory?

::: solution
1. **One channel.**
   $$4800\ \text{MT/s} \times 8\ \text{B} = 38.4\ \text{GB/s}$$
2. **Per core.** Eight channels give 307 GB/s in all, so each core gets
   $$307/64 = 4.8\ \text{GB/s}$$
3. **What the core needs.**
   $$0.5 \times 2 \times 3\ \text{GHz} = 3\ \text{GB/s}$$
   It fits, with little to spare.
4. **A hungrier program.** Two bytes per instruction would need 12 GB/s, and the program would run at the bandwidth's pace instead.

**Answer:** each core gets 4.8 GB/s, enough for the 3 GB/s this core needs, with little to spare.
:::
:::

::: example
## A thread makes 30% of its memory accesses to the other socket, at 140 ns against 90 ns locally. What is its average latency?

::: solution
1. **Average latency.**
   $$\begin{aligned} t &= 0.7 \times 90 + 0.3 \times 140 \\ &= 105\ \text{ns} \end{aligned}$$
   which is 17% worse than all-local.
2. **A common trap.** If one thread initialized an array that all threads later use, first touch put the whole array on one socket, and half the threads see the remote latency on every access.
3. **The fix.** Initializing the array in parallel spreads it out.

**Answer:** the average latency is 105 ns, 17% worse than all-local.
:::
:::

::: example
## A kernel reads 1 GB from DDR at 15 pJ per bit and performs $10^9$ floating-point operations at 20 pJ each. Where does the energy go?

::: solution
1. **Reading.**
   $$\begin{aligned} E &= 8 \times 10^9\ \text{bits} \times 15\ \text{pJ} \\ &= 0.12\ \text{J} \end{aligned}$$
2. **Computing.**
   $$\begin{aligned} E &= 10^9 \times 20\ \text{pJ} \\ &= 0.02\ \text{J} \end{aligned}$$
3. **Compare.** Moving the data takes six times the energy of the arithmetic; reusing each byte from cache six times would make them equal.

**Answer:** most of the energy goes to moving data: 0.12 J for the reads against 0.02 J for the arithmetic.
:::
:::

::: check
kind: parts
given: c = 2 | 8 | 12; R = 3200 | 4800 | 6400; n = 16 | 64 | 96

## A server has {c} memory channels at {R} MT/s, 8 bytes wide, shared by {n} cores.

::: part
answer: c*R*8/1000
unit: GB/s
tolerance: 0.5%

### What is its peak bandwidth?

::: misses
- [c*R/1000] Each transfer moves 8 bytes.
- [any] Channels × rate × 8 bytes.
:::
:::

::: part
answer: c*R*8/1000/n
unit: GB/s
tolerance: 0.5%

### How much is that per core?

::: misses
- [any] Total over cores.
:::
:::
:::

::: check
kind: parts
given: B = 50 | 200 | 400; L = 80 | 120; m = 12 | 32

## A memory system delivers {B} GB/s at {L} ns latency in 64-byte lines; each core can have {m} misses outstanding.

::: part
answer: B*L/64
unit: lines
tolerance: 0.5%

### How many misses must be in flight to reach full bandwidth?

::: misses
- [B*L] Divide by the line size.
- [any] Bandwidth × latency ÷ line.
:::
:::

::: part
answer: m*64/L
unit: GB/s
tolerance: 0.5%

### What bandwidth can one core sustain?

::: misses
- [any] MSHRs × line ÷ latency.
:::
:::
:::

::: check
kind: parts
given: a = 80 | 100; b = 130 | 200; r = 0.1 | 0.3 | 0.5; T = 100 | 120

## Local memory takes {a} ns and remote memory {b} ns; a thread makes {r} of its accesses remote.

::: part
answer: (1 - r)*a + r*b
unit: ns
tolerance: 0.5%

### What is its average latency?

::: misses
- [any] Weighted average.
:::
:::

::: part
answer: (T - a)/(b - a)*100
unit: %
tolerance: 0.5%

### What fraction of remote accesses keeps the average at {T} ns?

::: misses
- [any] Solve $(1 - r)a + rb = T$.
:::
:::
:::

::: check
kind: parts
given: D = 1 | 10; e = 4 | 15; N = 1 | 20; f = 10 | 20

## A kernel reads {D} GB from DRAM at {e} pJ per bit and performs {N} × 10⁹ operations at {f} pJ each.

::: part
answer: D*8e9*e*1e-12
unit: J
tolerance: 0.5%

### How much energy does reading take?

::: misses
- [D*1e9*e*1e-12] Count bits, not bytes.
- [any] Bits × energy per bit.
:::
:::

::: part
answer: D*8*e/(N*f)
tolerance: 0.5%

### How many times the computing energy is that?

::: misses
- [any] Data energy over compute energy.
:::
:::
:::

::: check
kind: choice

## One thread allocates and zeroes a large array; then 32 threads on two sockets process it in parallel, and half of them run slowly. What happened, and what fixes it?

- [ ] The array is too large for the cache
- [x] First-touch placement put every page on the initializing thread's socket, so the threads on the other socket access it remotely; initializing it in parallel, by the threads that will use each part, places pages locally
- [ ] The threads are contending for a lock
- [ ] The second socket has a slower clock

::: misses
- [1] Both sockets' threads would suffer equally from that.
- [3] Nothing in the description shares a lock.
- [4] The asymmetry follows where the data lives.
:::
:::

::: check
kind: derivation
vars: B = 1e9..1e12, L = 5e-8..3e-7, S = 32..128, m = 4..64

## Memory delivers bandwidth B (bytes per second) at latency L in lines of S bytes; a core tracks m misses.

::: step
answer: B*L/S

### How many misses must be in flight for full bandwidth?
:::

::: step
answer: m*S/L

### What bandwidth can one core sustain?
:::

::: step
answer: B*L/(S*m)

### How many such cores does it take to saturate the memory?
:::

::: right
Right. Little's law ties bandwidth, latency and concurrency together: raise any two and the third must follow.
:::
:::

::: check
kind: python

## Write `sustained_bw(misses, line, latency_ns, peak_gbs)`: the bandwidth in GB/s that a given number of misses in flight sustain, capped at the peak; and `cores_to_saturate(peak_gbs, latency_ns, line, mshrs)`: the fewest cores, each with `mshrs` misses in flight, that reach the peak.

```python starter
def sustained_bw(misses, line, latency_ns, peak_gbs):
    return peak_gbs

def cores_to_saturate(peak_gbs, latency_ns, line, mshrs):
    return 1
```

```python solution
import math

def sustained_bw(misses, line, latency_ns, peak_gbs):
    return min(peak_gbs, misses * line / latency_ns)

def cores_to_saturate(peak_gbs, latency_ns, line, mshrs):
    per_core = mshrs * line / latency_ns
    return math.ceil(peak_gbs / per_core - 1e-12)
```

```python tests
assert abs(sustained_bw(16, 64, 90, 100) - 11.38) < 0.01, "16 misses at 90 ns: 11.4 GB/s."
assert sustained_bw(500, 64, 90, 100) == 100, "Past the knee, the peak."
assert cores_to_saturate(100, 90, 64, 16) == 9, "141 lines in flight need 9 cores with 16 each."
```

::: right
Right. Bytes per nanosecond are gigabytes per second, which keeps the arithmetic simple.
:::
:::

::: explain
## Why can a single core rarely use a whole chip's memory bandwidth, even when it does nothing but read memory?

::: model
By Little's law, the bandwidth a requester sustains equals the amount of data it has in flight divided by the latency. A core can have only as many misses outstanding as it has miss-status holding registers, typically 10 to 20, and each brings one 64-byte line per memory latency of about 100 ns. That's around 10 GB/s, while a server's memory system can deliver several hundred. To use it all, many cores (or aggressive prefetchers that issue requests ahead) must keep enough misses in flight together to cover the latency.
:::
:::

::: practice
::: check
given: c = 4 | 8; R = 4000 | 5600
answer: c*R*8/1000
unit: GB/s
tolerance: 0.5%

## Peak bandwidth of {c} channels at {R} MT/s?

::: misses
- [any] Channels × rate × 8 B.
:::
:::

::: check
given: B = 80 | 300; L = 70 | 110
answer: B*L/64
unit: lines
tolerance: 0.5%

## Lines in flight for {B} GB/s at {L} ns?

::: misses
- [any] $BL/64$.
:::
:::

::: check
given: r = 0.2 | 0.4; b = 150 | 180
answer: (1 - r)*90 + r*b
unit: ns
tolerance: 0.5%

## Average latency, 90 ns local, {b} ns remote, {r} remote?

::: misses
- [any] Weighted average.
:::
:::
:::
