5.4.8 · D4 · HinglishMemory Hierarchy & Caches

ExercisesMulti-level cache hierarchy (L1 - L2 - L3)

3,152 words14 min read↑ Read in English

5.4.8 · D4 · Hardware › Memory Hierarchy & Caches › Multi-level cache hierarchy (L1 - L2 - L3)

Ye problems "formula padhna" se lekar "hierarchy design karna" tak build karte hain. Har solution ek collapsible callout ke andar chhupa hua hai taaki tum pehle khud try kar sako. Har ek ko haath se karo, phir reveal karo.

Jo tool hum poore time use karte hain woh hai recursive AMAT formula from Multi-level cache hierarchy (L1 - L2 - L3). Chaliye har symbol ko use karne se pehle re-anchor karte hain — yahan koi bhi symbol aisa nahi hai jo tumne defined nahi dekha ho.

Ek cheez dhyan rakhna, kyunki yeh almost sabko trip karti hai:


Level 1 — Recognition

Exercise 1.1 (L1)

Apne shabdon mein batao ki quantity kya represent karti hai aur uski units kya hain.

Recall Solution

YEH KYA hai: ek pure fraction hai (koi units nahi); cycles mein hai. Unka product cycles mein hai. KYUN: = un accesses ka fraction jo L1 miss karte hain aur isliye L2 visit karna padta hai. Pay karne ki chance ko pay karne ki cost se multiply karne par milta hai average (expected) cycles per access jo sirf L2 enter karne mein kharch hote hain. Yeh total AMAT ka ek term hai — "woh extra cost jo L2 guaranteed L1 hit time ke upar add karta hai."

Exercise 1.2 (L1)

Ek single-level cache mein , hit rate , hai. AMAT formula likho aur compute karo.

Recall Solution

Formula (one level): . YEH shape KYUN hai: har access pay karta hai (hum hamesha pehle L1 mein dekhte hain). Ek fraction miss karta hai aur poora pay karta hai.


Level 2 — Application

Exercise 2.1 (L2)

Two-level cache: , , , , . AMAT nikalo.

Recall Solution

Andar se bahar. Pehle: ek L1-miss ka average cost kya hai? Woh L2 tak pahunchta hai (14 pay karo), aur un mein se ek chauthaai DRAM par miss karta hai (120 pay karo): KYUN: yeh "44" ek L1 miss ki average price hai. Ab ise weight karo ki L1 kitni baar miss karta hai:

Exercise 2.2 (L2)

Full 3-level: , , , , , , . AMAT nikalo.

Recall Solution

Brackets ko sabse deep se bahar ki taraf build karo. Innermost (L2 miss ki cost): Middle (L1 miss ki cost): Outer (poori cheez):


Level 3 — Analysis

Exercise 3.1 (L3)

Exercise 2.2 ke numbers use karte hue, ek engineer propose karta hai L3 miss rate ko se half karke karna (better replacement policy) — ya phir — L1 hit time se 1 cycle shave karna (4 → 3). Kaunsa change AMAT zyada kam karta hai?

Recall Solution

Option A — better L3 (baaki sab same): Innermost: Middle: Outer: cycles. se improvement: roughly cycles.

Option B — faster L1 (, baaki sab 2.2 jaisa): Nested brackets unchanged hain (abhi bhi ), sirf base badla hai: se improvement: exactly cycle.

KYUN B itna decisively jeetta hai: L1 hit time 100% accesses pay karte hain, isliye 1 cycle off = 1 cycle off AMAT, full stop. L3 improvement sirf accesses ko touch karta hai jo kabhi L3 tak pahunchte hi hain — wahan ek huge relative gain front par aakar ek tiny absolute gain ban jaata hai. Common case ko faster banana rare misses ko beat karta hai.

Exercise 3.2 (L3)

Tumhe global miss rates di gayi hain: saari accesses mein se 6% L1 miss karti hain (), saari accesses mein se 1.8% L2 miss karti hain (), saari accesses mein se 0.27% L3 miss karti hain (). ke saath, AMAT compute karo. (Ise Ex. 2.2 se match karo.)

Recall Solution

KYUN ek alag formula: ek global miss rate pehle se hi us level tak pahunchne ki probability include karta hai (yeh saari accesses ke against measure hota hai). Toh global language mein hum har level ki extra cost directly add karte hain, apni global rate se weighted — hum nest-multiply nahi karte. Poora additive global formula: ek access hamesha pay karta hai. Jab bhi woh ek level globally miss karta hai, usse next level probe karna padta hai aur us next level ki hit time pay karni padti hai — aur aisa karne wale accesses ka fraction exactly us level ki global miss rate hai jo abhi chhoot gayi: Values plug in karo: Local rates ke zariye cross-check: se convert karo aur Ex. 2.2 reuse karo:

  • .
  • .
  • . Ye exactly Exercise 2.2 ki local rates hain, isliye nested formula bhi deta hai. Lesson: global aur local ek hi hierarchy ki do languages hain — additive global form aur nested local form hamesha agree karte hain.

Level 4 — Synthesis

Exercise 4.1 (L4)

Ek design mein AMAT cycles (Ex. 2.2) aur hai. Cached memory system ka speedup no-cache machine ke upar compute karo, aur use karo batane ke liye ki uncached machine CPU ki peak performance ka kitna fraction retain karegi.

Recall Solution

Speedup = (no-cache time per access) / (cached time per access): KYUN yeh sahi ratio hai: bina kisi cache ke, har access full 180-cycle DRAM trip hai; hierarchy ke saath average trip 5.89 cycles hai. Ratio yeh hai ki memory system kitni baar faster data deliver karta hai. Peak performance ka fraction jo uncached machine retain karti hai: agar CPU otherwise apni clock rate par useful work issue kar sakta par har memory access use access latency ke liye stall karta hai, toh throughput ke scale par hota hai. Uncached machine ki memory slower hai, isliye memory-bound stream par woh sirf roughly throughput retain karti hai jo cached machine achieve karta hai. Ise padhna: hierarchy ~97% ka heavy lifting kar raha hai — ise nikaalo aur ek memory-bound workload roughly one-thirtieth speed par crawl karta hai. (Hum jaanbujhkar ek single "effective MHz" number quote karne se bache hain, kyunki woh exactly compute vs. memory work ke mix par depend karta hai; clean, assumption-free statement speedup itself hai.)

Exercise 4.2 (L4)

Design decision: tum ek fixed transistor budget L3 add karne mein spend kar sakte ho (2-level ko 3-level mein turn karna) ya L2 enlarge karne mein taaki uski local miss rate drop ho. Ex. 2.1 ki 2-level machine (AMAT ) diye gaye, option A ek L3 add karta hai ke saath (toh L2 misses ab pehle L3 hit karti hain, phir DRAM at ). Option B L2 enlarge karta hai, ko se tak drop karte hue aur ko tak raise karte hue. Kaunsa better hai?

Recall Solution

Ex. 2.1 yaad karo: .

Option A (L3 add karo): L2-miss tail ab L3 se guzarti hai: Innermost: Middle: Outer: cycles.

Option B (bigger L2): 2-level rahega, : Middle: Outer: cycles.

Verdict: A deta hai , B deta hai Option A (L3 add karo) barely jeetta hai. KYUN: L3 poore DRAM tail (woh expensive 120) ko ek 20%-only escape ke peeche shield karta hai, jabki L2 enlarge karne se uski miss rate help hui lekin surviving 12% abhi bhi full DRAM pay karta hai. Jab DRAM penalty large ho, toh ek filtering level insert karna ek existing one ko improve karne se better hota hai. Neeche ka figure dono AMATs ko directly stacked bars ke roop mein comparable banata hai.

Figure — Multi-level cache hierarchy (L1 - L2 - L3)

Level 5 — Mastery

Exercise 5.1 (L5)

General break-even miss rate derive karo jis par L3 add karna (hit time , miss rate ) seedha DRAM jaane ke comparison mein worthwhile hai. Yaani: ek L2 miss ke liye, kab "L3 se detour" seedha DRAM jaane se faster hai?

Recall Solution

Do tails compare karo jo ek L2-miss le sakta hai:

  • Seedha DRAM: cost .
  • L3 se: cost . L3 add karna tab help karta hai jab: Break-even miss rate ke liye solve karo (equal set karo): Ise padhna: L3 tab tak pay off karta hai jab tak uski actual miss rate ho. Example: . Toh L3 ek whopping 77.8% miss rate tak help karta hai — koi bhi real L3 (10–30% miss) easily worth it hai. KYUN itna lenient: ek mostly-missing L3 bhi zyaadatar trips ko ek cheap 40-cycle probe mein turn karta hai, aur sirf misses 180 pay karti hain. Detour cheap hai; woh kabhi kabhi provide karta hai shortcut huge hai.

Exercise 5.2 (L5)

Ek access stream: 70% loads, 30% stores. L1 cache write-through, no-write-allocate policy use karta hai (dekho Write Policies). Reads (loads) Ex. 2.1 ka normal 2-level path use karte hain, toh ek load Ex. 2.1 AMAT cost karta hai. Ek store, write-through/no-allocate ke under, aise behave karta hai: woh cycles mein L1 hit karta hai (store L1 copy write karta hai), aur kyunki policy write-through hai usse aur bhi write L2 tak push karna padta hai, L2 hit time cycles upar se pay karte hue (no-write-allocate ka matlab hai ki jo store miss karta hai woh line ko L1 mein pull up nahi karta, toh koi extra fetch nahi — store simply L2 par complete hota hai). Stream ka effective average access time compute karo.

Recall Solution

Load cost (stream ka 70%). Ek load poora Ex. 2.1 two-level path leta hai: (Yeh exactly wahi AMAT hai jo Exercise 2.1 mein compute ki gayi thi — units: cycles.)

Store cost (stream ka 30%). Write-through ka matlab hai har store L2 tak propagate karta hai, toh ek store L1 hit time plus L2 hit time pay karta hai: No-write-allocate ka matlab hai ki ek missing store line ko L1 mein fetch nahi karta — woh simply L2 par land karta hai — toh koi additional DRAM term nahi hai. Units: cycles.

Frequency se blend karo. Loads aur stores do populations hain alag costs ke saath; stream average har cost ko kitni baar occur hoti hai us se weighted hoti hai: Ise padhna: stores minority hain (30%) phir bhi average mein zyada se zyada contribute karte hain, kyunki write-through constant L2 traffic force karta hai ( cycles each). Yahi wajah hai ki real designs write buffers add karte hain store latency ko useful work ke peeche hide karne ke liye.

Exercise 5.3 (L5)

Do cores ek inclusive L3 share karte hain. Core A ek line likhta hai; Core B phir use padhta hai. AMAT framework use karte hue explain karo ki shared L3 B ki access ko kyun cheaper banata hai usse jab har core ka private L3 hota, aur , use karte hue B ki read cost estimate karo, yeh maante hue ki line shared L3 mein present hai (hit) versus absent hai (private-L3 case ek coherence trip force karta hai jo DRAM-latency ki tarah model ki gayi hai).

Recall Solution

Shared L3 (line wahan hai): B apne private L1/L2 miss karta hai, phir shared L3 hit karta hai. Us access ki tail cost — B ke apne private levels se aage ka part — exactly L3 hit time hai: A ne jo data produce kiya woh common L3 mein baitha hai, B ke liye ready.

Private L3 (line sirf A ke caches mein hai): B apne saare levels miss karta hai, aur coherence protocol ko A ki copy interconnect ke across fetch karni padti hai — yahan full memory-latency trip ki tarah model ki gayi hai:

Saving:

KYUN sharing yahan jeetti hai: ek shared last-level cache ek inter-core coherence miss (≈180, near DRAM) ko ek ordinary L3 hit (40) mein turn karta hai. Producer→consumer data ek ek jagah rehta hai jahan har core saste mein pahunch sakta hai, bajay ek core ke private cache mein phansa rehne aur on demand interconnect ke across haule jaane ke. Real designs LLC ko shared isliye banate hain (dekho Cache Coherence): multicore mein jo latency matter karti hai woh sirf apne-data-tak latency nahi balki dusre-core-ke-data-tak latency hai, aur L3 sharing baad wale ko ek coherence miss se ek cache hit mein collapse karta hai.


Recall Quick self-check ledger (apne numbers confirm karne ke liye reveal karo)

Ex 1.2 ::: 14 cycles Ex 2.1 ::: 6.52 cycles Ex 2.2 ::: 5.89 cycles Ex 3.1 ::: faster L1 jeetta hai (4.89 vs 5.647) Ex 3.2 ::: 5.89 cycles (additive global form aur local form agree karte hain) Ex 4.1 ::: speedup ≈ 30.6×, uncached ≈ 3.3% throughput retain karta hai Ex 4.2 ::: L3 add karo (5.40) bigger L2 (5.432) ko beat karta hai Ex 5.1 ::: m★ = 1 − T_L3/T_mem ≈ 0.778 Ex 5.2 ::: 9.664 cycles (loads 6.52, stores 17) Ex 5.3 ::: 140-cycle saving (40 vs 180)

Connections

Prerequisite ideas aur neighbours: Cache Organization, Cache Replacement Policies, Write Policies, Cache Coherence, Memory Access Patterns, TLB and Virtual Memory, CPU Pipeline, DRAM Architecture, aur parent Multi-level cache hierarchy (L1 - L2 - L3).