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.
Difference between Direct-mapping, Associative Mapping & Set-Associative Mapping- FAQs
What is the method of mapping the consecutive memory blocks?
The direct-mapping is the method of mapping the consecutive memory blocks.
How many sets are in direct-mapped cache?
In direct-mapped cache, as the block size is 4 bytes so there are 8 sets in it.
Which of the mapping techniques is the fastest?
Associative mapping is the fastest and flexible mapping technique.
Similar Reads
Difference between Direct-mapping, Associative Mapping & Set-Associative Mapping
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 requir
9 min read
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
Difference between Direct and Immediate Addressing Modes
Whenever an instruction is execute it require the operands to be operated own. The operand means the data. There are different ways of getting access to the operands. These are know as addressing modes. There are many methods of addressing modes but in this article, we will learn about direct and im
4 min read
Differences between Associative and Cache Memory
Memory system is the only subsystem of computer architecture that contributes a lot to the speed and efficiency of result processing. Among different types of memory that can be mentioned in discussions, associative and cache memories play crucial roles. Because associative memory is also called con
5 min read
Difference between Indirect and Immediate Addressing Modes
Prerequisite - Addressing Modes 1. Indirect Addressing Mode: In indirect addressing mode, the address field in the instruction points to the memory location or register where the effective address of the operand is present. Two memory accesses are required. One to get the value stored at the given a
2 min read
Difference between identifying and non-identifying relationships
In this article, we will discuss the overview of an entity and identifying and non-identifying relationships and finally will focus on differences between both of them. Let's discuss it one by one. Overview : Entity types that do not have their own key attributes are known as the weak entity type. U
7 min read
Difference between Abstract Data Types and Objects
1. Abstract data type (ADT) :An Abstract data type (ADT) is a mathematical model for data types. We can think of Abstract Data Type as a black box. As black box hides internal structure, ADT hides the design of data type. An abstract data type's behavior (semantics) can be specified from the perspec
2 min read
Difference between entity, entity set and entity type
The Entity-Relationship (ER) Model is one of the primary components of Database Management Systems and is very important for designing the logical structure of databases. It helps define data, and the relationship between the data entities and it makes the system easier to visualize. This is the rea
7 min read
Difference between Relational Algebra and Relational Calculus
Relational Algebra and Relational Calculus are two preliminarily important topics in the study of DBMS. Both are standardized query languages used for querying databases but the two have different approaches and operations. Relational Algebra is imperative as it deals mainly with how the data is to
6 min read
Difference between JOIN and UNION in SQL
Pre-requisites: JOIN, UNION JOIN in SQL is used to combine data from many tables based on a matched condition between them. The data combined using the JOIN statement results in new columns. Consider the two tables: Boys Girls Example: sql> SELECT Boys.Name, Boys.Age, Girls.Address, FROM Boys INN
2 min read
What is Mapping Cardinalities in ER Diagrams
Mapping Cardinalities in a Database Management System (DBMS) defines how entities (tables) interact and relate to each other, establishing crucial constraints for data relationships. By specifying the number of instances of one entity that can link to instances of another, they enable the design of
7 min read
Difference between E-R Model and Relational Model in DBMS
In database management systems (DBMS), two key methods are the Relational model and the Entity-Relationship (E-R) model. Each has a specific function in the development and operation of databases. While the Relational model provides the practical structure for organizing and managing data in relatio
4 min read
Differentiate between Write Through and Write Back Methods
Prerequisite - Write Through and Write Back in Cache During a read operation, when the CPU determines a word in the cache, the main memory is not included in the transfer. Thus, there are two ways that the system can proceed when the operation is a write. 1. Write Through Method : The simplest metho
2 min read
Difference between Entity and Object
When talking about databases and data modeling, it's important to understand the distinction between entities and objects. Both are necessary for a database management system's (DBMS) data administration and representation. An entity is a unique, recognizable real-world object or notion that is char
4 min read
Difference between Hierarchical and Relational Data Model
Database data models define the way data is structured, stored, and accessed. The Hierarchical Data Model and the Relational Data Model are two popular examples. The Hierarchical Data Model is the oldest model, it looks like a tree with parent-child relationships, while the Relational Data Model was
5 min read
Difference between Abstract and Concrete Data Structure
In this article we will discuss the differences between Abstract and Concrete data structure or type. Abstract Data Types(ADT) : It is a type (or class) of objects whose behaviour is defined by a set of values and a set of operations. The user interacts with the interface, using the operations that
2 min read
Classes of Functions | Injective, Surjective and Bijective Functions
Classes of Functions: Functions are fundamental in mathematics and have wide-ranging applications in engineering, computer science, and various other fields. Understanding the different classes of functions—injective, surjective, and bijective—is essential for analyzing mathematical models and solvi
6 min read
Cache Organization | Set 1 (Introduction)
Cache is close to CPU and faster than main memory. But at the same time is smaller than main memory. The cache organization is about mapping data in memory to a location in cache. A Simple Solution: One way to go about this mapping is to consider last few bits of long memory address to find small ca
3 min read
Inverse functions and composition of functions
Inverse Functions - In mathematics a function, a, is said to be an inverse of another, b, if given the output of b a returns the input value given to b. Additionally, this must hold true for every element in the domain co-domain(range) of b. In other words, assuming x and y are constants, if b(x) =
3 min read