Sub topic 9
k-way set-associative cache
This chapter assumes you can split an address into tag, index and offset. It also assumes you know how a direct-mapped cache and a fully associative cache work. You should be comfortable with powers of two and log2.
The idea: a compromise between two extremes
A direct-mapped cache gives each memory block exactly one possible line. It is fast and cheap, but two blocks that share a line keep evicting each other, even when the rest of the cache is empty. A fully associative cache lets any block use any line. It removes that problem, but it must compare the tag against every line at once.
A k-way set-associative cache sits between the two. The cache lines are grouped into sets of k lines each. A memory block maps to exactly one set, and it can occupy any of the k lines (called ways) inside that set.
- The set is chosen like in a direct-mapped cache, by the index bits.
- The line inside the set is chosen like in a fully associative cache, by comparing tags.
The two extremes are special cases:
| Associativity k | Sets | Name |
|---|---|---|
| 1 | number of lines | Direct-mapped |
| between 1 and number of lines | lines / k | k-way set-associative |
| number of lines | 1 | Fully associative |
The mapping rule
Let the cache have L lines and associativity k. Then:
- Number of sets:
S = L / k - Set index of a block:
block number mod S - Block number:
byte address / block size
Memory block B may be placed in any of the k ways of set B mod S, and nowhere else.
For 8 lines and a 32-bit address with 16-byte blocks (4 offset bits), changing k changes the split like this:
| k | Sets | Index bits | Tag bits |
|---|---|---|---|
| 1 | 8 | 3 | 25 |
| 2 | 4 | 2 | 26 |
| 4 | 2 | 1 | 27 |
| 8 | 1 | 0 | 28 |
With capacity fixed, doubling k halves the number of sets. The index loses one bit and the tag gains one bit. Total tag storage therefore grows slightly as associativity grows.
How a lookup works
- Take the index bits of the address and read all k ways of that set at once.
- Compare the address tag with each way's stored tag, using k comparators in parallel.
- A way hits if its valid bit is 1 and its tag matches. The cache hits if any way hits.
- On a hit, a multiplexer selects the matching way's block. The offset bits then pick the byte or word.
- On a miss, the block is fetched. If the set has an empty (invalid) way, it goes there. Otherwise the replacement policy picks a victim.
Worked example: sizing a cache
A 32 KB, 4-way set-associative cache has 64-byte blocks and a 32-bit byte address.
- Lines:
32768 / 64 = 512. - Sets:
512 / 4 = 128. - Offset bits:
log2(64) = 6. - Index bits:
log2(128) = 7. - Tag bits:
32 - 7 - 6 = 19. - Tag storage:
512 × 19 = 9728bits. Adding one valid bit per line gives512 × 20 = 10240bits. A write-back cache also needs a dirty bit per line. - Hardware: four 19-bit comparators and a 4-to-1 multiplexer for the data.
Simulating it in code
The helper below splits an address. The simulator keeps one ordered dictionary per set. Entries are ordered from least to most recently used, so the first entry is the LRU victim.
from collections import OrderedDict
def split_address(addr, block_size, num_sets):
# block_size and num_sets must be powers of two
offset_bits = block_size.bit_length() - 1
index_bits = num_sets.bit_length() - 1
offset = addr & (block_size - 1)
index = (addr >> offset_bits) & (num_sets - 1)
tag = addr >> (offset_bits + index_bits)
return tag, index, offset
def simulate(addresses, block_size, num_sets, ways):
sets = [OrderedDict() for _ in range(num_sets)]
hits = 0
for a in addresses:
tag, idx, _ = split_address(a, block_size, num_sets)
s = sets[idx]
if tag in s:
hits += 1
s.move_to_end(tag) # mark as most recently used
else:
if len(s) == ways:
s.popitem(last=False) # evict least recently used
s[tag] = True
return hits, len(addresses) - hits
print(split_address(0x1234, 16, 16)) # (18, 3, 4)
trace = [0, 4, 0, 4]
print(simulate(trace, 1, 4, 1)) # direct-mapped, 4 lines: (0, 4)
print(simulate(trace, 1, 2, 2)) # 2-way, 2 sets: (2, 2)Both caches hold four blocks in the first trace. Addresses 0 and 4 both map to set 0 in the direct-mapped cache, because 4 mod 4 = 0. They evict each other on every access. In the 2-way cache they share a set and both stay resident, so the two repeat accesses hit.
Associativity does not fix everything
More ways help only when a set's demand is at most k blocks. Consider this trace on a 2-way cache with 2 sets and 1-byte blocks:
print(simulate([0, 2, 4, 0, 2, 4], 1, 2, 2)) # (0, 6)Addresses 0, 2 and 4 are all even, so they all map to set 0. Three blocks compete for two ways. Under LRU, each access evicts the block that is needed soonest, so every access misses. The cyclic pattern is the worst case for LRU. Growing the cache to 3 ways in that set would remove the misses.
Replacement inside a set
Replacement only matters when the set is full. The common policies are:
- LRU evicts the way that was used longest ago. A set needs enough state to track the order. A 2-way set needs 1 bit. Larger k usually uses an approximation (pseudo-LRU).
- FIFO evicts the block that has been in the set longest, regardless of hits since. It needs only a pointer per set.
- Random picks any way. It needs almost no state and performs close to LRU in many workloads.
LRU is not always better than FIFO. Take a 2-way cache where blocks 0, 8 and 16 share a set, and the references are 0, 8, 0, 16, 8, 0.
- LRU gives 1 hit and 5 misses. On the miss for 16 it evicts 8, because the hit on 0 made 0 more recent. Then 8 misses, and so does 0 after it.
- FIFO gives 2 hits and 4 misses. The hit on 0 does not refresh it. On the miss for 16 it evicts 0, and the later reference to 8 hits.
Trade-offs of raising k
| Effect of larger k | Direction |
|---|---|
| Conflict misses | Fewer |
| Comparators and multiplexer width | More |
| Hit time | Slightly longer |
| Replacement state | More |
| Tag bits per line | One more bit each time k doubles |
Gains shrink quickly. Going from direct-mapped to 2-way removes many conflict misses. Going from 4-way to 8-way removes few. Real first-level caches commonly use 4-way to 8-way.
Common errors
- Using lines as sets. Sets are
lines / k. The index haslog2(sets)bits, notlog2(lines). - Reading "4-way" as "4 sets". The 4 is the number of lines per set.
- Taking the modulo of the byte address. The set is
block number mod sets. Divide by the block size first, or discard the offset bits. - Forgetting that tag bits grow. If a question changes k and keeps capacity, recompute the index and tag bits.
- Counting only tag bits as overhead. Valid bits, dirty bits and replacement state are also overhead if the question asks for total storage.
- Mistaking free ways for a full set. A miss in a set with an empty way needs no eviction. Only a full set triggers replacement.
Exercise
A 64 KB, 8-way set-associative cache has 32-byte blocks and a 40-bit physical address. Find the number of sets, the offset, index and tag widths, and the total number of tag bits stored.
Show answer
Lines = 65536 / 32 = 2048. Sets = 2048 / 8 = 256. Offset = log2(32) = 5 bits. Index = log2(256) = 8 bits. Tag = 40 - 8 - 5 = 27 bits. Total tag storage = 2048 × 27 = 55296 bits.
Exercise
A 2-way set-associative cache has 4 sets and one-word blocks. The word addresses 0, 8, 0, 16, 8, 0 are referenced in order, and the cache starts empty. Count the hits under LRU and under FIFO.
Show answer
Every address is a multiple of 4, so all map to set 0 (block mod 4 = 0). Under LRU there is 1 hit (the second reference to 0) and 5 misses. Under FIFO there are 2 hits (the second reference to 0 and the second reference to 8) and 4 misses.
Exercise
A cache has 1024 lines. Going from direct-mapped to 4-way associative with the same capacity and block size, how do the index bits and tag bits change?
Show answer
Direct-mapped has 1024 sets, so 10 index bits. 4-way has 1024 / 4 = 256 sets, so 8 index bits. The index shrinks by 2 bits and the tag grows by 2 bits. The offset is unchanged.