6.1.5Parallelism & Multicore

Shared memory vs distributed memory

3,124 words14 min readdifficulty · medium4 backlinks

What Are These Architectures?

Figure — Shared memory vs distributed memory

Why This Distinction Matters

The problem being solved: When you split a computation across N processors, they need to share results. The architecture determines:

  1. HOW data moves (implicit hardware vs explicit messages)
  2. WHEN consistency is guaranteed (automatic vs programmer-controlled)
  3. WHERE bottlenecks appear (memory bus vs network)

Shared Memory: The Implicit Communication Model

Distributed Memory: The Explicit Communication Model

The Coherence Problem in Shared Memory

Comparative Analysis

Aspect Shared Memory Distributed Memory
Addressing Global address space (any CPU → any address) Local address space (CPU → only own memory)
Communication Implicit (load/store instructions) Explicit (send/receive, MPI)
Synchronization Locks, barriers (hardware atomic ops) Messages carry synchronization (blocking receive)
Scalability Limited by memory bus (≈ 8-64 cores typical) Scales to1000s of nodes (limited by network)
Programming Easier (threads, OpenMP) Harder (message orchestration, deadlock risks)
Latency Cache hit: ~1 ns, miss: ~100 ns Message: ~1-50 μs (1000× slower)
Bandwidth Memory bus: ~100 GB/s Network: 10-200 Gbps (~1-25 GB/s)
Recall Feynman Explanation (ELI12)

Imagine you and your friends are building a huge LEGO castle.

Shared memory = You all sit around one giant table with all the LEGO bricks in the middle. Anyone can grab any brick anytime. Good: You can instantly see what others built and use their bricks. Bad: If you both reach for the same brick, you fight! And if 10 friends all crowd the table, you're constantly bumping into each other. Distributed memory = Everyone gets their own small table in different rooms, with their own pile of bricks. Good: No fighting—you have your own space! You can have100 friends each in their own room building their section. Bad: If you need a blue brick and your friend has it, you have to walk to their room and ask for it (slow!). And you need to plan: "I'll give you my red bricks, you give me your blue bricks."

The choice depends on your LEGO castle design. If everyone needs to constantly share bricks and change the same part → shared table is better (despite crowding). If everyone builds independent towers and only occasionally combines them → separate rooms scale better.

Hybrid Models: The Real World

Modern systems combine both:

Performance Models


Connections

  • Cache Coherence Protocols – MESI, MOESI, snooping vs directory
  • NUMA Architecture – Non-uniform memory access in multi-socket shared memory
  • Message Passing Interface (MPI) – Standard API for distributed memory
  • OpenMP – Directive-based shared memory paralelism
  • Interconnect Networks – Topologies for distributed systems (torus, fat-tree)
  • Parallel Programming Models – Task parallelism, data parallelism, PGAS
  • Memory Consistency Models – Sequential consistency, release consistency
  • False Sharing – Cache line interference in shared memory

#flashcards/hardware

What is the key difference between shared and distributed memory architectures? :: Shared memory: all processors access single global address space (implicit communication via loads/stores). Distributed memory: each processor has private local memory, communication via explicit message passing.

Why does shared memory not scale beyond ~64 cores?
The memory bus becomes a bottleneck—cache coherence traffic grows as O(N) to O(N²) under write contention, saturating bus bandwidth. Also, maintaining coherence requires invalidating remote caches for every write to shared data.

Derive the message passing latency formula :: T_send = T_latency + M/B. T_latency is the fixed cost to initiate the message (OS overhead, network setup). M/B is the transmission time for M bytes at bandwidth B. For small messages, latency dominates; for large messages, bandwidth matters.

What is false sharing and why does it hurt performance?
False sharing occurs when two threads write to different variables that happen to be in the same cache line (typically 64 bytes). Writing one variable invalidates the entire cache line in other cores' caches, causing unnecessary coherence traffic and cache misses—performance can drop 5-10×.
When should you choose distributed memory over shared memory?
Choose distributed memory when: (1) you need to scale beyond ~64 cores, (2) communication is coarse-grained (bulk data transfers, not fine-grained random access), (3) the problem naturally decomposes into independent subproblems (embarrassingly parallel), or (4) you need to aggregate memory capacity beyond a single machine.
What is the role of cache coherence protocols in shared memory?
Cache coherence protocols (e.g., MESI) ensure all caches see a consistent view of memory. When one core writes to a cache line, the protocol automatically invalidates or updates copies in other cores' caches—using the memory bus to broadcast these control messages. This is "hidden message passing."
Why is MPI_Allreduce a common bottleneck in distributed systems?
Allreduce combines values from all processes (e.g., suming gradients in ML) and broadcasts the result to everyone. It requires O(log N) communication rounds (tree-based algorithms) and transfers O(N × message_size) total data. Latency and bandwidth are both critical—dominates when frequent synchronization is needed.
What is NUMA and how does it blur the shared vs distributed boundary?
NUMA (non-uniform memory access) occurs in multi-socket systems where CPUs share an address space but accessing "remote" memory (attached to another socket) is 2-3× slower than "local" memory. It's architecturally shared memory but has performance characteristics of distributed memory—programmers must be locality-aware.

Explain the deadlock risk in distributed memory with an example :: If both processes do send(other); receive(other); and send is blocking, both wait forever for the other to receive before they can send. The fix: use non-blocking sends (MPI_Isend), post receives first, or carefully order communications (e.g., even ranks send first, odd ranks receive first).

What is a hybrid MPI+OpenMP model?
Use MPI for inter-node communication (distributed memory) and OpenMP threads for intra-node parallelism (shared memory). Each node runs one MPI process with multiple OpenMP threads. Exploits low-latency shared memory within nodes and scales across nodes via message passing.

Concept Map

architecture choice

architecture choice

uses

requires

communicates via

accessed over

becomes

traffic grows

each node has

communicates via

limited by

scales poorly

scales well

Parallel Computing

Shared Memory

Distributed Memory

Single Global Address Space

Cache Coherence MESI

Implicit Hardware Access

Memory Bus

Bottleneck at high N

O of N squared Cost

Private Local Memory

Explicit Message Passing

Network Latency

Scalability

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho ek library mein bohot saare students baith kar padh rahe hain. Shared memory matlab sabke saamne ek hi bada table hai jismein sari books rakhi hain—koi bhi kisi bhi book kouthaa sakta hai instantly. Lekin problem yeh hai ki agar 50 studentsek hi table ke pas khade hon, toh dhaka-muki hogi aur sabko wait karna padega. Yeh hai memory bus ka bottleneck.

Distributed memory matlab har student apne pas ek choti table aur apni books rakhta hai. Ab koi interference nahi, har koi apni jagah par kaam kar raha hai peacefully. Lekin agar tumhe kisi aur student ki book chahiye, toh tumhe jake explicitly request karna hoga aur wo tumhe courier se bhejega—matlab slow messaging overhead. MPI, MapReduce ye sab distributed memory use karte hain taki thousands of machines ko coordinate kar sakein.

Aaj kal real systems hybrid use karte hain—ek node ke andar shared memory (threads), aur nodes ke bech distributed memory (network messages). Isse best of both worlds milta hai: local speed aur global scalability. CPU cores badhane hain toh eventually distributed memory ki taraf jana padta hai kyunki single memory bus sirf 64-100 cores tak hi handle kar sakta hai—uske bad coherence traffic itna zyada ho jata hai ki performance gir jati hai. Toh choice simple hai: small scale → shared, massive scale → distributed.

Go deeper — visual, from zero

Test yourself — Parallelism & Multicore

Connections