Repository navigation
Computer Architecture 09 multithreading
Relevant source files
The following references were used as context for generating this wiki page:
- Wikipedia: Multithreading (computer architecture)
- Wikipedia: Simultaneous multithreading
- Book: Hennessy and Patterson, Computer Architecture: A Quantitative Approach, chapter 3
A single thread often leaves a core idle: it waits for a cache miss, recovers from a mispredicted branch, or simply has no independent instruction ready. Hardware multithreading lets one core hold the state of several threads at once and switch to another thread whenever the current one stalls, so the expensive execution units stay busy. To the operating system each hardware thread looks like a separate CPU.
Timelines on this page are illustrative.
This page covers what a hardware thread is, the difference between software and hardware threads, three styles of multithreading (coarse-grained, fine-grained, simultaneous), what is shared and duplicated, the benefits and costs, and how GPUs take the same idea much further. Multiple separate cores are on Multicore.
A chef is cooking one dish and puts it in the oven for 20 minutes. Rather than stand and wait, the chef starts chopping for a second dish, and comes back to the first when the timer rings.
| Kitchen | Core |
|---|---|
| The chef | The execution units of one core |
| Each dish with its own recipe and progress | A hardware thread: its own registers and PC |
| Waiting for the oven | Waiting for a cache miss |
| Switching to another dish | Switching to another thread |
| One stove shared by all dishes | Caches and execution units shared by all threads |
The chef doesn't get faster at any single dish, but finishes more dishes per hour.
| Software thread | Hardware thread | |
|---|---|---|
| Created by | The program (pthread_create, std::thread) |
Built into the core |
| Switched by | The operating system, thousands of cycles | The hardware, 0 to a few cycles |
| How many | As many as you like | Fixed: often 1, 2 or 4 per CPU core; many dozens per GPU core |
The operating system schedules software threads onto hardware threads. A 4-core chip with 2 hardware threads per core shows 8 "logical CPUs".
flowchart TD
subgraph SW["Software threads (many)"]
T1["T1"]
T2["T2"]
T3["T3"]
T4["T4"]
T5["T5"]
end
OS["Operating system scheduler"]
T1 --> OS
T2 --> OS
T3 --> OS
T4 --> OS
T5 --> OS
subgraph Core0["Core 0"]
H0["HW thread 0"]
H1["HW thread 1"]
end
subgraph Core1["Core 1"]
H2["HW thread 0"]
H3["HW thread 1"]
end
OS --> H0
OS --> H1
OS --> H2
OS --> H3
| Duplicated per hardware thread | Shared between threads on a core |
|---|---|
| Program counter | Execution units (ALUs, load/store, FP/SIMD) |
| Architectural registers (or their rename table) | Caches (L1, L2) and TLBs |
| Some status / system registers | Branch predictor tables |
| Issue queues, reorder buffer, physical registers (split or shared) |
Duplicating a register file and a PC is cheap compared with a whole core, so a second hardware thread costs only a small amount of chip area.
Each diagram row is one cycle; each box is an issue slot of a 4-wide core. A, B, C, D are four threads.
flowchart LR
subgraph ST["Single thread"]
direction TB
s1["A A . ."]
s2[". . . . (cache miss)"]
s3[". . . . (cache miss)"]
s4["A . . ."]
end
subgraph CG["Coarse-grained"]
direction TB
c1["A A . ."]
c2["B B B . (switched on miss)"]
c3["B B . ."]
c4["B . . ."]
end
subgraph FG["Fine-grained"]
direction TB
f1["A A . ."]
f2["B B B ."]
f3["C . . ."]
f4["D D . ."]
end
subgraph SMT["Simultaneous (SMT)"]
direction TB
m1["A A B C"]
m2["B B B D"]
m3["C D D A"]
m4["A B C D"]
end
| Style | When it switches | Fills | Example uses |
|---|---|---|---|
| Coarse-grained (switch on event) | Only on a long stall such as a cache miss | Long stalls (vertical waste) | Some older and embedded designs |
| Fine-grained (interleaved) | Every cycle, round-robin between ready threads | Short and long stalls | Barrel processors, some DSPs, GPU warp schedulers |
| Simultaneous multithreading (SMT) | No switching: instructions from several threads issue in the same cycle | Stalls and unused slots within a cycle (horizontal waste) | Intel Hyper-Threading, AMD Zen, some Arm Neoverse and Cortex cores, IBM POWER |
Vertical waste is a whole cycle with nothing issued; horizontal waste is a cycle where only some issue slots are used. Only SMT removes both.
Sources: Wikipedia: Multithreading (computer architecture), Wikipedia: Simultaneous multithreading
SMT fits naturally into the out-of-order engine from ILP: renaming turns each thread's registers into physical registers, so instructions from different threads can sit in the same issue queue without confusion.
flowchart LR
FA["Fetch thread A"] --> R["Rename<br/>(one table per thread)"]
FB["Fetch thread B"] --> R
R --> IQ["Shared issue queue"]
IQ --> EX["Shared execution units"]
EX --> ROBA["Commit thread A in order"]
EX --> ROBB["Commit thread B in order"]
Each thread still commits its own instructions in its own program order; the threads simply share the machinery in between.
| Benefit | Cost |
|---|---|
| Higher total throughput when threads stall a lot (memory-bound, branchy code) | Each thread runs somewhat slower than if it had the core alone |
| Cheap: a fraction of a core's area | Threads compete for caches, predictor entries and queues |
| Hides memory latency without software changes | Little gain for code that already uses the core fully (dense maths) |
| Shared structures can leak information between threads (side channels), so some systems disable SMT for security |
Typical gains for two threads per core range from nothing to a few tens of percent, depending on the workload. It is never 2×, because the two threads share one set of execution units.
A GPU applies fine-grained multithreading at huge scale: each GPU core keeps dozens of thread groups (warps or wavefronts) resident at once and picks a ready one every cycle. When one group waits hundreds of cycles for memory, others run. This is how GPUs hide memory latency without large caches or out-of-order logic. See SIMD and GPUs.
sequenceDiagram
participant S as Warp scheduler
participant W1 as Warp 1
participant W2 as Warp 2
participant W3 as Warp 3
S->>W1: issue (load from memory)
Note over W1: waiting ~hundreds of cycles
S->>W2: issue
S->>W3: issue
S->>W2: issue
S->>W3: issue
Note over W1: data arrives
S->>W1: issue (uses loaded data)
- A hardware thread is a PC plus a register set; several share one core's execution units and caches.
- Coarse-grained switches on long stalls, fine-grained every cycle, SMT issues from several threads in the same cycle.
- Multithreading raises throughput, not single-thread speed; GPUs push the same idea to dozens of threads per core.
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look