Sub topic 8
Fully associative cache
This chapter assumes you can split an address into tag, index and offset, and that you know how a direct-mapped cache places a block in exactly one line. It also assumes you can work with powers of two in binary.
The idea: any block can go in any line
A direct-mapped cache gives each memory block one possible line. Two hot blocks that share that line evict each other on every access, even while the rest of the cache sits empty. A fully associative cache removes the restriction. A memory block may be placed in any line of the cache.
This has three consequences:
- There is no index field. The address splits into only a tag and a block offset.
- The tag is the entire block number, so it is longer than in any other organization.
- A lookup cannot go straight to one line. The hardware compares the incoming tag with the tag of every valid line, all at the same time.
A fully associative cache is the limiting case of a set-associative cache with exactly one set. The number of ways equals the number of lines.
Address breakdown
If blocks hold B bytes, the offset has log2(B) bits. All remaining bits are the tag.
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
# Fully associative: one set, so index_bits = 0
print(split_address(0x1234, 16, 1)) # (291, 0, 4), i.e. tag=0x123, index=0, offset=4With one set, index_bits is 0 and the index is always 0. The tag is 0x123, which is the whole block number. In the 16-set example from the direct-mapped chapter, the tag was only 0x12.
How a lookup works
The following diagram shows one access.
A match requires both an equal tag and a set valid bit. Without the valid check, a line that has never been filled could match by accident, because tags start out as arbitrary bits.
On a miss there is a free choice of line. If any line is invalid, the block normally goes there. Only when the cache is full does the replacement policy select a victim. In a direct-mapped cache no such choice exists.
Cost: comparators and tag storage
The flexibility is paid for in hardware. A cache with N lines needs N tag comparators working in parallel, each as wide as the tag. A wide comparison across all lines also takes more time and power. This is why fully associative caches are used only when N is small, for example a TLB with a few dozen entries or a small victim cache. Large data caches use set-associative designs instead.
Tag storage grows too. Take a 32-bit physical address, a 64 KB cache and 64-byte blocks.
- Number of lines = 64 KB / 64 B = 1024.
- Offset bits = log2(64) = 6.
- Tag bits = 32 − 6 = 26.
- Tag storage = 1024 × 26 = 26624 bits, which is 3.25 KB.
With a valid bit, each line holds 512 data bits + 26 tag bits + 1 valid bit = 539 bits. A write-back cache adds a dirty bit, giving 540 bits per line.
The table compares the three organizations for this same cache.
| Organization | Index bits | Tag bits | Tag comparators | Conflict misses |
|---|---|---|---|---|
| Direct-mapped | 10 | 16 | 1 | Most |
| 4-way set-associative (256 sets) | 8 | 18 | 4 | Fewer |
| Fully associative | 0 | 26 | 1024 | None |
Each step towards full associativity lengthens the tag by the number of index bits removed.
Replacement is now a real decision
In a direct-mapped cache the victim is forced. In a fully associative cache, every line is a candidate. The three common policies are:
- LRU evicts the line unused for the longest time. It exploits temporal locality, but exact LRU needs history for every line. A counter of log2(N) bits per line is one way to track it.
- FIFO evicts the line that has been resident longest, ignoring whether it was used recently. It needs only a queue or a pointer.
- Random evicts any line. It is cheap, and its average behaviour is often close to LRU.
The simulator below counts hits and misses for LRU and FIFO.
from collections import OrderedDict
def simulate_fa(addresses, block_size, lines, policy="LRU"):
block_bits = block_size.bit_length() - 1
cache = OrderedDict() # tag -> True, oldest entry first
hits = 0
for a in addresses:
tag = a >> block_bits # the whole block number is the tag
if tag in cache:
hits += 1
if policy == "LRU":
cache.move_to_end(tag) # refresh recency; FIFO leaves order alone
else:
if len(cache) == lines:
cache.popitem(last=False) # evict the oldest or least recent
cache[tag] = True
return hits, len(addresses) - hits
trace = [1, 2, 3, 1, 4, 1, 2]
print(simulate_fa(trace, 1, 3, "LRU")) # (2, 5)
print(simulate_fa(trace, 1, 3, "FIFO")) # (1, 6)Here is the LRU trace by hand. The cache lists blocks from least to most recently used.
| Access | Result | Cache after |
|---|---|---|
| 1 | miss | 1 |
| 2 | miss | 1, 2 |
| 3 | miss | 1, 2, 3 |
| 1 | hit | 2, 3, 1 |
| 4 | miss, evict 2 | 3, 1, 4 |
| 1 | hit | 3, 4, 1 |
| 2 | miss, evict 3 | 4, 1, 2 |
Under FIFO, the second access to block 1 is a hit, but it does not move block 1 to the back of the queue. So block 4 evicts block 1, and the next access to 1 misses. The result is 1 hit instead of 2.
Conflict misses disappear
The earlier set-associative example used the trace 0, 4, 0, 4. In a 4-set direct-mapped cache with one-byte blocks, addresses 0 and 4 both map to set 0 and evict each other four times. In a fully associative cache of the same four lines, both blocks stay resident.
from collections import OrderedDict
def simulate_fa(addresses, block_size, lines, policy="LRU"):
block_bits = block_size.bit_length() - 1
cache = OrderedDict()
hits = 0
for a in addresses:
tag = a >> block_bits
if tag in cache:
hits += 1
if policy == "LRU":
cache.move_to_end(tag)
else:
if len(cache) == lines:
cache.popitem(last=False)
cache[tag] = True
return hits, len(addresses) - hits
print(simulate_fa([0, 4, 0, 4], 1, 4)) # (2, 2): two compulsory misses, then two hitsThe two remaining misses are compulsory misses, the first touch of each block. A fully associative cache has no conflict misses by construction. Every miss is either compulsory or a capacity miss, which means the working set did not fit in N lines.
Common errors
- Reserving index bits. The index field has 0 bits. If your answer shows index bits, you have computed a set-associative cache.
- Forgetting the offset. Tag bits = address bits − log2(block size), not address bits alone.
- Dropping the valid bit from total storage when the question asks for the full bit count.
- Treating an unfilled line as a miss victim. An invalid line is used first. No replacement policy runs until the cache is full.
- Confusing lines with sets. Number of lines = cache size / block size. A fully associative cache has one set containing all lines.
Exercises
Exercise
A fully associative cache has a 16 KB capacity and 32-byte blocks. Physical addresses are 32 bits. Find the number of lines, the tag width, and the total tag storage in bits. Ignore valid and dirty bits.
Show answer
Lines = 16 KB / 32 B = 16384 / 32 = 512. Offset bits = log2(32) = 5. Tag bits = 32 − 5 = 27. Tag storage = 512 × 27 = 13824 bits, which is 1.6875 KB. The cache also needs 512 comparators, each 27 bits wide.
Exercise
A fully associative cache with 3 lines and one-byte blocks starts empty. The block reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. Count the hits under FIFO and under LRU.
Show answer
FIFO: 1, 2, 3 miss. 4 evicts 1. 1 evicts 2. 2 evicts 3 (cache 4, 1, 2). 5 evicts 4 (cache 1, 2, 5). 1 hit. 2 hit. 3 evicts 1 (cache 2, 5, 3). 4 evicts 2 (cache 5, 3, 4). 5 hit. That gives 3 hits and 9 misses. LRU: 1, 2, 3, 4, 1, 2 all miss, and 5 misses too (cache 1, 2, 5, in LRU order). 1 and 2 hit. 3 misses, evicting 5 (cache 1, 2, 3). 4 misses, evicting 1 (cache 2, 3, 4). 5 misses, evicting 2. That gives 2 hits and 10 misses. FIFO does better on this trace.