多级。 If the data your program needs are stored in a CPU register, then they can be accessed in 0 cycles during the execution of the instruction. If stored in a cache, 4 to 75 cycles. If stored in main memory, hundreds of cycles. And if stored in disk, tens of millions of cycles!

Static RAM (SRAM) is faster and significantly more expensive than dynamic RAM (DRAM).
SRAM is used for cache memories, both on and off the CPU chip.
DRAM is used for the main memory plus the frame buffer of a graphics system.


DRAM需要周期刷新,对噪声敏感。

DRAMs and SRAMs are volatile in the sense that they lose their information if the supply voltage is turned off.
There are a variety of nonvolatile memories. For historical reasons, they are referred to collectively as read-only memories (ROMs), even though some types of ROMs can be written to as well as
read.
Programs stored in ROM devices are often referred to as firmware. When a
computer system is powered up, it runs firmware stored in a ROM. Some systems
provide a small set of primitive input and output functions in firmware—for
example, a PC’s BIOS (basic input/output system) routines. Complicated devices
such as graphics cards and disk drive controllers also rely on firmware to translate
I/O (input/output) requests from the CPU.

locality的概念:
you can write your application programs so that their data items are stored higher in the hierarchy.
Programs with good locality tend to access the same set of data items over and over again, or they tend to access sets of nearby data items. Programs with good locality tend to access more data items from the upper levels of the memory hierarchy than programs with poor locality, and thus run faster.
Locality is typically described as having two distinct forms: temporal locality and spatial locality. In a program with good temporal locality, a memory location that is referenced once is likely to be referenced again multiple times in the near future. In a program with good spatial locality, if a memory location is referenced once, then the program is likely to reference a nearby memory location in the near future.
如果一个函数内的变量要么temporal locality要么spatial locality,我们认为它good locality。

操作系统层面locality的体现:
At the operating system level, the principle of locality allows the system to use the main memory as a cache of the most recently referenced chunks of the virtual address space.

在内存金字塔中,disk还不是塔底,远程服务器才是塔底。
Web browsers exploit temporal locality by caching recently referenced documents on a local disk.
此外,ssd的流行使得dram跟机械盘之间有了一个过渡。


It is important to realize that while the block size is fixed between any particular pair of adjacent levels in the hierarchy, other pairs of levels can have different block sizes. In general, devices lower in the hierarchy (further from the CPU) have longer access times, and thus tend to use larger block sizes in order to amortize these longer access times.

其实我们平时最经常写的二层循环内也蕴含locality。一般把第一维的下标放在外层,第二维的下标放在内层。如果反过来,那就违背了spatial locality。
上面提到6.22中的block size是会变化的。miss之后会replace一整个block,而一个block中可能有数个相邻的data objects,因此spatial locality好的程序会更快。(比如,在访问a[0]的时候miss了,于是就replace了a[0]~a[3],因此访问a[0]-a[3]的速度会比访问四个不相干数据的速度要快。)

Loops have good temporal and spatial locality with respect to instruction fetches. The smaller the loop body and the greater the number of loop iterations, the better the locality.

locality可以用cache hits和cache misses来量化。

cache miss也可分为三类。

  • cold miss
    cache中没有数据。
  • conflict miss
    the cache is large enough to hold the referenced data objects, but because they map to the same cache block, the cache keeps missing.(以上图6.22为例,反复访问0和8时,会一直miss,0、4、8、12共享上一级cache的第一个位置。)
  • capacity miss
    工作集比cache的容量更大。

the essence of the memory hierarchy is that the storage device at each level is a cache for the next lower level.

低级存储和高级存储之间的三种映射方法:直接映射、全相联、组相联(参考cany1000的博文)
直接映射实现简单。cache的利用率较低,就如每个人的停车位是固定分配好的,可以直接找到。适用于大容量cache。(n人共享第a个车位,这n个人的车牌号都以a结尾)
全相联,就像停车位可以大家随便停一样,停的时候简单,找车的时候需要一个一个停车位的找了。
(所有人随意使用所有车位)
组相联则是折中方案,组内全相联,组间直接映射。(n人共享第a组车位,这n个人的车牌号都以a结尾,这n个人使用第a组内的车位时,可以随意使用。这称为a路组相联)

内存金字塔中,读时是层进的hit or miss。写时,分为write-through和write-back,前者就是马上逐级下写,后者是等替换时再下写(由于temporal locality,write-back的下写次数较少)。write miss发生时,分为write-allocate和no-write-allocate。

对矩阵乘法六种实现的分析:



书中提到,虽然浮点乘法的次数一样,但最好和最差之间有四十倍的差距。所以一定要充分考虑缓存对性能的影响。
从图6.46可以看出,最内层循环为BC的两种方案是最好的,并且随n的增大,优势越来越明显。
这其实还是一个spatial locality的例子,最内层循环为BC的方案(kij和ikj)是stride-1的,最内层循环所用的index出现在最后一维,所以缓存的优势很大。书中还提到,intel对stride-1做了特别的优化。

CS:APP第六章知识总结(内存、缓存、locality)相关推荐

  1. CS:APP第三章知识总结(汇编语言、机器码、寄存器、编译器优化、函数底层实现、浮点指令)

    文章目录 高级语言相对汇编语言的优势 编译器优化的选项 高级语言相对汇编语言的优势 开发效率高.IDE和编译器会提醒你的错误.由于编译器优化的存在,高级语言在执行效率上的劣势并不大. 出错概率 跨平台 ...

  2. 第十六章:Java内存模型——Java并发编程实战

    一.什么是内存模型,为什么要使用它 如果缺少同步,那么将会有许多因素使得线程无法立即甚至永远看到一个线程的操作结果 编译器把变量保存在本地寄存器而不是内存中 编译器中生成的指令顺序,可以与源代码中的顺 ...

  3. C语言永真循环,c语言第六章总结 循环语句

    第六章知识总结 1.梳理知识点 2.重点+可解决的问题 3.用知识解决问题的感想 (一).感想. 通过学习循环结构这一章,我明白用更简单的方法去处理数据,要先看清题目的要求,原则所需的循环结构来解决问 ...

  4. 操作系统思考 第六章 内存管理

    第六章 内存管理 作者:Allen B. Downey 原文:Chapter 6 Memory management 译者:飞龙 协议:CC BY-NC-SA 4.0 C提供了4种用于动态内存分配的函 ...

  5. 【系统分析师之路】第六章 多媒体基础知识

    [系统分析师之路]第六章 多媒体基础知识 要求考生掌握的知识点有: 1.信息系统综合知识:多媒体压缩编码与存储技术.一般上午会考的知识点有:多媒体应用框架标准,蓝光DVD,媒体分类,采样,量化,JPE ...

  6. 【软考软件评测师】第二十六章 计算机安全设计(其他知识)

    [软考软件评测师]第二十六章 计算机安全设计(其他知识) 第二十六章 计算机安全设计(其他知识) [软考软件评测师]第二十六章 计算机安全设计(其他知识) 第一部分 知识点集锦 1.5G技术 2.云计 ...

  7. NPDP知识推送-第六章市场研究(4)

    NPDP知识推送-第六章市场研究-4 6.7在新产品流程特定阶段的市场研究 答案:2A  7A 18D 20A 题型分为概念题和情景题.大家只要牢记定义和概念就可以,情景题也是从概念中阐述.

  8. 六上计算机基础知识ppt课件,计算机应用基础(windows 7+office 2010)课件 第六章 文稿演示软件PowerPoint 2010.ppt.pdf-汇文网...

    计算机应用基础(windows 7+office 2010)课件 第六章 文稿演示软件PowerPoint 2010.ppt.pdf-汇文网 计算机应用基础 ( windows 7office 201 ...

  9. ​DMBOK知识梳理for CDGA/CDGP——第六章 数据存储与操作(附常考知识点)

    第六章 数据存储与操作 第六章在CDGA|CDGP考试中的分值占比较少,知识点比较密集,主要考点包括:数据存储与操作的定义.目标.数据库管理员(DBA)的角色定位及类型.数据处理的类型ACID和BAS ...

  10. 软件测试之第六章 网络基础知识

    第六章 网络基础知识 一.计算机网络基本概念与分类 1 计算机网络的定义 计算机网络是在网络协议的控制下,通过通信设备和线路将分布在不同地理位置,且具有独立功能的多个计算机系统连接起来,通过网络操作系 ...

最新文章

  1. 是北京晚报!不,是中国最大的讽刺!!!
  2. 日益谨慎的谷歌AI,会在自我限制中越走越慢吗?
  3. 一年春事,桃花红了谁……
  4. Runtime 系列 3-- 给 category 添加属性
  5. 召集最强的智,昇腾计算产业射出一支「穿云箭」
  6. win10 计算机休眠后无法唤醒,win10休眠后无法唤醒怎么办 win10系统怎么设置休眠时间...
  7. bootstrap的滚动监听
  8. Kubernetes集群(概念篇)
  9. Docker 入门使用 (二)
  10. 设计模式学习笔记——外观(Facade)模式
  11. apiclod 上传图片_Apicloud——关于上传图片、视频(二)
  12. oracle命令分析3
  13. vue global filters
  14. php什么是耦合关系,什么是耦合
  15. 地图索引文件MXD保存到数据库中
  16. 西电2019计算机导论期中考试,西安电子科技大学203上学期期末考试计算机导论试卷.doc...
  17. java设计模式学习-代理模式
  18. 数据宝贝儿放云上,你放心么?
  19. 如何区分零线和地线,及其相关理解
  20. 信息系统安全期末复习笔记

热门文章

  1. Nginx的配置与开发学习(五):配置属于自己的HTTPS证书
  2. matlab 去高光,Specular-Highlight-Mitigation-Removal-master
  3. 手机邮箱怎么发送电子邮件?163邮箱登陆界面好看么?
  4. 【微信公众号】第一步:申请公众号及测试号
  5. Java基础英语单词表
  6. jhin 不在 sudoers 文件中。此事将被报告。
  7. 大数据热词科普(三)
  8. leetcode报错reached end of file while parsing
  9. [Android问答] 如何获得手机屏幕分辨率?
  10. 跳马问题C++递归调用