Difference between Direct-mapping, Associative Mapping & Set-Associative Mapping
Last Updated :
26 Sep, 2024
Cache memory stores frequently accessed data for easy retrieval. When Read request is received from the CPU, the contents of block of memory words containing the location which is specified are transferred into the cache. The cache memory can store a number of blocks. The mapping technique is required to bring the data of main memory blocks into the cache block. This article discusses about the three mapping techniques and its difference.
What is Cache ?
The small section of SRAM memory, added between main memory and processor(CPU) to speed up the process of execution, is known as cache memory. It includes a small amount of SRAM & more amount of DRAM. It is a high speed & expensive memory.
Cache hit ratio: It measures how effectively the cache fulfills the request for getting content.
Cache hit ratio = No of cache hits/ (No of cache hits + No. of cache Miss)
If data has been found in the cache, it is a cache hit else a cache miss.
Prerequisite - Cache mapping Types - Direct-mapping, Associative Mapping & Set-Associative Mapping
What is Cache Mapping?
The process /technique of bringing data of main memory blocks into the cache block is known as Cache Mapping.
The mapping techniques can be classified as :
- Direct Mapping
- Associative
- Set-Associative
What is Direct Mapping?
Direct Mapping:
Each block from main memory has only one possible place in the cache organization in this technique.
For example : every block i of the main memory can be mapped to block j of the cache using the formula :
j = i modulo m
Where : i = main memory block number
j = cache block number
m = number of blocks in the cache
The address here is divided into 3 fields :
i)Tag
ii)Block
iii)Word.
To map the memory address to cache: The BLOCK field of the address is used to access the cache's BLOCK. Then, the tag bits in the address is compared with the tag of the block. For a match, a cache hit occurs as the required word is found in the cache. Otherwise, a cache miss occurs and the required word has to be brought into the cache from the Main Memory. The word is now stored in the cache together with the new tag (old tag is replaced).
Example:
If we have a fully associative mapped cache of 8 KB size with block size = 128 bytes and say, the size of main memory is = 64 KB. (Assuming word size = 1 byte) Then :
Number of bits for the physical address = 16 bits (as memory size = 64 KB = 26 × 210 = 216)
Number of bits for WORD = 7 bits (as block size = 128 bytes = 27)
No of Index bits = 13 bits (as cache size = 8 KB = 23 × 210 = 213)
No of BLOCK bits = Number of Index bits- Number of bits for WORD = 13 – 7 = 6bits
OR
(No of cache Blocks = Cache size/block size = 8 KB / 128 Bytes = 8×1024 Bytes/128 Bytes = 26 blocks → 6bits)
No of TAG bits = Number of bits for the physical address — Number of bits in Index = 16-13 = 3 bits
Advantages of Direct-mapping
- It requires very less hardware complexity.
- It is a very simple method of implementation.
- Access time is very low.
Disadvantages of Direct-mapping
- Cache performance is unpredictable in direct mapping.
- Handling of spatial locality is poor.
- Use of cache space is inefficient.
- Conflict misses are high.
What is Associative Mapping?
In the associate mapping of the main memory block can be done with any of the cache block. This technique is called as fully associative cache mapping. The memory address has only 2 fields
i)word
ii)tag.
Example:
If we have a fully associative mapped cache of 8 KB size with block size = 128 bytes and say, the size of main memory is = 64 KB. Then:
Number of bits for the physical address = 16 bits (as memory size = 64 KB = 26 × 210 = 216)
Number of bits in block offset = 7 bits (as block size = 128 bytes = 27)
No of tag bits = Number of bits for the physical address - Number of bits in block offset = 16-7 = 9 bits
No of cache Blocks = Cache size/block size = 8 KB / 128 Bytes = 8×1024 Bytes/128 Bytes = 26 blocks.
Advantages of Associative Mapping &
- It is the fastest and most flexible technique.
- It solves the conflict miss problem
Disadvantages of Associative Mapping &
- The comparison time for search a block is very high.
- It is expensive.
What is Set-Associative Mapping?
It is the combination of advantages of both direct & associative mapping.
Here, the cache consists of a number sets, each of which consists of a number of blocks. The relationships are :
n = w * L
i = j modulo w
where
i : cache set number
j : main memory block number
n : number of blocks in the cache
w : number of sets
L : number of lines in each set
This is referred to as L-way set-associative mapping. Block Bj can be translated into any of the blocks in set j using this mapping.
To map the memory address to cache: Using set field in the memory address, we access the particular set of the cache. Then, the tag bits in the address are compared with the tag of all L blocks within that set. For a match, a cache hit occur as the required word is found in the cache. Otherwise, a cache miss occurs and the required word has to be brought in the cache from the Main Memory. According to the replacement policy used, a replacement is done if the cache is full.
Example:
If we have a fully associative mapped cache of 8 KB size with block size = 128 bytes and say, the size of main memory is = 64 KB, and we have “2-way” set-associative mapping (Assume each word has 8 bits). Then :
Number of bits for the physical address = 16 bits (as memory size = 64 KB = 26 * 210 = 216)
No of cache Blocks = Cache size/block size = 8 KB / 128 Bytes = 8×1024 Bytes/128 Bytes = 26 cache blocks.
No of Main Memory Blocks = MM size/block size = 64 KB / 128 Bytes = 64×1024 Bytes/128 Bytes = 29 MM blocks.
No of sets of size 2 = No of Cache Blocks/ L = 26/2 = 25 cache sets.(L = 2 as it is 2-way set associative mapping)
Advantages of Set-Associative Mapping
- It has highest hit rate.
- Conflict Misses are very few.
Disadvantages of Set-Associative Mapping
- It is very expensive.
- It is slower than direct mapping.
Difference between Direct-mapping, Associative Mapping & Set-Associative Mapping
Direct-mapping | Associative Mapping | Set-Associative Mapping |
Needs only one comparison because of using direct formula to get the effective cache address. | Needs comparison with all tag bits, i.e., the cache control logic must examine every block’s tag for a match at the same time in order to determine that a block is in the cache/not. | Needs comparisons equal to number of blocks per set as the set can contain more than 1 blocks. |
Main Memory Address is divided into 3 fields : TAG, BLOCK & WORD. The BLOCK & WORD together make an index. The least significant WORD bits identify a unique word within a block of main memory, the BLOCK bits specify one of the blocks and the Tag bits are the most significant bits. | Main Memory Address is divided into 1 fields : TAG & WORD. | Main Memory Address is divided into 3 fields : TAG, SET & WORD. |
There is one possible location in the cache organization for each block from main memory because we have a fixed formula. | The mapping of the main memory block can be done with any of the cache block. | The mapping of the main memory block can be done with a particular cache block of any direct-mapped cache. |
If the processor need to access same memory location from 2 different main memory pages frequently, cache hit ratio decreases. | If the processor need to access same memory location from 2 different main memory pages frequently, cache hit ratio has no effect. | In case of frequently accessing two different pages of the main memory if reduced, the cache hit ratio reduces. |
Search time is less here because there is one possible location in the cache organization for each block from main memory. | Search time is more as the cache control logic examines every block’s tag for a match. | Search time increases with number of blocks per set. |
The index is given by the number of blocks in cache. | The index is zero for associative mapping. | The index is given by the number of sets in cache. |
It has least number of tag bits. | It has the greatest number of tag bits. | It has less tags bits than associative mapping and more tag bits than direct mapping. |
Advantages- - Simplest type of mapping
- Fast as only tag field matching is required while searching for a word.
- It is comparatively less expensive than associative mapping.
| Advantages- - It is fast.
- Easy to implement
| Advantages- - It gives better performance than the direct and associative mapping techniques.
|
Disadvantages- - It gives low performance because of the replacement for data-tag value.
| Disadvantages- - Expensive because it needs to store address along with the data.
| Disadvantages- - It is most expensive as with the increase in set size cost also increases.
|
Conclusion
The cache memory mapping techniques which are used to transfer the data form memory block to cache block in the request of CPU. The mapping of memory block with cache block can be done in three ways such as Direct Mapping, Associative Mapping, Set-Associative Mapping. Each has different set of mapping techniques and their address structure is also different.
Similar Reads
Difference between Direct and Implied Addressing Modes
Prerequisite - Addressing Modes 1. Direct Addressing Mode : In direct addressing mode, the address field contains the address of the operand. Effective Address(EA) = address field of operand Example - Add the contents of register A to the accumulator. ADD A Look in memory at address A for operand. O
2 min read
Difference between Direct and Indirect Addressing Modes
In computer programming, addressing modes help the CPU find data in memory. Two common types are direct addressing and indirect addressing. Direct addressing gives the exact location of the data, making it quick and easy to access. Indirect addressing, on the other hand, uses a pointer to find the d
6 min read
Direct Memory Access (DMA) Controller in Computer Architecture
In modern computer systems, transferring data between input/output devices and memory can be a slow process if the CPU is required to manage every step. To address this, a Direct Memory Access (DMA) Controller is utilized. A Direct Memory Access (DMA) Controller solves this by allowing I/O devices t
5 min read
Map Abstract Data Type
ADT stands for Abstract data type. This data type is defined by the programmer. Abstract data type contains a set of well-defined values and operations. ADT acts as an asset in programming as its implementation can be changed without modifying the rest of the program. With the help of an abstract da
7 min read
Solidity - Mappings
Mapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types but reference types are not allowed while the value can be of any type. Mappings are mostly used to associate t
4 min read
Difference between pair in Multiset and Multimap in C++ STL
Pairs in C++: The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as âfirstâ and the second element as âsecondâ and the order is fixed (first, second). Pair is used to combine together two values which
5 min read
Introduction to Map â Data Structure and Algorithm Tutorials
Maps is also known as dictionaries or associative arrays, are fundamental data structures that allow you to efficiently store and retrieve data based on unique keys. This tutorial will cover the basics of maps, including their main ideas, how they are used in different programming languages, and how
15+ min read
Map Interface in Java
In Java, the Map Interface is part of the java.util package and represents a mapping between a key and a value. The Java Map interface is not a subtype of the Collections interface. So, it behaves differently from the rest of the collection types.Key Features:No Duplicates in Keys: Keys should be un
11 min read
Map of Sets in C++ STL with Examples
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The
2 min read
Difference between Association and Aggregation
Prerequisite - Association, Composition and Aggregation in Java Association: An association is defined as an organization of people with a common purpose and having a formal structure. It represents a binary relationship between two objects that describes an activity. It is a relationship between ob
1 min read