Set-Associative Caches: Sets, Ways and Their Cost
Two blocks that fight over one line can both stay, if each index gets more than one line. The cache pays for that with a comparator for each line of the set.
You Will Be Able To
- Describe a cache as sets and ways, and find the set and the tag of a block.
- Explain why every way of a set is compared at the same time.
- Count what one more way costs in comparators, tag bits and logic.
- Lesson
- S3.4
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
A loop copies one array into another. Both arrays are small, and the cache is mostly empty. Still every single read is a miss. The two arrays happen to start at addresses with the same index.
Before You Start
You need S3.3: tag, index and offset, and the hit path of a direct-mapped cache. You need S0.7: two blocks that fight over one line.
You also need division with a remainder: 16 divided by 4 is 4, remainder 0.
| Question | |
|---|---|
| 1 | What are a set and a way? |
| 2 | How does a block find its set, and where in the set may it sit? |
| 3 | Why are all ways of a set compared at the same time? |
| 4 | What does each extra way cost? |
| 5 | What are the two ends of the scale: one way in a set, and one set for all lines? |
The Idea: More Than One Line for an Index
A direct-mapped cache gives every block exactly one line. That makes the hit path short, and it makes one kind of miss possible: two blocks with the same index push each other out, while other lines stand empty. The loop from the top of the page hits this on every read.
The cure is simple to say. Give each index several lines. The lines that share an index are a set, and each line of a set is a way. A block must still go to the set of its index, but inside the set it may use any way. Two blocks with the same index now fit side by side. Such a cache is called set-associative.
The cache no longer knows where a block is. It could be in any way of the set. Looking at the ways one after another would take too long. So the cache reads all of them and compares all their tags at the same time. Any match is a hit.
That is the price. Every way needs its own comparator. The answer must also be steered out of the way that matched, through one more multiplexer. The tags grow too. When the lines stay the same and the ways double, the sets halve. So the index loses a bit, and every tag gains one.
There is a scale here with two ends. At one end each set has one way: that is the direct-mapped cache. At the other end there is one set, and all lines are its ways. Every tag in the cache is then compared on every read.
New Words
| Word | Meaning |
|---|---|
| Set of a cache | The group of cache lines that share one index. The index of an address picks one set. |
| Way of a cache | One line of a set. A cache with two lines in each set has two ways. |
| Set-associative cache | A cache in which a block must use the set of its index, and may use any way of that set. |
| Two-way cache | A set-associative cache with two ways in each set. A four-way cache has four. |
| Fully associative cache | A cache with one set. A block may use any line, and every tag is compared on every read. |
| Associativity | The number of ways in a set. |
| Way select | The multiplexer that passes on the block of the way that matched. |
| Remainder | What is left over after a division. 22 divided by 4 is 5, remainder 2. |
One Picture: A Set of Two Ways
- Processor
- Memory
- Control / FSM
- Bus
- One line for an index
In a direct-mapped cache the index picks one line. Two blocks with the same index cannot both stay.
- Two lines for an index
Now give each index two lines. Together they are a set, and each of them is a way. A block may sit in either way of its set.
- Every way is compared
The cache does not know which way holds the block. So it reads both ways and compares both tags, at the same time.
- A hit in any way
It is a hit when any way is valid and has the right tag. At most one way can match.
- The way that matched gives the word
The matches steer one more multiplexer. It passes on the block of the way that matched. This step is new, and it takes time.
Read the diagram as text
- Address: tag, index, offset
- One set: the lines that share one index
- Way 0: a valid bit, a tag, a block
- Way 1: a valid bit, a tag, a block
- Compare 0: tag of way 0 against the address
- Compare 1: tag of way 1 against the address
- Hit: any way matched
- Way select: the block of the way that matched
- Address → One set: the index
- One set → Way 0: read out
- One set → Way 1: read out
- Way 0 → Compare 0: tag, valid
- Way 1 → Compare 1: tag, valid
- Compare 0 → Hit: match
- Compare 1 → Hit: match
- Compare 1 → Way select: which way
One set of a cache with two ways. With four ways there are four compares, and the way select has four inputs.
The Smallest Example, Worked by Hand
Take eight lines, and arrange them as 4 sets of 2 ways. To keep the sums short, a read names a whole block by its number. The set of a block is the remainder of its number divided by 4. The tag is the result of that division.
The trace is the loop from the top of the page, with a third block joining in: 0, 8, 0, 8, 16, 0, 8, 16. All three blocks have remainder 0, so all use set 0. When the set is full, this page gives up the block that was used longest ago.
| Read | Set | Tag | Way 0 before | Way 1 before | Result |
|---|---|---|---|---|---|
| block 0 | 0 | 0 | empty | empty | miss, into way 0 |
| block 8 | 0 | 2 | block 0 | empty | miss, into way 1 |
| block 0 | 0 | 0 | block 0 | block 8 | hit in way 0 |
| block 8 | 0 | 2 | block 0 | block 8 | hit in way 1 |
| block 16 | 0 | 4 | block 0 | block 8 | miss: block 0 goes |
| block 0 | 0 | 0 | block 16 | block 8 | miss: block 8 goes |
| block 8 | 0 | 2 | block 16 | block 0 | miss: block 16 goes |
| block 16 | 0 | 4 | block 8 | block 0 | miss: block 0 goes |
Two blocks fit into the two ways, and both hit. The third block is one too many.
Two hits. A direct-mapped cache of eight lines would have none: blocks 0, 8 and 16 all have remainder 0 when divided by 8. With 2 sets of 4 ways, all three blocks fit into one set. Only the first read of each is a miss: five hits.
Now the cost. Take addresses of 16 bits and eight lines with blocks of 16 bytes. The offset has 4 bits in every arrangement. The rest is shared between the index and the tag.
| Arrangement | Index bits | Tag bits | Comparators | Tag bits stored in all |
|---|---|---|---|---|
| 8 sets of 1 way | 3 | 9 | 1 | 72 |
| 4 sets of 2 ways | 2 | 10 | 2 | 80 |
| 2 sets of 4 ways | 1 | 11 | 4 | 88 |
| 1 set of 8 ways | 0 | 12 | 8 | 96 |
Each time the ways double, the index loses one bit, the tag gains one, and the comparators double.
The Rules
Rule 1: The index picks a set, not a line.
The number of index bits follows from the number of sets.
Rule 2: A block may sit in any way of its set.
So a set of two ways holds two blocks with the same index at once.
Rule 3: Every way of the set is compared at the same time.
Each way needs its own comparator. A hit is a match in any valid way.
Rule 4: The way that matched gives the word.
A way select stands behind the comparators, so the hit path gets longer.
Rule 5: With the same lines, twice the ways means half the sets.
The index loses one bit and every tag gains one.
Rule 6: One way in a set is direct-mapped. One set for all lines is fully associative.
Every cache lies on the scale between these two.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.4. See "Reducing Cache Misses by More Flexible Placement of Blocks" and "Locating a Block in the Cache": the three arrangements, and the comparators that each needs.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.2: sets and ways, the fully associative cache, and a table of the arrangements.
The eight lines, the trace of block numbers and the addresses of 16 bits are our own teaching example.
When a set is full, one block must go. This page gives up the block that was used longest ago. That choice has a page of its own, the next one of this module.
Now Run It
The lab at the end of this page is a counting model in test code. It holds block numbers and no data. It runs the trace through 4 sets of 2 ways with a row for each read, and then through three other arrangements.
read block 0: set 0 tag 0 | miss, into way 0
read block 8: set 0 tag 2 | miss, into way 1
read block 0: set 0 tag 0 | hit in way 0, which holds block 0
read block 8: set 0 tag 2 | hit in way 1, which holds block 8
read block 16: set 0 tag 4 | miss, into way 0, and block 0 goes
read block 0: set 0 tag 0 | miss, into way 1, and block 8 goes
read block 8: set 0 tag 2 | miss, into way 0, and block 16 goes
read block 16: set 0 tag 4 | miss, into way 1, and block 0 goes
2 ways: reads=8 hits=2
1 way : reads=8 hits=0
4 ways: reads=8 hits=5
8 ways: reads=8 hits=5Rules 1 and 2 at work: the first four rows. Blocks 0 and 8 both have set 0. They go into way 0 and way 1, and both hit on their second read.
Rule 2 has a limit, and the fifth row shows it. Block 16 also wants set 0, and both ways are taken. Block 0 goes. From there on the three blocks chase each other.
Rule 6 at work: the last four rows. One way gives no hit at all. Two ways give 2 hits. Four ways give 5, and eight ways give 5 as well: three blocks never needed more than three ways.
What do the ways cost in logic? We built the compare and the way select for each arrangement and ran them through a synthesis tool. The stored tags and blocks are left out of this count.
| Arrangement | Comparators | Cells | Gates in the longest path |
|---|---|---|---|
| 8 sets of 1 way | 1 | 58 | 7 |
| 4 sets of 2 ways | 2 | 165 | 8 |
| 2 sets of 4 ways | 4 | 387 | 9 |
| 1 set of 8 ways | 8 | 819 | 10 |
Measured with Yosys on generic gates, for words of 32 bits. The numbers are for this small example and this tool.
Rules 3 and 4 at work: each doubling of the ways roughly doubles the cells, and adds a gate to the longest path. Going from 4 ways to 8 bought no hit on this trace, and cost twice the logic.
This lab leaves out the data and the addresses. It also leaves out how the block that goes is chosen in hardware.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Several ways for an index | One line for an index | Blocks with the same index stay side by side. One comparator for each way, and a longer hit path. |
| All ways compared at once | The ways checked one after another | The answer comes in one cycle. Every comparator switches on every read, which costs power. |
| Two or four ways | One set for all lines | Most of the gain for a small cost. A few blocks can still fight over a set. |
| A block bound to the set of its index | A block in any set | Only a few tags are compared. The index must still be right. |
The Trap: The Tag That Was One Bit Too Short
A team turns its direct-mapped cache of eight lines into 4 sets of 2 ways. They halve the sets and take one bit off the index. They forget that the tag must grow by that bit. The tag is still worked out as for eight sets. Here is that cache in the lab:
read block 0: set 0 tag 0 | miss, into way 0
read block 4: set 0 tag 0 | hit in way 0, which holds block 0
read block 1: set 1 tag 0 | miss, into way 0
read block 5: set 1 tag 0 | hit in way 0, which holds block 1The second row is the failure. Block 4 has set 0 and, with the short tag, tag 0. Block 0 has the same set and the same tag. So the read of block 4 is a hit, and it gets the copy of block 0. The fourth row does the same to block 5.
Nothing flags this. The cache returns valid data from the wrong address. Rule 5 is the check: when the index loses a bit, find the place where the tag gains it.
At Scale
This section looks ahead. Real caches choose their place on this scale level by level.
Small and fast first
A level-1 cache has few ways, because its hit path must fit into the clock cycle. Larger and slower levels can afford more.
Fully associative, but tiny
One set for all lines is used where there are only a few dozen lines, and a miss is very costly.
Which block goes
A full set must give up a block. The next lesson is about that choice.
Shared levels
Large chips stack several caches: The Cache Hierarchy of a Core Cluster.
Try It
Run the lab. Check the first eight rows against the hand table.
Change the first start(2) to start(4). Predict every row before you run it.
Add read(4) and read(12) to the trace. Which arrangement gains hits, and why?
read block 0: set 0 tag 0 | miss, into way 0 read block 8: set 0 tag 2 | miss, into way 1 read block 0: set 0 tag 0 | hit in way 0, which holds block 0 read block 8: set 0 tag 2 | hit in way 1, which holds block 8 read block 16: set 0 tag 4 | miss, into way 0, and block 0 goes read block 0: set 0 tag 0 | miss, into way 1, and block 8 goes read block 8: set 0 tag 2 | miss, into way 0, and block 16 goes read block 16: set 0 tag 4 | miss, into way 1, and block 0 goes 2 ways: reads=8 hits=2 1 way : reads=8 hits=0 4 ways: reads=8 hits=5 8 ways: reads=8 hits=5Check Yourself
Common Mistakes
Wrong Keep the tag width when the ways change.
Fewer sets mean fewer index bits, and those bits must go into the tag.
Fix Work out index and tag again for the new number of sets.
Wrong Check the ways of a set one after another.
A read would take a cycle for each way.
Fix Compare all ways at once, with one comparator for each.
Wrong Expect twice the ways to give twice the hits.
More ways help only the blocks that fight over a set.
Fix Measure on the trace. Past a few ways the gain is small.
Remember
- The lines that share an index are a set. Each line of a set is a way.
- A block must use its set and may use any way of it. All ways are compared at once.
- Each way costs a comparator and makes the hit path longer.
- Twice the ways with the same lines: half the sets, one index bit less, one tag bit more.