S4 · Lesson 32 · Multiprocessors and snooping coherence
Shared memory, the coherence problem, MSI and MESI on a snooping bus, false sharing and bus bandwidth
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.
- 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 ; 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).
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?
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.
Name it
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 ; at most cores before the bus saturates.
- A write to a line shared by s other caches sends s invalidations (one broadcast on a bus).
Worked 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?
Show the solution
- Traffic per core.
- Cores the bus supports.
- 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.
Worked 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?
Show the solution
- MSI. Each line takes a BusRd (to S), then a BusUpgr to write:
- MESI. The BusRd finds no other copy and grants E, and the write moves E to M silently: 1000 transactions.
- False sharing. After the first access, every increment finds its line taken by the other core, so about 2000 misses.
- 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.
Worked 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?
Show the solution
- Shared line. The line can be owned by one core at a time, so the misses serialize:
- Padded. Each thread's million hits take 1 ms, and the threads run in parallel: 1 ms in all.
- 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
A core at 3 GHz and IPC 1 sends 0.002 misses per instruction to the bus, each moving a 128-byte line. How much bus bandwidth does it use?
Check
A 100 GB/s bus serves cores at 2 GHz, IPC 1, each with 0.008 bus misses per instruction and 64-byte lines. How many cores saturate it?
Check
Under MSI, a thread reads and then writes 1000 lines that no other core holds. How many bus transactions does that take?
Check
Under MESI, a thread reads and then writes 1000 lines; a fraction 0.5 of them are held by no other cache when read. How many bus transactions?
Check
Two cores each increment their own counter 2000 times, alternating, with both counters in one cache line. About how many coherence misses result?
Check
A line is shared by 7 other caches, and a core writes to it 10 times, with the other caches rereading it between writes. How many invalidations reach other caches in all?
Check
A 64 GB/s snooping bus serves cores at 2 GHz and IPC 2; each misses 0.01 times per instruction with 64-byte lines.
(a)
How much bus traffic does each core make?
(b)
How many cores can the bus support?
Check
A line is shared by s other caches when a core writes it; each message takes t on a directory network.
Step 1
How many invalidations must be sent?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many messages in all, counting the request, the acknowledgments and the reply?
Explain why
Why does false sharing slow a program down even though the threads never touch each other's data?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.