5.4.16 · D3 · HinglishMemory Hierarchy & Caches

Worked examplesMemory consistency models

3,083 words14 min read↑ Read in English

5.4.16 · D3 · Hardware › Memory Hierarchy & Caches › Memory consistency models


The scenario matrix

Har litmus test jo hum face kar sakte hain wo is table ki ek row hai. Neeche ke 8 examples ka goal hai har cell ko kam se kam ek baar fill karna.

# Case class Concrete question Models that differ here
A S→L reorder (store buffer) Store-Buffering: kya dono loads stale 0 read kar sakte hain? SC=No, TSO/x86=Yes
B S→S reorder Message-passing flag: kya consumer flag dekh sakta hai lekin stale data? SC/TSO=No, Weak=Yes
C L→L reorder Load-buffering: kya do loads apne stores se pehle reorder ho sakte hain? SC/TSO=No, Weak=Yes
D Degenerate: single core Kya ek core apne khud ke ops ko kabhi out of order dekhta hai? All models: No
E Degenerate: no sharing Do cores, disjoint addresses — koi interaction? All models: No
F Cycle test (proof) Prove karo ki SC, SB ke bad outcome ko forbid karta hai SC only
G Multi-copy atomicity (IRIW) Kya do readers do writes ke order par agree karte hain? SC/TSO=Yes agree, POWER/ARM=No
H Limiting case: full fences MFENCE insert karo — kya TSO, SC recover kar leta hai? recovers SC
I Real-world twist Spin-lock acquire/release correctness needs acquire/release
J Exam twist: transitivity WWC / cumulative barriers across 3 cores Weak needs cumulative fence

Example A — Store→Load reorder (cell A)

Answer: Yes.

  1. Kya: S1, x=1 write karta hai lekin value pehle Core 1 ke store buffer mein jaati hai, memory mein nahi. Yeh step kyun? TSO ki sirf ek relaxation hai ki ek store buffered baith sakta hai jab core aage race karta hai — yeh S→L cell hai.
  2. Kya: L2, y padhta hai. Memory mein abhi bhi y=0 hai (S3 bhi Core 2 ke buffer mein atak gaya hai). To r1 = 0. Kyun: doosre core par buffered store abhi globally visible nahi hai.
  3. Kya: Symmetrically, L4, x=0 padhta hai, to r2 = 0. Kyun: dono cores ne ek saath yahi trick ki — yeh store buffer ka poora point hai.
Figure — Memory consistency models

Verify: SC ke under yeh impossible hai (Example F prove karta hai). TSO ke under do buffered stores plus do early loads exactly (r1,r2) = (0,0) dete hain. Yeh parent ke canonical claim se match karta hai. ✅


Example B — Store→Store reorder (cell B)

Answer: Yes — r stale 0 ho sakta hai.

  1. Kya: Weak hardware par S1 aur S2 ka koi S→S ordering nahiflag=1, Core 2 tak data=42 se pehle pahunch sakta hai. Yeh step kyun? Yeh S→S cell hai; TSO ise forbid karta hai, ARM nahi karta.
  2. Kya: Consumer ka loop L3, flag==1 dekhta hai, exit karta hai, phir L4, data padhta hai — lekin data=42 abhi propagate nahi hua, to r=0. Kyun: dono writes independently travel kiye.
  3. Fix: producer, S1 aur S2 ke beech ek release fence insert karta hai; consumer, L3 ke baad ek acquire fence insert karta hai. Dekho Memory Barriers and Fences aur C++ Memory Model (acquire-release). Kyun: ek release-acquire pair us S→S / L→L ordering ko recreate karta hai jo algorithm ko chahiye.

Verify: TSO/x86 par S→S preserved hai, to r guaranteed 42 hai — pattern wahan bina fence ke kaam karta hai. ARM par bina fences ke r ∈ {0, 42}. Weak par dono outcomes legal hain, TSO par sirf 42. ✅


Example C — Load→Load reorder (cell C)

Answer: Sirf Weak (ARM/POWER) par; SC aur TSO par No.

  1. Kya: r1==1 matlab L1 ne wo value padhi jo S4 ne likhi. r2==1 matlab L3 ne wo value padhi jo S2 ne likhi. Yeh step kyun? Hum pooch rahe hain ki kya ek load effectively uske neeche wale store ke baad slide kar sakta hai — L→S / L→L relaxation.
  2. Kya: Iske liye, S4 L1 se pehle visible honi chahiye, aur S2, L3 se pehle — lekin program order mein L1<S2 aur L3<S4 hai. Kyun: SC/TSO par loads kabhi same core ke baad wale stores ko bypass nahi karte, to koi bhi core apna store apne earlier load ke complete hone se pehle publish nahi kar sakta.
  3. Kya: ARM par load ko store ke past delay/speculate kiya ja sakta hai, dono 1s ko allow karte hue. Kyun: weak models by default L→L aur L→S ordering drop kar dete hain.

Verify: SC/TSO table rows dono L→S = ✅ dikhate hain, to LB ka (1,1) wahan impossible hai; ARM ki row sab ❌ hai to wahan allowed hai. ✅


Example D — Single core khud ko order mein dekhta hai (cell D)

Answer: r = 5, hamesha, har model par.

  1. Kya: TSO mein bhi store x=5 buffer mein baith jaata hai, lekin same address ka baad wala load pehle buffer check karta hai — store forwarding. Yeh step kyun? Yeh degenerate single-core case hai; consistency models sirf yeh govern karte hain ki doosre cores kya dekhte hain, khud core ka apna view nahi.
  2. Kya: To L2, drain timing se independent, fresh value 5 dekhta hai. Kyun: har model per-core, per-address program order preserve karta hai — yeh Cache Coherence guarantee plus store forwarding hai, dekho Store Buffers and Write Buffering.

Verify: r = 5. Koi model yahan r = 0 allow nahi karta. Yeh woh invariant hai jo single-threaded code ko correct rakhta hai. ✅


Example E — Koi sharing nahi, koi interaction nahi (cell E)

Answer: (1, 1) har model par.

  1. Kya: Har core sirf apna address padhta hai, jo usne abhi likha. Yeh step kyun? Koi shared address nahi hone se consistency model ke paas constraint karne ke liye kuch nahi — yeh do independent single-core programs mein degenerate ho jaata hai (cell D do baar).
  2. Kya: Store forwarding har r ko uske apne core ki fresh write deta hai. Kyun: cross-core ordering rules tab fire nahi hote jab koi location shared nahi hoti.

Verify: (r1, r2) = (1, 1). Reordering sharing ke bina invisible hai. ✅


Example F — Cycle proof: SC, SB ke bad outcome ko forbid karta hai (cell F)

  1. Kya: r1==0 ⇒ single total order mein, load L2 (y padhna) store S3 (y likhna) se pehle aaya. To . Yeh step kyun? SC ek ऐसा ek total order deta hai jo har program order se consistent ho; initial 0 return karne wala load us store se pehle hona chahiye jo use change karta.
  2. Kya: r2==0 ⇒ isi tarah .
  3. Kya: Program order force karta hai (Core 1) aur (Core 2). Kyun: SC kisi core ke khud ke operations ko kabhi reorder nahi karta.
  4. Kya: Unhe chain karo: . Kyun: charon facts ko head-to-tail substitute karo.
  5. Kya: Yeh kehta hai — ek cycle. Ek total order antisymmetric hota hai, to yeh contradiction hai. ∎
Figure — Memory consistency models

Verify: Dependency graph mein ek 4-node directed cycle hai, to koi total order exist nahi karta ⇒ SC, (0,0) ko forbid karta hai. Isliye store buffers (TSO) SC se strictly weaker hain. ✅


Example G — Kya do readers agree karte hain? IRIW (cell G)

Answer: SC aur TSO par No. POWER/ARM (non-multi-copy-atomic) par Yes.

  1. Kya: Yeh outcome poochta hai ki kya do independent writes alag-alag readers dwara alag-alag orders mein observe ki jaati hain. Yeh step kyun? Yeh kisi bhi single-core reordering se zyada strong property hai — yeh is baare mein hai ki kya ek single global store order exist bhi karta hai.
  2. Kya: SC/TSO dono stores ko ek total store order mein commit karte hain, to sab readers agree karte hain ⇒ yeh outcome impossible hai. Kyun: "single total order" exactly wahi hai jo SC/TSO stores ke baare mein promise karte hain.
  3. Kya: POWER/ARM, x=1 ko C3 tak jaldi propagate kar sakta hai jabki y=1, C4 tak jaldi pahunchta hai — writes multi-copy-atomic nahi hain. Kyun: weak interconnects writes ko alag-alag cores tak alag-alag times par deliver karte hain.

Verify: SC/TSO, split-observation (1,0,1,0) ko forbid karte hain; POWER/ARM ise allow karte hain. Weak hardware par agreement recover karne ke liye ek cumulative barrier chahiye (cell J). ✅


Example H — Limiting case: fences SC recover kar lete hain (cell H)

Answer: Nahi — fences is test ke liye SC restore kar dete hain.

  1. Kya: MFENCE store buffer drain karta hai, S1 ko L2 execute hone se pehle globally visible hone pe majboor karta hai. Yeh step kyun? Yeh limiting case hai: TSO ki ek relaxation (S→L) ko wapas on karne se locally full SC ordering milti hai. Dekho Memory Barriers and Fences aur Atomic Operations and Locks.
  2. Kya: Ab global order mein kam se kam ek store truly pehle hai, to dono loads 0 nahi padh sakte — Example F ka cycle argument reuse karo. Kyun: S→L re-enforce hone ke baad, SB ordering graph (0,0) ke liye acyclically impossible hai.

Verify: Fences ke saath, TSO ka L→L, L→S, S→S, aur S→L sab enforce hain ⇒ table row SC ke barabar ⇒ (0,0) forbidden. Fence woh "dial" hai jo TSO se SC tak wapas jaata hai. ✅


Example I — Real-world twist: ek spin-lock (cell I)

Answer: Sirf tab jab test_and_set ke paas acquire semantics hon aur release store ke paas release semantics hon.

  1. Kya: ARM par ek plain load/store lock, critical-section reads ko acquire ke upar hoist hone deta hai (L→L relaxed) ya writes ko release ke neeche sink hone deta hai (S→S relaxed). Yeh step kyun? Ordering ke bina, doosra thread "unlocked" state observe kar sakta hai jabki hamare writes abhi in-flight hain — shared data corrupt ho sakta hai.
  2. Kya: Acquire = koi baad wala op uske pehle reorder nahi ho sakta. Release = koi pehle wala op uske baad reorder nahi ho sakta. Kyun: saath milke yeh ek one-way fence pair banate hain jo critical section ko box karta hai — exactly C++ Memory Model (acquire-release) ki guarantee.
  3. Kya: x86/TSO par ek LOCK-prefixed test-and-set already buffer drain karta hai, to lock wahan weaker source annotations ke saath kaam karta hai. Kyun: TSO mein sirf S→L ki kami thi, jo locked atomic fix kar deta hai.

Verify: lock-in par acquire + lock-out par release ⇒ har critical-section op unke beech ordered hai ⇒ koi leak nahi. Yeh weak models ke under mutual exclusion ka standard correctness proof hai. ✅


Example J — Exam twist: transitivity / cumulativity (cell J)

Answer: SC/TSO par haan. Plain ARM/POWER par nahi jab tak C2 ki fence cumulative na ho.

  1. Kya: r1==1 matlab C2 ne C1 ka x=1 dekha; phir C2 ne y=1 publish kiya; r2==1 matlab C3 ne woh y dekha. Yeh step kyun? Intuitively causality ko C1→C2→C3 chain ke through x=1 carry karni chahiye.
  2. Kya: Non-multi-copy-atomic hardware par, x=1 abhi bhi C3 tak nahi pahuncha hoga chahe y=1 pahunch gaya ho, to r3 0 ho sakta hai. Kyun: ordinary barriers sirf fencing core ke khud ke ops order karte hain; yeh force nahi karte ki pehle observed writes bhi propagate hon — woh zyada strong property cumulativity hai.
  3. Kya: Ek cumulative release fence (e.g. ARM dmb ish / release store) force karta hai ki C1 ka x=1, C2 ke y=1 se pehle propagate ho, to r3 1 hona chahiye. Kyun: cumulativity fence ki reach un writes tak extend karti hai jo core ne padhi hain, na sirf likhi hain.

Verify: SC/TSO: single store order ⇒ r3 1 forced hai. Plain weak: r3 ∈ {0,1}. C2 par cumulative fence ⇒ r3 = 1. Yeh classic "barriers automatically transitive nahi hote" exam trap hai. ✅


Recall Har example kaun sa cell hit karta hai?

Example A kaun sa reordering pair hit karta hai? ::: S→L (store buffer) Example B kaun sa pair relax karta hai, aur sirf kaun se model par? ::: S→S, sirf Weak/ARM par Example C (Load-Buffering) kaun se models par impossible hai? ::: SC aur TSO Example F impossibility prove karne ke liye kaun si graph property use karta hai? ::: memory order mein ek directed cycle IRIW (G) kaun si hardware property distinguish karta hai? ::: multi-copy atomicity (single vs. per-core store propagation) Example H dikhata hai ki ek fence TSO ko kaun se model tak dial karta hai? ::: Sequential Consistency (SC) Example J ka trap yeh hai ki ordinary barriers mein kaun si property nahi hoti? ::: cumulativity (cores ke across transitivity)


Back to Memory consistency models · prerequisite recap: Out-of-Order Execution, Store Buffers and Write Buffering.