Worked examples — Principle of locality (temporal - spatial)
5.4.1 · D3· Hardware › Memory Hierarchy & Caches › Principle of locality (temporal - spatial)
Yeh page parent note on locality ka hands-on companion hai. Wahan humne dono localities define ki thi. Yahan hum unhe compute karte hain — ek worked example per "flavour of situation" jo ek program cache ke saath kar sakta hai — including kuch weird degenerate cases (zero reuse distance, infinite stride, random access) jo parent note mein sirf passing mein mention hue the.
Koi bhi number dekhne se pehle, aao hum un do rulers par agree kar lein jinse hum locality measure karte hain. Dono parent note mein introduce ki gayi thi, lekin chaliye inhe re-anchor karte hain taaki kuch assume na karna pade.
Hum ek aur derived quantity bhi poore note mein use karenge:
The scenario matrix
Is chapter mein locality ka har problem in cells mein se ek hoga. Har worked example neeche us cell ke saath tagged hai jo wo cover karta hai, isliye milke poori table cover ho jaati hai.
| Cell | Access pattern | Stride | Reuse distance | What it stresses |
|---|---|---|---|---|
| A | Sequential sweep, stride 1 | word | (kabhi reuse nahi) | Pure spatial |
| B | Same address repeated | Pure temporal | ||
| C | Loop over array plus ek accumulator | mixed | mixed | Dono saath mein |
| D | Row-major matrix ka column walk | words (bahut bada) | large | Spatial broken |
| E | Reuse distance vs cache size boundary | near | Hit ↔ miss threshold | |
| F | Bada block, lekin random / large stride | large | large | Block-size backfire |
| G | Degenerate: single access, empty loop | undefined | undefined | Edge / limiting case |
| H | Real-world word problem (image blur) | 2-D | mixed | Applied synthesis |
| I | Exam twist: block size doubles | fixed | Trade-off reasoning |
Example 1 — Cell A: pure spatial locality
Forecast: Abhi guess karo — kya hit rate ke kareeb hai ya ke?
- Block ko words mein convert karo. Block bytes, word bytes, isliye words per block. Yeh step kyun? Locality ek block ki granularity par rehti hai: ek miss poora block drag karta hai, isliye words mein count karna batata hai kitne free hits saath milte hain.
- Total accesses count karo. words touch hue, har ek exactly ek baar (figure mein sweep dekho — har access par ek blue dot, right ki taraf badhta hua). Yeh step kyun? Humein hit rate ka denominator chahiye.
- Sweep formula apply karo. . Yeh step kyun? Har 16th access ek fresh block mein land karta hai (ek miss); baaki 15 pehle hi drag in ho chuke the.

- Hit rate. .
Verify: blocks words words — exactly array ke barabar, na kam na zyada. Hit rate per block parent ke se match karta hai. ✓
Example 2 — Cell B: pure temporal locality
Forecast: Kya sum misses cause karega, ya sirf ?
- Reuse distance. Consecutive
sumaccesses ke beech, zero distinct other addresses aate hain. Isliye . Yeh step kyun? strongest possible temporal locality hai — parent ka rule " hit" kisi bhi cache ke liye satisfy hota hai jahan ho. - Stride. (har baar same address). Yeh parent ka "stride ⇒ pure temporal" cell hai. Yeh step kyun? Confirm karta hai ki koi spatial component nahi hai — yeh 100% temporal hai.
- Miss count. Pehla access miss hoga (cold), baaki hit honge. Misses . Yeh step kyun? Ek block, ek baar load hone ke baad, kabhi evict nahi hota kyunki koi cheez uski slot ke liye compete nahi karti.
Verify: Hit rate . Jaise accesses , hit rate — perfect temporal locality ka limiting behaviour. ✓
Example 3 — Cell C: dono localities ek saath
Forecast: misses se zyada ya kam?
- Words per block. words. (Kyun: wahi granularity argument jaise Ex 1 mein.)
- Spatial part — array. Stride bytes word ⇒ sequential. Misses . Yeh step kyun? , aur hum up round karte hain — woh aakhri half-block bhi ek poora miss maangta hai.
- Temporal part — sum, i. Reuse distance (registers, koi eviction nahi) ⇒ misses. Yeh step kyun? Parent ka combo example: temporal hot scalars rakhta hai, spatial array stream karta hai.
- Overall array hit rate. .
Verify: ; parent ke stated se exactly match karta hai. ✓
Example 4 — Cell D: spatial locality broken (column walk)
Forecast: Kya yahan hit rate ke kareeb hai (jaise Ex 3) ya ke kareeb?
- Rows ke beech address gap.
B[k][j]&B + (k·1000 + j)·4bytes par rehta hai.k → k+1step karne par bytes ka jump hota hai. Yeh step kyun? Stride yahan poora diagnostic hai — figure dekho: consecutive accesses (red arrows) poori rows ke across uchhalte hain.

- Stride vs block. Stride bytes -byte block. Har access ek alag block mein land karta hai. Yeh step kyun? Agar jump ek block se zyada ho, to jo neighbours hum fetch karte hain unhe reuse nahi kiya jaayega.
- Fetched block ka utilisation. Drag in kiye gaye 16 ints mein se hum use karte hain: . Yeh step kyun? "Wasted bandwidth" quantify karta hai — parent ka terrible-spatial-locality case.
- Misses. Effectively har access par ek miss ⇒ column reads ke liye misses.
Verify: Utilisation , wasted — Ex 1 ki hit rate ka mirror image. Fix ke liye Loop blocking and tiling dekho. ✓
Example 5 — Cell E: reuse-distance / cache-size threshold
Forecast: ya mein se kaun hit karega?
- Rule yaad karo. Parent se: hit, miss (LRU cache). Yeh step kyun? Cell E ka poora point is inequality par exactly baiThna hai.
- Case . ⇒ hit. intervening blocks plus blocks, sab fit ho jaate hain. Yeh step kyun? least-recently-used hai lekin abhi tak push out nahi hua.
- Case . ⇒ miss. Aath other blocks aa gaye, saare 8 slots fill ho gaye aur evict ho gaya. Yeh step kyun? Razor edge dikhata hai: ek aur intervening block add karo aur hit miss mein flip ho jaata hai.

Verify: Boolean check: (hit), (miss). Threshold strict inequality hai. Working set model dekho. ✓
Example 6 — Cell F: bada block backfire karta hai
Forecast: Kaun sa block size poora working set hold karega?
- Blocks jo fit honge — 16 B block. slots. Working set blocks ⇒ sab fit, near-zero capacity misses. Yeh step kyun? Temporal locality tab hi fayda deta hai jab reused blocks resident rahen.
- Blocks jo fit honge — 256 B block. slots. Working set (500 items, har ek apne 256 B block mein kyunki access scattered hai) ⇒ constant eviction. Yeh step kyun? Kam, mote blocks matlab kam distinct items resident — parent ki warning ko numeric banaya.
- 256 B block ke saath wasted bytes. Har fetch 256 B drag karta hai lekin sirf 16 B chahiye: use hota hai. Yeh step kyun? Random access koi spatial payoff nahi deta lost temporal capacity ko offset karne ke liye.
Verify: (16 B fit hota hai), (256 B thrash karta hai). Bada worse hai yahan. Cache organization fundamentals dekho. ✓
Example 7 — Cell G: degenerate / limiting inputs
Forecast: Kya -iteration loop misses cause karega ya ?
- (a) Empty loop. accesses ⇒ misses. Hit rate undefined hai (), nahi. Yeh step kyun? Total accesses zero — aapko kehna hoga "undefined", silently print nahi karna.
- (b) Single access. : miss, hit rate . Reuse distance undefined (koi doosra access nahi). Yeh step kyun? Ek akela cold miss locality ka floor hai — abhi kuch exploit karne ko nahi.
- (c) Exact multiple, . — koi rounding penalty nahi kyunki already integer hai. Yeh step kyun? Dikhata hai ki ceiling tab hi kaati hai jab remainder ho; yahan clean hai.
Verify: ; ; . Teeno edge values confirm hue. ✓
Example 8 — Cell H: real-world word problem (image blur)
Forecast: Centre row ki horizontal sweep ke liye locality acchi hai ya buri?
- Row ke saath stride.
j → j+1move karne par byte step hota hai (chars contiguous hain). Stride B B block. Yeh step kyun? Row ke andar horizontal moves row-major storage mein acchi direction hain. - Ek row ke liye misses. bytes, bytes: misses. Yeh step kyun? exactly — row sweep karne par 30 cold block loads lagte hain.
- Neighbour rows. Upar aur neeche ki row bytes door hai — alag block, lekin har ek khud sequential sweep hoti hai, isliye unhe bhi misses lagenge, phir reuse hoga. Yeh step kyun? Stencil ki teeno rows individually acchi tarah stream hoti hain; vertical gap ek baar pay hota hai, har pixel ke liye nahi.
- Row hit rate. .
Verify: ; hit rate . Row-major + horizontal sweep = strong spatial locality. ✓
Example 9 — Cell I: exam twist (block double karo)
Forecast: Kya misses roughly half hokar ~ ho jaate hain?
- Nayi sweep misses. : misses. Yeh step kyun? Bade ke saath sweep formula directly apply karo — spatial locality improve hoti hai.
- Compare karo. misses, roughly half — is pure-sequential pattern ke liye acha hai. Yeh step kyun? Naive "bada block = kam misses" intuition confirm karta hai jab stride 1 ho.
- Hidden cost. Fixed cache size ke liye, block double karne par blocks ki count half ho jaati hai (Ex 6 ka lesson) aur miss penalty double ho jaata hai (per transfer do gune bytes). Yeh step kyun? Exam trap: win aur dono costs report karo, warna aadhe marks hi milenge.
Verify: vs pehle — nearly half, exactly jaise claim kiya. ✓
Recall Quick self-test
Stride-0 access pattern mein kaun si locality hoti hai? ::: Pure temporal (same address repeatedly, reuse distance 0). LRU cache mein blocks ke saath reuse distance — hit ya miss? ::: Miss (rule hai strict). Bada cache block kyun hit rate lower kar sakta hai? ::: Kam blocks fit hote hain ⇒ worse temporal capacity, plus wasted bytes jab stride bada ho. -iteration loop: kitne misses aur hit rate kya? ::: misses; hit rate undefined () hai, nahi.
Related deeper dives: Cache mapping strategies · Virtual memory · Working set model · Loop blocking and tiling.