---
title: Multicore cache hierarchies
heading: Inclusion, replacement policies, distributed last-level caches and partitioning
eyebrow: S4 · Lesson 20 · Multicore cache hierarchies
xp: 50
---

::: hook
A multicore chip's last-level cache is shared by every core, split into slices spread across the chip, and managed by policies that decide which lines stay. Whether it duplicates what the private caches hold, how it picks victims, how far away the right slice is, and how its capacity is divided among competing programs together decide much of the chip's memory performance. LRU, the textbook policy, turns out to fail badly on common access patterns, and a slice at the far corner of the chip can take twice as long as one next door.
:::

::: play
- The curve is a working set's miss rate in a shared cache when it gets only a share of the capacity, by the √2 rule: $m_0\sqrt{S_0/S}$ for a program that misses $m_0$ when it has the whole cache $S_0$ to itself.
- Each co-runner that takes an equal share raises the program's miss rate: four equal sharers double it.
- Way partitioning gives each program a guaranteed share, so a cache-hungry neighbor can't push it further.
:::

```sim plot
view: graph
x: s = 0.05..1
y: m = 0..0.2
slider: a = 0.005..0.05 = 0.02
plot: a*sqrt(1/s) | label: miss rate with a share s of the cache
```

::: predict
## Eight cores each have a 1 MB private L2 in front of a 16 MB shared L3. How much distinct data can the hierarchy hold if the L3 is inclusive, and if it is exclusive?

- [x] 16 MB inclusive, 24 MB exclusive
- [ ] 24 MB both ways
- [ ] 16 MB both ways
- [ ] 8 MB inclusive, 16 MB exclusive

::: reveal
An inclusive L3 holds a copy of everything in the L2s, so the distinct data fits in its 16 MB. An exclusive L3 holds only lines that aren't in any L2, so the L2s' 8 MB adds to it: 24 MB. Inclusion wastes capacity but makes coherence simple, since a miss in the L3 proves no L2 has the line.
:::
:::

::: nameit
## Inclusion

- **Inclusive:** every line in a private cache is also in the LLC. Snoops can check the LLC alone, but an LLC eviction must **back-invalidate** the private copies, and the effective capacity is just the LLC's.
- **Exclusive:** a line lives in one level at a time; capacity adds, but lines move between levels on every miss.
- **Non-inclusive:** neither guarantee; common in recent server chips, with a separate snoop filter.

## Replacement

- **True LRU** orders all W lines of a set: $\lceil\log_2W!\rceil$ bits per set. **Tree pseudo-LRU** approximates it with W − 1 bits.
- LRU fails on scans and on cyclic working sets slightly larger than the cache: each line is evicted just before it's reused, and the hit rate drops to zero. **LRU-insertion** (placing new lines at the LRU end) keeps most of a cyclic set resident; **RRIP** keeps 2-bit re-reference predictions per line and adapts between the two.

## Distributed last-level caches

The LLC is split into slices, often one per core, joined by an on-chip network; an address hashes to one slice. Latency = slice access + the round trip across the network, about $2h\,t_\text{hop}$ for h hops: **non-uniform cache access** (NUCA).

## Sharing and partitioning

Co-running programs compete for capacity. Under the √2 rule, a program's miss rate with a share s of the cache is about $m_0/\sqrt s$. **Way partitioning** reserves ways per core or per class of service so one program can't evict another's working set.
:::

::: example
## How much replacement state does a 16-way set need under true LRU, tree pseudo-LRU and 2-bit RRIP?

::: solution
1. **True LRU.** It must encode one of $16!$ orders:
   $$\begin{aligned} \lceil\log_2 16!\rceil &= \lceil 44.25 \rceil \\ &= 45\ \text{bits} \end{aligned}$$
2. **Tree pseudo-LRU.** It uses 15 bits, one per node of a binary tree over the ways.
3. **2-bit RRIP.** It stores 2 bits per line:
   $$16 \times 2 = 32\ \text{bits}$$
4. **Across a whole cache.** A 32 MB cache of 64-byte lines has 32 768 sets, so LRU would cost 180 KB of state against 60 KB for PLRU.

**Answer:** 45 bits per set for true LRU, 15 for tree pseudo-LRU and 32 for 2-bit RRIP.
:::
:::

::: example
## An LLC is spread over a 4 × 4 mesh of slices, 10 cycles per slice access and 3 cycles per hop. What is the average access latency for a core's random address?

::: solution
1. **Average distance.** On a k × k mesh it's about
   $$2k/3 = 2.7\ \text{hops}$$
   each way.
2. **Average latency.** The request and the reply both cross it:
   $$\begin{aligned} t &= 10 + 2 \times \tfrac{8}{3} \times 3 \\ &= 26\ \text{cycles} \end{aligned}$$
3. **Nearest and farthest.** The nearest slice costs 10 cycles and the farthest
   $$10 + 2 \times 6 \times 3 = 46\ \text{cycles}$$
   which is why systems map a thread's private data to nearby slices.

**Answer:** a random address takes about 26 cycles on average.
:::
:::

::: example
## A program misses 2% of the time with a 16 MB cache to itself. What happens when it shares the cache equally with three other programs?

::: solution
1. **Its share.** Shared equally four ways, its share falls to 4 MB, a quarter of the cache.
2. **New miss rate.** By the √2 rule, a quarter of the cache doubles the miss rate:
   $$2\% \times \sqrt{16/4} = 4\%$$
3. **Protecting it.** If the other programs are streaming data they'll never reuse, partitioning them into a few ways protects it without hurting them.

**Answer:** its miss rate doubles to about 4%.
:::
:::

::: check
kind: parts
given: N = 4 | 8 | 16; L = 0.5 | 1 | 2; C = 8 | 16 | 32

## {N} cores each have an {L} MB private L2; the shared L3 holds {C} MB.

::: part
answer: C
unit: MB
tolerance: 0.5%

### How much distinct data fits if the L3 is inclusive?

::: misses
- [C + N*L] An inclusive L3 already holds the L2s' contents.
- [any] Just the L3.
:::
:::

::: part
answer: C + N*L
unit: MB
tolerance: 0.5%

### And if it is exclusive?

::: misses
- [any] The L2s' capacity adds.
:::
:::
:::

::: check
kind: parts
given: W = 4 | 8 | 16; S = 8 | 32

## A {S} MB cache with 64-byte lines is {W}-way set associative.

::: part
answer: S*1024*1024/(64*W)
unit: sets
tolerance: 0

### How many sets does it have?

::: misses
- [any] Lines over ways.
:::
:::

::: part
answer: (W - 1)*S*1024*1024/(64*W)/8/1024
unit: KB
tolerance: 0.5%

### How much tree-PLRU state does it need?

::: misses
- [any] W − 1 bits per set.
:::
:::

::: part
answer: 2*W*S*1024*1024/(64*W)/8/1024
unit: KB
tolerance: 0.5%

### And 2-bit RRIP?

::: misses
- [any] 2 bits per line.
:::
:::
:::

::: check
kind: parts
given: k = 4 | 6 | 8; a = 8 | 12; t = 2 | 3

## An LLC is sliced over a {k} × {k} mesh; a slice takes {a} cycles and each hop {t}.

::: part
answer: a + 2*(2*k/3)*t
unit: cycles
tolerance: 0.5%

### What is the average latency to a random slice?

::: misses
- [a + (2*k/3)*t] The reply crosses the network too.
- [any] Slice time + $2 \times (2k/3) \times t_\text{hop}$.
:::
:::

::: part
answer: a + 2*2*(k - 1)*t
unit: cycles
tolerance: 0.5%

### And to the farthest slice?

::: misses
- [any] Diameter $2(k - 1)$ each way.
:::
:::
:::

::: check
kind: parts
given: m = 0.01 | 0.02 | 0.05; s = 2 | 4 | 8

## A program misses {m} of the time with the whole LLC; it now shares the cache equally with {s} programs in all.

::: part
answer: m*sqrt(s)
tolerance: 0.5%

### What miss rate does the √2 rule predict?

::: misses
- [m*s] The rate grows as the square root of the capacity lost.
- [any] $m_0\sqrt{S_0/S}$.
:::
:::
:::

::: check
kind: choice

## An inclusive LLC evicts a line that a core is actively using in its L1. What happens, and why do some designs avoid inclusion?

- [ ] Nothing: the L1 keeps its copy
- [x] The LLC must back-invalidate the L1's copy, so the core misses on a line it was using; designs drop inclusion (with a snoop filter for coherence) to avoid such inclusion victims
- [ ] The LLC refuses to evict lines held in L1s
- [ ] The L1 writes the line back to memory

::: misses
- [1] Inclusion requires every L1 line to be in the LLC, so the copy must go.
- [3] The LLC's replacement doesn't know which lines are hot in the L1s; that's the problem.
- [4] A clean line is simply invalidated; the cost is the core's next miss.
:::
:::

::: check
kind: derivation
vars: W = 2..32, S = 1..256, m = 0.001..0.1, s = 0.05..1

## A W-way cache of S MB with 64-byte lines; a program that misses m with the whole cache gets a share s.

::: step
answer: S*1024*1024/64

### How many lines does the cache hold?
:::

::: step
answer: (W - 1)*S*1024*1024/(64*W)

### How many bits of tree-PLRU state in all?
:::

::: step
answer: m/sqrt(s)

### What miss rate does the program see with its share?
:::

::: right
Right. Replacement state is a small tax on a big cache; sharing is the larger effect.
:::
:::

::: check
kind: python

## Write `set_hits(pattern, ways, policy)`: simulate one cache set; policy 'lru' inserts misses at the most-recent end, 'lip' inserts them at the least-recent end (hits always move to the most-recent end, and the least-recent line is evicted). Return the hit rate.

```python starter
def set_hits(pattern, ways, policy):
    return 0.0
```

```python solution
def set_hits(pattern, ways, policy):
    st, hits = [], 0
    for x in pattern:
        if x in st:
            hits += 1
            st.remove(x)
            st.append(x)
        else:
            if len(st) == ways:
                st.pop(0)
            if policy == 'lru':
                st.append(x)
            else:
                st.insert(0, x)
    return hits / len(pattern)
```

```python tests
cyc = list(range(9)) * 200
assert set_hits(cyc, 8, 'lru') == 0.0, "Nine lines cycling through eight ways: LRU never hits."
assert abs(set_hits(cyc, 8, 'lip') - 7 / 9) < 0.01, "LRU-insertion keeps seven of the nine: about 78%."
small = list(range(4)) * 200
assert set_hits(small, 8, 'lru') == set_hits(small, 8, 'lip') == 0.995, "A set that fits: both hit after the first pass."
```

::: right
Right. Inserting new lines where they'll be evicted first protects lines that were already reused, the idea behind RRIP.
:::
:::

::: explain
## Why does LRU replacement get zero hits on a loop over a working set slightly larger than the cache?

::: model
LRU evicts the line used longest ago. When a program cycles through more lines than a set can hold, the line it will need next is always the one it used longest ago, because the loop visits lines in the same order every time. So at each miss LRU evicts exactly the line about to be reused, and every access misses even though the cache holds nearly the whole working set. Inserting new lines at the least-recently-used position instead lets most of the set stay put while one way churns, giving a hit rate close to the fraction of the working set that fits.
:::
:::

::: practice
::: check
given: N = 8 | 16; L = 1 | 2; C = 16 | 32
answer: C + N*L
unit: MB
tolerance: 0.5%

## Distinct capacity with an exclusive {C} MB L3 and {N} private {L} MB L2s?

::: misses
- [any] L3 plus the L2s.
:::
:::

::: check
given: k = 5 | 10; a = 10 | 15; t = 2 | 4
answer: a + 2*(2*k/3)*t
unit: cycles
tolerance: 0.5%

## Average NUCA latency on a {k} × {k} mesh, {a}-cycle slices, {t} cycles per hop?

::: misses
- [any] Slice + round trip over the average distance.
:::
:::

::: check
given: m = 0.03 | 0.06; s = 3 | 6
answer: m*sqrt(s)
tolerance: 0.5%

## Miss rate of a program missing {m} alone, sharing equally among {s}?

::: misses
- [any] $m\sqrt s$.
:::
:::
:::
