Exercises — Shared memory vs distributed memory
6.1.5 · D4· Hardware › Parallelism & Multicore › Shared memory vs distributed memory
Shuru karne se pehle, teen numbers jo hum baar baar use karenge (sab parent note se):
Level 1 — Recognition
Exercise 1.1 (L1)
Har system ke liye bolo shared ya distributed memory: (a) Ek laptop jisme 8-core Intel CPU hai jo ek program 8 threads ke saath run kar raha hai. (b) Ek 500-node cluster jahaan har node ek alag computer hai jo InfiniBand se connected hai. (c) 100 cloud VMs jo network ke zariye coordinate kar rahe hain.
Recall Solution 1.1
(a) Shared. Ek physical memory, saare 8 cores ek hi global address space ko directly read/write karte hain. (b) Distributed. Har node ki apni private memory hai; nodes messages pass karke baat karte hain. (c) Distributed. VMs ek doosre ki RAM touch nahi kar sakti; sirf network hi channel hai.
Rule of thumb: Kya core X, core Y ka data ek plain load instruction se read kar sakta hai? Haan → shared. Sirf message bhejke → distributed.
Exercise 1.2 (L1)
Is sentence mein "Processor A send(data, size, dest) call karta hai aur Processor B receive() call karta hai," yeh kaunsa memory model hai, aur doosre model mein equivalent action kaisa dikhta?
Recall Solution 1.2
Yeh distributed memory hai — communication explicit hai (tum literally operation ka naam lete ho).
Shared memory mein equivalent hota hai implicit: A simply x = 5; karta hai aur B baad mein read x karta hai. Koi send/receive nahi aata — hardware ne tumhare liye data move kar diya (cache-coherence protocol ke zariye). Dekho Parallel Programming Models.
Level 2 — Application
Exercise 2.1 (L2)
Ek core ke liye , , aur hit rate hai. calculate karo.
Recall Solution 2.1
. Matlab: bhale hi 95% accesses fast hain, rare 5% slow misses average ko dominate karte hain (5.95 ns mein se 5.0 ns). Misses hi dushman hain.
Exercise 2.2 (L2)
Ek network mein aur hai. Compare karo ek 1 MB message bhejna vs 1000 messages of 1 KB each (same total 1 MB). ( bytes, bytes, bytes use karo.)
Recall Solution 2.2
Har byte-batch ka transfer time hai. bytes/s ke saath:
Ek bada message: bytes.
1000 chhote messages: har ek apna pay karta hai.
Batched version faster hai. Kyun: fixed latency har message par pay hota hai, toh data ko 1000 pieces mein kaatna ise 1000 baar pay karwata hai. Isliye MPI programs batch karte hain — parent note dekho. Related: Interconnect Networks.

Level 3 — Analysis
Exercise 3.1 (L3)
cores ek variable share karte hain MSI protocol ki Shared (S) state mein. Core 1 isme likhta hai. Parent ke rule "S se write karne par baaki caches invalidate hone chahiye" ko use karte hue, kitne invalidate messages fire hote hain? Agar aur har invalidate mein 30 ns bus time lagta hai (serialized), toh coherence burst kitna lamba hai?
Recall Solution 3.1
Invalidates needed . ke liye: messages. Ek bus par serialized: Kyun aur nahi: writer apni copy already khud rakhta hai — use sirf baaki caches ko silence karna hai. Yeh growth hi wajah hai ki shared-memory bus machines 8–64 cores ke aaspaas top out kar jaati hain. Dekho Cache Coherence Protocols.
Exercise 3.2 (L3)
Ek struct int count[8] ko ek 64-byte cache line mein pack karta hai (har int = 8 bytes → 8×8 = 64). Aath threads mein se har ek ek alag index par likhta hai. Explain karo kyun performance collapse ho jaati hai, aur ek-line fix do.
Recall Solution 3.2
Yeh False Sharing hai. Saare 8 counters ek cache line mein rehte hain. Coherence poori lines track karta hai, individual bytes nahi. Toh jab Thread 0 count[0] likhta hai, hardware us line ko har doosre thread ki cache mein invalidate kar deta hai — chahe kisi ne count[0] touch na kiya ho. Har write line ko saare cores ke beech ping-pong karata hai.
Kaisa dikhta hai: line har update par core→core→core bounce karti hai, 8 independent writes ko ek fully serialized coherence storm mein badal deta hai (parent 5–10× slowdown report karta hai).
Fix: har counter ko apni cache line par pad karo:
struct { int val; char pad[56]; } count[8]; // har entry = 64 bytesAb writes alag lines par hit karte hain → koi cross-invalidation nahi.

Level 4 — Synthesis
Exercise 4.1 (L4)
compute karo jahaan matrices hain jo 4 nodes mein row-wise split hain. Node 0 poora ( compressed yahan arithmetic ke liye) baaki 3 nodes ko broadcast karta hai, ek message each. Network: , . Broadcast time compute karo. Phir decide karo: kya communication ya computation likely bottleneck hai agar local compute hai?
Recall Solution 4.1
bytes ka ek message: Teen sequential sends (Node 0 → Nodes 1,2,3): Compute se compare karo. Communication sirf runtime hai. Conclusion: computation dominate karta hai — yeh problem ek one-time broadcast ke baad "embarrassingly parallel" hai. ka transfer ek baar pay karo, phir crunch karo. Yeh distributed memory at its best hai.
Exercise 4.2 (L4)
100 GPUs har batch mein gradient AllReduce karte hain. Model: , , , bits, bits/s ke saath. estimate karo. Kaunsa term dominate karta hai?
Recall Solution 4.2
Latency term: . Kyunki : Bandwidth term: Bandwidth bilkul dominate karta hai (80 ms vs 0.027 ms). Bade messages ke liye, — Exercise 2.2 ka bilkul ulta jahaan tiny messages ne latency ko king banaya. Lesson: message size decide karta hai kaunsa term jeetega.
Level 5 — Mastery
Exercise 5.1 (L5)
Ek single node mein 32 cores shared memory share karte hain (shared-memory model, OpenMP andar). Tumhare paas 64 aise nodes hain ek InfiniBand network par (distributed model, Message Passing Interface (MPI) across). Total = 2048 cores. Programming model design karo, aur justify karo kyun flat "MPI-everywhere" ya "threads-everywhere" scheme worse hai.
Recall Solution 5.1
Design: hybrid MPI + OpenMP.
- Ek node ke andar (32 cores): shared memory. OpenMP threads use karo jo ek address space share karte hain — communication free implicit loads/stores hai, koi message overhead nahi.
- Nodes ke across (64 nodes): distributed memory. MPI use karo: har node par ek MPI rank, ranks messages exchange karte hain.
Threads-everywhere kyun nahi? Tum nahi kar sakte — 64 alag machines ka koi shared address space nahi hai; node 0 par ek thread physically node 5 ki RAM load nahi kar sakta. Shared memory network cross nahi karta.
MPI-everywhere kyun nahi (2048 ranks)? Yeh kaam karta hai, lekin har node ke andar shared memory ko waste karta hai: ek hi node ke cores data ko message buffers ke through copy karte instead of directly padhne ke. Tum 32×64 = 2048 ranks ki coherence-free-but-copy-heavy traffic aur unnecessary memory duplication pay karte. Hybrid sahi tool har level par use karta hai: implicit sharing jahaan memory shared hai, explicit messaging sirf jahaan network cross karna ho. Dekho NUMA Architecture aur Memory Consistency Models.
Exercise 5.2 (L5)
Parent ke claim "cache coherence is hidden message passing" ko steel-man karo. Phir woh ek property batao jo distributed memory scalability ke badle mein chhod deta hai, aur ek jo rakhta hai.
Recall Solution 5.2
Steel-man: Shared memory mein, ek variable likhna secretly caches ko invalidate broadcasts trigger karta hai (Ex 3.1). Woh invalidations bus par messages hain — tumne kabhi send() nahi likha. Toh shared memory ne communication khatam nahi ki; usne use chhupaaya aur automate kiya hai. Jab hidden traffic bus ko saturate karta hai, shared memory scaling band kar deta hai — wohi wall jo distributed memory face karta hai, bas invisible.
Distributed memory chhod deta hai: implicit communication + automatic consistency ki suwidha — programmer ko ab har transfer orchestrate karna hai aur deadlock ka risk hai. Distributed memory rakhta hai (aur yahi jeet hai): independent, non-shared memory — koi single bus nahi jiske liye har core lade. Isliye yeh hazaaron nodes tak scale karta hai jabki shared memory tens of cores par ruk jaata hai.
Recall Self-test: gap bharo
Latency message time dominate karta hai jab message ::: chhota ho (toh , ke muqable mein tiny hai). MSI state S se ek write cores mein se itne invalidates bhejta hai ::: . False sharing ki unit hai ::: cache line (~64 bytes), variable nahi. Woh interface jo shared vs distributed decide karta hai ::: load/store (implicit) vs send/receive (explicit).