Lecture 7: Memory 3

Section 1: Reducing Hit Time and Miss Penalty — Victim Caches, Way Prediction, Critical Word First, and Non-Blocking Caches

Recalling \text{AMAT} = \text{Hit time} + \text{Miss rate} \times \text{Miss penalty}, we now examine five architectural techniques targeting Hit time, Miss penalty, and conflict Miss rate.

1. Victim Caches (Reducing Conflict Misses Without Hurting Hit Time)

Direct-mapped L1 caches have the fastest Hit time (no way-mux!), but suffer high conflict misses. In the HP 7200, architects added a tiny Victim Cache—a 4-block fully-associative backup cache holding lines recently evicted from the direct-mapped L1:

2. Way-Predicting Caches (Reducing Set-Associative Hit Time)

In a set-associative cache (like the MIPS R10000 L2), reading and comparing all ways takes time and energy. A Way-Predicting Cache maintains a small prediction table indexed by the processor address that guesses which way holds the line:

3. Early Restart and Critical Word First (Reducing Miss Penalty of Long Blocks)

Long cache lines (e.g., 64 bytes) save tag area, but waiting for the entire block to arrive word-by-word from DRAM increases Miss penalty. Instead of waiting for all 4 words (Word 0, Word 1, Word 2, Word 3) when the CPU asked for Word 2:

4. Non-Blocking (Lockup-Free) Caches (Increasing Bandwidth & Hiding Miss Penalty)

A simple blocking cache freezes the entire CPU on any D-cache miss—even if the very next instruction hits in the D-cache! A Non-Blocking (Lockup-Free) Cache lets the D-cache keep servicing hits during an outstanding miss:

✎ Check Your Understanding

  1. Protocol check: In a direct-mapped L1 paired with a Victim Cache (VC), what happens to the evicted L1 line when a requested address misses in L1 but hits in the VC? Answer: The two lines are swapped: the hit line moves from the VC into L1, and the line evicted from L1 moves into that slot in the VC.

  2. Workload check: Why does increasing a non-blocking cache from 1 outstanding miss to 64 outstanding misses slash AMAT dramatically for floating-point code (0.52 -> 0.26) while barely affecting integer code (0.20 -> 0.19)? Answer: Floating-point code contains long loops of independent array accesses that can overlap many memory misses in parallel, whereas integer code has tight pointer/control dependencies that quickly stall before many misses can accumulate.


Section 2: Reducing Miss Rate — Hardware/Software Prefetching and Compiler Locality Optimizations

Our final group of optimizations attacks Miss rate—specifically compulsory and capacity misses—by either fetching data before the CPU asks for it (Prefetching) or restructuring loops so data stays hot in cache (Compiler Optimizations).

Prefetching: Hardware and Software

Prefetching speculates on future instruction and data accesses and brings them into caches ahead of demand (instructions are much easier to predict than data). Every prefetcher must balance three issues:

  1. Usefulness: Prefetched blocks should actually turn into hits.
  2. Timeliness: Not too late (miss penalty not hidden) and not too early (evicted before use).
  3. Cache and bandwidth pollution: Speculative fetches consume memory bandwidth and can evict useful lines!

Hardware Instruction Prefetching (Alpha AXP 21064): On an I-cache miss to line i, fetch two lines: place i into L1I, and place `i+1` into a small separate **Stream Buffer** (avoiding polluting L1I if a branch jumps away!). If the next fetch misses L1I$ but hits the Stream Buffer, move i+1 into L1I$ and prefetch i+2.

Hardware Data Prefetching Schemes:

Software Prefetching: Inserts explicit non-blocking prefetch(&a[i + P]); instructions into loops:

for (i = 0; i < N; i++) {
    prefetch(&a[i + P]);
    prefetch(&b[i + P]);
    SUM = SUM + a[i] * b[i];
}

Here, timing—not predictability—is the hardest problem: setting lookahead distance P too small arrives too late; setting P too large pollutes the cache; and guessing P is hard because DRAM/L2 latency varies dynamically—on top of the instruction-count overhead of executing extra prefetch instructions!

Compiler Optimizations for Locality

Compilers can eliminate misses with zero hardware cost by restructuring loops (or marking single-use streaming data with “no-allocate” hints / dead-line replacement so streaming data doesn’t evict reusable data):

  1. Loop Interchange (Improves Spatial Locality): Suppose a 2D array x[i][j] is stored in row-major order (where x[i][0], x[i][1], ... sit in the same cache line):

    // Before: inner loop jumps down columns (stride N*sizeof(elem))!
    for (j = 0; j < N; j++)
        for (i = 0; i < M; i++)
            x[i][j] = 2 * x[i][j];
    
    // After: inner loop walks across rows (stride 1 = full spatial locality!)
    for (i = 0; i < M; i++)
        for (j = 0; j < N; j++)
            x[i][j] = 2 * x[i][j];

    Swapping the loops turns strided column misses into sequential unit-stride cache hits.

  2. Loop Fusion (Improves Temporal Locality):

    // Before: first loop writes all of a[0..N-1], evicting early elements before loop 2!
    for (i = 0; i < N; i++) a[i] = b[i] * c[i];
    for (i = 0; i < N; i++) d[i] = a[i] * c[i];
    
    // After: a[i] and c[i] are reused immediately while still hot in cache/registers!
    for (i = 0; i < N; i++) {
        a[i] = b[i] * c[i];
        d[i] = a[i] * c[i];
    }
  3. Matrix Multiply Cache Tiling / Blocking (Improves Temporal Locality): In naive matrix multiply x[i][j] += y[i][k] * z[k][j], each (i, j) inner k-loop sweeps across an entire row of y and down an entire column of z. For large N, all N^2 elements of z exceed cache capacity, so every single iteration of i re-fetches the entire matrix z from DRAM! Cache Tiling breaks the computation into B \times B blocks (choosing B so the active B \times B tiles of x, y, and z fit inside the cache at once):

    for (jj = 0; jj < N; jj = jj + B)
      for (kk = 0; kk < N; kk = kk + B)
        for (i = 0; i < N; i++)
          for (j = jj; j < min(jj + B, N); j++) {
            r = 0;
            for (k = kk; k < min(kk + B, N); k++)
              r = r + y[i][k] * z[k][j];
            x[i][j] = x[i][j] + r;
          }

    Now the B \times B sub-matrix of z (and tiles of y and x) stays resident in cache across all N rows of i, cutting DRAM traffic by roughly a factor of B!

[Editor Note: removed repeated roadmap slides, GoFetch administrative banner, and acknowledgements.]

✎ Check Your Understanding

  1. Concept check: Why is a hardware One-Block Lookahead (OBL) prefetcher using a stream buffer better than simply doubling the cache block size? Answer: Doubling the block size cuts the number of cache lines in half (increasing conflict misses) and wastes bandwidth when only part of a block is used, whereas OBL keeps smaller blocks and puts prefetched blocks in a stream buffer without polluting the cache.

  2. Locality classification: Identify which type of locality (spatial or temporal) is primarily improved by Loop Interchange, Loop Fusion, and Cache Tiling. Answer: Loop Interchange primarily improves spatial locality (unit-stride row access); Loop Fusion and Cache Tiling primarily improve temporal locality (reusing a[i]/c[i] or a B \times B matrix tile while it remains in cache).