2.4(Consistency-Agnostic) Coherence Invariants

book pp. 12–15 · ~5 min read

  • SWMR invariant
  • data-value invariant
  • epoch
  • token counting
  • invalidate protocol
  • coherence granularity

What invariants make caches invisible?

§2.3 promised that consistency-agnostic coherence presents the abstraction of an atomic memory system. What must a protocol actually guarantee to deliver that illusion? Many definitions of coherence exist in the literature (the sidebar below surveys them); the book commits to the one it finds most useful for designing protocols, built from two invariants.

The SWMR invariant

Coherence is defined first through the single-writer–multiple-reader ( SWMR ) invariant:

For any given memory location, at any given moment in time, there is either a single core that may write it (and that may also read it) or some number of cores that may read it.

There is never a time when a location may be written by one core and simultaneously read or written by any other. An equivalent picture: divide each memory location’s lifetime into epochs — in each epoch, either a single core has read-write access, or some number of cores (possibly zero) have read-only access. Explore the book’s example lifetime, then extend it yourself:

Read-only
C2, C5
A = 1
Read-write
C3
A = 2
Read-write
C1
A = 3
Read-only
C1, C2, C3
A = 3

This starting timeline is exactly Figure 2.3 from the book (with values added). Issue reads and writes below to extend it.

C1
C2
C3
C4
C5
A single memory location's lifetime as SWMR epochs (interactive recreation of Figure 2.3). Issue loads and stores; the protocol rules of §2.4.1 keep both invariants true.

The data-value invariant

SWMR alone is not enough, because it says nothing about values. Even with SWMR holding, the system is incoherent if Cores 2 and 5 can read different values during their shared read-only epoch, or if Core 1 fails to see the last value Core 3 wrote in the preceding read-write epoch. So the definition adds the data-value invariant , governing how values pass from epoch to epoch:

The value of a memory location at the start of an epoch is the same as its value at the end of its last read-write epoch.

An equivalent view: tokens

A notable reinterpretation (Martin, Hill, and Wood) recasts the invariants as token counting: each memory location has a fixed number of tokens, at least as large as the number of cores. A core holding all the tokens may write the location; a core holding one or more may read it. One core writing concurrently with any other reader is then arithmetically impossible — SWMR by counting. File this away: it resurfaces as Token Coherence in Chapter 9.

2.4.1 Maintaining the invariants

The vast majority of protocols — invalidate protocols — are designed explicitly around these invariants:

  • To read a location, a core sends messages to obtain the current value and to ensure no other core holds it in a read-write state. That ends any active read-write epoch and begins (or joins) a read-only epoch.
  • To write a location, a core sends messages to obtain the current value (if it lacks a valid read-only copy) and to invalidate all other cached copies. That ends any active epoch and begins a new read-write epoch.

Chapters 6–9 expand enormously on this two-sentence sketch — but the basic intuition you just exercised in the explorer above never changes.

2.4.2 The granularity of coherence

Cores load and store at granularities from 1 to 64 bytes, and in theory coherence could be tracked per byte. In practice it is maintained per cache block: the SWMR invariant really reads “for any block of memory, one writer or some number of readers.” In typical systems it is impossible for one core to write the first byte of a block while another core writes a different byte of the same block. Block granularity is assumed for the rest of the book — though protocols at finer and coarser granularities have existed.

2.4.3 When is coherence relevant?

Two closing points every architect must internalize:

  • Coherence applies to every storage structure holding blocks from the shared address space — the L1 data cache, L2, shared LLC, and main memory, but also the L1 instruction cache and the TLBs (with the caveat that some architectures’ TLB entries aren’t strict copies of shared-memory blocks).
  • Coherence is not architecturally visible. The pipeline and the coherence protocol jointly enforce the consistency model, and only the consistency model is visible to the programmer. Remember the pop quiz: several of its traps dissolve once you hold this line firmly.

Check yourself

1.State the SWMR invariant.

2.Why is the SWMR invariant alone NOT a sufficient definition of coherence?

3.In the token-based interpretation of the invariants, what must a core hold to write a location, and to read it?

4.In an invalidate protocol, core C2 wants to WRITE a location that C1 and C3 currently cache read-only. What happens?

5.At what granularity is coherence maintained in practice, and what does that imply?

6.Is coherence architecturally visible to the programmer?

6 questions
Chapter 2 references
  1. K. Gharachorloo. Memory Consistency Models for Shared-Memory Multiprocessors. Ph.D. thesis, Computer System Laboratory, Stanford University, December 1995.

  2. K. Gharachorloo, D. Lenoski, J. Laudon, P. Gibbons, A. Gupta, and J. Hennessy. Memory consistency and event ordering in scalable shared-memory. In Proc. of the 17th Annual International Symposium on Computer Architecture, pp. 15–26, May 1990. DOI: 10.1109/isca.1990.134503.

  3. J. L. Hennessy and D. A. Patterson. Computer Architecture: A Quantitative Approach, 4th ed. Morgan Kaufmann, 2007.

  4. IBM. Power ISA Version 2.06 Revision B. July 2010.

  5. M. M. K. Martin, M. D. Hill, and D. A. Wood. Token coherence: Decoupling performance and correctness. In Proc. of the 30th Annual International Symposium on Computer Architecture, June 2003. DOI: 10.1109/isca.2003.1206999.