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

S4 · Lesson 23 · Cache lab

Classifying misses into the three Cs, blocking a matrix multiply, and fitting the √2 rule, in Python

The three Cs, blocking and the √2 rule from lessons 18 to 20 are all statements about miss counts, and miss counts are what a cache simulator produces. This lab writes a set-associative LRU simulator and uses it three ways: to split a trace's misses into compulsory, capacity and conflict by simulating an infinite cache, a fully associative one and the real one; to measure how much blocking cuts a matrix multiply's misses; and to fit the exponent of the √2 rule to measured miss rates.

  • The lines are miss rate against cache size on log–log axes for the rule m=m0(S/S0)−α; the slider sets α.
  • α = 0.5 is the √2 rule: each doubling of the cache divides the miss rate by √2. Programs with little reuse beyond a working set show smaller α until the working set fits, then a cliff.
  • The lab fits α to measured points by least squares on the logarithms.

Predict

A 256 × 256 matrix of 4-byte integers, stored by rows, is read down its columns through a 32 KB, 4-way cache with 64-byte lines. Its 65 536 accesses all miss. How do those misses split into the three Cs?

Name it

The simulator

For a cache of S bytes, B-byte lines and W ways: block = address ÷ B, set = block mod (S/(BW)), tag = block ÷ (S/(BW)); each set is an LRU list of up to W tags.

The three Cs, measured

  • Compulsory = distinct lines touched (an infinite cache's misses).
  • Capacity = fully associative LRU cache of the same size's misses − compulsory.
  • Conflict = the real cache's misses − the fully associative cache's.

Blocking

A matrix multiply's inner loop touches a row of A and a column of B; once N grows past what the cache holds, B's column lines are evicted before reuse. Working on tiles small enough that three fit in the cache reuses each line many times.

Fitting α

On log axes the rule is a line: ln⁡m=ln⁡m0−αln⁡(S/S0), so α is minus the least-squares slope of ln⁡m against ln⁡S.

Check

Write misses(addrs, size, line, ways): an LRU set-associative cache simulator returning the miss count, and three_c(addrs, size, line, ways): return (compulsory, capacity, conflict).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write mm_trace(N, block): the address trace of C=C+AB for N × N matrices of 8-byte doubles at base addresses 0, 220 and 221, reading A[i][k] and B[k][j] in the inner loop and writing C[i][j] after it; with block = None use plain i, j, k loops, otherwise tile the j and k loops in blocks of that size (jj, kk outermost, then i, j, k).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write fit_alpha(sizes, rates): the least-squares exponent α in m∝S−α.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

How does simulating a fully associative cache of the same size separate capacity misses from conflict misses?