---
title: Multiprocessors and snooping coherence
heading: Shared memory, the coherence problem, MSI and MESI on a snooping bus, false sharing and bus bandwidth
eyebrow: S4 · Lesson 32 · Multiprocessors and snooping coherence
xp: 50
---

::: hook
Put several cores on one chip, each with its own cache, sharing one memory, and a problem appears at once: two caches can hold different values for the same address. Core A writes a flag that core B is spinning on, and if B keeps reading its own stale copy it waits forever. Cache coherence protocols prevent that by guaranteeing that every write eventually reaches every reader, and that writes to one location are seen in the same order by everyone. On small systems the caches watch (snoop) a shared bus and invalidate their copies when another core writes. The protocol is simple; its costs, in bus traffic and in misses caused purely by sharing, shape how parallel programs must be written.
:::

::: play
- Bus utilization against the number of cores, when each core runs at f GHz and IPC 1 and sends m misses per instruction to the bus, each moving a B-byte line, on a bus of bandwidth W.
- Each core's traffic is $f \times m \times B$; the bus saturates when N cores' traffic reaches W, and beyond that adding cores adds nothing.
- Raising the miss rate (or adding coherence misses from sharing) moves the saturation point down quickly. That's why snooping buses serve a few to a few dozen cores, and larger systems use directories (lesson 33).
:::
:::

```sim plot
view: graph
x: N = 1..128 log
y: u = 0..1.2
slider: f = 1..4 = 3 GHz
slider: m = 0.001..0.02 = 0.004
slider: B = 32..128 step 32 = 64 bytes
slider: W = 16..256 = 64 GB/s log
plot: N*f*m*B/W | samples: 300 | label: bus utilization
plot: 1 | dashed | color: ch3 | label: saturated
readout: cores at saturation = W/(f*m*B)
```

::: predict
## Two threads on different cores each increment their own counter, a million times each. The two counters happen to sit in the same 64-byte cache line. What happens?

- [ ] Nothing: each thread touches only its own counter
- [ ] The counters get corrupted
- [x] Nearly every increment misses, as the line bounces between the caches
- [ ] The second thread waits until the first finishes

::: reveal
The line ping-pongs. Coherence works on whole lines, so each core's write invalidates the other core's copy, and the other core's next increment misses and takes the line back. That's **false sharing**: no data is actually shared, but up to two million coherence misses replace what should have been cache hits. Padding each counter to its own line removes them all.
:::
:::

::: nameit
## Coherence

A memory system is coherent if a read returns the most recent write to that location, and all processors see writes to one location in the same order. (Consistency, lesson 35, is about the order of writes to *different* locations.)

## MSI and MESI on a snooping bus

Each line in each cache has a state:

| State | Meaning |
|---|---|
| M (modified) | only copy, dirty; must be written back or supplied on a snoop |
| E (exclusive, MESI only) | only copy, clean; can be written silently |
| S (shared) | clean, possibly in other caches; read-only |
| I (invalid) | not present |

- A read miss issues **BusRd**: another cache in M supplies the data and drops to S; the requester gets S, or E if no one else has it.
- A write to S or I issues **BusRdX** (or **BusUpgr** when the data is already present): every other copy is invalidated.
- E lets private data be read and then written with one bus transaction instead of two.

## Costs

- **Coherence misses** (the fourth C): true sharing (data really passed between cores) and **false sharing** (different data in one line).
- **Bus bandwidth:** each core's traffic is about $f \times \text{IPC} \times m \times B$; at most $W/(f\,\text{IPC}\,m\,B)$ cores before the bus saturates.
- A write to a line shared by s other caches sends s invalidations (one broadcast on a bus).
:::

::: example
## A 64 GB/s bus serves cores at 3 GHz and IPC 1, each sending 0.004 misses per instruction with 64-byte lines. How many cores can it support?

::: solution
1. **Traffic per core.**
   $$\begin{aligned} B &= 3 \times 10^9 \times 0.004 \times 64 \\ &= 0.768\ \text{GB/s} \end{aligned}$$
2. **Cores the bus supports.**
   $$64/0.768 = 83\ \text{cores}$$
3. **In practice.** Coherence traffic, writebacks and arbitration lower that, and the bus's latency grows long before it saturates. Doubling the miss rate halves the count.

**Answer:** the bus can support about 83 cores.
:::
:::

::: example
## A thread reads and then writes 1000 lines that no other core uses. How many bus transactions does MSI need, and MESI? And how many misses do two cores incrementing falsely shared counters 1000 times each, alternately, take?

::: solution
1. **MSI.** Each line takes a BusRd (to S), then a BusUpgr to write:
   $$2 \times 1000 = 2000\ \text{transactions}$$
2. **MESI.** The BusRd finds no other copy and grants E, and the write moves E to M silently: 1000 transactions.
3. **False sharing.** After the first access, every increment finds its line taken by the other core, so about 2000 misses.
4. **Separate lines.** With the counters on separate lines, only the first access to each misses.

**Answer:** MSI needs 2000 bus transactions and MESI 1000; the falsely shared counters take about 2000 misses.
:::
:::

::: example
## Eight threads each increment their own 8-byte counter a million times, but the counters share one 64-byte line. If every increment becomes an 80 ns coherence miss as the line bounces, how long does that take, against padding each counter to its own line with 1 ns L1 hits?

::: solution
1. **Shared line.** The line can be owned by one core at a time, so the misses serialize:
   $$\begin{aligned} t &= 8 \times 10^6 \times 80\ \text{ns} \\ &= 0.64\ \text{s} \end{aligned}$$
2. **Padded.** Each thread's million hits take 1 ms, and the threads run in parallel: 1 ms in all.
3. **Compare.** False sharing makes the program 640 times slower without any data being shared.

**Answer:** 0.64 s with the shared line against 1 ms padded, 640 times slower.
:::
:::

::: check
given: f = 2 | 3 | 4; m = 0.002 | 0.005 | 0.01; B = 64 | 128
answer: f*m*B
unit: GB/s
tolerance: 0.5%

## A core at {f} GHz and IPC 1 sends {m} misses per instruction to the bus, each moving a {B}-byte line. How much bus bandwidth does it use?

::: right
Right. $f\,m\,B$ = {answer} GB/s.
:::

::: misses
- [f*m] Each miss moves a whole line.
- [any] Not yet. Misses per second × bytes each.
:::
:::

::: check
given: W = 32 | 100 | 400; f = 2 | 3.5; m = 0.003 | 0.008; B = 64
answer: W/(f*m*B)
tolerance: 0.5%

## A {W} GB/s bus serves cores at {f} GHz, IPC 1, each with {m} bus misses per instruction and {B}-byte lines. How many cores saturate it?

::: right
Right. $W/(fmB)$ = {answer} cores.
:::

::: misses
- [W/(f*m)] Each miss moves B bytes.
- [any] Not yet. Bus bandwidth over one core's traffic.
:::
:::

::: check
given: n = 200 | 1000 | 5000
answer: 2*n
tolerance: 0

## Under MSI, a thread reads and then writes {n} lines that no other core holds. How many bus transactions does that take?

::: right
Right. A read and an upgrade per line: {answer}.
:::

::: misses
- [n] Without an E state, the write needs its own bus transaction.
- [any] Not yet. Count transactions per line.
:::
:::

::: check
given: n = 200 | 1000 | 5000; p = 0.5 | 0.8 | 1
answer: n + (1 - p)*n
tolerance: 0.5%

## Under MESI, a thread reads and then writes {n} lines; a fraction {p} of them are held by no other cache when read. How many bus transactions?

::: right
Right. {answer}: lines granted E need no upgrade.
:::

::: misses
- [2*n] Lines read into E can be written silently.
- [any] Not yet. Every line needs a read; only the shared ones need an upgrade.
:::
:::

::: check
given: k = 500 | 2000 | 10000
answer: 2*k
tolerance: 0.5%

## Two cores each increment their own counter {k} times, alternating, with both counters in one cache line. About how many coherence misses result?

::: right
Right. About {answer}: every increment finds the line in the other cache.
:::

::: misses
- [k] Both cores miss, in turn.
- [0] They write different counters, but coherence works on whole lines.
- [any] Not yet. Each write invalidates the other core's copy.
:::
:::

::: check
given: s = 3 | 7 | 15; w = 10 | 100
answer: s*w
tolerance: 0

## A line is shared by {s} other caches, and a core writes to it {w} times, with the other caches rereading it between writes. How many invalidations reach other caches in all?

::: right
Right. {s} per write: {answer}.
:::

::: misses
- [w] Each write invalidates every other copy.
- [any] Not yet. Copies invalidated per write × writes.
:::
:::

::: check
kind: parts
given: W = 64 | 128; f = 2 | 3; i = 1 | 2; m = 0.002 | 0.01

## A {W} GB/s snooping bus serves cores at {f} GHz and IPC {i}; each misses {m} times per instruction with 64-byte lines.

::: part
answer: f*i*m*64
unit: GB/s
tolerance: 0.5%

### How much bus traffic does each core make?

::: misses
- [any] $f \times \text{IPC} \times m \times B$.
:::
:::

::: part
answer: W/(f*i*m*64)
unit: cores
tolerance: 0.5%

### How many cores can the bus support?

::: misses
- [any] Bus bandwidth over each core's traffic.
:::
:::
:::

::: check
kind: derivation
vars: s = 1..63, t = 1..100

## A line is shared by s other caches when a core writes it; each message takes t on a directory network.

::: step
answer: s

### How many invalidations must be sent?
:::

::: step
answer: 2*s + 2

### How many messages in all, counting the request, the acknowledgments and the reply?
:::

::: right
Right. Widely shared data that's written often is expensive under any protocol; on a bus the invalidation is one broadcast, but the bus is shared by everyone.
:::
:::

::: explain
## Why does false sharing slow a program down even though the threads never touch each other's data?

::: model
Coherence protocols track ownership of whole cache lines, not individual words. When two threads write different variables that happen to sit in the same line, the hardware can't tell the writes apart: each write needs the line in modified state, which invalidates the other core's copy. The other thread's next access then misses and pulls the line back, invalidating the first core's copy in turn. The line bounces between the caches on nearly every access, turning what should be cache hits into slow coherence misses and loading the bus or interconnect, although the program's results are correct. Placing each thread's data in its own line removes the problem.
:::
:::

::: practice

::: check
given: f = 2.5 | 3.5; m = 0.004 | 0.012; B = 64
answer: f*m*B
unit: GB/s
tolerance: 0.5%

## Bus traffic per core: {f} GHz, {m} misses per instruction, {B}-byte lines?

::: misses
- [any] $fmB$.
:::
:::

::: check
given: W = 50 | 200; f = 3; m = 0.002 | 0.006; B = 64 | 128
answer: W/(f*m*B)
tolerance: 0.5%

## Cores to saturate a {W} GB/s bus: {f} GHz, {m}, {B}-byte lines?

::: misses
- [any] $W/(fmB)$.
:::
:::

::: check
given: n = 300 | 3000; p = 0.6 | 0.9
answer: n + (1 - p)*n
tolerance: 0.5%

## MESI transactions: {n} lines, {p} unshared?

::: misses
- [any] $n + (1 - p)n$.
:::
:::
:::
