S435 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S4 · Lesson 35 · Memory consistency and synchronization

Sequential consistency, store buffers and TSO, relaxed models and fences, atomics, locks and barriers

Coherence makes every core agree on the history of each single address. It says nothing about the order in which a core's writes to different addresses become visible to others, and that's where parallel programs break. On a real x86 machine, two threads that each set their own flag and then read the other's can both see the other's flag still clear, an outcome impossible if memory operations happened one at a time in program order. The memory consistency model is the contract that says which outcomes are allowed; fences and atomic instructions are how programs ask for more order than the hardware gives by default, and locks and barriers are built from them.

  • Coherence transactions until each of N contending cores has held a lock once, on log scales. Test-and-test-and-set (solid): every waiter misses twice at each release, N(N − 1) in all. Ticket lock (dashed): once per waiter, half that. Queue (MCS) lock: one transaction per handoff, N − 1.
  • The first two grow with the square of the core count, so a lock that's fine on 4 cores can swamp the interconnect on 64.
  • The program at the heart of the lesson is below: thread 1 runs x = 1; r1 = y and thread 2 runs y = 1; r2 = x, with x and y starting at 0.

Predict

In the program above, which outcomes can sequential consistency produce?

Name it

Store buffers and the flag example

In the program above, under sequential consistency some write must come first, so (r1, r2) = (0, 0) is impossible. With a store buffer, each core's store waits in the buffer while its load runs ahead, so both loads can read 0. A fence between each store and the following load forces the buffer to drain first and restores the SC outcomes.

Consistency models

  • Sequential consistency (SC): the result is as if all operations ran in some single order that respects every thread's program order. Simple, but it forbids store buffers and most reordering.
  • Total store order (TSO, x86): a store may be delayed past later loads (to different addresses) via the store buffer; all other orders are kept.
  • Relaxed models (ARM, RISC-V RVWMO, POWER): loads and stores to different addresses may be reordered in most ways; programs insert fences or use acquire and release operations where order matters.
  • Data-race-free guarantee: a program whose shared accesses are all ordered by synchronization behaves as if sequentially consistent on all these models. Languages (C++, Java) build on that.

Atomics

  • Test-and-set, swap, fetch-and-add, compare-and-swap (CAS): read-modify-write instructions the coherence protocol performs atomically, by holding the line in M state.
  • Load-reserved / store-conditional (RISC-V lr/sc, ARM ldxr/stxr): the store succeeds only if no other core wrote the line since the load, so a retry loop builds any atomic operation.

Locks and their traffic

  • Test-and-set spin lock: every attempt writes, so waiters keep stealing the line from each other.
  • Test-and-test-and-set: waiters spin reading their cached copy; a release invalidates all of them, and each then misses and tries to set: about two transactions per waiter per handoff.
  • Ticket lock: a release makes each waiter reread the "now serving" counter: one miss per waiter per handoff, and FIFO fairness.
  • Queue (MCS) locks: each waiter spins on its own flag; a handoff touches one line.

A lock held for a fraction c of each thread's time limits the whole program to at most 1/c threads' worth of throughput (Amdahl's law with the critical section as the serial part).

Barriers

A centralized barrier counts arrivals with fetch-and-add: N serialized atomic operations. A tree barrier with fan-in k combines arrivals in ⌈logk⁡N⌉ levels.

Worked example

N cores contend for a test-and-test-and-set lock: one holds it and N − 1 wait. Each release makes every waiting core miss twice (a read, then its test-and-set), and one fewer core waits after each handoff. How many transactions until everyone has had the lock, for N = 8 and N = 64? What would an MCS lock need?

Show the solution
  1. Test-and-test-and-set. Handoffs see N − 1, N − 2, …, 1 waiters: 2∑k=1N−1k=N(N−1) which is 56 for N = 8 and 4032 for N = 64.
  2. Ticket lock. It halves that (one miss per waiter): 28 and 2016.
  3. MCS lock. It needs one handoff transaction per release: 7 and 63.
  4. Why it matters. The quadratic growth is why scalable systems use queue locks.

Answer 56 transactions for N = 8 and 4032 for N = 64; an MCS lock needs only 7 and 63.

Worked example

Threads spend 5% of their time in a critical section protected by one lock. What's the best throughput with any number of threads? And how long does a 1024-thread barrier take as a tree with fan-in 4, at 50 ns per level, against a centralized counter at 30 ns per atomic?

Show the solution
  1. Lock limit. At most 1/0.05=20 threads' worth of throughput, however many threads run.
  2. Tree barrier depth. ⌈log4⁡1024⌉=5levels so 250 ns per arrival wave, and as much again to release.
  3. Centralized counter. 1024×30ns=30.7µs

Answer the best throughput is 20 threads' worth; the tree barrier takes 250 ns per wave against 30.7 µs for the centralized counter.

Worked example

Sixty-four cores queue on a ticket lock. Each handoff makes every remaining waiter reread the "now serving" counter once. How many misses until all have had the lock, and how many with an MCS lock?

Show the solution
  1. Ticket lock. With w waiters, a handoff costs w misses; summing w from 63 down to 1 gives 64×63/2=2016misses
  2. MCS lock. It hands off by writing one waiter's own flag: 63 transfers in all.
  3. Why it matters. At large core counts the queue lock's linear traffic is the difference between a lock that scales and one that doesn't.

Answer 2016 misses with the ticket lock, and 63 transfers with an MCS lock.

Check

On a TSO machine (x86), which of these reorderings can a program observe?

Check

Which instruction sequence lets a thread atomically add to a shared counter on RISC-V without a lock?

Check

8 cores contend for a test-and-test-and-set lock, one holding it and the rest waiting. Each release makes every waiting core miss twice, and one fewer waits after each handoff. How many transactions until all have held it?

Check

The same 32 cores use a ticket lock, where each release costs one miss per waiting core. How many transactions until all have held it?

Check

Every thread spends 0.04 of its time holding one shared lock. At most how many threads' worth of throughput can the program reach?

Check

A tree barrier with fan-in 2 gathers 1000 threads, taking 60 ns per level. How long does the arrival phase take?

Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.

Check

Threads spend 0.05 of their time in one critical section; a barrier joins 64 threads.

(a)

What is the most throughput, in threads' worth, the lock allows?

(b)

How long does a tree barrier with fan-in 8 take, at 50 ns per level?

Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.

(c)

And a centralized counter at 50 ns per atomic?

Check

N cores contend for a lock until each has held it once.

Step 1

How many transactions does a test-and-test-and-set lock take (two per waiter per handoff)?

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°.

Explain why

Why does a store buffer break sequential consistency, and why do processors keep it anyway?

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.