📜  直接映射,关联映射和集合关联映射之间的区别

📅  最后修改于: 2021-06-28 16:02:16             🧑  作者: Mango

先决条件–缓存映射类型–直接映射,关联映射和集合关联映射

快取:
SRAM存储器的一小部分被添加到主存储器和处理器(CPU)之间以加速执行过程,被称为高速缓存存储器。它包括少量的SRAM和更多的DRAM。这是一个高速且昂贵的内存。

缓存命中率衡量缓存如何有效地满足获取内容的请求的度量。

Cache hit ratio = No of cache hits/ (No of cache hits + No. of cache Miss)

如果已在缓存中找到数据,则为缓存命中,否则为缓存未命中。

缓存映射:
将主内存块的数据放入缓存块的过程/技术称为缓存映射。
映射技术可以分类为:

  1. 直接映射
  2. 联想的
  3. 集合关联

1.直接映射:
在这种技术中,主存储器中的每个块在缓存组织中仅具有一个可能的位置。
例如:可以使用以下公式将主存储器的每个块i映射到缓存的块j:

j = i modulo m
Where : i = main memory block number
       j = cache block number
       m = number of blocks in the cache

这里的地址分为3个字段:标签,块和字。

要将内存地址映射到缓存
地址的BLOCK字段用于访问缓存的BLOCK。然后,将地址中的标记位与块的标记进行比较。对于匹配项,由于在缓存中找到了所需的单词,因此发生了缓存命中。否则,将发生高速缓存未命中,并且必须从主存储器将所需的字带入高速缓存中。现在,单词与新标记一起存储在高速缓存中(替换了旧标记)。

例子 –
如果我们有一个8 KB大小的完全关联的映射缓存,块大小= 128字节,并且说主内存的大小是= 64 KB。 (假设字大小= 1个字节)然后:

物理地址的位数= 16位(因为存储器大小= 64 KB = 2 6 ×2 10 = 2 16)
WORD的位数= 7位(因为块大小= 128字节= 2 7 )
索引位数= 13位(因为高速缓存大小= 8 KB = 2 3 ×2 10 = 2 13)
块位数=索引位数-WORD的位数= 13 – 7 = 6位

或者
(缓存块数=缓存大小/块大小= 8 KB / 128字节= 8×1024字节/ 128字节= 2 6个块→6位)
TAG位的数量=物理地址的位数–索引中的位数= 16-13 = 3位

2.关联映射:
此处,可以使用任何高速缓存块完成主存储块的映射。此处的存储地址只有2个字段:单词和标签。此技术称为完全关联的缓存映射。

例子 –
如果我们有一个8 KB大小的完全关联的映射缓存,块大小= 128字节,并且说主内存的大小是= 64 KB。然后 :
物理地址的位数= 16位(因为存储器大小= 64 KB = 2 6 ×2 10 = 2 16)
块偏移量中的位数= 7位(因为块大小= 128字节= 2 7 )
标签位数=物理地址的位数–块偏移量的位数= 16-7 = 9位
缓存块数=缓存大小/块大小= 8 KB / 128字节= 8×1024字节/ 128字节= 2 6个块。

3. Set –关联映射:
它是直接映射和关联映射的优点的结合。
此处,高速缓存由多个数字集组成,每个数字集均由多个块组成。关系是:

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

这被称为L向集合关联映射。使用此映射,可以将块Bj转换为集合j中的任何块。

要将内存地址映射到缓存–
使用内存地址中的set字段,我们访问缓存的特定集合。然后,将地址中的标记位与该组内所有L个块的标记进行比较。对于匹配项,由于在缓存中找到了所需的单词,因此发生了缓存命中。否则,将发生高速缓存未命中,并且必须从主存储器将所需的字带入高速缓存中。根据所使用的替换策略,如果缓存已满,则会进行替换。

示例:如果我们有一个8 KB大小的完全关联的映射缓存,块大小= 128字节,并且说主内存的大小= = 64 KB,并且我们有“ 2-way”集合关联映射(假设每个单词都有8位)。然后 :

物理地址的位数= 16位(因为内存大小= 64 KB = 2 6 * 2 10 = 2 16)
缓存块数=缓存大小/块大小= 8 KB / 128字节= 8×1024字节/ 128字节= 2 6个缓存块。
主内存块数= MM大小/块大小= 64 KB / 128字节= 64×1024字节/ 128字节= 2 9 MM块。
大小集的数量2 =缓存块的数量/ L = 2 6/2 = 2 5个缓存集。(L = 2,因为它是2路集关联映射)

直接映射,关联映射和集合关联映射之间的区别:

  Direct-mapping Associative Mapping Set-Associative Mapping
1. 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. 
2. Main Memory Address is divided into 3 fields : TAG, BLOCK & WORD. The BLOCK & WORD together make an index. The least significant TAG 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.
3. 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.
4. 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.
5. 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.