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

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?

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.

AMAT=thit+m⋅tmiss penalty

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:

tagindexoffset
the rest, stored to check it's the right linewhich line, log2⁡(lines) bitswhich byte in the line, log2⁡(line size) 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:

cycle1234567
lwIFIDEXMEMWB
addIFIDEXMEMWB
subIFIDEXMEMWB

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
  1. Average memory access time. t=1+0.04×60=3.4ns
  2. 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
  1. Offset. 16-byte lines take 4 offset bits: 0x5.
  2. Index. 256 lines take 8 index bits: 0x34.
  3. 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
  1. Set up the condition. 1+m×50≤2
  2. Solve it. m≤2%, a hit rate of at least 98%.
  3. Sensitivity. Each extra percent of misses adds half a nanosecond, half the hit time again.
  4. 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.

  1. Athe next instruction depends on a branch that hasn't been decided
  2. Ban instruction needs a result that isn't written back yet
  3. Cpassing a result straight from the ALU to the next instruction
  4. Dholding an instruction back a cycle, leaving a bubble
  5. 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?

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.