1 / 60

高等计算机系统结构

高等计算机系统结构. 高速缓冲存储器. (第七讲). 程 旭. 2011 年 4 月 25 日. Proc 60%/yr. CPU. Processor-Memory Performance Gap: (grows 50% / year). DRAM 7%/yr. DRAM. 微处理器 -DRAM 的延迟差距. 1000. 100. Performance. 10. 1. 1980. 1981. 1982. 1983. 1984. 1985. 1986. 1987. 1988. 1989. 1990. 1991. 1992.

mieko
Download Presentation

高等计算机系统结构

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 高等计算机系统结构 高速缓冲存储器 (第七讲) 程 旭 2011年4月25日

  2. Proc 60%/yr. CPU Processor-Memory Performance Gap:(grows 50% / year) DRAM 7%/yr. DRAM 微处理器-DRAM的延迟差距 1000 100 Performance 10 1 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 Time • 1980: no cache in µproc; 1995 2-level cache on chip (1989 first Intel µproc with a cache on chip)

  3. 削减 处理器-存储器性能差距 处理器 面积比 晶体管数比 (成本) (功率) • Alpha 21164 37% 77% • StrongArm SA110 61% 94% • Pentium Pro 64% 88% • 每个封装体 两个2芯片(dies):Proc/I$/D$ + L2$ • Cache本身并没有特殊的内在意义,它仅是缩小处理器-存储器之间性能差距的一种手段

  4. Alpha微处理器 • Time of a full cache miss in instructions executed: 1st Alpha : 340 ns/5.0 ns = 68 clks x 2 or 136 2nd Alpha : 266 ns/3.3 ns = 80 clks x 4 or 320 3rd Alpha : 180 ns/1.7 ns =108 clks x 6 or 648 • 1/2X latency x 3X clock rate x 3X Instr/clock  ?X

  5. 存储层次设计的四个问题 • Q1: 信息块可以放在高层的哪里? (Block placement) • 全相联、组相联、直接映射 • Q2: 如果信息块在高层,那么如何找到它?(Block identification) • 标记/信息块 • Q3: 在失效时,应该替换掉哪个信息块?(Block replacement) • 随机、 LRU、FIFO • Q4: 在写操作时,会发生什么情况 (Write strategy) • 回写(Write Back)或 直写(Write Through) (使用写缓冲器)

  6. Cache性能 CPU time = (CPU execution clock cycles + Memory stall clock cycles)  clock cycle time Memory stall clock cycles = (Reads  Read miss rate  Read miss penalty + Writes  Write miss rate  Write miss penalty) Memory stall clock cycles = Memory accesses  Miss rate  Miss penalty

  7. Cache 性能(续) CPUtime = Instruction Count  (CPIexecution + Mem accesses per instruction  Miss rate  Miss penalty)  Clock cycle time Misses per instruction = Memory accesses per instruction  Miss rate CPUtime = IC  (CPIexecution + Misses per instruction  Miss penalty)  Clock cycle time Memory stall cycles per instruction = Misses per instruction  (Total miss latency – Overlapped miss latency) Average Memory access time = Hit time + Miss rate  Miss penalty

  8. 改进Cache性能 Average Memory access time = Hit time + Miss rate  Miss penalty 1. 降低失效率 2. 降低失效损失,或者 3. 减少在cache中命中的时间

  9. 降低失效 • 对失效进行分类: 3 Cs • Compulsory 第一次访问一个不在cache中的数据块,该块必须被调入。也称为 cold start misseso或first reference misses。 (即使Cache无穷大,也会失效) • Capacity在程序执行中,cache不能存放其所需的所有数据块,就会先放弃一些块然后再找回,这就出现了 capacity misses。(有限大小的全相联Cache也会出现的失效) • Conflict 如果采用组相联或直接映射的策略,除了义务失效和容量失效,还会因为有太多块要同时映射到同一组中,就会先放弃一些块然后再找回,这就出现了 conflict misses 。也称为 collision misses或 interference misses。(有限大小的 N路组相联Cache中出现的失效)

  10. 3Cs的绝对失效率 (SPEC92) Conflict 义务失效率非常低

  11. 2:1 Cache规律 miss rate 1-way associative cache size X = miss rate 2-way associative cache size X/2 Conflict

  12. 3Cs 的相对失效率 Conflict Flaws: for fixed block size Good: insight => invention

  13. 如何能减少失效? • 3 Cs: Compulsory, Capacity, Conflict • 在所有情况,假设总的cache大小不变: • 在下列情况,会发生什么变化: 1) 改变块大小: 3Cs中哪些失效会受到明显影响? 2) 改变相联度: 3Cs中哪些失效会受到明显影响? 3) 改变编译器: 3Cs中哪些失效会受到明显影响?

  14. 1. 通过增大块大小来减少失效

  15. 2. 通过增大相联度来减少失效 • 8路组相联实际上在减少Miss rate方面与全相效果接近。 • 2:1 Cache规律: • Miss Rate DM cache size N = Miss Rate 2-way cache size N/2 • 小心:执行时间是唯一最终度量标准! • 是否时钟周期时间会增加? • Hill [1988]的研究表明2-way cache的命中时间比1-way 外部cache的时间会 +10%, 比内部cache的时间会 + 2% • 通过增加Cache容量来减少失效。

  16. 示例:平均存储器访问时间 与 失效率 • 假设与直接映射的时钟周期时间相比 对2路cache为1.10、对4路1.12、对8路 1.14 Cache Size Associativity (KB) 1-way 2-way 4-way 8-way 1 2.33 2.15 2.07 2.01 2 1.98 1.86 1.76 1.68 4 1.72 1.67 1.61 1.53 8 1.46 1.48 1.47 1.43 16 1.29 1.32 1.32 1.32 32 1.20 1.24 1.25 1.27 64 1.14 1.20 1.21 1.23 128 1.10 1.17 1.18 1.20 (红 表示 平均存储器访问时间 没有 被更高的相联度减少) 假设失效损失为10周期

  17. 3. 使用Victim Cache来减少失效 • 如何结合直接映射的高速命中时间,而又避免冲突失效? • 增加一个存放从cache中放弃数据的缓冲器(全相联cache) • Jouppi [1990]: 对于4KB的直接映射数据cache,4-entry victim cache可以消除 20% 至95%的冲突。 • 在Alpha、HP等中使用

  18. DATA TAGS One Cache line of Data Tag and Comparator One Cache line of Data Tag and Comparator One Cache line of Data Tag and Comparator One Cache line of Data Tag and Comparator To Next Lower Level In Hierarchy Victim Cache

  19. 4. 通过伪相联减少失效 • 如何结合直接映射的快速命中时间和两路组相联cache的低冲突失效的优势? • 分解cache: 在失效时,检查cache的另一半看是否有所需信息,如果有称为 伪命中(pseudo-hit)(慢命中) • 缺点:如果命中需要1或2个周期,那么CPU难以流水 • 适用于不与处理器直接连接的cache(二级cache) • 用于MIPS R1000和UltraSPARC的二级cache。 • 路预测(Way Prediction) 命中时间 失效损失 伪命中时间 时间

  20. 5. 通过 硬件 预取指令和数据减少失效 • 例如,指令预取 • Alpha 21064在失效时取2个信息块 • 额外的块放置在 流缓冲器(stream buffer)中 • 失效时,检测流缓冲器 • 对于数据块也可使用上述策略 • Jouppi [1990] 对于4KB cache,1个数据流缓冲器可以减少25%的损失;4个流缓冲器,减少43% • Palacharla & Kessler [1994] 对于科学计算程序,对于两个64KB,四路组相联cache,8个流缓冲器减少 50%至70%的失效 • 采用预测策略的前提是具有额外的存储带宽,它的使用没有“其他破坏”代价

  21. 6. 通过 软件 预取数据减少失效 • 数据预取 • 将数据装入寄存器 (HP PA-RISC loads) • Cache预取: 装入cache(MIPS IV, PowerPC, SPARC v. 9) • 不会产生故障的特殊预取指令;一种推测式执行 • 发射预取指令需要时间 • 发射预取指令的开销 是否小于 减少失效的收益? • 超标量的能力越强 越可以减小 发射带宽的难度

  22. 7. 通过编译优化减少失效 • McFarling [1989] 对于块大小为4字节的8KB直接映射cache,软件可以75% 的失效 • 指令 • 对存储访问重排序,因而可以减少冲突失效 • 进行剖视(Profiling)来观测冲突(使用他们开发的工具) • 数据 • 合并数组(data merge):通过将两个独立数组合并为一个复合元素的数组来改进空间局部性 • 循环交换(loop interchange): 通过改变循环嵌套来按序访问存储器中存储的数据 • 循环合并(loop fusion): 将两个具有相同循环类型且有一些变量重叠的独立循环合并 • 块化(blocking):通过不断使用一些数据块(而不是完整地遍历一行和一列)来改进时间局部性

  23. 合并数据的示例 /* Before: 2 sequential arrays */ int val[SIZE]; int key[SIZE]; /* After: 1 array of stuctures */ struct merge { int val; int key; }; struct merge merged_array[SIZE]; 减少 val和 key之间的冲突 改进空间局部性

  24. 循环交换示例 /* Before */ for (k = 0; k < 100; k = k+1) for (j = 0; j < 100; j = j+1) for (i = 0; i < 5000; i = i+1) x[i][j] = 2 * x[i][j]; /* After */ for (k = 0; k < 100; k = k+1) for (i = 0; i < 5000; i = i+1) for (j = 0; j < 100; j = j+1) x[i][j] = 2 * x[i][j]; 用顺序访问代替跳步(100个存储字)访问存储器 改进空间局部性

  25. 循环合并示例 /* Before */ for (i = 0; i < N; i = i+1) for (j = 0; j < N; j = j+1) a[i][j]= 1/b[i][j] * c[i][j]; for (i = 0; i < N; i = i+1) for (j = 0; j < N; j = j+1) d[i][j] = a[i][j]+ c[i][j]; /* After */ for (i = 0; i < N; i = i+1) for (j = 0; j < N; j = j+1) { a[i][j] = 1/b[i][j] * c[i][j]; d[i][j] = a[i][j] + c[i][j];} 每次对a & c访问两次失效 与 每次访问一次失效; 改进空间局部性

  26. 分块示例 /* Before */ for (i = 0; i < N; i = i+1) for (j = 0; j < N; j = j+1) {r = 0; for (k = 0; k < N; k = k+1){ r = r + y[i][k]*z[k][j];}; x[i][j] = r; }; • 两个内层循环: • 读取z[]的所有 NN个元素 • 分别读取y[]一行的N个元素 • 写x[]一行的N个元素 • 容量失效是 N和Cache容量的函数: • 3 N  N  4 => 无容量失效;否则 ... • 思路:计算满足条件的 B  B子阵

  27. 分块示例(续) /* After */ for (jj = 0; jj < N; jj = jj+B) for (kk = 0; kk < N; kk = kk+B) for (i = 0; i < N; i = i+1) for (j = jj; j < min(jj+B-1,N); j = j+1) {r = 0; for (k = kk; k < min(kk+B-1,N); k = k+1) { r = r + y[i][k]*z[k][j];}; x[i][j] = x[i][j] + r; }; • B 称为 块化因子(Blocking Factor) • 容量失效从2N3 + N2减至 2N3/B +N2 • 是否也会降低冲突失效?

  28. 通过分块减少冲突失效 • 没有全相联的cache的冲突失效 与 块化大小 • Lam et al [1991] a blocking factor of 24 had a fifth the misses vs. 48 despite both fit in cache MISS RATIO

  29. 编译优化减少cache失效 小结

  30. 总 结 • 3 Cs: Compulsory, Capacity, Conflict • 降低失效率 1. 通过增大块大小减少失效 2.通过增大相联度减少失效 3.通过Victim Cache减少失效 4.通过伪-相联减少失效 5.通过硬件预取指令或数据减少失效 6.通过软件预取数据减少失效 7.通过编译优化减少失效 • 注意:在评价性能时仅仅侧重于某一个参数是危险的

  31. 改进cache性能(续) Average Memory access time = Hit time + Miss rate  Miss penalty 1. 降低失效率 2. 降低失效损失,或者 3. 减少在cache中命中的时间

  32. 1. 减少失效损失: 在失效时读比写优先 • Write through with write buffers offer RAW conflicts with main memory reads on cache misses • If simply wait for write buffer to empty, might increase read miss penalty (old MIPS 1000 by 50% ) • Check write buffer contents before read; if no conflicts, let the memory access continue • Write Back? • Read miss replacing dirty block • Normal: Write dirty block to memory, and then do the read • Instead copy the dirty block to a write buffer, then do the read, and then do the write • CPU stall less since restarts as soon as do read

  33. CPU in out write buffer DRAM (or lower mem) 1.1 在失效时读比写优先 1.2 Merging Write Buffer Write Buffer

  34. 2. 减少失效损失: 子块放置 • Don’t have to load full block on a miss • Have valid bitsper subblock to indicate valid • (Originally invented to reduce tag storage) Subblocks Valid Bits

  35. 3. 减少失效损失: 提前重启和关键字先送 • Don’t wait for full block to be loaded before restarting CPU • Early restartAs soon as the requested word of the block arrives, send it to the CPU and let the CPU continue execution • Critical Word FirstRequest the missed word first from memory and send it to the CPU as soon as it arrives; let the CPU continue execution while filling the rest of the words in the block. Also called wrapped fetch and requested word first • Generally useful only in large blocks, • Spatial locality a problem; tend to want next sequential word, so not clear if benefit by early restart block

  36. 4. 减少失效损失: 用Non-blocking Caches来减少失效时暂停 • Non-blocking cacheor lockup-free cacheallow data cache to continue to supply cache hits during a miss • requires out-of-order executuion CPU • hit under miss reduces the effective miss penalty by working during miss vs. ignoring CPU requests • hit under multiple miss or miss under miss? may further lower the effective miss penalty by overlapping multiple misses • Significantly increases the complexity of the cache controller as there can be multiple outstanding memory accesses • Requires muliple memory banks (otherwise cannot support) • Penium Pro allows 4 outstanding memory misses

  37. 对SPEC,失效下命中的情况 • 浮点程序的平均: AMAT= 0.68  0.52  0.34  0.26 • 整数程序的平均: AMAT= 0.24  0.20  0.19  0.19 • 8 KB Data Cache、直接映射、32B 数据块、 失效需要16周期 0->1 1->2 2->64 Base n次失效下命中 Integer Floating Point

  38. 5.二级cache • 二级cache的计算公式 AMAT = Hit TimeL1 + Miss RateL1 x Miss PenaltyL1 Miss PenaltyL1 = Hit TimeL2 + Miss RateL2 x Miss PenaltyL2 AMAT = Hit TimeL1 +Miss RateL1x (Hit TimeL2 +Miss RateL2+ Miss PenaltyL2) 定义: • 局部失效率 该cache的失效次数除以对 该级cache进行的总的存储访问次数 (Miss rateL2) • 总失效率 该cache的失效次数除以 CPU产生的 总的存储器访问次数 (Miss RateL1 x Miss RateL2) • 总失效率是我们真正关心的

  39. 局部和全局失效率的比较 • 一级cache:32 KByte;增加二级cache • 总失效率接近于二级Cache的单级cache失效率 使得 二级 >> 一级(大小) • 对二级Cache不要使用局部失效率 • 二级cache与CPU时钟周期无关! • 成本 和 平均存储访问时间 • 通常,快命中时间 和 更少的失效 • 由于命中增多,目标失效减少 Linear Cache Size Log Cache Size

  40. 减少失效损失:哪些适用于二级 Cache? • 降低失效率 1. 通过增大块大小减少失效 2.通过增大相联度减少失效 3.通过Victim Cache减少失效 4.通过伪-相联减少失效 5.通过硬件预取指令或数据减少失效 6.通过软件预取数据减少失效 7.通过编译优化减少失效

  41. 二级cache 块大小 和平均存储器访问时间 • 第一级32KB , 与存储器的通路8字节宽

  42. 减少失效损失小结 • 五种技术 • 失效时,读比写优先;合并写缓存 • 子块放置 • 失效时,提前重启和关键存储字先送 • 非阻塞Cache (Hit under Miss, Miss under Miss) • 二级Cache • 可适用于多级cache • 问题:到DRAM的时间可能随着cache的级数而增长 • 乱序执行CPU可以隐藏第一级数据cache的失效,但在第二级cache失效时会暂停

  43. Cache优化小结 技术 MR MP HT Complexity 增大块大小 + - 0增高相联度 + - 2淘汰块Cache + 2伪相联Cache + 2指令/数据的硬件预取 + 2编译控制的预取 + 3编译减少失效 + 0 读失效优先 + 1子块放置 + + 1提前重启和关键存储字优先 + 2非阻塞Cache + 3二级Cache + 2 失效率 失效损失

  44. 改进Cache性能 Average Memory access time = Hit time + Miss rate  Miss penalty 1. 降低失效率 2. 降低失效损失,或者 3. 减少在cache中命中的时间

  45. 1. 通过小、简单的Cache来加快命中时间 • 为什么Alpha 21164设置8KB指令cache和 8KB数据cache + 96KB二级cache? • 小数据cache和时钟频率 • 片载cache直接映射

  46. 2. 通过避免地址变换加快命中 • 将虚拟地址送给cache? 称为 虚拟地址cache(Virtually Addressed Cache)或者 虚拟Cache(Virtual Cache)性对于 物理cache(Physical Cache) • 每次进程间的逻辑切换都必须冲洗cache; 否则将会发生错误命中 • 代价是 冲洗时间 + 空cache的义务失效 • 需要处理处理 别名(aliases)(也称为 化名(synonyms)); 两个不同的虚拟地址映射到同一物理地址 • I/O必然与cache相互影响,因而需要虚拟地址 • 处理别名的策略 • 硬件保证 Index域和直接映射,它们都是唯一的。 • 称为“页面染色(page coloring)” • 冲洗cache的解决策略 • 增加进程标识符(process identifier tag):与进程内的地址一起还标识进程本身:如果进程错误就不会命中

  47. 虚拟地址Caches CPU CPU CPU VA VA VA VA Tags PA Tags $ TB $ TB VA PA PA L2 $ TB $ MEM PA PA MEM MEM Cache访问与虚拟地址变换重叠:需要cahe索引来保持变换间的不变性 常规组织 虚拟地址cache 只在失效时才变换 别名问题

  48. 2.通过避免地址变化加快cache命中: 进程标识符的效果 • 黑色为单进程 • 浅灰为冲洗cache时的多进程 • 深灰为使用进程标识符的多进程 • Y轴:失效率达20% • X轴:Cache大小从2 KB到 1024 KB

More Related