S414 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 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.

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?

Name it

Three ways to multithread

KindSwitchesHidesCost
coarse-grainedon a long stallmisses to memoryC cycles to drain and refill the pipeline
fine-grainedevery cycle, round robinall stallseach thread runs 1/N as fast
simultaneous (SMT)issues from several threads in one cyclestalls and empty issue slotsshared 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 RR+L of the time. With N threads:

  • fine-grained: U=min⁡(1,NRR+L), saturating at N=R+LR;
  • coarse-grained, with C cycles lost per switch: U=min⁡(RR+C,NRR+L), saturating at N=R+LR+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.

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
  1. One thread. 20/220=9.1%
  2. Four threads. 4×9.1%=36%
  3. Fine-grained saturation. 220/20=11threads
  4. Coarse-grained limit. Utilization can't exceed 20/25=80% which it reaches at 220/25=8.8threads 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
  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.

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
  1. Saturation point. N=(R+L)/R=408/8=51threads
  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

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°.

Explain why

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

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.