---
title: Multithreading
heading: Coarse-grained, fine-grained and simultaneous multithreading, and how many threads hide a latency
eyebrow: S4 · Lesson 14 · Multithreading
xp: 50
---

::: hook
A core waiting 200 cycles for memory does nothing useful, and no amount of out-of-order hardware hides a miss that long within a single thread. Multithreading fills the gap with other threads' work. The core keeps several threads' registers and program counters and switches among them: on a long stall (coarse-grained), every cycle (fine-grained, as in GPUs), or by issuing from several threads in the same cycle (simultaneous multithreading, SMT, Intel's Hyper-Threading). The arithmetic of how many threads it takes to hide a latency is simple and explains why GPUs keep thousands of threads in flight while CPUs keep two.
:::

::: play
- Core utilization against the number of threads N, when each thread runs R cycles between misses and then waits L cycles.
- Fine-grained switching (solid) costs nothing per switch: utilization $NR/(R + L)$ rises until it reaches 1 at $N = (R + L)/R$.
- Coarse-grained switching (dashed) loses C cycles flushing the pipeline at each switch, so it saturates lower, at $R/(R + C)$. Raise L: more threads are needed, but the ceiling doesn't move.
:::
:::

```sim plot
view: graph
x: N = 1..32
y: U = 0..1
slider: R = 5..100 = 20 cycles
slider: L = 20..500 = 200 cycles
slider: C = 1..30 = 5 cycles
plot: (1 + N*R/(R + L) - abs(1 - N*R/(R + L)))/2 | samples: 400 | label: fine-grained utilization
plot: (R/(R + C) + N*R/(R + L) - abs(R/(R + C) - N*R/(R + L)))/2 | samples: 400 | dashed | color: ch2 | label: coarse-grained utilization
readout: threads to saturate (fine-grained) = (R + L)/R
```

::: predict
## Each thread runs 20 cycles, then misses for 200. With fine-grained multithreading and no switch cost, how many threads keep the core busy every cycle?

- [ ] 2
- [ ] 4
- [x] 11
- [ ] 200

::: reveal
11. While one thread waits 200 cycles, the other ten each run for 20, which covers the gap exactly: $(R + L)/R$ = 220/20 = 11. One thread alone keeps the core busy only 9% of the time. GPUs face misses of hundreds of cycles with short runs between them, which is why each of their cores holds dozens of warps at once.
:::
:::

::: nameit
## Three ways to multithread

| Kind | Switches | Hides | Cost |
|---|---|---|---|
| coarse-grained | on a long stall | misses to memory | C cycles to drain and refill the pipeline |
| fine-grained | every cycle, round robin | all stalls | each thread runs 1/N as fast |
| simultaneous (SMT) | issues from several threads in one cycle | stalls and empty issue slots | shared caches, queues and predictors |

Each thread needs its own program counter and architectural registers (renamed onto a shared physical register file in SMT); everything else is shared.

## The utilization model

A thread runs R cycles, then waits L cycles. Alone it keeps the core busy $\frac{R}{R + L}$ of the time. With N threads:

- fine-grained: $U = \min\left(1, \frac{NR}{R + L}\right)$, saturating at $N = \frac{R + L}{R}$;
- coarse-grained, with C cycles lost per switch: $U = \min\left(\frac{R}{R + C}, \frac{NR}{R + L}\right)$, saturating at $N = \frac{R + L}{R + C}$.

## SMT in practice

A wide out-of-order core rarely fills all its issue slots from one thread; a second thread fills some of the rest, typically adding 15 to 30% throughput. The threads compete for caches and queues, so each runs slower than it would alone. For latency-critical work that can be a bad trade; for throughput it's nearly free.
:::

::: example
## With R = 20 and L = 200 cycles, find utilization for 1 and 4 threads, the fine-grained saturation point, and the coarse-grained limits with C = 5.

::: solution
1. **One thread.**
   $$20/220 = 9.1\%$$
2. **Four threads.**
   $$4 \times 9.1\% = 36\%$$
3. **Fine-grained saturation.**
   $$220/20 = 11\ \text{threads}$$
4. **Coarse-grained limit.** Utilization can't exceed
   $$20/25 = 80\%$$
   which it reaches at
   $$220/25 = 8.8\ \text{threads}$$
   Beyond that, extra threads only wait.

**Answer:** 9.1% utilization with one thread and 36% with four; fine-grained saturates at 11 threads, and coarse-grained tops out at 80%, reached at 8.8 threads.
:::
:::

::: example
## A 4-wide SMT core runs two threads that each reach IPC 1.6 alone, and together reach 2.4. What's the throughput gain, and how much slower is each thread?

::: solution
1. **Throughput gain.**
   $$2.4/1.6 = 1.5$$
   times one thread's throughput.
2. **Each thread's speed.** Each thread now runs at 1.2 IPC:
   $$1.2/1.6 = 75\%$$
   of its solo speed.
3. **Each thread's time.** The program on each thread takes
   $$1.6/1.2 = 1.33$$
   times as long.
4. **Who gains.** A server running many independent requests gains; a single latency-critical thread loses.

**Answer:** throughput rises 1.5 times, but each thread runs at 75% of its solo speed and takes 1.33 times as long.
:::
:::

::: example
## Each thread on a throughput core issues 8 instructions between memory accesses that take 400 cycles. How many threads does fine-grained multithreading need to keep the core busy?

::: solution
1. **Saturation point.**
   $$\begin{aligned} N &= (R + L)/R \\ &= 408/8 \\ &= 51\ \text{threads} \end{aligned}$$
2. **Why GPUs do this.** That's why GPUs keep dozens of warps resident per core: each thread is often waiting, and the scheduler always finds one that's ready.

**Answer:** fine-grained multithreading needs 51 threads to keep the core busy.
:::
:::

::: check
given: R = 10 | 30 | 50; L = 100 | 300
answer: 100*R/(R + L)
unit: %
tolerance: 0.5%

## A thread runs {R} cycles, then waits {L} cycles for memory. What fraction of the time does it keep the core busy alone?

::: right
Right. $R/(R + L)$ = {answer}%.
:::

::: misses
- [100*R/L] The run time counts in the period too.
- [any] Not yet. Run time over run plus wait.
:::
:::

::: check
given: R = 8 | 16 | 40; L = 120 | 400
answer: (R + L)/R
tolerance: 0.5%

## Each thread runs {R} cycles between misses of {L} cycles. How many threads does fine-grained multithreading need to keep the core busy?

::: right
Right. $(R + L)/R$ = {answer}.
:::

::: misses
- [L/R] Count the running thread as well as the ones covering its wait.
- [any] Not yet. Enough runs to cover one run plus one wait.
:::
:::

::: check
given: R = 15 | 40; C = 3 | 10
answer: 100*R/(R + C)
unit: %
tolerance: 0.5%

## A coarse-grained multithreaded core loses {C} cycles at each switch, and threads run {R} cycles between misses. What's the highest utilization it can reach?

::: right
Right. $R/(R + C)$ = {answer}%.
:::

::: misses
- [100*(1 - C/R)] Each run of R cycles costs R + C.
- [any] Not yet. Useful cycles over useful plus switching.
:::
:::

::: check
given: N = 2 | 3 | 5; R = 20 | 30; L = 150 | 250
answer: 100*N*R/(R + L)
unit: %
tolerance: 0.5%

## {N} threads each run {R} cycles and wait {L}, with fine-grained switching. What's the core's utilization?

::: right
Right. $NR/(R + L)$ = {answer}%, below saturation.
:::

::: misses
- [100*R/(R + L)] Each thread adds its own share.
- [any] Not yet. N threads' shares add until they reach 100%.
:::
:::

::: check
given: p = 1.2 | 1.8; t = 1.8 | 2.6
answer: t/(2*p)
tolerance: 0.5%

## Two threads each reach IPC {p} alone; together on an SMT core they reach a total of {t}. What fraction of its solo speed does each thread keep?

::: right
Right. $\frac{t/2}{p}$ = {answer}.
:::

::: misses
- [t/p] That's the throughput gain; each thread gets half the total.
- [any] Not yet. Each thread's share over its solo IPC.
:::
:::

::: check
given: T = 2 | 8 | 48; A = 32 | 64; b = 64
answer: T*A*b/8/1024
unit: KB
tolerance: 0.5%

## A core holds {T} hardware threads, each with {A} architectural registers of {b} bits. How much register state is that?

::: right
Right. {answer} KB, before renaming.
:::

::: misses
- [A*b/8/1024] Every thread needs its own registers.
- [any] Not yet. Threads × registers × bytes.
:::
:::

::: check
kind: parts
given: R = 10 | 40; L = 100 | 400; N = 2 | 4 | 8; C = 4 | 16

## Each thread runs {R} cycles and then waits {L} cycles; the core has {N} threads.

::: part
answer: N*R/(R + L) - (N*R/(R + L) - 1)*step(N*R/(R + L) - 1)
tolerance: 0.5%

### What utilization does fine-grained multithreading reach?

::: misses
- [R/(R + L)] That's one thread alone.
- [any] $\min(1, NR/(R + L))$.
:::
:::

::: part
answer: (R + L)/R
tolerance: 0.5%

### How many threads would saturate it?

::: misses
- [any] $(R + L)/R$.
:::
:::

::: part
answer: R/(R + C)
tolerance: 0.5%

### What is coarse-grained multithreading's ceiling with {C} cycles lost per switch?

::: misses
- [any] $R/(R + C)$.
:::
:::
:::

::: check
kind: derivation
vars: R = 2..100, L = 10..1000, C = 1..30

## A thread runs R cycles, then waits L; a coarse-grained switch costs C cycles.

::: step
answer: R/(R + L)

### What utilization does one thread give?
:::

::: step
answer: (R + L)/R

### How many fine-grained threads saturate the core?
:::

::: step
answer: (R + L)/(R + C)

### How many coarse-grained threads reach its ceiling?
:::

::: right
Right. Switch overhead lowers the ceiling but also the number of threads needed to reach it.
:::
:::

::: explain
## Why do GPUs run dozens of threads per core while CPUs run one or two?

::: model
The number of threads needed to hide a latency is about the latency divided by how long each thread runs between stalls. GPU workloads issue long-latency memory accesses after only a few instructions each and are built to have huge numbers of independent threads, so GPUs keep dozens of warps per core and switch every cycle at no cost, hiding hundreds of cycles of memory latency without big caches or out-of-order logic. CPU workloads often have one or a few important threads, and CPUs hide latency inside a thread with large caches, prefetching and deep out-of-order windows. Adding a second SMT thread fills some leftover issue slots, but more threads would slow the threads that matter.
:::
:::

::: practice

::: check
given: R = 12 | 25; L = 180 | 350
answer: (R + L)/R
tolerance: 0.5%

## Threads to saturate: run {R}, wait {L}?

::: misses
- [any] $(R + L)/R$.
:::
:::

::: check
given: R = 20 | 50; C = 4 | 12
answer: 100*R/(R + C)
unit: %
tolerance: 0.5%

## Coarse-grained ceiling: run {R}, switch {C}?

::: misses
- [any] $R/(R + C)$.
:::
:::

::: check
given: N = 2 | 4; R = 15 | 25; L = 200 | 300
answer: 100*N*R/(R + L)
unit: %
tolerance: 0.5%

## Utilization of {N} threads, run {R}, wait {L}?

::: misses
- [any] $NR/(R + L)$.
:::
:::
:::
