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.

Five questions this page answers
Question
1What are a set and a way?
2How does a block find its set, and where in the set may it sit?
3Why are all ways of a set compared at the same time?
4What does each extra way cost?
5What 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

New words on this page
WordMeaning
Set of a cacheThe group of cache lines that share one index. The index of an address picks one set.
Way of a cacheOne line of a set. A cache with two lines in each set has two ways.
Set-associative cacheA cache in which a block must use the set of its index, and may use any way of that set.
Two-way cacheA set-associative cache with two ways in each set. A four-way cache has four.
Fully associative cacheA cache with one set. A block may use any line, and every tag is compared on every read.
AssociativityThe number of ways in a set.
Way selectThe multiplexer that passes on the block of the way that matched.
RemainderWhat is left over after a division. 22 divided by 4 is 5, remainder 2.

One Picture: A Set of Two Ways

One Set, Several Ways, Compared at OnceStep through the picture. It starts with the fight over one line and ends with a set of two ways.
  • Processor
  • Memory
  • Control / FSM
  • Bus
One Set, Several Ways, Compared at OnceStep through the picture. It starts with the fight over one line and ends with a set of two ways. component Address tag, index, offset component One set the lines that shareone index component Way 0 a valid bit, a tag, ablock component Way 1 a valid bit, a tag, ablock component Compare 0 tag of way 0 againstthe address component Compare 1 tag of way 1 againstthe address component Hit any way matched component Way select the block of the waythat matched the indexread outread outtag, validtag, validmatchmatchwhich way
  1. component Addresstag, index, offset
    • the index → One set
  2. component One setthe lines that share one index
    • read out → Way 0
    • read out → Way 1
  3. component Way 0a valid bit, a tag, a block
    • tag, valid → Compare 0
  4. component Way 1a valid bit, a tag, a block
    • tag, valid → Compare 1
  5. component Compare 0tag of way 0 against the address
    • match → Hit
  6. component Compare 1tag of way 1 against the address
    • match → Hit
    • which way → Way select
  7. component Hitany way matched
  8. component Way selectthe block of the way that matched
  1. One line for an index

    In a direct-mapped cache the index picks one line. Two blocks with the same index cannot both stay.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

Eight reads through 4 sets of 2 ways
ReadSetTagWay 0 beforeWay 1 beforeResult
block 000emptyemptymiss, into way 0
block 802block 0emptymiss, into way 1
block 000block 0block 8hit in way 0
block 802block 0block 8hit in way 1
block 1604block 0block 8miss: block 0 goes
block 000block 16block 8miss: block 8 goes
block 802block 16block 0miss: block 16 goes
block 1604block 8block 0miss: 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.

The same eight lines in four arrangements
ArrangementIndex bitsTag bitsComparatorsTag bits stored in all
8 sets of 1 way39172
4 sets of 2 ways210280
2 sets of 4 ways111488
1 set of 8 ways012896

Each time the ways double, the index loses one bit, the tag gains one, and the comparators double.

The Rules

  1. Rule 1: The index picks a set, not a line.

    The number of index bits follows from the number of sets.

  2. 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.

  3. 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.

  4. Rule 4: The way that matched gives the word.

    A way select stands behind the comparators, so the hit path gets longer.

  5. Rule 5: With the same lines, twice the ways means half the sets.

    The index loses one bit and every tag gains one.

  6. 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.

Printed by the lab on Icarus. The last four rows are the same trace in four arrangements of the same eight lines.
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=5

Rules 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.

The compare-and-select logic, synthesised
ArrangementComparatorsCellsGates in the longest path
8 sets of 1 way1587
4 sets of 2 ways21658
2 sets of 4 ways43879
1 set of 8 ways881910

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 choiceThe alternativeWhat the choice buys, and costs
Several ways for an indexOne line for an indexBlocks with the same index stay side by side. One comparator for each way, and a longer hit path.
All ways compared at onceThe ways checked one after anotherThe answer comes in one cycle. Every comparator switches on every read, which costs power.
Two or four waysOne set for all linesMost of the gain for a small cost. A few blocks can still fight over a set.
A block bound to the set of its indexA block in any setOnly 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:

The same lab, with the tag taken as the block number divided by 8, where it should be divided by 4.
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 1

The 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 page8 lines, 1 to 8 ways
A real cachehundreds of sets of 4 to 16 ways

This section looks ahead. Real caches choose their place on this scale level by level.

  1. 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.

  2. 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.

  3. Which block goes

    A full set must give up a block. The next lesson is about that choice.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=5

Check Yourself

Predict the outputBlocks 3, 11, 19 and 27 are read in turn, twice round, through eight lines. First as 4 sets of 2 ways, then as 2 sets of 4 ways. What does the last row say?
// Eight cache lines, arranged as sets and ways. A read names a block number. This lab only counts.
module tb;
  localparam LINES = 8;
  int WAYS = 2;                    // lines in one set: 1, 2, 4 or 8
  int SETS;                        // LINES / WAYS
  logic valid [0:7][0:7];          // [set][way]
  int   tags  [0:7][0:7];
  int   holds [0:7][0:7];          // the block whose copy the line really holds
  int   used  [0:7][0:7];          // when the line was last used
  int now, reads, hits;
  bit quiet = 0;

  task automatic start(input int ways);
    WAYS = ways;  SETS = LINES / WAYS;
    for (int s = 0; s < 8; s++) for (int w = 0; w < 8; w++) valid[s][w] = 0;
    now = 0;  reads = 0;  hits = 0;
  endtask

  task automatic read(input int block);
    int set, tag, way, victim;
    set = block % SETS;            // the low part of the block number picks the set
    tag = block / SETS;            // the rest is the tag
    now++;  reads++;
    way = -1;
    for (int w = 0; w < WAYS; w++)            // every way of the set is compared, all at once in hardware
      if (valid[set][w] && tags[set][w] == tag) way = w;
    if (way >= 0) begin
      hits++;
      if (!quiet) $display("read block %2d: set %0d tag %0d | hit in way %0d, which holds block %0d", block, set, tag, way, holds[set][way]);
    end else begin
      victim = -1;                            // an empty way first, or else the way used longest ago
      for (int w = WAYS - 1; w >= 0; w--) if (!valid[set][w]) victim = w;
      if (victim < 0) begin
        victim = 0;
        for (int w = 1; w < WAYS; w++) if (used[set][w] < used[set][victim]) victim = w;
      end
      if (!quiet && !valid[set][victim]) $display("read block %2d: set %0d tag %0d | miss, into way %0d", block, set, tag, victim);
      if (!quiet &&  valid[set][victim]) $display("read block %2d: set %0d tag %0d | miss, into way %0d, and block %0d goes", block, set, tag, victim, holds[set][victim]);
      valid[set][victim] = 1;  tags[set][victim] = tag;  holds[set][victim] = block;
      way = victim;
    end
    used[set][way] = now;
  endtask

  initial begin
    quiet = 1;
    start(2);  read(3); read(11); read(19); read(27); read(3); read(11); read(19); read(27);
    $display("2 ways: reads=%0d hits=%0d", reads, hits);
    start(4);  read(3); read(11); read(19); read(27); read(3); read(11); read(19); read(27);
    $display("4 ways: reads=%0d hits=%0d", reads, hits);
  end
endmodule
ConceptWhat is a way?
ConceptA cache has 4 sets of 2 ways. Where may a block with index 3 sit?
ConceptWhy does a four-way cache have four comparators?
ConceptThe same eight lines go from 4 sets of 2 ways to 2 sets of 4 ways. What happens to index and tag?
ConceptWhat is a cache with one set for all its lines called?

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.

Practice

Report a problem with this page