IS CS-2018S2-04

题目来源Problem 4 日期:2024-08-10 题目主题:CS-计算机体系结构-缓存存储器

解题思路

这个问题涉及缓存存储器的设计和性能分析。我们需要:

  1. 计算不同缓存映射方案下的标记、索引和偏移的位长。
  2. 模拟不同缓存映射方案下的缓存命中情况,并展示缓存表项的变化。

关键点包括:

  • 理解全相联、二路组相联和直接映射的缓存组织方式
  • 计算标记、索引和偏移的位长
  • 模拟 LRU 替换算法
  • 注意 4 字节读操作的影响

Solution

1. Bit length calculation for different cache mapping schemes

Given:

  • 32-bit memory addressing
  • Cache capacity: bytes = 32 KB
  • Block size: 64 bytes

a) Fully Associative Cache

In a fully associative cache, there is no index, only tag and offset.

  • Offset: bits
  • Tag: bits
  • Index: 0 bits

b) Two-way Set-associative Cache

  • Offset: bits
  • Number of sets: sets
  • Index: bits
  • Tag: bits

c) Direct Mapping Cache

  • Offset: bits
  • Number of blocks: blocks
  • Index: bits
  • Tag: bits

Summary of bit lengths for each scheme

Mapping SchemeTag BitsIndex BitsOffset Bits
Full Associative2606
Two-Way Set-Associative1886
Direct Mapping1796

2. Cache hit analysis for different mapping schemes

Given:

  • Cache capacity: 64 bytes
  • Block size: 8 bytes
  • Cache lines: 8
  • 4-byte read operations
  • LRU replacement policy

Memory access sequence (in hexadecimal):

0x20, 0x48, 0x40, 0x4C, 0x58, 0x80, 0xB8, 0xC8, 0x40, 0x44, 0x48, 0x4C, 0x50, 0x54, 0x58, 0x30, 0x28

Now, let’s analyze each mapping scheme:

a) Fully Associative Cache

  • Offset: 3 bits
  • Tag: bits, or the first 5 bits of the address

Since 4-byte read operations are used, we need to consider the possibility of a read operation spanning two blocks.

The cache state after each access (LRU order from left to right):

Read Address (Hex)Read Address (Binary)Tag (Hex)Cache State (Tag) (8 lines, LRU)Evict?Cache Hit/Miss
0x200010 00000x40x4NoneMiss
0x480100 10000x90x4, 0x9NoneMiss
0x400100 00000x80x4, 0x9, 0x8NoneMiss
0x4C0100 11000x90x4, 0x8, 0x9NoneHit
0x580101 10000xB0x4, 0x8, 0x9, 0xBNoneMiss
0x801000 00000x100x4, 0x8, 0x9, 0xB, 0x10NoneMiss
0xB81011 10000x160x4, 0x8, 0x9, 0xB, 0x10, 0x16NoneMiss
0xC81100 10000x190x4, 0x8, 0x9, 0xB, 0x10, 0x16, 0x19NoneMiss
0x400100 00000x80x4, 0x9, 0xB, 0x10, 0x16, 0x19, 0x8NoneHit
0x440100 01000x80x4, 0x9, 0xB, 0x10, 0x16, 0x19, 0x8NoneHit
0x480100 10000x90x4, 0xB, 0x10, 0x16, 0x19, 0x8, 0x9NoneHit
0x4C0100 11000x90x4, 0xB, 0x10, 0x16, 0x19, 0x8, 0x9NoneHit
0x500101 00000xA0x4, 0xB, 0x10, 0x16, 0x19, 0x8, 0x9, 0xANoneMiss
0x540101 01000xA0x4, 0xB, 0x10, 0x16, 0x19, 0x8, 0x9, 0xANoneHit
0x580101 10000xB0x4, 0x10, 0x16, 0x19, 0x8, 0x9, 0xA, 0xBNoneHit
0x300011 00000x60x10, 0x16, 0x19, 0x8, 0x9, 0xA, 0xB, 0x60x4Miss
0x280010 10000x50x16, 0x19, 0x8, 0x9, 0xA, 0xB, 0x6, 0x50x10Miss

Number of cache hits: 7 (Address 0x4C, 0x40, 0x44, 0x48, 0x4C, 0x54, 0x58)

b) Two-way Set-associative Cache

  • Offset: 3 bits
  • Index: bits
  • Tag: bits, or the first 3 bits of the address

The cache state after each access (LRU order from left to right):

Read Address (Hex)Read Address (Binary)Tag (Hex)IndexSet 00Set 01Set 10Set 11Evict?Cache Hit/Miss
0x20001 00 0000x1000x1NoneMiss
0x48010 01 0000x2010x10x2NoneMiss
0x40010 00 0000x2000x1, 0x20x2NoneMiss
0x4C010 01 1000x2010x1, 0x20x2NoneHit
0x58010 11 0000x2110x1, 0x20x20x2NoneMiss
0x80100 00 0000x4000x2, 0x40x20x2100 00Miss
0xB8101 11 0000x5110x2, 0x40x20x2, 0x5NoneMiss
0xC8110 01 0000x6010x2, 0x40x2, 0x60x2, 0x5NoneMiss
0x40010 00 0000x2000x4, 0x20x2, 0x60x2, 0x5NoneHit
0x44010 00 1000x2000x4, 0x20x2, 0x60x2, 0x5NoneHit
0x48010 01 0000x2010x4, 0x20x6, 0x20x2, 0x5NoneHit
0x4C010 01 1000x2010x4, 0x20x6, 0x20x2, 0x5NoneHit
0x50010 10 0000x2100x4, 0x20x6, 0x20x20x2, 0x5NoneMiss
0x54010 10 1000x2100x4, 0x20x6, 0x20x20x2, 0x5NoneHit
0x58010 11 0000x2110x4, 0x20x6, 0x20x20x5, 0x2NoneHit
0x30001 10 0000x1100x4, 0x20x6, 0x20x2, 0x10x5, 0x2NoneMiss
0x28001 01 0000x1010x2, 0x10x6, 0x20x2, 0x10x5, 0x2100 00Miss

Number of cache hits: 6 (Address 0x4C, 0x40, 0x44, 0x48, 0x4C, 0x54)

c) Direct Mapping Cache

  • Offset: 3 bits
  • Index: bits
  • Tag: bits, or the first 2 bits of the address

Cache state after each access:

Read Address (Hex)Read Address (Binary)Tag (Hex)Index000001010011100101110111Evict?Cache Hit/Miss
0x2000 100 0000x01000NoneMiss
0x4801 001 0000x100110NoneMiss
0x4001 000 0000x1000110NoneMiss
0x4C01 001 1000x1001110NoneHit
0x5801 011 0000x10111110NoneMiss
0x8010 000 0000x2000211001 000Miss
0xB810 111 0000x211121102NoneMiss
0xC811 001 0000x30012310201 001Miss
0x4001 000 0000x10001310210 000Miss
0x4401 000 1000x100013102NoneHit
0x4801 001 0000x10011110211 001Miss
0x4C01 001 1000x100111102NoneHit
0x5001 010 0000x1010111102NoneMiss
0x5401 010 1000x1010111102NoneHit
0x5801 011 0000x1011111102NoneHit
0x3000 110 0000x01101111002NoneMiss
0x2800 101 0000x010111110002NoneMiss

Number of cache hits: 6 (Address 0x4C, 0x44, 0x4C, 0x54, 0x58)

知识点

缓存存储器缓存映射缓存替换算法计算机体系结构CacheLRU

难点思路

  1. 理解不同缓存映射方案的特点和区别。
  2. 正确计算标记、索引和偏移的位长。
  3. 模拟 LRU 替换算法的过程,特别是在二路组相联缓存中。
  4. 注意 4 字节读操作可能跨越缓存块边界的情况。

解题技巧和信息

  1. 缓存映射方案的特点:

    • 全相联:灵活性最高,但硬件复杂度高
    • 直接映射:硬件简单,但冲突概率高
    • 组相联:平衡了灵活性和硬件复杂度
  2. 位长计算:

    • 偏移位 = (块大小)
    • 索引位 = (组数)
    • 标记位 = 地址总位数 - 索引位 - 偏移位
  3. LRU 替换算法:

    • 记录每个缓存块的使用时间
    • 替换最长时间未使用的块
  4. 缓存命中判断:

    • 检查标记是否匹配
    • 考虑 4 字节读操作可能跨越的块
  5. 性能分析:

    • 缓存命中率 = 缓存命中次数 / 总访问次数

重点词汇

cache memory 缓存存储器

fully associative 全相联

set-associative 组相联

direct mapping 直接映射

tag 标记

index 索引

offset 偏移

LRU (Least Recently Used) 最近最少使用

cache hit 缓存命中

cache miss 缓存缺失

replacement policy 替换策略

参考资料

  1. Patterson, D. A., & Hennessy, J. L. (2011). Computer Organization and Design: The Hardware/Software Interface. Morgan Kaufmann. Chapter 5: Memory Hierarchy Design.
  2. Stallings, W. (2015). Computer Organization and Architecture: Designing for Performance. Pearson. Chapter 4: Cache Memory.

请参考这份解析。这份解析不一定对,请小心