Download Exploiting Memory Hierarchy in Computer Organization and more Slides Computer Architecture and Organization in PDF only on Docsity!
CSE 311
Computer Organization
Mostafa I. Soliman
Professor of Computer Engineering
CSE Department
[email protected] [email protected]
Lecture 11
Exploiting Memory
Hierarchy (1)
Exploiting Memory Hierarchy
- Memories: Review - Memory Hierarchy - Locality - Hit and Miss - Caches - Direct Mapped Cache - Accessing Cache - Direct Mapped Cache - Caches read hit/miss - Put All Together:
DECStation 3100 Cache
(MIPS R2000 processor)
Life is beautiful in its
simplicity.
Thomas Matthiessen
- Users want large and fast memories…
- expensive and they don’t like to pay…
- Make it seem like they have what they want…
- memory hierarchy
- hierarchy is inclusive, every level is subset of lower level
- performance depends on hit rates
Memory Hierarchy
Processor
Data are transferred
CPU
Level n
Level 2
Level 1
Levels in the memory hierarchy
Increasing distance from the CPU in access time
Size of the memory at each level
Block of data (unit of data copy)
Locality
- Locality is a principle that makes having a memory hierarchy a
good idea
- If an item is referenced then because of
- temporal locality: it will tend to be again referenced soon
- spatial locality: nearby items will tend to be referenced soon
- why does code have locality – consider instruction and data?
- By simple example
- assume block size = one word of data
- Issues:
- how do we know if a data item is in the cache?
- if it is, how do we find it?
- if not, what do we do?
- Solution depends oncache addressing scheme …
Caches
a. Before the reference to Xn
X
Xn – 1
Xn – 2
X
X
b. After the reference to Xn
X
Xn – 1
Xn – 2
X
X
Xn
X2 X
Reference to Xn causes miss so it is fetched from memory
- Addressing scheme in direct mapped cache:
- cache block address = memory block address mod cache size (unique)
- if cache size = 2m, cache address = lower m bits of n-bit memory address
- remaining upper n-m bits kept as tag bits at each cache block
- also need a valid bit to recognize valid entry
Direct Mapped Cache
00001 00101 01001 01101 10001 10101 11001 11101
000
Cache
Memory
(^001010011100101110111)
Index V Tag Data 000 N 001 N 010 Y 11 Mem(11010) 011 N 100 N 101 N 110 Y 10 Mem(10110) 111 N
(2) Address referred 11010 (miss): Index V Tag Data 000 N 001 N 010 Y 11 Mem(11010) 011 N 100 N 101 N 110 Y 10 Mem(10110) 111 N
(3) Address referred 10110 (hit):
Index V Tag Data 000 N 001 N 010 Y 10 Mem(10010) 011 N 100 N 101 N 110 Y 10 Mem(10110) 111 N
(4) Address referred 10010 (miss):
to CPU
Direct Mapped Cache
Address (showing bit positions)
20 10
offset^ Byte
Index Valid Tag Data 0 1 2
1021 1022 1023
Tag Index
Hit Data
(^20 )
31 30 13 12 11 2 1 0
Address showing bit positions
Cache with 1024 1-word blocks:byte offset (least 2 significant bits) is ignored and next 10 bits used to index into cache
DECStation 3100 Cache
(MIPS R2000 processor)
Address (showing bit positions)
16 14 Byte offset
Valid Tag Data
Hit Data
16 32
16K entries
16 bits 32 bits
31 30 17 16 15 5 4 3 2 1 0
Address showing bit positions
Cache with 16K 1-word blocks:byte offset (least 2 significant bits) is ignored and next 14 bits used to index into cache
- Write-through scheme
- on write hit: replace data in cache and memory with every write hit to avoid inconsistency
- on write miss: write the word into cache and memory – obviously no need to read missed word from memory!
- Write-through is slow because of always required memory write » performance is improved with a write buffer where words are stored while waiting to be written to memory – processor can continue execution until write buffer is full » when a word in the write buffer completes writing into main that buffer slot is freed and becomes available for future writes » DEC 3100 write buffer has 4 words
- Write-back scheme
- write the data block only into the cache and write-back the block to main only when it is replaced in cache
- more efficient than write-through, more complex to implement
Cache Write Hit/Miss
Direct Mapped Cache: Taking
Advantage of Spatial Locality
- Cache replacement in large (multiword) blocks:
- word read miss: read entire block from main memory
- word write miss: cannot simply write word and tag! Why?!
- writing in a write-through cache:
» if write hit, i.e., tag of requested address and and cache entry
are equal, continue as for 1-word blocks by replacing word
and writing block to both cache and memory
» if write miss, i.e., tags are unequal, fetch block from memory,
replace word that caused miss, and write block to both
cache and memory
» therefore, unlike case of 1-word blocks, a write miss with a
multiword block causes a memory read
- Miss rate falls at first with increasing block size as
expected, but, as block size becomes a large fraction of
total cache size, miss rate may go up because
- there are few blocks
- competition for blocks increases
- blocks get ejected before most of their words are accessed
(thrashing in cache)
Direct Mapped Cache: Taking
Advantage of Spatial Locality
1 KB 8 KB 16 KB 64 KB 256 KB
256
40% 35% 30% 25% 20% 15% 10% 5% 0%
Miss rate
4 16 64 Block size (bytes) Miss rate vs. block size for various cache sizes
Example Problem
- How many total bits are required for a direct-mapped cache with 128 KB
of data and 4-word block size, assuming a 32-bit address?
- Cache size = 128 KB = 2^17 bytes = 2^15 words = 2^13 blocks
- Cache entry size = block data bits + tag bits + valid bit
= 128 + (32 – 13 – 2 – 2) + 1 = 144 bits
- Therefore, cache size = 2^13 144 bits = 2^13 (1.25 128) bits
= 1.25 220 bits = 1.25 Mbits
- data bits in cache = 128 KB 8 = 1 Mbits
- total cache size/actual cache data = 1.
Example Problem
- Consider a cache with 64 blocks and a block size of 16 bytes. What
block number does byte address 1200 map to?
- As block size = 16 bytes:
byte address 1200 block address 1200/16 = 75
- As cache size = 64 blocks:
block address 75 cache block (75 mod 64) = 11