6.1.5 · HinglishParallelism & Multicore

Shared memory vs distributed memory

3,188 words14 min readRead in English

6.1.5 · Hardware › Parallelism & Multicore

What Are These Architectures?

Figure — Shared memory vs distributed memory

Why This Distinction Matters

Jo problem solve ho rahi hai: Jab aap ek computation ko N processors mein split karte ho, unhe results share karne padte hain. Architecture determine karta hai:

  1. HOW data move hota hai (implicit hardware vs explicit messages)
  2. WHEN consistency guarantee hoti hai (automatic vs programmer-controlled)
  3. WHERE bottlenecks aate hain (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 (koi bhi CPU → koi bhi address) Local address space (CPU → sirf apni memory)
Communication Implicit (load/store instructions) Explicit (send/receive, MPI)
Synchronization Locks, barriers (hardware atomic ops) Messages synchronization carry karte hain (blocking receive)
Scalability Memory bus se limited (≈ 8-64 cores typical) 1000s of nodes tak scale hota hai (network se limited)
Programming Aasaan (threads, OpenMP) Mushkil (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 karo tum aur tumhare dost ek bada LEGO castle bana rahe ho.

Shared memory = Tum sab ek giant table ke aas-paas baithe ho jisme bich mein sare LEGO bricks hain. Koi bhi kisi bhi brick ko anytime utha sakta hai. Achha: Tum instantly dekh sakte ho doosron ne kya banaya aur unke bricks use kar sakte ho. Bura: Agar tum dono ek hi brick ke liye pahuncho, tum ladte ho! Aur agar 10 dost table ke aas-paas bheed lagate hain, tum constantly ek doosre se takraate ho. Distributed memory = Har kisi ko apni choti table milti hai alag kamron mein, apne bricks ke saath. Achha: Koi ladai nahi—tumhara apna space hai! Tumhare paas 100 dost ho sakte hain har apne kamre mein apna section banate hue. Bura: Agar tumhe ek blue brick chahiye aur tumhare dost ke paas hai, tumhe unke kamre mein jaana padega aur maangna padega (slow!). Aur tumhe plan karna padega: "Main tumhe apni red bricks dunga, tum mujhe apni blue bricks dena."

Choice tumhare LEGO castle design par depend karti hai. Agar sab ko constantly bricks share karni hain aur ek hi part change karna hai → shared table better hai (crowding ke bawajood). Agar sab independent towers banate hain aur sirf kabhi kabhi combine karte hain → alag kamre better scale karte hain.

Hybrid Models: The Real World

Modern systems dono combine karte hain:

Performance Models


Connections

  • Cache Coherence Protocols – MESI, MOESI, snooping vs directory
  • NUMA Architecture – Multi-socket shared memory mein non-uniform memory access
  • Message Passing Interface (MPI) – Distributed memory ke liye standard API
  • OpenMP – Directive-based shared memory parallelism
  • Interconnect Networks – Distributed systems ke liye topologies (torus, fat-tree)
  • Parallel Programming Models – Task parallelism, data parallelism, PGAS
  • Memory Consistency Models – Sequential consistency, release consistency
  • False Sharing – Shared memory mein cache line interference

#flashcards/hardware

What is the key difference between shared and distributed memory architectures? :: Shared memory: sare processors single global address space access karte hain (implicit communication loads/stores ke zariye). Distributed memory: har processor ki private local memory hoti hai, communication explicit message passing ke zariye.

Why does shared memory not scale beyond ~64 cores?
Memory bus bottleneck ban jaata hai—cache coherence traffic write contention mein O(N) se O(N²) tak badhta hai, bus bandwidth saturate ho jaati hai. Saath hi, coherence maintain karne ke liye shared data par har write ke liye remote caches ko invalidate karna padta hai.

Derive the message passing latency formula :: T_send = T_latency + M/B. T_latency message initiate karne ki fixed cost hai (OS overhead, network setup). M/B, M bytes ke liye bandwidth B par transmission time hai. Chhote messages ke liye latency dominate karti hai; bade messages ke liye bandwidth matter karti hai.

What is false sharing and why does it hurt performance?
False sharing tab hoti hai jab do threads alag variables likhte hain jo ek hi cache line mein hote hain (typically 64 bytes). Ek variable likhna doosre cores ke caches mein poori cache line invalidate kar deta hai, unnecessary coherence traffic aur cache misses cause karta hai—performance 5-10× girr sakti hai.
When should you choose distributed memory over shared memory?
Distributed memory tab choose karo jab: (1) ~64 cores se zyada scale karna ho, (2) communication coarse-grained ho (bulk data transfers, fine-grained random access nahi), (3) problem naturally independent subproblems mein decompose hoti ho (embarrassingly parallel), ya (4) ek single machine se zyada memory capacity aggregate karni ho.
What is the role of cache coherence protocols in shared memory?
Cache coherence protocols (jaise MESI) ensure karte hain ki sare caches memory ka consistent view dekhein. Jab ek core kisi cache line par likhta hai, protocol automatically doosre cores ke caches mein copies invalidate ya update karta hai—memory bus par yeh control messages broadcast karke. Yahi "hidden message passing" hai.
Why is MPI_Allreduce a common bottleneck in distributed systems?
Allreduce sare processes se values combine karta hai (jaise ML mein gradients sum karna) aur result sabko broadcast karta hai. Ise O(log N) communication rounds chahiye (tree-based algorithms) aur total O(N × message_size) data transfer hota hai. Latency aur bandwidth dono critical hain—jab frequent synchronization chahiye tab dominate karta hai.
What is NUMA and how does it blur the shared vs distributed boundary?
NUMA (non-uniform memory access) multi-socket systems mein hoti hai jahan CPUs ek address space share karte hain but "remote" memory access (doosre socket se attached) "local" memory se 2-3× slower hoti hai. Architecturally shared memory hai but performance characteristics distributed memory jaisi hain—programmers ko locality-aware rehna padta hai.

Explain the deadlock risk in distributed memory with an example :: Agar dono processes send(other); receive(other); karte hain aur send blocking hai, toh dono forever wait karte hain doosre ke receive karne ka pehle woh send kar sakein. Fix: non-blocking sends (MPI_Isend) use karo, pehle receives post karo, ya communications carefully order karo (jaise even ranks pehle send karte hain, odd ranks pehle receive karte hain).

What is a hybrid MPI+OpenMP model?
Inter-node communication ke liye MPI use karo (distributed memory) aur intra-node parallelism ke liye OpenMP threads (shared memory). Har node ek MPI process run karta hai multiple OpenMP threads ke saath. Nodes ke andar low-latency shared memory exploit karta hai aur nodes ke beech message passing ke zariye scale karta hai.

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