Docsity

Cache Design: Block Size, Replacement Strategies, and Write Policies - Prof. Leona Mcmilla, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Cache Design: Block Size, Replacement Strategies, and Write Policies - Prof. Leona Mcmilla and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Cache Structure

Cache Structure

-^ Set-Associativity •^ Replacement policies -^ Overhead •^ Implementation -^ Handling writes •^ Cache simulations

Study 7.3, 7.

Cache Structure

Fully Associative Cache

(from last Lecture)

TAG

Data

=? TAG

Data

=? TAG

Data

Addressbits 31-2from CPU

Miss Data to CPU

Data from memory

READ HIT:One of the cache tags matchesthe incoming address; the dataassociated with that tag isreturned to CPU.READ MISS:None of the cache tagsmatched, so initiate access tomain memory and stall CPUuntil complete. Update cacheentry with new tag (address)and data.

One cache “LINE”

A cache“line”is the unitof memorythat has a“tag”associatedwith it

Cache Structure

Amortize Tag Costs: More Data/Tag

A31:

Mem[A]

Mem[A+4]

Mem[A+8]

Mem[A+12]

[3:2] [31:4]

ADDR

DATAHIT

-^ blocks

of 2

B^ words, on 2

B^ word boundaries

-^ always read/write 2

B^ word BLOCK from/to memory

-^ exploits

spatial locality

: nearby words in block, likely to accessed

BIG Cache Lines:^ •^ cost: some fetches of unaccessed words^ •^ BIG WIN if path to memory is wide, or sequential accesses are fast

Enlarge each line in fully-associative cache TAG

D^

D^

D^

D

Tag bits/cache entry = (30 – 2) bitsData bits/cache entry = 4*32 bits

Only 18% of cache’s memory used for tags

Cache Structure

Block Size vs. Miss Rate

-^ spatial locality: larger blocks

→→→→

reduce miss rate

-^ fixed cache size: larger blocks

→→→→^

fewer lines in cache →→→→^

higher miss rate, especially in small caches

If the block sizegets too big, westart fetchingitems that wenever access, atthe expense ofother items wemight actuallyneed. Thus, themiss rate startsto go up again.

Cache Structure

Fully-Assoc. vs. Direct-mapped

Fully-associative N-line cache: •^ N tag comparators, registers usedfor tag/data storage ($$$) •^ Location A can be stored in ANYof the N cache lines; no “collisions” •^ Replacement strategy (e.g., LRU)used to pick which line to use whenloading new word(s) into cache

Direct-mapped N-line cache: •^ 1 tag comparator, SRAM used fortag/data storage ($) •^ Location A is stored in aSPECIFIC line of the cachedetermined by its address;address “collisions” possible •^ Replacement strategy not needed:each word can only be cached in onespecific cache line

COLLISIONs occur when theremultiple items that we’d like tokeep cached, we have room, butour management policies allowsus to only a subset of them.

Is there something in- between?

Cache Structure

N-Way Set-Associative Cache k HIT

DATA TO CPU

INCOMING ADDRESS

=? t

There areN possibleplacesthat agiven itemcould bestored inthe cache^ MEM DATA

TARGET
INDEX

N direct-mapped caches, each with 2

t^ entries of N lines^ =?

=?

This lines of each index are a set

(N lines/cache index)

Cache Structure

14 12 10 8 6 4 2 0

1 k^
2k^
4k^
8k^
1 6k^
32k^
64k^
1 28k

1-way 2-way 4-way 8-way fully assoc.

Associativity vs. Miss Rate

Miss rate

(%)

Cache size (bytes)

Associativity

-^ 8-way is (almost) as effective as fully-associative •^ rule of thumb: N-byte M-way set assoc ≈ N/2-byte 2M-way set assoc.

Direct-mapped

Cache Structure

N

address

N-way set associative • compares addr with

N

tags simultaneously •^ Data can be stored inany of the N cache linesbelonging to a “set” •^ like N Direct-mappedcaches

Continuum of Associativity

Fully associative address • compares addr with all tags simultaneously • location A can bestored in any cache line

address

Direct-mapped

-^ compare addr withonly

one

tag

-^ location A can bestored in exactly onecache line

Allocates a cache entry

Allocates 1 of N lines in a set

Only one place to put it

ON A MISS?

Cache Structure

Three Replacement Strategies

LRU (Least-recently used)

-^ replaces the item that has gone UNACCESSED the LONGEST •^ favors the most recently accessed

data

FIFO/LRR (first-in, first-out/least-recently replaced)

-^ replaces the OLDEST item in cache •^ favors recently loaded

items over older STALE items

Random

-^ replace some item at RANDOM •^ no favoritism – uniform distribution •^ no “pathological” reference streams causing worst-case results •^ use pseudo-random generator to get reproducible behavior

Cache Structure

Keeping Track of LRU

-^ Needs to keep ordered list of N items for anN-way associative cache, that is

updated on

every access

. Example for N = 4:

(0,1,2,3)

Hit 2

Current Order

Action

Resulting Order

(2,0,1,3)

(2,0,1,3)

Hit 1

(1,2,0,3)

(1,2,0,3)

Miss, Replace 3

(^3 ,1,2,0)

(3,1,2,0)

Hit 3

(3,1,2,0)

-^

N! possible orderings

log

N! bits per set 2

approx

O(N log

N) “LRU bits” 2

+ update logic

Cache Structure

Example: LRU for 4-Way Sets

-^

Bits needed?

-^

How?

-^

One Method:

“One-Out/Hidden Line” coding (and variants) Directly encode the indices of the N-2 most recently accessed lines,plus one bit indicating if the smaller (0) or larger (1) of theremaining lines was most recently accessed

(2,0,1,3)

->

10 00 0

(3,2,1,0)

->

1 1 10 1

(3,2,0,1)

->

1 1 10 0

Requires (N-2)*log

N + 1 bits 2

-^ 8-Way sets? log

8! = 16, (8-2)*log 2

8 + 1 = 19 2

log

4! = log 2

24 = 5 per set 2

Bottom line,LRUreplacementrequiresconsiderableoverhead asassociativityincreases Overhead isO(N log

N) 2 bits/set

Cache Structure

FIFO Replacement

-^

Each set keeps a modulo-N counter that points to victimline that will be replaced on the next miss

-^

Counter is only

updated only on cache misses

Ex: for a 4-way set associative cache:

(0)

Miss, Replace 0

Next Victim

Action

( 1)

Hit 1

( 1)

Miss, Replace 1

(2)

Miss, Replace 2

(3)

Miss, Replace 3

(0)

Miss, Replace 0

Overhead isO(log

N) 2 bits/set

Cache Structure

FIFO For 4-way Sets

-^

Bits Needed?

-^

Low-cost, easy to implement (no tricks here)

-^

8-way?

-^

16-way?

-^

LRU 16-way?

-^

FIFO summary^ –^

Easy to implement, scales well,BUT CAN WE AFFORD IT?

log

4 = 2 per set 2

log

8 = 3 per set 2

I’m starting tobuy into allthat “big O”stuff!

log

16 = 4 per set 2

log

16! = 45 bits per set 2

14*log

16 + 1 = 57 bits per set 2

Cache Structure

Random Replacement

-^

Build a single Pseudorandom Number generator forthe WHOLE cache. On a miss, roll the dice andthrow out a cache line at random.

-^

Updates only on misses.

-^

How do you build a random numbergenerator (easier than you might think).

0x1F 01111

0x0F 00111

0x 10011

0x 11001

0x 01100

0x0C 10110

0x 01011

0x0B 00101

0x 10010

0x 01001

0x 00100

0x 00010

0x 00001

0x 10000

0x

0x 10100

0x 01010

0x0A 10101

0x 11010

0x1A 11101

0x1D 01110

0x0E 10111

0x 11011

0x1B 01101

0x0D 00110

0x 00011

0x 10001

0x 11000

0x 11100

0x1C 11110

0x1E

Counting

Sequence

Pseudorandom Linear Feedback Shift Register

Overhead isO(log

N) 2 bits/cache!