Download Cache Memory Organization: Direct Mapped Cache with Valid and Dirty Bit - Prof. D. Palseti and more Study notes Computer Science in PDF only on Docsity!
CacheCIT 595
Spring 2007
11 - 2
CIT 595
Cache
-^ The purpose of cache memory is to
speed up
data
accesses for processor by storing information in fastermemory made of SRAM
¾^ SRAM access time is 3ns to 10ns ¾^ DRAM access time is 30ns to 90ns
- The data stored in cache is data that the processor is likely to use in the very near future
¾^ SRAM is fast but has less memory, so store only a subset ofthe data stored in main memory
11 - 3
CIT 595
Basic Terminology
blocks
fixed numbers of
words
-^ Word = size of data stored in onelocation e.g. 8 bits, 16 bits etc.. - One block is used as the
minimum unit
of transfer
between main memory and
cache• Hence, each
location
in the cache
stores
1 block
Word 0Word 1Word 2 Word 3
00011011 Main Memory
Block 0Block 1 Word 2
Word 3
Word 0
Word 1
Cache
11 - 4
CIT 595
Cache Mapping Scheme
- If the CPU generates an address for a particular
word
in main memory, and that data happens to be in thecache, the same main memory address cannot be usedto access the cache• Hence a
mapping scheme
is required that converts the
generated main memory address into a cache location• Mapping Scheme also determines where the block willplaced when it originally copied into the cache
11 - 5
Address Conversion to Cache Location
- Address Conversion is done by giving specialsignificance to the
bits of the main memory address
- The address is split into distinct groups called
fields
¾^ Just like instruction decoding is done based on certain bit fields
- The group fields are a way to find:
¾^ Which cache location? ¾^ Which word in the block? ¾^ Whether it is the right data are looking for? Some kind ofunique identifier
11 - 6
CIT 595
Mapping Scheme 1: Direct Mapped Cache • In a direct mapped cacheconsisting of
N
blocks of
cache (i.e. N locations) , block X^ of main memory maps tocache block
Y
=^
X^
mod
N
.
- E.g. if we have 10 blocks ofcache, block 7 of cache mayhold blocks 7, 17, 27, 37,.. .of main memory.
11 - 7
Direct Mapped Scheme: Address Conversion
Word
Block
Tag
n-bit main memory address
Word = which block in word?Block = Which location in Cache?Tag = unique identifier w.r.t one blockNote: Tag is used to distinguish whethermain memory block 7 or 17 is stored incache block 7
Note:
Will
explain why thebreakup is thisway….later
11 - 8
CIT 595
Cache with 4 blocks and 8 words per block BlockNo.^0123
Data
Tag
11 - 13
Direct Mapped Cache Example
1AB
1AA
BlockNo.^012345131415
Data
Tag
11 - 14
CIT 595
Direct Mapped Cache Example (contd..)
-^ However, if the program generates the address, 3AB - 3AB also maps to block 0101, but we will not find data for3AB in cache
¾Tags will not match i.e. 0000111 (of addr 3AB) is notequal to 0000011 (of addr 1AB)
- Hence we get it from main memory• The block loaded for address
1AA
would be evicted
(removed) from the cache, and replaced by the blocksassociated with the
3AB
reference
11 - 15
Direct Mapped Cache with address 3AB
3AB
BlockNo.^012345131415
Data
Tag
11 - 16
CIT 595
Disadvantage of Direct Mapped Cache
- Suppose a program generates a series of memoryreferences such as:
1AB,
3AB,
1AB,
3AB
,..
¾The cache will continually evict and replace blocks
- The theoretical advantage offered by the cache islost in this extreme case• Other cache mapping schemes are designed toprevent this kind of
thrashing
11 - 17
Address Breakup
- Why is the address broken up in a particular manner ?• This is done if we use
high-order memory address interleaving
¾^ Due to spatial locality, data from consecutive addresses are broughtinto cache
- If the higher order bits (i.e. bits used for tag) are used fordetermining cache location then values from consecutive addresseswould map to same location in cache
¾^ The
middle
bits are preferred for block location as they would cause
less thrashing
Word
Block
Tag
11 - 18
CIT 595
Valid Cache block
- How do we know whether the block in cache is valid or not?• For example:
¾When processor just starts up, the cache will be emptyand tag fields in each location will be meaningless ¾Thus tag fields must be ignored initially when the cacheis starting to fill up
- For validity, another bit called
valid bit
is added to the cache
indicate whether the block contains valid information
¾0 – not valid, 1 – valid ¾All blocks at start up would be not valid ¾If data from main memory is got into cache for aparticular block, then valid bit for that field is set ¾Valid bit will contribute to the cache size
11 - 19
Direct Mapped Cache with Valid (V) Field (^012345131415) BlockNo.
Data
Tag
V^0000001000
Address 3ABreferenced forthe first time.Entire block isbrought intocache block 5.
11 - 20
CIT 595
Calculating Cache Size
Suppose our memory consists of 2
14 locations (or words), and
cache has 16 = 2
4 blocks, and each block holds 8 words
- There are 16 locations in the cache• Each row has 7 bits for tag + 8 words + 1 valid bit• Assume 1 word is 8 bits, the total bits in row (8 x 8) + 7 + 1 = 72• 72 bits = 9 bytes• Cache size = 16 x 9 bytes = 144 bytes
11 - 25
Fully Associate: Which block to replace if cache is full?CIT 595
- Recall that direct mapped cache evicts a blockwhenever another memory reference needs thatblock• With fully associative cache, we have no suchmapping, thus we must devise an
algorithm
to
determine which block to evict from the cache• The block that is evicted is called the
victim block
- There are a number of ways to pick a victim, wewill discuss them shortly….
11 - 26
CIT 595
Scheme 3: Set Associative
- Set associative cache combines the ideas of directmapped cache and fully associative cache• A set associative cache mapping is like directmapped cache in that a memory reference maps toa particular location in cache• But that
cache location can hold more than one
main memory block. The cache location is thencalled a
set.
¾Instead of mapping anywhere in the entire cache (fullyassociative), a memory reference can map only to thesubset of cache
11 - 27
CIT 595
Scheme 3: Set Associative
-^ The number of blocks per set in set associativecache varies according to overall system design• For example, a 2-way set associative cache canbe conceptualized as shown in the schematic below
¾Each set contains two different memory blocks^ K-way set associate cache will have K blocks per set
11 - 28
CIT 595
Scheme 3: Address Conversion
- Like direct-mapped cache except, middle bits of themain memory address indicate the
set
in cache
11 - 29
K-Set Associative Cache Example
- Suppose we have a main memory of 2
14 locations
- This memory is mapped to a
2-way
set associative cache having
^16
blocks
where each block contains 8 words
- Number of Sets = Number of Blocks in cache/ K
¾^ Since this is a 2-way cache, each set consists of 2 blocks, and there are 16sets i.e. 16/2 = 8 Thus, we need 3 bits for the set, 3 bits for the word, giving 8 leftover bitsfor the tag:
11 - 30
CIT 595
Advantage & Disadvantage Set Associative • Advantage^ ¾
Unlike direct mapped cache, if an address maps to a set, thereis^ choice
for placing the new block
¾^ If both slots are filled, then we need an
algorithm
that will
decide which old block to evict (like fully associative)
¾^ Tags
of each block in a set need to be
matched
(in parallel)
to figure out whether the data is present in cache. Need
k
comparators. ¾ Although, the hardware cost for matching is less than fullyassociative (need n comparators, where n = # blocks), but it ismore than direct mapped (need only one comparator)
11 - 31
Which block to replace?
-^ With fully associative and set associative cache, a replacement
algorithm/
policy
needs to be used
when it becomes necessary to evict a block fromcache• The replacement policy that we choose dependsupon the locality that we are trying to optimize
- E.g. if we are interested in
temporal locality
i.e.
referenced memory is likely to be referenced again soon(e.g. code within a loop) then we will keep the mostrecently used blocks
11 - 32
CIT 595
Replacement Algorithm/Policy
LRU - L
east recently used
- Evicts the block that has been unused for thelongest period of time• The disadvantage of this approach is itscomplexity: LRU has to maintain an access
history
for each block, which will slow down the cache
11 - 37
Direct Mapped Cache with Valid and Dirty Bit
BlockNo.
xxxxx xxxxx
Data
Tag
D
V
D – Dirty Bit
V- Valid Bit
Dirty Words within one block
11 - 38
CIT 595
What affects Performance of Cache?
- Programs that exhibit bad locality• E.g. Spatial Locality with matrix operations
¾^ Suppose Matrix data kept in memory is by rows (known asrow-major) i.e.
offset = rowNUMCOLS + column*
¾^ for (j = 0; j < numcols; j++)
for(i = 0; i < numrows; i++) ¾^ i.e. x[i][j] followed by x[i + 1][j] ¾^ The array is being accessed by column and we going to missin the cache every time
- Solution: switch the for loops• C/C++ are row-major, FORTRAN & MATLAB iscolumn-major
11 - 39
Multi-level Cache
- Most of today’s systems employ multilevel cachehierarchies• The levels of cache form their own small memoryhierarchyCurrent day processor uses:• Level1 cache (8KB to 64KB) is situated on theprocessor itself
¾^ Access time is typically about 4ns
- Level 2 cache (64KB to 2MB) located external to theprocessor
¾^ Access time is usually around 15 - 20ns
11 - 40
CIT 595
Multi-level Caches (contd..)
- In a multi-level cache:
- If the cache system used an
inclusive
cache, the same data
may be present at multiple levels of cache• Strictly inclusive
caches guarantee that all data in a smaller
cache also exists at the next higher level.• Exclusive
caches permit only one copy of the data
- The tradeoffs in choosing one over the other involveweighing the variables of access time, memory size, andcircuit complexity
11 - 41
Instruction and Data Caches
unified
or
integrated
cache is one where both
instructions and data are cached• Many modern systems employ
separate
caches
for data and instructions
¾This is called a
Harvard
cache
¾Allows accesses to be less random and more clustered ¾Less access time than unified cache (typically larger)
11 - 42
CIT 595
Review of Cache Organization
Q1: Where can a block be placed in the cache level? Mapping scheme Q2: How is a block found if it is in the cache?^ Mapping Scheme Q3: If cache is full, then where do we put the new blocki.e. which old block should we replace? Block replacement policy Q4: If we write to a block in cache, should we update themain memory at the same time? Write Policy
11 - 43
Looking Forward
- Studied interaction between cache and main memory
¾^ The memory is managed by
hardware
- Next study the interaction between main memory anddisk
¾^ The memory is managed by hardware and
complier