S4 · Lesson 14 · Multithreading
Coarse-grained, fine-grained and simultaneous multithreading, and how many threads hide a latency
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.
- 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 rises until it reaches 1 at .
- Coarse-grained switching (dashed) loses C cycles flushing the pipeline at each switch, so it saturates lower, at . Raise L: more threads are needed, but the ceiling doesn't move.
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?
- While one thread waits 200 cycles, the other ten each run for 20, which covers the gap exactly: = 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.
Name it
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 of the time. With N threads:
- fine-grained: , saturating at ;
- coarse-grained, with C cycles lost per switch: , saturating at .
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.
Worked 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.
Show the solution
- One thread.
- Four threads.
- Fine-grained saturation.
- Coarse-grained limit. Utilization can't exceed which it reaches at 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.
Worked 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?
Show the solution
- Throughput gain. times one thread's throughput.
- Each thread's speed. Each thread now runs at 1.2 IPC: of its solo speed.
- Each thread's time. The program on each thread takes times as long.
- 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.
Worked 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?
Show the solution
- Saturation point.
- 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
A thread runs 30 cycles, then waits 100 cycles for memory. What fraction of the time does it keep the core busy alone?
Check
Each thread runs 16 cycles between misses of 120 cycles. How many threads does fine-grained multithreading need to keep the core busy?
Check
A coarse-grained multithreaded core loses 3 cycles at each switch, and threads run 15 cycles between misses. What's the highest utilization it can reach?
Check
3 threads each run 20 cycles and wait 250, with fine-grained switching. What's the core's utilization?
Check
Two threads each reach IPC 1.2 alone; together on an SMT core they reach a total of 1.8. What fraction of its solo speed does each thread keep?
Check
A core holds 8 hardware threads, each with 32 architectural registers of 64 bits. How much register state is that?
Check
Each thread runs 10 cycles and then waits 100 cycles; the core has 8 threads.
(a)
What utilization does fine-grained multithreading reach?
(b)
How many threads would saturate it?
(c)
What is coarse-grained multithreading's ceiling with 16 cycles lost per switch?
Check
A thread runs R cycles, then waits L; a coarse-grained switch costs C cycles.
Step 1
What utilization does one thread give?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many fine-grained threads saturate the core?
Step 3
How many coarse-grained threads reach its ceiling?
Explain why
Why do GPUs run dozens of threads per core while CPUs run one or two?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.