4.1.9 · HinglishComputer Architecture (Deep)

Cache organization — direct-mapped, n-way set associative, fully associative

2,240 words10 min readRead in English

4.1.9 · Coding › Computer Architecture (Deep)


WHY does cache need "organization" at all?


WHAT are the address fields? (Derivation from scratch)

Hum split ko kabhi memorize nahi karte — hum use derive karte hain.

Step 1 — Offset. bytes ke ek block ke andar ek byte pick karne ke liye hume chahiye Kyun? distinct bytes ko index karne ke liye bits chahiye.

Step 2 — Set index. Block ko ek set pe map kiya jaata hai Kyun mod ? Hum address space ko groups mein kaatte hain; identical low bits → same set. Low block-number bits use karna consecutive blocks ko alag-alag sets mein spread karta hai (spatial locality ke liye acha).

Step 3 — Tag. Baaki bache hue high bits identify karte hain ki kaun sa block, jo is set pe map hota hai, actually stored hai:


The three organizations as ONE formula

Maano cache mein total blocks hain.

Organization (ways) (sets) Placement of block #
Direct-mapped exactly slot
n-way set assoc. set mein koi bhi slots
Fully associative koi bhi slot (no index bits!)
Figure — Cache organization — direct-mapped, n-way set associative, fully associative

HOW does a lookup work? (mechanism)

Kitne tag comparators chahiye (the associativity). Isliye fully-associative () hardware-expensive hai: comparators har access pe fire karte hain.


Worked examples


Three kinds of misses (the "3 C's")


Common mistakes (Steel-man + fix)


Active recall

Recall Associativity badhane se tag size kyun badhti hai (fixed cache size mein)?

Fixed . Zyada ways kam sets kam index bits kyunki , tag barta hai.

Recall Ek

-way cache ko kitne tag comparators chahiye? Exactly — set ki har line ke liye ek, sab parallel mein compare hote hain.

Recall Fully-associative kaun sa miss type eliminate karta hai, aur kaun sa nahi kar sakta?

Conflict misses eliminate karta hai; compulsory aur capacity abhi bhi hote hain.

Recall (Feynman, ek 12-saal ke bacche ko explain karo)

Ek coat-check room imagine karo. Direct-mapped: tumhara coat number batata hai ki use exactly kis ek hook pe lagana hai — fast hai, lekin agar do coats ka number same ho, toh ek ko hataana padega. Fully associative: koi bhi hook chalega, toh coats kabhi nahi hataaye jaate, lekin worker ko har hook dekhna padta hai tumhari coat dhundhne ke liye — slow. n-way: room choti shelves mein divided hai; tumhara number ek shelf choose karta hai, aur uspe hooks hain — kuch dost ek shelf share kar sakte hain bina ek doosre ko hataye, aur worker sirf hooks check karta hai. Tag wo naam hai jo coat pe likha hota hai taaki tum jaano wo sach mein tumhari hai.


Flashcards

Cache organization kya decide karta hai?
Kaun se slot(s) mein ek memory block cache mein reh sakta hai.
Address split, high se low bits?
Tag | Index | Offset.
Block offset bits ka formula diya block size B?
.
Index bits ka formula diya sets ki sankhya S?
.
Tag bits ka formula?
(address width minus index minus offset).
Direct-mapped (S,E) ke terms mein?
, (har set mein ek block).
Fully associative (S,E) ke terms mein?
, , toh index bits .
n-way set associative: set kaise choose hota hai?
, phir usme lines mein se koi ek.
E-way ke liye kitne tag comparators chahiye?
Exactly , sab parallel mein.
Fixed cache size mein, associativity badhne pe tag bits ka kya hota hai?
Tag bits increase hote hain (sets/index decrease hote hain).
Kaun si organization ko replacement policy ki zaroorat nahi aur kyun?
Direct-mapped — ek block ka sirf ek hi legal slot hota hai, choose karne ko kuch nahi.
Cache misses ke 3 C's?
Compulsory (cold), Capacity, Conflict.
Full associativity kaun sa miss type eliminate karta hai?
Conflict misses.
Fully associative hardware mein kyun expensive hai?
Har access pe comparators (har line ke liye ek) active hote hain, aur tags bhi sabse bade hote hain.
32-bit, 16KiB, 64B-block, 4-way cache field split?
Tag 20 | Index 6 | Offset 6.
Hit ke liye dono kya match karna chahiye?
Valid bit = 1 AUR stored tag = address tag.

Connections

Concept Map

is a

determines

contains

contains

contains

picks byte in block

selects set via mod S

identifies block in set

special case

special case

special case

max index min tag

no index bits

causes many

avoids

balances

Cache organization choice

Search cost vs flexibility

Address split t s b

Offset b = log2 B

Index s = log2 S

Tag t = m - s - b

Direct-mapped E=1

n-way set assoc E=n

Fully associative S=1

Conflict misses

Hit search hardware