Lecture 21: Cache Coherence
Section 1: Multiprocessor Communication Models — Shared Memory vs. Message Passing
Scaling performance across multiple cores—whether on a multi-core CPU, a GPU, or a large server—requires a system-level model for how cores communicate and access memory. There are two fundamental multiprocessor architectures:
1. Shared Memory
In a Shared Memory multiprocessor, multiple processors P connect through an interconnect to one global shared memory (MMMMMMM):
- Each node is a core running a thread.
- One global shared memory is accessible by any processor: the same physical address on two different processors refers to the exact same datum.
- Threads communicate implicitly using normal reads and writes (loads/stores) plus atomic operations, and use synchronization (locks, barriers) to coordinate ordering.
Trade-offs of Shared Memory:
(+)Well-understood architecture once paired with cache coherence.(+)Easy to program irregular communication patterns.(+)Supports different communication patterns implicitly (e.g., broadcast, multicast).(+)Lower communication overhead (just loads/stores + atomics) and lower bandwidth for small communications.(+)Uses caches to improve effective communication bandwidth.(-)Harder to express and tune regular communication patterns (such as blocked/tiled matrix algorithms where explicit data movement can be scheduled cleanly).
2. Message Passing
In a Message Passing multiprocessor, each node is an independent processor-memory pair (P—M) connected to an interconnect:
- Each node runs its own program; local memory is private and not accessible to other nodes (though an individual node may use shared memory internally among its own cores).
- Internode communication occurs explicitly by executing
SendandRecvmessages through the interconnect (like sending mail).
Trade-offs of Message Passing:
(+)Simpler hardware—no hardware cache coherence is needed across nodes.(+/-)Communication is explicit, which is painful for the programmer/compiler (who must explicitly manage channels and write code like “multicast this data to nodes a, b, c”), though explicitness can help optimize regular transfers.
Centralized Shared Memory (UMA) and the Cache Coherence Problem
The simplest shared-memory hardware is a Centralized Shared Memory System—also called a Uniform Memory Access (UMA) machine—where processors (PROC) share centralized MEMORY over a shared BUS. It is simple to build, but suffers from long latency to memory and severe contention on the bus and memory port.
Adding private caches (PROC \leftrightarrow CACHE \leftrightarrow BUS \leftrightarrow MEMORY) immediately decreases memory latency and reduces bus contention. However, adding private caches creates the Cache Coherence Problem!
Trace what goes wrong when two processors P1 and P2 cache the same shared variable A (initially A = 5 in Memory):
P1readsA\to bringsA = 5intoP1’s cache.P2readsA\to bringsA = 5intoP2’s cache.- Now
P1executes a storeA = 7into its local cache. Look at the system state:P1’s cache saysA = 7,P2’s cache still saysA = 5, andMemorystill saysA = 5! Multiple cached copies of the same memory block (whether modified by processor writes or I/O DMA writes) easily become inconsistent. WheneverP2readsA, it sees the stale value5instead of7.
✎ Check Your Understanding
What is the defining semantic rule of a Shared Memory multiprocessor compared to a Message Passing multiprocessor? Answer: In Shared Memory, there is one global address space where the same address on any two processors refers to the exact same datum (communicating via normal loads/stores), whereas in Message Passing each node’s memory is private and nodes communicate via explicit
Send/Recvmessages.Briefly describe how adding private write-back caches to a shared-memory bus system causes the Cache Coherence Problem. Answer: Two processors can both cache a copy of address
A = 5; when one processor updates its local cached copy toA = 7, the other processor’s cache (and main memory) still hold the stale value5, breaking the single-shared-datum illusion.
Section 2: Snoopy Cache Coherence, Protocols, and Design Trade-offs
To keep private caches coherent on a shared bus, we exploit a key property of any bus: every transaction placed on the bus is visible to every cache simultaneously. This gives us Snoopy Cache Coherence:
- Each processor makes local reads and writes to its own cache.
- Each local cache snoops (watches) all remote reads and writes broadcast on the shared bus.
- Each local cache maintains a state (a small status directory/dictionary per cache line) and updates that state—invalidating, supplying, or updating data—whenever local accesses or snooped bus transactions occur.
(Recall the three baseline coherence states of the standard MSI protocol: M (Modified) means this cache holds the only valid copy and it is dirty relative to memory; S (Shared) means one or more caches hold a clean copy matching memory; and I (Invalid) means the block is not valid in this cache.)
Extending MSI: MESI and MOESI Protocols
Why isn’t basic MSI enough for high performance? Imagine a purely sequential single-threaded program running on one core of a 4-core CPU. Suppose the program reads a variable x, then writes x:
- Under
MSI, the initial read miss putsxin stateS(Shared)—because the cache doesn’t know whether any other core also holdsx. - When the core immediately writes
x,MSImust put an invalidation request on the bus to move fromS\toM, even though no other cache actually hadx!
- MESI Protocol: Adds the
E(Exclusive) state—meaning this cache is the sole owner of the block, and the block is clean (matches memory).- If a core reads a line and no other cache asserts a “shared” signal on the bus, the line enters
Einstead ofS. - When that core later writes the line, it silently upgrades
E\toMwith zero bus traffic! Thus, MESI keeps bus traffic close to uniprocessor traffic for sequential apps and reduces bus contention on first writes.
- If a core reads a line and no other cache asserts a “shared” signal on the bus, the line enters
- MOESI Protocol: Adds the
O(Owned) state—meaning the block is modified (out of sync with memory), yet other caches are allowed to hold clean shared copies in stateS!- Under
MSIorMESI, if Core 1 holds a dirtyMline and Core 2 tries to read it, Core 1 must write the dirty block all the way back to lower-level memory (DRAM) before moving toS. - Under
MOESI, Core 1 moves fromM\toO(retaining responsibility to eventually write back to memory) and sends the block directly to Core 2 via a fast cache-to-cache transfer, with Core 2 enteringS. This eliminates unnecessary writebacks to slow main memory!
- Under
Write-Through vs. Write-Back / Ownership Schemes
When designing a snoopy bus protocol, how should writes propagate?
- Write-Through Schemes: Every processor write updates the local cache and triggers a global bus write that updates main memory and invalidates/updates all other caches with that item.
(+)Very simple to implement.(-)Since ~15% of all memory references are writes, sending every write onto the global bus consumes tremendous bus bandwidth, limiting the machine to only a few processors.
- Write-Back / Ownership Schemes: When a single cache holds ownership of a block (
MorE), subsequent processor writes stay purely local and do not result in bus writes, drastically conserving bus bandwidth. Most modern bus-based multiprocessors use ownership-based write-back schemes (classic examples include Goodman’s write-once scheme, Berkeley ownership, and the Firefly update protocol).
Invalidation vs. Update Strategies
When Core 1 does write to a shared block held by Core 2, should Core 1 invalidate Core 2’s copy or update it with the new value?
- Invalidation: On a write, all other caches holding a copy mark their copy Invalid (
I).- When Invalidation is bad: Single producer and many consumers—every time the producer writes a shared flag or data structure, all N-1 consumers are invalidated and immediately suffer cache misses on the bus to re-fetch the new value.
- Update: On a write, the new value is broadcast so all other caches holding a copy update their data in place.
- When Update is bad: First, multiple consecutive writes by one core before another core reads waste bus bandwidth broadcasting every intermediate write (
x = 1; x = 2; ... x = 100;). Second, junk data accumulates in large caches—caches keep receiving updates for lines that their local core stopped using long ago!
- When Update is bad: First, multiple consecutive writes by one core before another core reads waste bus bandwidth broadcasting every intermediate write (
Overall, invalidation schemes are much more popular as the default because programs typically perform multiple local writes between synchronization points, and invalidation naturally cleans out unused shared copies.
[Editor Note: removed three blank transition slides.]
✎ Check Your Understanding
What advantage does the MESI protocol’s
E(Exclusive) state provide over basicMSIfor a single-threaded application? Answer: When a single core reads a block that no other cache holds, it loads the line in stateE(clean, sole owner) rather thanS, allowing a subsequent write to upgradeE\toMlocally with zero bus contention/invalidation traffic.Give two reasons why Invalidation coherence protocols are generally preferred over Update protocols as the default. Answer: Update protocols waste bus bandwidth broadcasting every intermediate write when one core writes a variable multiple times before another core reads it, and they keep pushing updates (“junk data”) into large caches that no longer actively use those blocks.
Section 3: Scaling Beyond the Bus — Directory-Based Coherence and Modern Interconnects
Snoopy coherence is wonderfully simple because every coherence transaction completes in a single step (broadcast onto the bus, everyone snoops at once). However, a snoopy bus does not scale to dozens or hundreds of cores for two reasons:
- Performance bottleneck: One shared bus cannot carry traffic when all cores want to communicate constantly.
- Wasted work: Sending a coherence message intended for one other core forces a broadcast to every core on the chip, burning tag-lookup energy in every cache.
Directory-Based Cache Coherence
To eliminate broadcasts and scale to scalable point-to-point networks, processors replace the snoopy bus with Directories:
- A Directory is a memory structure that tracks each cache line’s coherence state in a single central location (its home node).
- For each memory block, the directory stores the MSI state plus a presence bit-vector with 1 bit per sharer (N cores = N sharer bits).
- Instead of 1-step broadcasts, coherence transactions occur in two (or three) targeted point-to-point steps:
- The requesting core (local node) sends a unicast request to the block’s directory (home node).
- The directory looks up the sharer bit-vector and sends invalidations (or data-forward requests) only to the specific cores currently caching that block (remote nodes).
Modern Multi-Banked Interconnects and Distributed Directories
Wait: if we put the entire directory in one central chip location, wouldn’t that single directory become a bottleneck just like the bus?
Intel multicores from Nehalem through Haswell (2008–2013) solved this using distributed directories over a multi-hop network:
- Each processor combines multiple memory banks, a multi-hop interconnect (such as a bidirectional ring connecting
Core 0..3andLLC slice 0..3, alongside theSystem agent,Memory controller,PCIe/display, andGraphics), and distributed Last-Level Cache (LLC) slices with a directory slice assigned to each memory/LLC bank! - To spread addresses evenly across all LLC slices and directories without hot spots, hardware runs a slice selection hash on the physical address bits (
p_i). For example, early single-slice/simple hashing used a single bit (Nehalem: p_{17}), whereas later multi-slice processors XORed subsets of upper physical address bits (e.g., Sandy Bridge/Ivy Bridge/Haswell computing 1-bit or 2-bit slice IDs via multi-bit XORs like p_{18} \oplus p_{19} \oplus p_{21} \oplus \dots \oplus p_{31}) to map every physical cache line uniformly to its home LLC slice and directory.
Thus, whether scaling inside a single core via out-of-order execution and SMT, across regular arrays via vectors and GP-GPUs, or across many independent cores via directory-coherent shared memory, modern computer architecture balances hardware simplicity, communication bandwidth, and exposed parallelism to match the structure of each workload.
[Editor Note: removed acknowledgements slide.]
✎ Check Your Understanding
What are the two scalability problems of snoopy bus coherence that directory-based coherence eliminates? Answer: Snoopy buses suffer from bus contention (all cores fighting over one shared broadcast medium) and wasted broadcast work (forcing every core to snoop and check tags even when only one or two cores share the block). Directories replace broadcasts with targeted point-to-point messages sent only to the cores listed in the line’s sharer bit-vector.
In a directory-based system with 64 cores, how many sharer tracking bits are required per directory entry in a full-map bit-vector directory, and why do modern multicore chips distribute directories across LLC slices? Answer: A full-map bit-vector needs 64 sharer bits per tracked block (1 bit per core) alongside the state bits. Modern chips distribute directories across LLC slices (selected by hashing physical address bits) so that directory lookups themselves do not bottleneck at a single central location.