5.4.8 · D4Memory Hierarchy & Caches

Exercises — Multi-level cache hierarchy (L1 - L2 - L3)

2,802 words13 min readBack to topic

These problems build from "read the formula" up to "design a hierarchy." Every solution is hidden inside a collapsible callout so you can test yourself first. Work each by hand, then reveal.

The tool we lean on throughout is the recursive AMAT formula from Multi-level cache hierarchy (L1 - L2 - L3). Let us re-anchor every symbol before using it — no symbol appears here that you have not seen defined.

One thing to watch, because it trips almost everyone:


Level 1 — Recognition

Exercise 1.1 (L1)

State, in words, what the quantity represents and give its units.

Recall Solution

WHAT it is: is a pure fraction (no units); is in cycles. Their product is in cycles. WHY: = the fraction of accesses that miss L1 and therefore must pay a visit to L2. Multiplying the chance of paying by the cost of paying gives the average (expected) cycles per access spent just entering L2. It is one term of the total AMAT — the "extra cost L2 adds on top of the guaranteed L1 hit time."

Exercise 1.2 (L1)

A single-level cache has , hit rate , . Write the AMAT formula and compute it.

Recall Solution

Formula (one level): . WHY this shape: every access pays (we always look in L1 first). A fraction miss and pay the full .


Level 2 — Application

Exercise 2.1 (L2)

Two-level cache: , , , , . Find AMAT.

Recall Solution

Inside-out. First: what does an L1-miss cost on average? It reaches L2 (pay 14), and a quarter of those miss on to DRAM (pay 120): WHY: this "44" is the average price of one L1 miss. Now weight it by how often L1 misses:

Exercise 2.2 (L2)

Full 3-level: , , , , , , . Find AMAT.

Recall Solution

Build the brackets from the deepest one outward. Innermost (cost of an L2 miss): Middle (cost of an L1 miss): Outer (the whole thing):


Level 3 — Analysis

Exercise 3.1 (L3)

Using the numbers from Exercise 2.2, an engineer proposes halving the L3 miss rate from to (better replacement policy) — orshaving 1 cycle off L1 hit time (4 → 3). Which change lowers AMAT more?

Recall Solution

Option A — better L3 (all else fixed): Innermost: Middle: Outer: cycles. Improvement vs. : about cycles.

Option B — faster L1 (, everything else as in 2.2): The nested brackets are unchanged (still ), only the base changes: Improvement vs. : exactly cycle.

WHY B wins so decisively: the L1 hit time is paid by 100% of accesses, so 1 cycle off = 1 cycle off AMAT, full stop. The L3 improvement only touches the of accesses that ever reach L3 — a huge relative gain there shrinks to a tiny absolute gain out front. Faster common case beats rarer misses.

Exercise 3.2 (L3)

You're given global miss rates: 6% of all accesses miss L1 (), 1.8% of all accesses miss L2 (), 0.27% of all accesses miss L3 (). With , compute AMAT. (Match it against Ex. 2.2.)

Recall Solution

WHY a different formula: a global miss rate already includes the probability of reaching that level (it is measured against all accesses). So in the global language we add each level's extra cost directly, weighted by its own global rate — we do not nest-multiply. The complete additive global formula: an access always pays . Whenever it globally misses a level it must probe the next level, paying that next level's hit time — and the fraction of accesses doing so is exactly the global miss rate of the level just left: Plugging in: Cross-check via local rates: convert with and reuse Ex. 2.2:

  • .
  • .
  • . These are exactly the local rates of Exercise 2.2, so the nested formula also gives . Lesson: global and local are two languages for the same hierarchy — the additive global form and the nested local form always agree.

Level 4 — Synthesis

Exercise 4.1 (L4)

A design has AMAT cycles (Ex. 2.2) and . Compute the speedup of the cached memory system over a no-cache machine, and use it to say what fraction of the CPU's peak performance the uncached machine would retain.

Recall Solution

Speedup = (no-cache time per access) / (cached time per access): WHY this is the right ratio: without any cache, every access is a full 180-cycle DRAM trip; with the hierarchy the average trip is 5.89 cycles. The ratio is how many times faster the memory system delivers data. Fraction of peak performance retained (uncached machine): if the CPU could otherwise issue useful work at its clock rate but each memory access stalls it for the access latency, then throughput scales as . The uncached machine's memory is slower, so on a memory-bound stream it retains only about of the throughput the cached machine achieves. Reading it: the hierarchy is doing ~97% of the heavy lifting — strip it out and a memory-bound workload crawls at roughly one-thirtieth speed. (We deliberately avoid quoting a single "effective MHz" number, since that depends on the exact mix of compute vs. memory work; the clean, assumption-free statement is the speedup itself.)

Exercise 4.2 (L4)

Design decision: you can spend a fixed transistor budget to add an L3 (turning a 2-level into 3-level) or to enlarge L2 so its local miss rate drops. Given the 2-level machine of Ex. 2.1 (AMAT ), option A adds an L3 with (so L2 misses now hit L3 first, then DRAM at ). Option B enlarges L2, dropping from to with rising to . Which is better?

Recall Solution

Recall Ex. 2.1: .

Option A (add L3): the L2-miss tail now goes through L3: Innermost: Middle: Outer: cycles.

Option B (bigger L2): stay 2-level, : Middle: Outer: cycles.

Verdict: A gives , B gives Option A (add L3) wins, barely. WHY: L3 shields the whole DRAM tail (that expensive 120) behind a 20%-only escape, whereas enlarging L2 helped its own miss rate but the surviving 12% still pays full DRAM. When the DRAM penalty is large, inserting a filtering level beats improving an existing one. The figure below makes the two AMATs directly comparable as stacked bars.

Figure — Multi-level cache hierarchy (L1 - L2 - L3)

Level 5 — Mastery

Exercise 5.1 (L5)

Derive the general break-even miss rate at which adding an L3 (hit time , miss rate ) is worthwhile compared to going straight to DRAM. That is: for an L2 miss, when is "detour through L3" faster than "straight to DRAM"?

Recall Solution

Compare the two tails an L2-miss could take:

  • Straight to DRAM: cost .
  • Through L3: cost . Adding L3 helps when: Solve for the break-even miss rate (set equal): Reading it: L3 pays for itself as long as its actual miss rate . Example: . So L3 helps up to a whopping 77.8% miss rate — almost any real L3 (10–30% miss) is easily worth it. WHY so lenient: even a mostly-missing L3 turns most trips into a cheap 40-cycle probe, and only the misses pay the 180. The detour is cheap; the shortcut it sometimes provides is huge.

Exercise 5.2 (L5)

An access stream: 70% loads, 30% stores. The L1 cache uses a write-through, no-write-allocate policy (see Write Policies). Reads (loads) use the normal 2-level path of Ex. 2.1, so a load costs the Ex. 2.1 AMAT. A store, under write-through/no-allocate, behaves as follows: it hits L1 in cycles (the store writes the L1 copy), and because the policy is write-through it must also push the write down to L2, paying the full L2 hit time cycles on top (no-write-allocate means a store that misses does not pull the line up into L1, so there is no extra fetch — the store simply completes at L2). Compute the effective average access time of the stream.

Recall Solution

Load cost (70% of the stream). A load takes the full Ex. 2.1 two-level path: (This is exactly the AMAT computed in Exercise 2.1 — units: cycles.)

Store cost (30% of the stream). Write-through means every store propagates to L2, so a store pays the L1 hit time plus the L2 hit time: No-write-allocate means a missing store does not fetch the line into L1 — it just lands at L2 — so there is no additional DRAM term. Units: cycles.

Blend by frequency. Loads and stores are two populations with different costs; the stream average is each cost weighted by how often it occurs: Reading it: stores are the minority (30%) yet contribute more than half the average, because write-through forces constant L2 traffic ( cycles each). This is exactly why real designs add write buffers to hide that store latency behind useful work.

Exercise 5.3 (L5)

Two cores share an inclusive L3. Core A writes a line; core B then reads it. Explain, using the AMAT framework, why the shared L3 makes B's access cheaper than if each core had a private L3, and estimate B's read cost using , , assuming the line is present in shared L3 (hit) versus absent (private-L3 case forces a coherence trip modeled as DRAM-latency ).

Recall Solution

Shared L3 (line is there): B misses its private L1/L2, then hits the shared L3. The tail cost of that access — the part beyond B's own private levels — is exactly the L3 hit time: The data A produced is sitting in the common L3, waiting for B to grab it.

Private L3 (line only in A's caches): B misses all of its own levels, and the coherence protocol must fetch A's copy across the interconnect — modeled here as a full memory-latency trip:

The saving:

WHY sharing wins here: a shared last-level cache turns an inter-core coherence miss (≈180, near DRAM) into an ordinary L3 hit (40). Producer→consumer data lives in one place that every core can cheaply reach, instead of being trapped in one core's private cache and hauled across the interconnect on demand. This is precisely why real designs make the LLC shared (see Cache Coherence): the latency that matters in multicore is not just latency-to-your-own-data but latency-to-another-core's-data, and sharing L3 collapses the latter from a coherence miss to a cache hit.


Recall Quick self-check ledger (reveal to confirm your numbers)

Ex 1.2 ::: 14 cycles Ex 2.1 ::: 6.52 cycles Ex 2.2 ::: 5.89 cycles Ex 3.1 ::: faster L1 wins (4.89 vs 5.647) Ex 3.2 ::: 5.89 cycles (additive global form and local form agree) Ex 4.1 ::: speedup ≈ 30.6×, uncached retains ≈ 3.3% of throughput Ex 4.2 ::: add L3 (5.40) beats bigger L2 (5.432) Ex 5.1 ::: m★ = 1 − T_L3/T_mem ≈ 0.778 Ex 5.2 ::: 9.664 cycles (loads 6.52, stores 17) Ex 5.3 ::: 140-cycle saving (40 vs 180)

Connections

Prerequisite ideas and neighbours: Cache Organization, Cache Replacement Policies, Write Policies, Cache Coherence, Memory Access Patterns, TLB and Virtual Memory, CPU Pipeline, DRAM Architecture, and the parent Multi-level cache hierarchy (L1 - L2 - L3).