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

S4 · Lesson 19 · Advanced cache optimizations

Faster hits, memory-level parallelism, prefetching, victim caches and blocking in software

The average memory access time has three terms, hit time, miss rate and miss penalty, and architects have a bag of tricks for each. Way prediction and pipelining shorten hits. Victim caches and prefetchers cut misses. Non-blocking caches let several misses overlap, so the penalty is paid once for many of them. And some of the largest gains come from software: restructuring a loop so it reuses data while it's still in the cache can cut a matrix multiply's memory traffic by a factor of thirty without changing the hardware at all.

  • Words moved between cache and memory for an N × N matrix multiply, divided by N3, against the block size B of a blocked (tiled) version: about 2/B+1/N.
  • The unblocked loop (B = 1) moves about 2 words per multiply-add. Blocking reuses each loaded block B times, cutting traffic by about B.
  • B is limited by the cache: three B × B blocks must fit in C words, so B≤C/3 (the marker).

Predict

A program misses 10 times in a stretch, each miss taking 200 cycles. If the cache can keep 4 misses outstanding at once and the core has enough independent work, about how many stall cycles do the 10 misses cost?

Name it

Faster hits

  • Small, simple L1s with low associativity; way prediction guesses which way will hit and reads only it. With prediction accuracy p, hit time = phfast+(1−p)hslow.
  • Pipelined cache access raises bandwidth (and adds cycles of latency); banks let several accesses proceed at once.

Fewer or cheaper misses

  • Victim cache: a small fully associative buffer of recently evicted lines catches conflict misses of a direct-mapped cache.
  • Hardware prefetching: stride and stream prefetchers detect regular patterns and fetch ahead. To hide a latency L with iterations taking t cycles, prefetch ⌈L/t⌉ iterations ahead.
  • Critical word first and early restart shorten the effective penalty.
  • Non-blocking caches: with an average of MLP misses overlapped, the stall per miss is about penalty/MLP.
  • Merging write buffers combine writes to the same line.

Compiler and programmer optimizations

  • Loop interchange walks arrays in the order they're stored.
  • Blocking (tiling) works on sub-blocks that fit in the cache. For an N × N matrix multiply, the unblocked version moves about 2N3+N2 words in the worst case; with B × B blocks, about 2N3B+N2. Three blocks must fit: 3B2≤C.

Worked example

A 1024 × 1024 matrix multiply in doubles runs with a 32 KB cache (4096 doubles). What's the largest block, and how much traffic does blocking with B = 32 save?

Show the solution
  1. Largest block. 4096/3=36.95 so a power-of-two block of 32.
  2. Unblocked traffic. W=2×10243+10242=2.15×109words
  3. Blocked traffic. W=2×10243/32+10242=6.8×107words
  4. Saving. That's 31.5 times less traffic for identical arithmetic. Libraries block at several levels (registers, L1, L2) for the same reason.

Answer B can be at most 36.95, so the largest power-of-two block is 32, and blocking with B = 32 cuts the traffic 31.5 times.

Worked example

A loop takes 25 cycles per iteration and memory latency is 200 cycles. How far ahead should it prefetch? And with way prediction 90% accurate, a 1-cycle fast hit and a 3-cycle mispredicted hit, what's the hit time?

Show the solution
  1. Prefetch distance. ⌈200/25⌉=8iterations ahead, so the data arrives just as it's needed (a little more allows for variation).
  2. Hit time with way prediction. t=0.9×1+0.1×3=1.2cycles against perhaps 2 cycles for reading every way of a set-associative cache in parallel.

Answer prefetch 8 iterations ahead; way prediction gives a 1.2-cycle hit time.

Worked example

A program makes 10 last-level misses per thousand instructions, each costing 200 cycles. What stall CPI results if misses are serviced one at a time, and if four overlap on average?

Show the solution
  1. One at a time. 10/1000×200=2.0 cycles per instruction.
  2. Four overlapping. With memory-level parallelism of 4 the effective cost per miss is about 50 cycles: 10/1000×50=0.5 cycles per instruction.
  3. The lesson. Non-blocking caches and enough MSHRs turn a latency problem into a bandwidth problem.

Answer 2.0 stall cycles per instruction one at a time, and 0.5 when four overlap.

Check

A 512 × 512 matrix multiply is blocked with 16 × 16 tiles. By what factor does the worst-case traffic, 2N3+N2 against 2N3/B+N2, fall?

Check

A 32 KB cache holds 4-byte elements. What's the largest whole B such that three B × B blocks fit?

Check

8 independent misses of 150 cycles each overlap 8 at a time. About how many stall cycles do they cost?

Check

Memory latency is 250 cycles and each loop iteration takes 15. How many iterations ahead should the loop prefetch?

Check

Way prediction is 0.85 accurate; a correct guess hits in 1 cycle(s) and a wrong one in 4. What's the average hit time?

Check

A direct-mapped cache misses 0.04 of the time; 0.3 of its misses are conflicts, and a victim cache catches 0.7 of those. What's the new miss rate?

Check

An N × N matrix multiply (N = 1024) is blocked into 16 × 16 tiles.

(a)

About how many words does the unblocked version move in the worst case?

(b)

And the blocked version?

(c)

How much cache do three tiles of doubles need?

Check

A loop takes 10 cycles per iteration against 150 cycles of memory latency. Way prediction is 0.95 accurate, with 2-cycle predicted hits and 3-cycle mispredicted ones.

(a)

How far ahead should the loop prefetch?

(b)

What is the average hit time?

Check

A program misses k times per instruction, each costing P cycles, with M misses overlapped on average.

Step 1

What stall CPI does it see with blocking misses?

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 does blocking a matrix multiply cut its memory traffic without changing how many arithmetic operations it does?

Practice

Keep practicing

11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.