Exercises — Write-allocate vs no-allocate
5.4.7 · D4· Hardware › Memory Hierarchy & Caches › Write-allocate vs no-allocate
Shuru karne se pehle, aao uss chhoti si vocabulary ko fix kar lein jis par hum poore page mein rely karenge. Yeh sab parent note mein build ki gayi theen; yahan hum sirf unhe pin down kar rahe hain taaki koi symbol bina bataye na aaye.
Hum sab kuch memory traffic mein measure karte hain: wo read/write operations ki sankhya jo actually cache se main memory tak cross karti hain. Kam hona behtar hai, kyunki main memory slow hoti hai.
Level 1 — Recognition
L1.1 Har phrase ke liye, policy ka naam batao (write-allocate ya no-allocate): (a) "fetch-on-write" (b) "write-around" (c) "the written data bypasses the cache" (d) "on a write miss we load the whole 8-byte block, then modify one byte".
Recall Solution L1.1
- (a) Write-allocate — "fetch on write" iska literally doosra naam hai.
- (b) No-allocate — data cache ke around jaata hai.
- (c) No-allocate — bypass = around.
- (d) Write-allocate — modify karne se pehle poora block load karna iska defining move hai.
L1.2 Sahi hai ya galat: "Write-back almost always write-allocate ke saath pair hota hai."
Recall Solution L1.2
Sahi hai. Write-back cache ke andar up-to-date copy ko (dirty mark karke) rakhta hai aur memory mein baad mein, eviction par, write karta hai. Ek dirty copy rakhne ke liye tumhare paas pehle ek line honi chahiye — jo exactly wahi hai jo write-allocate provide karta hai. Write-back ko no-allocate ke saath pair karne par deferred write store karne ki koi jagah nahi bachti, jisse immediate memory write forced ho jaata hai (yaani write-through behaviour). Dekho Write-back vs Write-through.
Level 2 — Application
L2 ke liye yeh fixed setup use karo (parent ke examples jaisi hi):
- Cache: 4 lines, 8 bytes/line, direct-mapped, initially empty.
- Line index rule: (block number paane ke liye low 3 bits drop karo, phir mod 4 lo).
L2.1 Write-allocate + write-back ke under, tum WRITE 0x28 karte ho. Memory traffic aur line 1 ki final state list karo (valid? dirty?).
Recall Solution L2.1
- Kaunsi line? . . . → line 1.
- Line 1 invalid hai → write miss.
- Write-allocate: memory se 8-byte block
0x28–0x2Ffetch karo. → 1 read. - Cached line mein
0x28par byte write karo. - Write-back: abhi memory ko mat touch karo; dirty = 1 set karo. Memory traffic: 1 read. Final line 1: valid = yes, dirty = yes.
L2.2 Wahi write 0x28 lekin no-allocate + write-through ke under. Traffic aur line 1 ki final state?
Recall Solution L2.2
- Line 1, invalid → write miss.
- No-allocate: fetch mat karo, allocate mat karo.
- Write-through: write seedha memory ko bhejo. → 1 write. Memory traffic: 1 write. Final line 1: abhi bhi invalid (valid = no).
L2.3 Upar dono policies ke liye, write ke turant baad ek READ 0x29 aata hai. Hit hai ya miss, aur kyun?
Recall Solution L2.3
0x29wahi block mein hai jahan0x28hai (dono0x28–0x2Fmein).- Write-allocate case: block pehle se line 1 mein hai → hit, 0 memory traffic. Yahi spatial locality ka fayda hai (dekho Temporal vs Spatial Locality).
- No-allocate case: block kabhi laaya hi nahi gaya → miss, ab block fetch karna padega (1 read).
Level 3 — Analysis
L3.1 Wahi setup. Yeh sequence consider karo:
WRITE 0x100, WRITE 0x104, WRITE 0x108, WRITE 0x10C, READ 0x100
(kya yeh chaaron addresses ek 8-byte block mein hain? Check karo!). Write-allocate + write-back vs no-allocate + write-through ke under total memory traffic count karo.
Recall Solution L3.1
Pehle, block check. Block number = address .
0x100 = 256, .0x104 = 260, .0x108 = 264, . Alag block!0x10C = 268, .
Toh 0x100,0x104 block 32 mein hain; 0x108,0x10C block 33 mein hain. Parent note ke Example 3 ne casually kaha tha "sab same block mein" — 8-byte line ke saath yeh galat hai. Hum ise neeche correct karte hain. (Figure dekho.)

Write-allocate + write-back:
WRITE 0x100: miss → block 32 fetch karo (1 read), write karo, dirty.WRITE 0x104: hit (block 32 present) → 0.WRITE 0x108: miss → block 33 fetch karo (1 read), write karo, dirty.WRITE 0x10C: hit → 0.READ 0x100: hit → 0. Block 32 aur 33 lines aur par map hote hain — alag lines, koi eviction nahi, abhi tak koi write-back nahi. Total: 2 reads, 0 writes = 2 operations.
No-allocate + write-through:
- Har write ek miss hai aur seedha memory jaata hai: 4 writes.
READ 0x100: kabhi cached nahi → miss, 1 read. Total: 1 read + 4 writes = 5 operations.
Write-allocate 2 vs 5 se jeetta hai, kyunki usne har fetched block ko un do writes pe amortise kiya jo usse hit hua aur final read par bhi.
L3.2 Ab maano cache line 8 ki jagah 16 bytes hoti. Wahi chaar writes + read ke liye write-allocate + write-back count redo karo.
Recall Solution L3.2
16-byte line ke saath, block number = address .
0x100: .0x104,0x108,0x10C: . Sab block 16 mein! Toh:WRITE 0x100: miss → 1 read, dirty.- Agle teen writes: sab hits → 0.
READ 0x100: hit → 0. Total: 1 read. Bade lines ne yahan zyada spatial locality capture ki — ek fetch ne chaaron ko cover kar liya. Yeh Cache Line Size se direct link hai.
Level 4 — Synthesis
L4.1 Ek frame-buffer loop 1920×1080 pixels ek baar each likhta hai, unhe kabhi read nahi karta, phir program aage badh jaata hai. Kaunsa allocation + write policy choose karo, aur galat choice ka cache-pollution damage estimate karo.
Recall Solution L4.1
No-allocate + write-through choose karo (equivalently, non-temporal / Streaming Stores use karo). Kyun: har pixel ek baar likha jaata hai aur kabhi re-read nahi hota → zero temporal locality, toh caching kuch nahi kharidta. Yahan write-allocate ka pollution damage: buffer pixels hai. Maano 4 bytes/pixel = bytes. 64-byte lines ke saath yeh distinct blocks hain jo cache se stream hote hain. Ek typical L1 sirf kuch sau lines hi hold karta hai — toh poora cache flush hokar un garbage se refill ho jaata hai jise program kabhi dobara touch nahi karega, genuinely useful data ko evict karta hua. Yahi Cache Pollution hai. No-allocate pixels ko cache se untouched around bhejta hai, aur Write Combining Buffers unhe full-line bursts mein coalesce karta hai. Dekho Streaming Stores.
L4.2 First principles se explain karo ki "write-back + no-allocate" ek self-contradiction kyun hai. Ise 3-step deduction ki tarah karo.
Recall Solution L4.2
- A ko write miss. Humein eventually A ki nai value memory mein pohonchaani hai.
- No-allocate kehta hai: A ke liye cache line mat banao. Toh modified value ka cache ke andar koi ghar nahi hai.
- Write-back kehta hai: memory write ko tab tak defer karo jab tak (dirty) line evict na ho jaaye. Lekin step 2 ne humein koi line hold karne ko nahi diya aur koi dirty bit set karne ko nahi diya — defer karne ke liye kuch hai hi nahi jahan se. Store karne ki jagah nahi aur defer karne ka tarika nahi, toh write ko memory mein turant jaana hi padega. Lekin "hit-or-miss write par memory mein turant likhna" write-through ki definition hai. Toh "write-back + no-allocate" silently write-through ban jaata hai — yeh apni alag policy ke roop mein exist hi nahi kar sakta. Isliye write-back ko write-allocate ki zaroorat hai.
Level 5 — Mastery
L5.1 Numbers ke saath design decision. Tum ek write-heavy region ke liye write-allocate aur no-allocate ke beech choose kar rahe ho. Costs (cycles mein): ek block fetch karna , cache mein write karna , seedha memory mein write karna . Maano probability hai ki write miss ke baad block ko eviction se pehle kam se kam ek baar reuse kiya jaata hai (read ya write) — aur maano ek reuse tumhe exactly ek bachata hai jo no-allocate ne otherwise pay kiya hota.
Threshold probability derive karo jiske upar write-allocate cheaper policy hai, aur isko evaluate karo.
Recall Solution L5.1
Per-write-miss expected cost setup karo.
- No-allocate: region ke har access par (uncached) through-write ka cost lagta hai. Initial miss ka cost hai, aur agar reuse hoti hai toh ek aur . Expected cost .
- Write-allocate: miss par ek baar fetch pay karo () plus ek sasta cache write (); agar reuse hoti hai toh woh ki jagah ek sasta cache access hai. Expected cost .
Write-allocate tab cheaper hai jab terms group karo: Toh Plug in karo : Interpretation: kyunki yahan ek through-write ek fetch jitna hi expensive hai, write-allocate ko jeetne ke liye sirf lagbhag 1% reuse chance chahiye. High-fetch / low-reuse wahi ek regime hai jahan no-allocate dominate karta hai. Yeh parent ke decision-framework inequality se match karta hai, denominator mein chhoti correction tak.
L5.2 Framework ko sanity-check karo: agar fetching free hoti () wahi ke saath, toh kya hoga, aur uska sign tumhe kya bataata hai?
Recall Solution L5.2
Ek negative threshold ka matlab hai "write-allocate ke liye bhi jeetta hai" — kyunki probabilities negative nahi ho sakti, condition hamesha true hai. Reading: agar fetching free hai, hamesha allocate karo, kyunki tum bina kisi downside ke ek possibly-cheaper future access gain karte ho. ka sign ek quick "kya ek policy unconditionally better hai?" test hai.
Recall Quick self-test clozes
Write-allocate ek miss par pehle fetches the whole block, phir write karta hai. No-allocate write ko around the cache to memory bhejta hai. Write-back ko write-allocate ke saath pair karna padta hai kyunki isse dirty copy rakhne ke liye ek line chahiye. Ek write-once frame buffer ke liye sahi policy no-allocate hai taaki cache pollution se bacha ja sake.
Related: Write-back vs Write-through · Cache Line Size · Temporal vs Spatial Locality · Streaming Stores · Cache Pollution · Write Combining Buffers · Dirty Bit