6.1.5 · D3Parallelism & Multicore

Worked examples — Shared memory vs distributed memory

4,175 words19 min readBack to topic

This page is the drill ground for the parent topic. The parent told you the ideas; here we count every kind of question these ideas can throw at you, then solve one example for each. Nothing is hand-waved: every number is derived, every unit is checked.

Before we touch a single formula, one promise: I will not use a symbol until I have said, in plain words, what it means and drawn the picture it lives in.

The figure below draws exactly these two pictures side by side. Look at the left half: three cores (lavender circles) all reach into ONE butter-coloured memory with cheap two-way whisper arrows (coral) — but notice how crowded the single table is. Now the right half: each core owns its own mint memory box, and the only way to share is a one-way coral "letter" arrow between nodes. That left-vs-right contrast is the whole page in one image; every example below just puts numbers on one side of it.

Figure — Shared memory vs distributed memory

The scenario matrix

A "scenario" here = one shape of problem. If we solve one example per row, no exam question can surprise you. The table below names each cell in plain words only — the compact symbols (how many cores/caches), (message size in bytes) and (network bandwidth) are all defined immediately after, in "The three tools", before any example uses them.

# Cell (case class) What makes it distinct Covered by
A Shared, cache-hit dominated Almost all accesses hit cache → tiny average time Ex 1
B Shared, coherence-storm Many cores writing one line → bus floods with invalidations Ex 2
C Shared, degenerate: false sharing Different variables, same cache line → hidden invalidations Ex 3
D Distributed, latency-bound (small msg) Message so small the fixed stamp cost dominates Ex 4
E Distributed, bandwidth-bound (big msg) Message so big the per-byte cost dominates Ex 5
F Distributed, collective (AllReduce) Cost grows slowly (like a logarithm) with core count Ex 6
G Zero / limiting input Empty message; single core; what do formulas say? Ex 7
H Crossover word problem Real decision: pick shared or distributed for a task Ex 8
I Exam twist: mixed / trap Setup that looks like it favours one but doesn't Ex 9

We reuse three formulas from the parent, restated here so nothing is assumed.


Cell A — Shared, cache-hit dominated


Cell B — Shared, coherence-storm


Cell C — Shared, degenerate: false sharing


Cell D — Distributed, latency-bound (small message)

The next figure plots this exact idea. The horizontal axis is message size in bytes (log scale, running from byte on the left to bytes on the right); the vertical axis is total send time in microseconds (also log scale). Follow the lavender curve (total ): on the far left it hugs the coral dashed "latency floor" — that flat stamp is Ex 4's answer, marked with a coral dot. The mint dotted line is the pure per-byte term ; on the far right the lavender curve rides up along it — that steep region is Ex 5, marked with a mint dot. The legend names all three lines. Where the coral floor and mint slope cross is the "latency vs bandwidth" crossover — the whole distributed story in one plot.

Figure — Shared memory vs distributed memory

Cell E — Distributed, bandwidth-bound (big message)


Cell F — Distributed, collective (AllReduce grows like log N)


Cell G — Zero and limiting inputs


Cell H — Crossover word problem


Cell I — Exam twist / trap


Recall Self-test (answer before revealing)

The average access time formula weights hit and miss by what? ::: The hit rate and its complement ; it is a weighted average, always between and . In Ex 2, why is the total 14, not 64? ::: After the first broadcast (7 invalidations) only one cache owns the line, so later writes just migrate ownership (7 hand-offs) — nobody else has a copy to invalidate. Why does false sharing (Ex 3) hurt even with independent variables? ::: Coherence acts on whole cache lines; two logically-separate ints in one 64-byte line ping-pong the line between cores. A zero-byte message costs how much (Ex 7)? ::: Exactly — the fixed latency; you cannot send anything, even nothing, for free. In Ex 8, why does shared memory win? ::: For many tiny frequent updates, the message stamp () dwarfs a shared update ( ns), making messaging ~33× slower. The batching win (Ex 9) exists because latency is paid per ___ and bandwidth per ___. ::: per message; per byte.