Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

2Placement Policy

A set is a group of blocks in the cache. A block is first mapped onto a set, and then the block can be placed anywhere within that set.

A cache is N-Way Set Associative if sets of N cache blocks are assigned a unique index. The associativity of a cache is therefore the number of slots (i.e., ways) assigned to each indexed set. Figure 1 is a 16-byte, 2-way set-associative cache with 4B blocks.

"Small 2-way set-associative cache table showing set 0 as the first two cache lines and set 1 as the second two cache lines."

Figure 1:A 16-byte, 2-way set-associative cache with 4B blocks.

3Identification

Determining a cache hit in a set-associative cache works similarly to the process in direct-mapped caches, because the data can only be stored at one index. However, now the index has multiple slots (i.e., ways).

As an example, we can connect the 12-bit memory address in Figure 3 to the set-associative cache in Figure 4 (below).

"Memory address split for 2-way set-associative cache into fields: tag at bits 11 through 4, index at bits 3 through 2, and block offset at bits 1 through 0."

Figure 3:For a set-associative cache, the memory address is split into three fields: the tag, the index, and the offset. For the cache in Figure 4, a 12-bit memory address is split into an 8-bit tag, a 2-bit index, and a 2-bit offset.

Notes:

4Replacement Policy

5Write Policy

6Walkthrough

The following animation traces through four memory accesses to a 12-bit address space on our 32B, 2-way set-associative cache with 4B blocks. Assume a write-back policy. Assume the cache starts out cold, like in Figure 4.

"Table showing cold 2-way set-associative cache with 4 sets. Each cache line has a valid and dirty bit."

Figure 4:A cold snapshot of a 32B, 2-way set-associative cache with 4B blocks and a dirty bit for write-back.

(a)

"Address decomposition table showing memory address in bytes. The block address is split into tag and index, and the remaining part of the address is the block offset. Tag is used to connect cache to memory, index is used to select the cache block for placement in cache, and the byte offset specifies the byte within the cache block."

(b)A (byte-addressed) memory address can be decomposed into a block address and a block offset. For direct-mapped caches and set-associative caches, the block address can be further divided into a tag and an index. Fully associative caches have no index field.

"Placement-policy spectrum chart from fully-associative cache to direct mapped cache with set-associative cache in between. Fully associative cache is labeled as putting a new block anywhere in the cache. Set-associative cache is labeled as putting a new block in one of N places, called sets. Direct mapped cache is labeled as putting a new block in one specific place."

(c)The spectrum of cache placement policies, with set-associative as the in-between approach.

Contrast this set-associative cache walkthrough with the one for direct-mapped caches:

  • Identification of a cache hit occurs by checking M tags in an M-way set associative cache: each of the tags for the M blocks in the set.

  • Memory accesses 2 and 3 create cache entries in cache entries 3 and 2, respectively; these cache entries share the same tag. However, the blocks in these entries have different block addresses.

  • Memory access 4 did not incur a block replacement/memory write. Because there are two ways in a set, the existing block in the set with index 3 was not replaced. At the end of memory access 4, the set with index 3 is full.

7Associativity: A Discussion

We illustrate in Figure 5b the relationship between block address, tag, index, and offset.

Notes:

  • Fully associative caches have no index field; the block address is the tag.

  • In direct-mapped caches and set-associative caches, the index is the lower bits of the block address used to select the set.

  • The tag is compared against every block’s tag in the set.

  • The offset is the address of the desired data within the block.