Caches III: Direct Mapped
Assistant
Teaching Professor
Lisa Yan
CS61C
Great Ideas
in
Computer Architecture
(a.k.a. Machine Structures)
cs61c.org
Head TA
Nicolas Reed
Yan, SP26
28-Caches III: Direct Mapped (1)
Great Idea #3: Principle of Locality / Memory Hierarchy
Storage Latency Analogy: How Far Away is the Data?
Jim Gray�1998 Turing Award
B.S. Cal 1966
Ph.D. Cal 1969
On-chip cache
Sacramento
This Campus
This Room
My Head
10 min
1.5 hr
2 Years
1 min
Pluto
200 Years
Alpha Centauri
Registers
On-board cache
RAM/Memory
Storage/Disk
1
2
10
100
Cloud
[ns]
108
106
Yan, SP26
28-Caches III: Direct Mapped (2)
Line Replacement Policies
Agenda
—
Yan, SP26
28-Caches III: Direct Mapped (3)
Fully Associative Cache with LRU policy
Suppose that LRU = 0 means most recently used, and 3 means least recently used.
After the end of instruction 5, what are the LRU tags on each row?
Valid | LRU | Tag | Data | |||
11 | 10 | 01 | 00 | |||
| | | … | … | … | … |
| | | … | … | … | … |
| | | … | … | … | … |
| | | … | … | … | … |
Valid | LRU | Tag |
1 | 0 | 0x10F |
0 | | |
0 | | |
0 | | |
Valid | LRU | Tag |
1 | 1 | 0x10F |
1 | 0 | 0x178 |
0 | | |
0 | | |
Valid | LRU | Tag |
1 | 2 | 0x10F |
1 | 1 | 0x178 |
1 | 0 | 0x209 |
0 | | |
Valid | LRU | Tag |
1 | 2 | 0x10F |
1 | 0 | 0x178 |
1 | 1 | 0x209 |
0 | | |
Valid | LRU | Tag |
1 | 3 | 0x10F |
1 | 1 | 0x178 |
1 | 2 | 0x209 |
1 | 0 | 0x149 |
Yan, SP26
28-Caches III: Direct Mapped (4)
A Warmed up Cache Can Still Miss
Valid | LRU | Tag | Data | |||
11 | 10 | 01 | 00 | |||
1 | 3 | 0x10F | … | … | … | … |
1 | 1 | 0x178 | … | … | … | … |
1 | 2 | 0x209 | … | … | … | … |
1 | 0 | 0x149 | … | … | … | … |
LRU | Tag |
0 | 0x25C |
2 | 0x178 |
3 | 0x209 |
1 | 0x149 |
… | … | … | … |
c. Read byte at 0x2 offset, return to processor.
Yan, SP26
28-Caches III: Direct Mapped (5)
Line Replacement Policies
LRU is ideal for temporal locality but in practice, FIFO is good enough.
Yan, SP26
28-Caches III: Direct Mapped (6)
Cache Design: Placement Policies, Soon
Fully Associative Cache
Put a new line anywhere
(Caches II)
Direct Mapped Cache
Put a new line in one specific place
(Caches III, today)
Set-Associative Cache
Something in-between (how?)
(Caches IV)
Yan, SP26
28-Caches III: Direct Mapped (7)
Write Policies
Agenda
—
Yan, SP26
28-Caches III: Direct Mapped (8)
Stores: How to write back to memory?
Valid | LRU | Tag | Data | |||
11 | 10 | 01 | 00 | |||
1 | 0 | 0x25C | … | … | … | … |
1 | 2 | 0x178 | … | … | … | … |
1 | 3 | 0x209 | … | … | … | … |
1 | 1 | 0x149 | … | … | … | … |
Cache hit! …and then?
How to handle stores?
Yan, SP26
28-Caches III: Direct Mapped (9)
Write-through vs. Write-back Policies
Valid | LRU | Tag | Data | |||
11 | 10 | 01 | 00 | |||
1 | 0 | 0x25C | … | … | … | … |
1 | 2 | 0x178 | … | … | … | … |
1 | 3 | 0x209 | … | … | … | … |
1 | 1 | 0x149 | … | … | … | … |
Store byte 0x524(0x149,0x0)
Yan, SP26
28-Caches III: Direct Mapped (10)
What does Write-Back Look Like? Dirty Bit
Valid | Dirty | LRU | Tag | Data | |||
11 | 10 | 01 | 00 | ||||
1 | 0 | 0 | 0x25C | … | … | … | … |
1 | 0 | 2 | 0x178 | … | … | … | … |
1 | 0 | 3 | 0x209 | … | … | … | … |
1 | 1 | 1 | 0x149 | … | … | … | … |
Cache hit w/write-back:
update cache line, and wait until this line is replaced before writing back to memory
Store byte 0x524(0x149,0x0)
Yan, SP26
28-Caches III: Direct Mapped (11)
Write-through vs. Write-back Policies
Valid | LRU | Tag | Data | |||
11 | 10 | 01 | 00 | |||
1 | 0 | 0x25C | … | … | … | … |
1 | 2 | 0x178 | … | … | … | … |
1 | 3 | 0x209 | … | … | … | … |
1 | 1 | 0x149 | … | … | … | … |
Store byte 0x524(0x149,0x0)
Write policies have tradeoffs:
simple to implement
(typically) lower traffic to memory
Yan, SP26
28-Caches III: Direct Mapped (12)
Direct Mapped Cache
Agenda
—
Yan, SP26
28-Caches III: Direct Mapped (13)
Cache Design: Placement Policies
Fully Associative Cache
Find data in any line
(Caches II)
Direct Mapped Cache
Find data in one specific line
(Caches III, today)
Set-Associative Cache
(Caches IV)
Fully associative caches need expensive hardware.
Yan, SP26
28-Caches III: Direct Mapped (14)
Direct Mapped Cache
flags | Tag | Data | ||||
11 | 10 | 01 | 00 | |||
… | … | … | … | … | … | |
… | … | … | … | … | … | |
… | … | … | … | … | … | |
… | … | … | … | … | … | |
How do we identify this location from the memory address?
31 | | 0 |
Full 32b address | ||
Yan, SP26
28-Caches III: Direct Mapped (15)
Direct Mapped Cache
flags | Tag | Data | ||||
11 | 10 | 01 | 00 | |||
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
tag to connect line to memory address
31 | | 0 |
Full 32b address | ||
tag | index | offset |
index to select line in cache
byte offset within line
Example: 0x61B
0b0110 0001 1011
tag
0x61
offset
0x3
index
0x2
0
1
2
3
indices
Yan, SP26
28-Caches III: Direct Mapped (16)
Direct Mapped Caches: 4B Cache
Line starting… maps to… has tag…
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
0x
(assume 8 bit address)
Different addresses get same line index but different tags.
Tag: 000000
000001
flags | Tag | Data | |
… | … | … | … |
… | … | … | … |
… | … | … | … |
… | … | … | … |
Yan, SP26
28-Caches III: Direct Mapped (17)
Direct Mapped Caches: 4B Cache
Line starting… maps to… has tag…
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
0x
Lines with same line index replace each other.
flags | Tag | Data | |
… | … | … | … |
… | … | … | … |
… | … | … | … |
… | … | … | … |
Yan, SP26
28-Caches III: Direct Mapped (18)
Direct Mapped Caches: 8B Cache
Line starting… maps to… has tag…
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
0x
Tag: 00000
flags | Tag | Data | ||
1 | 0 | |||
… | … | … | … | … |
… | … | … | … | … |
… | … | … | … | … |
… | … | … | … | … |
Yan, SP26
28-Caches III: Direct Mapped (19)
Direct Mapped Caches: 16B Cache
Line starting… maps to… has tag…
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
0x
Tag: 0000
flags | Tag | Data | ||||
11 | 10 | 01 | 00 | |||
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
Yan, SP26
28-Caches III: Direct Mapped (20)
Direct Mapped Cache
flags | Tag | Data | ||||
11 | 10 | 01 | 00 | |||
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
Unlike FA caches, DM caches have an index to identify the only cache line for this memory address.
tag to connect line to memory address
31 | | 0 |
Full 32b address | ||
tag | index | offset |
index to select line in cache
byte offset within line
Yan, SP26
28-Caches III: Direct Mapped (21)
Fill in the blank: Direct Mapped, 12 bit addresses
Suppose we have the below direct mapped cache for 12 bit addresses.
A. B. C. D. E. F.
2 4 8 10 16 Other
2 4 8 10 16 Other
2 4 8 10 16 Other
2 4 8 10 16 Other
flags | Tag | Data | |||
11 | 10 | 01 | 00 | ||
| | | | | |
| | | | | |
| | | | | |
| | | | | |
Yan, SP26
28-Caches III: Direct Mapped (22)
Yan, SP26
28-Caches III: Direct Mapped (23)
Terminology for Direct Mapped, 12b addresses
1. Line size / block size:
2. Capacity
3. Offset
4. Index
5. Tag
11 4 | 3 2 | 1 0 |
| | |
offset
tag
memory address
4 bytes
4 x 4 bytes
= 16 bytes
log2(line size) = 2 bits
# address bits - # offset bits - # index bits = 8 bits
Tag | Data | |||
11 | 10 | 01 | 00 | |
| | | | |
| | | | |
| | | | |
| | | | |
log2(# lines) = 2 bits
index
Yan, SP26
28-Caches III: Direct Mapped (24)
Direct Mapped Cache Analysis
Agenda
—
Yan, SP26
28-Caches III: Direct Mapped (25)
Example: Direct Mapped Cache
Suppose we have the following direct mapped cache, for 12 bit addresses.
� Load byte 0xFE2
11 4 | 3 2 | 1 0 |
| | |
offset
tag
memory address
index
valid | dirty | Tag | Data | |||
11 | 10 | 01 | 00 | |||
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0b1111 1110 0010
tag
0xFE
offset
0x2
index
0x0
Compute T/I/O
0 | … | … | … | … | … | … |
Yan, SP26
28-Caches III: Direct Mapped (26)
Warming up the Direct Mapped Cache
Load byte 0xFE2 �
valid | dirty | Tag | Data | |||
11 | 10 | 01 | 00 | |||
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0b1111 1110 0000 ← 0xFE0
0b1111 1110 0011 ← 0xFE3
b. Load into cache the 4-byte line � from 0xFE0 to 0xFE3.� Mark valid bit.
c. Read byte at 0x2 offset� and return to processor.
1 | 0 | 0xFE | … | … | … | … |
0b1111 1110 0010
tag
offset
index
0xFE,0x0,0x2
Yan, SP26
28-Caches III: Direct Mapped (27)
Warming up the Direct Mapped Cache
Store byte 0x61C �
valid | dirty | Tag | Data | |||
11 | 10 | 01 | 00 | |||
1 | 0 | 0xFE | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
1 | 1 | 0x61 | … | … | … | … |
0b0110 0001 1100
tag
offset
index
0x61,0x3,0x0
b. Load into cache the 4-byte line.� Mark valid bit.
c. Write byte at 0x0 offset.� Mark dirty bit.
Yan, SP26
28-Caches III: Direct Mapped (28)
Warming up the Direct Mapped Cache
Load byte 0x61B �
valid | dirty | Tag | Data | |||
11 | 10 | 01 | 00 | |||
1 | 0 | 0xFE | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
1 | 1 | 0x61 | … | … | … | … |
1 | 0 | 0x61 | … | … | … | … |
0b0110 0001 1011
tag
offset
index
0x61,0x2,0x3
b. Load into cache the 4-byte line.� Mark valid bit.
c. Read byte at 0x3 offset� and return to processor.
Yan, SP26
28-Caches III: Direct Mapped (29)
Warming up the Direct Mapped Cache
Load byte 0xCAD �
valid | dirty | Tag | Data | |||
11 | 10 | 01 | 00 | |||
1 | 0 | 0xFE | … | … | … | … |
0 | … | … | … | … | … | … |
1 | 0 | 0x61 | … | … | … | … |
1 | 1 | 0x61 | … | … | … | … |
1 | 0 | 0xCA | … | … | … | … |
0b1100 1010 1101
tag
offset
index
0xCA,0x3,0x1
b. Evict current line at index 3. � Write back data.
c. Load into cache the 4-byte line.� Mark valid bit.
d. Read byte at 0x1 offset� and return to processor.
Yan, SP26
28-Caches III: Direct Mapped (30)
Direct Mapped Cache
flags | Tag | Data | ||||
11 | 10 | 01 | 00 | |||
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
… | … | … | … | … | … | … |
tag to connect line to memory address
31 | | 0 |
Full 32b address | ||
tag | index | offset |
index to select line in cache
byte offset within line
Example: 0x61B
0b0110 0001 1011
tag
0x61
offset
0x3
index
0x2
Yan, SP26
28-Caches III: Direct Mapped (31)
Direct Mapped: Policies
1. Write Policy
A. Write-through� (memory access per write)
B. Write-back� (dirty bit, write to memory � on replacement)
What policies can be implemented for a direct-mapped cache?
Select all that apply.
2. Line Replacement Policy
A. Least Recently Used
B. Most Recently Used
C. FIFO
D. Random
E. None of the Above
Yan, SP26
28-Caches III: Direct Mapped (32)
Direct Mapped: Policies Solution
1. Write Policy
A. Write-through� (memory access per write)
B. Write-back� (dirty bit, write to memory � on replacement)
What policies can be implemented for a direct-mapped cache?
Select all that apply.
2. Line Replacement Policy
A. Least Recently Used
B. Most Recently Used
C. FIFO
D. Random
E. None of the Above
In direct mapped caches, there is only ever one line to replace—the existing line with matching index.
Yan, SP26
28-Caches III: Direct Mapped (33)
Cache Design: Placement Policies, Summary
For the above reasons, smaller caches are generally fully associative.
Yan, SP26
28-Caches III: Direct Mapped (34)
(pause)
Yan, SP26
28-Caches III: Direct Mapped (35)
Looking Ahead
Yan, SP26
28-Caches III: Direct Mapped (36)
Types of Misses
Agenda
—
Yan, SP26
28-Caches III: Direct Mapped (37)
Types of Misses
In this class, we will only distinguish between compulsory and non-compulsory misses.
“Non-compulsory” miss
Yan, SP26
28-Caches III: Direct Mapped (38)
Types of Misses
A. FA: Compulsory
B. DM: Compulsory
C. FA: Capacity
D. DM: Capacity
E. FA: Conflict
F. DM: Conflict
G. None of the above
Which types of misses can occur for Fully Associative caches (FA)? For Direct Mapped caches (DM)?
Select all that apply.
Yan, SP26
28-Caches III: Direct Mapped (39)
Yan, SP26
28-Caches III: Direct Mapped (40)
Types of Misses
A. FA: Compulsory
B. DM: Compulsory
C. FA: Capacity
D. DM: Capacity
E. FA: Conflict
F. DM: Conflict
G. None of the above
Which types of misses can occur for Fully Associative caches (FA)? For Direct Mapped caches (DM)?
Select all that apply.
Yan, SP26
28-Caches III: Direct Mapped (41)
Types of Misses
In this class, we will only distinguish between compulsory and non-compulsory misses.
Yan, SP26
28-Caches III: Direct Mapped (42)
How to categorize misses
[reference]
In this class, we will only distinguish between compulsory and non-compulsory misses.
Yan, SP26
28-Caches III: Direct Mapped (43)
[Extra] Practice
Agenda
—
Yan, SP26
28-Caches III: Direct Mapped (44)
Direct Mapped Cache (write-back)
Suppose the 4-line, 4B line size cache below starts cold.
valid | dirty | Tag | Data | |||
11 | 10 | 01 | 00 | |||
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
Yan, SP26
28-Caches III: Direct Mapped (45)
Direct Mapped Cache (write-back)
Suppose the 4-line, 4B line size cache below starts cold.
valid | dirty | Tag | Data | |||
11 | 10 | 01 | 00 | |||
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
0 | … | … | … | … | … | … |
Yan, SP26
28-Caches III: Direct Mapped (46)