VC hit: swap the VC line with L1's victim; no L2 access
VC miss: fetch from L2 into L1; L1 victim → VC; VC victim → L2
Direct-mapped hit time with associative conflict rate
Pseudo-associative caches: probe a primary index, then a re-hashed secondary slot on a miss.
Way-Predicting Caches
MIPS R10000 L2: reading and comparing all ways costs time and energy
Small table, indexed by address, predicts which way holds the line
Alpha 21264-like I-cache: each line stores a sequential way and a branch-target way prediction (chosen by jump control): wide fetch at direct-mapped speed
Early Restart and Critical Word First
Long blocks save tag area but raise miss penalty. CPU asked for Word 2:
Early restart: normal order; forward Word 2 as soon as it arrives
Critical word first: request Word 2 first, then wrap around; widely used
Non-Blocking (Lockup-Free) Caches
Blocking cache freezes the CPU on any D-miss, even if the next access would hit
Hit under miss
Needs full/empty bits on registers (in-order scoreboard) or out-of-order execution
Hit under multiple miss / miss under miss
Overlaps misses to lower effective penalty
Track many in-flight misses; merge secondary misses to a line already in flight
8 KB direct-mapped D$, 32 B blocks, 16-cycle miss. AMAT vs. outstanding misses allowed:
Outstanding misses
0 (blocking)
1
2
64
Floating point
0.68
0.52
0.34
0.26
Integer
0.24
0.20
0.19
0.19
FP code streams independent arrays and overlaps many misses; integer code is pointer-dependent and saturates after one
Check Your Understanding
1. Protocol: direct-mapped L1 + victim cache. An access misses L1 but hits the VC. What happens to the evicted L1 line?
The lines swap: the VC line moves into L1 and L1's evicted line takes its slot in the VC.
2. Workload: why does 1 → 64 outstanding misses cut AMAT a lot for FP code (0.52 → 0.26) but barely for integer code (0.20 → 0.19)?
FP loops issue many independent array accesses that overlap; integer code has pointer/control dependences that stall before misses can pile up.
Prefetching
Speculate on future instruction and data accesses; bring them in early
Instructions are much easier to predict than data
Issues:
Usefulness: prefetches should become hits
Timeliness: not too late, not too early
Pollution: uses bandwidth and can evict useful lines
Alpha AXP 21064 instruction prefetch
I-miss to line i: put i in L1I$, i+1 in a separate stream buffer
Miss in L1I$ but hit in stream buffer: move i+1 to L1I$, prefetch i+2
Hardware Data Prefetching
Scheme
Rule
Prefetch-on-miss
Miss to b → prefetch b+1
One-block lookahead (OBL)
Any access to b → prefetch b+1 (generalizes to N blocks)
Strided
b, b+N, b+2N → prefetch b+3N
Spatial memory streaming (SMS)
Offsets a, b, c seen in a region of size R; touching a+R → prefetch b+R, c+R
Pointer-chasing (CDP, DMP)
p, *p, **p → prefetch ***p, ****p
OBL vs. doubling block size: b and b+1 stay separate blocks (or in a stream buffer), so no extra conflicts and unused lookahead lines are replaced independently
Apple M-series: stride + SMS + data-memory-dependent prefetchers; reading memory contents to form addresses can leak secrets
Software Prefetching
for (i = 0; i < N; i++) {
prefetch(&a[i + P]);
prefetch(&b[i + P]);
SUM = SUM + a[i] * b[i];
}
Timing is the hardest problem, not predictability
$P$ too small: arrives too late
$P$ too large: pollutes the cache
Right $P$ is hard to pick: L2/DRAM latency varies at run time
Extra prefetch instructions add overhead
Compiler Optimizations
Restructure code to change the address stream: zero hardware cost
Mark single-use streaming data no-allocate (dead-line replacement) so it doesn't evict reusable data
Three loop transformations:
Loop interchange: spatial locality
Loop fusion: temporal locality
Cache tiling (blocking): temporal locality
Loop Interchange
x[i][j] stored row-major: x[i][0], x[i][1], … share a cache line
// Before: inner loop walks
// down a column (stride N)
for (j = 0; j < N; j++)
for (i = 0; i < M; i++)
x[i][j] = 2 * x[i][j];
// After: inner loop walks
// along a row (stride 1)
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
x[i][j] = 2 * x[i][j];
Strided column misses become sequential unit-stride hits
Loop Fusion
// Before: loop 1 writes all of
// a[], evicting early elements
// before loop 2 reads them
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] reused
// while still hot
for (i = 0; i < N; i++) {
a[i] = b[i] * c[i];
d[i] = a[i] * c[i];
}
Matrix Multiply with Cache Tiling
Naive x[i][j] += y[i][k] * z[k][j]: each (i, j) sweeps a row of y and a column of z
For large N, z doesn't fit: every i re-fetches all of z from DRAM
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;
}
Pick B so the B×B tiles fit; the z tile stays resident across all N rows: DRAM traffic drops ~B×
Check Your Understanding
1. Concept: why is an OBL prefetcher with a stream buffer better than doubling the block size?
Doubling halves the number of lines (more conflicts) and wastes bandwidth on unused halves; OBL keeps small blocks and stages prefetches without polluting the cache.
2. Locality: which locality do loop interchange, loop fusion, and cache tiling each mainly improve?
Interchange: spatial. Fusion and tiling: temporal.
← → / space to move · A to reveal answers · F fullscreen