16-Cache Memory-13-03-2024
16-Cache Memory-13-03-2024
MEMORY
CACHE
MEMORY
• Cache memory is based on Locality of
reference(principle of locality)
It is the phenomenon of the same value or
related storage locations being frequently
accessed.
Types of reference locality:
Temporal locality
Spatial locality
• Temporal Locality:
• Definition: Temporal locality refers to the tendency of a program to
access the same memory locations repeatedly over a short period of
time.
• Example: If a program uses a variable in a loop, there is temporal
locality because the same memory location is accessed in each
iteration of the loop.
• Implication: Caching mechanisms, like CPU caches, take advantage
of temporal locality by keeping recently accessed data in a cache.
This helps in reducing the time it takes to access the data again, as it
is likely to be needed in the near future.
• Spatial Locality:
• Definition: Spatial locality refers to the tendency of a program to
access memory locations that are near each other.
• Example: When a program accesses a memory location, it is likely
to access nearby locations as well. This can occur when iterating
through an array, for instance.
• Implication: Spatial locality is exploited by caching systems that
fetch not only the requested data but also a block of contiguous
memory around it. This is based on the assumption that if a program
accesses one memory location, it is likely to access nearby locations
soon.
• temporal locality is concerned with repeated access to the same
memory locations over time.
Yes
Select the cache line to receive
the block from Main
Deliver Block To CPU Memory
Done
CACHE MEMORY
MANAGEMENT
TECHNIQUES
• Block Placement
Direct Mapping
Set Associative
Fully Associative
• Block
Identification
• Block Replacement
• Update Policies
Direct Mapping
Tag
Offset
Cache Memory Management
Techniques
FCFS
Random
Write
Through
Update Policies
Write
Writearound
back
Write allocate
DIRECT
MAPPING
A structure in which each memory location is mapped
to exactly one location in the cache.
No replacement policies are used.
• simpler, but much larger than an associative one
to give comparable performance
Cache block= (MM Block address)mod(number of
block in cache)
15
14 DIRECT MAPPING
14
13
7
12
6
11
5
10
4
9
3
8
2
7
1
6
0
5
4
Cache
3
2
1 (MM Block address) mod (Number of lines in a cache)
0
(12) mod (8) =4
Main Memory
DIRECT MAPPING -
EXAMPLE
• Consider the following configuration of system:
Main memory – 32 blocks
cache memory – 8 block
frames
cache is empty and the block
referred by CPU is 12.
Find the block frame of cache
where the requested
block is placed.
• Cache block frame = (Block address)mod(number of
block
in cache)
= 12 mod 8
=4
Now if the block referred by CPU is 20.
= 20 mod 8
=4
ie – both will be mapping to the same memory location.
frames
• Number of sets = 4 each
with 2 blocks
• cache is empty and the block
referred by CPU is 12.
• Find the block frame of cache where the requested
block is placed.
EXAMP
• LE
Set number= (Block address)mod(number of sets in
cache)
= 12 mod 4
=0
12th block can be placed in either in 0th or 1st block frame of
set 0.
• First map is to map the set in the cache and then to
the corresponding block frame.
FULLY
ASSOCIATIVE
• Incoming main memory block can be placed in
any available cache block.
• The block 12 (for previous example) can be placed
in any one of the available block frames.
• Replacement algorithm is used.
15
FULLY ASSOCIATIVE
14 MAPPING
14
13
7
12
6
11
5
10
4
9
3
8
2
7
1
6
0
5
4
Cache
3
2
1 Random
0
Main Memory
FULLY ASSOCIATIVE
• MAPPING
Fastest, most flexible but very expensive
• Any block location in cache can store any block in memory
• Stores both the address and the content of the memory word
• CPU address of 15 bits is placed in the argument register and
the associative memory is searched for a matching address
• If found data is read and sent to the CPU else main memory
is
accessed.
• CAM – content addressable memory
• 1.Assume there are three small caches, each consisting of four one-
word blocks. One cache is fully associative, a second is two-way set-
associative, and the third is direct-mapped. Find the number of
misses for each cache organization given the following sequence of
block addresses: 0, 8, 0, 6, and 8.
• Solu:
• The direct-mapped case is easiest. First, let’s determine to which
cache blockeach block address maps:
The direct-mapped cache generates five misses for the five accesses.
• The set-associative cache has two sets (with indices 0 and 1) with
two elements per set.
The fully associative cache has the best performance, with only
three misses
BLOCK IDENTIFICATION
• Block Identification: The block identification is the part
of the memory address used to determine which block of
data in the main memory corresponds to the requested
data. By using block identification, the cache controller
can quickly check whether the required data is present in
the cache.
• The offset is used to identify the specific byte within a cache line.
• The index is used to determine which set (or way) in the cache the
block belongs to.
Example 1
• Tag = 212/24
PROBLEM 2
• Advantage:
- writes occur at the speed of the cache
memory
-multiple writes within a block require only one
write to main memory
Disadvantage:
- harder to implement
-main memory is not always consistent with
cache
UPDATE POLICIES –
CONTD..
• Write-Allocate
• update the item in main memory and bring the block
containing the updated item into the cache.
• Write-Around or Write-no-allocate
• correspond to items not currently in the cache (i.e. write
misses) the item is updated in main memory only without
affecting the cache.
UPDATE POLICIES – CONTD..
• Write back:-Write only in cache, updating main
memory only at the time of replacement.
• Write through:-Both are updating for each write operation.
• Write-Allocate:- first in main memory, then copy the block
into cache.
• Write-Around or Write-no- when write miss
allocate:- occurred, updated in main without affecting the
memory cache.
REFERENCES