3.4Basic Idea of Sequential Consistency (SC)

book pp. 22–23 · ~4 min read

  • sequential consistency
  • memory order
  • program order
  • respects
  • constraint cycle

Lamport’s two definitions

Arguably the most intuitive memory model is sequential consistency , first formalized by Lamport. He built it in two steps. A single core is sequential if:

“the result of an execution is the same as if the operations had been executed in the order specified by the program.”

A multiprocessor is sequentially consistent if:

“the result of any execution is the same as if the operations of all processors (cores) were executed in some sequential order, and the operations of each individual processor (core) appear in this sequence in the order specified by its program.”

That total order of operations is the memory order , written <m (op1 <m op2 means op1 precedes op2 in memory order). Each core’s program order is written <p. Under SC, memory order respects each core’s program order: op1 <p op2 implies op1 <m op2. Other models will permit memory orders that don’t always respect program order — that is exactly what “relaxed” will mean.

Seeing an SC execution

Here is the flag/data program from §3.1 executing under SC (the book’s Figure 3.2). Each operation occupies one slot of the central memory order; the comment gives the value stored or loaded:

Program Order (<p) of Core C1Program Order (<p) of Core C2Memory Order (<m)L1: r1 = flag; /* 0 */S1: data = NEW; /* NEW */L1: r1 = flag; /* 0 */L1: r1 = flag; /* 0 */S2: flag = SET; /* SET */L1: r1 = flag; /* SET */L2: r2 = data; /* NEW */

Figure 3.2 (recreated): a sequentially consistent execution of Table 3.1’s program.

Under SC, every execution of this program ends with r2 = NEW. The only non-determinism — how many times L1 loads 0 before it finally loads SET — is unimportant. If in §3.1 you felt r2 must be NEW, you were independently inventing SC, just less precisely than Lamport.

The four (plus one) executions of Dekker

The value of SC shows even more clearly on Table 3.3’s program. Three outcomes are intuitive, and each corresponds to SC executions — six in total (outcome (NEW, NEW) alone has four interleavings; one is shown):

Program Order (<p) of Core C1Program Order (<p) of Core C2Memory Order (<m)S1: x = NEW; /* NEW */L1: r1 = y; /* 0 */S2: y = NEW; /* NEW */L2: r2 = x; /* NEW */Outcome: (r1, r2) = (0, NEW)
(a) SC execution 1
Program Order (<p) of Core C1Program Order (<p) of Core C2Memory Order (<m)S2: y = NEW; /* NEW */L2: r2 = x; /* 0 */S1: x = NEW; /* NEW */L1: r1 = y; /* NEW */Outcome: (r1, r2) = (NEW, 0)
(b) SC execution 2
Program Order (<p) of Core C1Program Order (<p) of Core C2Memory Order (<m)S1: x = NEW; /* NEW */S2: y = NEW; /* NEW */L1: r1 = y; /* NEW */L2: r2 = x; /* NEW */Outcome: (r1, r2) = (NEW, NEW)
(c) SC execution 3 (one of four with this outcome)
Program Order (<p) of Core C1Program Order (<p) of Core C2Memory Order (<m)S2: y = NEW; /* NEW */L2: r2 = x; /* 0 */S1: x = NEW; /* NEW */L1: r1 = y; /* 0 */r1 = 0 needs L1 <m S2r2 = 0 needs L2 <m S1Outcome: (r1, r2) = (0, 0) — impossible under SC
(d) NOT an SC execution — the constraints form a cycle

For outcome (0, 0), program order dictates S1 <p L1 and S2 <p L2, while the loaded zeros dictate L1 <m S2 and L2 <m S1. Chase the arrows: S1 → L1 → S2 → L2 → S1. Honoring all four constraints requires a cycle — which no total order can contain. The red arcs in panel (d) close that cycle visually.

What this means for builders

We have seen six SC executions and one non-SC execution. An SC implementation must allow one or more of the six and can never allow the seventh. (It need not allow all six — recall the partition view.) How to build such implementations, from naive to aggressive, is the business of the next four sections.

Check yourself

1.State Lamport's definition of a sequentially consistent multiprocessor.

2.For the Dekker program (Table 3.3), how many distinct SC executions are there, across how many outcomes?

3.Why exactly is the outcome (r1, r2) = (0, 0) impossible under SC?

4.What must an SC implementation guarantee regarding the Dekker program's seven candidate executions (six SC + one non-SC)?

4 questions