Sub topic 9
K-way Set-Associative Cache
Prerequisites and working model
This chapter assumes that you know binary and hexadecimal numbers, powers of two, byte-addressable memory, and the basic ideas of cache blocks, hits, misses, tags, indexes, and block offsets.
A k-way set-associative cache combines direct mapping with flexible placement. The cache is divided into sets. Each set contains exactly \(k\) cache lines, called ways. A memory block maps to one set, but it may occupy any of that set's \(k\) ways.
If a cache has \(L\) total lines and is \(k\)-way set-associative:
\[ \text{number of sets} = \frac{L}{k} \]
The address still divides into three fields:
| Field | Purpose |
|---|---|
| Block offset | Selects a byte within a block |
| Set index | Selects one cache set |
| Tag | Identifies the memory block within that set |
For a byte address, block size \(B\), and \(S\) sets:
\[ \text{offset bits}=\log_2 B \]
\[ \text{index bits}=\log_2 S \]
\[ \text{tag bits}=\text{address bits}-\text{index bits}-\text{offset bits} \]
Mapping a block to a set
The memory block number is:
\[ \text{block number}=\left\lfloor\frac{\text{address}}{B}\right\rfloor \]
The set number is:
\[ \text{set number}=\text{block number}\bmod S \]
Within that set, the block can go into any available way. On a hit, the cache compares the requested tag with the tags stored in all \(k\) ways of the selected set.
For example, consider a cache with 16-byte blocks, 4 sets, and 2 ways. It has:
\[ 4 \times 2=8\text{ lines} \]
An address of 0x54 belongs to block:
\[ 0x54 / 16 = 5 \]
The block maps to:
\[ 5\bmod 4=1 \]
Therefore, the cache checks set 1. The block may be stored in either way 0 or way 1 of that set.
Why associativity helps
In a direct-mapped cache, each set has one way. Two frequently used blocks that map to the same set continually replace one another. This is a conflict miss.
With two or more ways, those blocks can occupy different lines in the same set. Associativity therefore reduces conflict misses. It does not remove all misses. If more than \(k\) active blocks map to one set, some blocks must still be evicted.
A fully associative cache is the extreme case in which there is one set and every line is a way. It has no index bits, but it needs many tag comparisons. A direct-mapped cache is the one-way case. Higher associativity usually reduces conflict misses but increases comparison hardware, lookup complexity, and sometimes hit time.
Worked simulation
The following program models least-recently-used replacement. It compares a direct-mapped cache with a two-way cache.
from collections import OrderedDict
def split_address(address, block_size, num_sets):
offset_bits = block_size.bit_length() - 1
index_bits = num_sets.bit_length() - 1
offset = address & (block_size - 1)
index = (address >> offset_bits) & (num_sets - 1)
tag = address >> (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 address in addresses:
tag, index, _ = split_address(address, block_size, num_sets)
current_set = sets[index]
if tag in current_set:
hits += 1
current_set.move_to_end(tag)
else:
if len(current_set) == ways:
current_set.popitem(last=False)
current_set[tag] = True
return hits, len(addresses) - hits
trace = [0, 4, 0, 4]
print(simulate(trace, 1, 4, 1)) # (0, 4)
print(simulate(trace, 1, 2, 2)) # (2, 2)With four direct-mapped sets, addresses 0 and 4 map to the same set and replace each other. With two sets and two ways, both blocks fit in that set, so the repeated references hit.
Exercise
A cache has 64 total lines and is 4-way set-associative. How many sets does it have, and how many ways are checked for a lookup?
Show answer
It has 64 / 4 = 16 sets. The set index selects one set, and the cache compares the requested tag against 4 ways.