C8 · Lesson 39 · Caches and pipelines, previewed
How real processors go faster
The processor in this course does one instruction per clock and reaches memory in the same clock. Real processors can't, for two reasons. Main memory (DRAM) is slow: a core running at a few GHz finishes an instruction in under a nanosecond, while DRAM takes tens of nanoseconds to answer. And one clock period long enough for a whole instruction is a slow clock. Two ideas recover most of the speed: a cache, a small fast memory next to the core holding the data used recently, and a pipeline, which overlaps instructions like an assembly line. This lesson is a first look at both; S4 covers them in depth.
Play
- The curve is the average time per memory access as the miss rate rises. Move the miss rate slider to the marker's spot and read the average.
- Make the miss penalty longer, as for slower DRAM. How much do a few percent of misses cost now?
- What miss rate doubles the average access time compared with a cache that always hits?
Predict
A cache answers in 1 ns. Misses go to DRAM and cost 60 ns more. If 5% of accesses miss, what's the average access time?
4 ns: every access pays the 1 ns hit time, and 5% of them also pay 60 ns, an average of 0.05 × 60 = 3 ns more. So even a 95% hit rate leaves accesses four times slower than a perfect cache. Miss rates matter a lot, which is why caches are built to exploit how programs actually use memory.
Name it
Caches
Locality. Programs reuse what they used recently (temporal locality: a loop's instructions, its counter) and use what's next to it (spatial locality: the next element of an array). A cache keeps recently used data in fast SRAM beside the core.
Lines. A cache stores memory in lines (blocks), commonly 64 bytes. A miss brings in the whole line, so the neighbors of the missed word arrive with it.
Average memory access time.
with m the fraction of accesses that miss.
Finding a line (direct-mapped). Each memory address can live in exactly one cache line. The address splits into three fields:
| tag | index | offset |
|---|---|---|
| the rest, stored to check it's the right line | which line, bits | which byte in the line, bits |
A 4 KiB cache with 16-byte lines has 256 lines: 4 offset bits, 8 index bits, and with 32-bit addresses a 20-bit tag. Set-associative caches let each address live in any of a few lines, which reduces the misses from two addresses fighting over one line.
Pipelines
Split the single-cycle path into stages with a register between each: fetch (IF), decode and read registers (ID), execute (EX), memory (MEM) and write back (WB). Each stage works on a different instruction at once:
| cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
lw | IF | ID | EX | MEM | WB | ||
add | IF | ID | EX | MEM | WB | ||
sub | IF | ID | EX | MEM | WB |
The clock period only has to cover the slowest stage, so it can be several times shorter. Each instruction still takes k cycles, but one finishes every cycle: N instructions in a k-stage pipeline take k + N − 1 cycles.
Hazards break the overlap:
- A data hazard: an instruction needs a result an earlier one hasn't written back yet. Forwarding passes the ALU's result straight to the next instruction; a load followed at once by a use of its value still costs a one-cycle stall.
- A control hazard: after a branch, the pipeline doesn't know which instruction comes next until the branch is resolved. Processors predict, and a wrong guess throws away the instructions fetched since.
Stalls raise the average cycles per instruction above 1: CPI = 1 + (stall cycles per instruction).
The ideal speedup of a k-stage pipeline (with a clock k times faster) over N instructions: short runs don't gain much, because the pipeline takes k cycles to fill.
Worked example
A cache hits in 1 ns and misses 4% of the time with a 60 ns miss penalty. What is the average memory access time?
Show the solution
- Average memory access time.
- Without the cache. Every access would take about 61 ns.
Answer 3.4 ns.
Worked example
Split the address 0x00012345 for a direct-mapped 4 KiB cache with 16-byte lines.
Show the solution
- Offset. 16-byte lines take 4 offset bits: 0x5.
- Index. 256 lines take 8 index bits: 0x34.
- Tag. The other 20 bits are the tag: 0x00012.
Answer tag 0x00012, index 0x34 and offset 0x5.
Worked example
A cache hits in 1 ns, and a miss costs 50 ns. What miss rate keeps the average access at 2 ns?
Show the solution
- Set up the condition.
- Solve it. , a hit rate of at least 98%.
- Sensitivity. Each extra percent of misses adds half a nanosecond, half the hit time again.
- A longer penalty. Doubling the miss penalty halves the miss rate you can afford, which is why caches matter more as memory falls further behind the processor.
Answer a miss rate of at most 2%, a hit rate of at least 98%.
Check
A cache hits in 1 ns, a miss costs 100 ns more, and 2% of accesses miss. What's the average access time?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A direct-mapped cache holds 4 KiB in 16-byte lines, for 32-bit addresses.
(a)
How many offset bits?
(b)
How many index bits?
(c)
How many tag bits?
Check
A 5-stage pipeline with no stalls runs 19 instructions. How many cycles from the first fetch to the last write back?
Check
On a 5-stage pipeline, 25% of instructions are loads, and half of those are followed at once by an instruction that uses the loaded value (1 stall cycle each). 10% are branches, and 40% of those are mispredicted (2 cycles lost each). What's the average CPI?
Check
A loop adds up a large array one 4-byte element at a time. Why does a cache with 64-byte lines speed up its loads even though each element is used only once?
Check
Match each term to what it means.
- Athe next instruction depends on a branch that hasn't been decided
- Ban instruction needs a result that isn't written back yet
- Cpassing a result straight from the ALU to the next instruction
- Dholding an instruction back a cycle, leaving a bubble
- Ethe block of memory a miss brings in
Check
A cache hits in 1 ns and a miss costs 40 ns.
(a)
What's the largest miss rate that keeps the average at 5 ns?
(b)
What's the average at a 1% miss rate?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A direct-mapped 16 KiB cache has 16-byte lines and 32-bit addresses.
(a)
How many offset bits?
(b)
How many index bits?
(c)
How many tag bits?
Explain why
A pipelined processor doesn't finish any single instruction faster than a single-cycle one. Why does it still run programs faster?
Each instruction still passes through every stage, so its own start-to-finish time is about the same (a little longer, because of the registers between stages). But the stages work on different instructions at the same moment: while one instruction executes, the next is being decoded and the one after is being fetched. Once the pipeline is full, one instruction finishes every cycle, and each cycle is only as long as the slowest stage rather than the whole path. So the throughput, instructions finished per second, rises by up to the number of stages, even though the latency of each instruction doesn't fall.
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.