CS:APP第六章知识总结(内存、缓存、locality)_缓存 locality-程序员宅基地

技术标签: 读书  

多级。 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做了特别的优化。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42100211/article/details/112789479

智能推荐

selectpage.js 下拉框实现多选的使用方法_js selectpage-程序员宅基地

文章浏览阅读1.7k次。第一步:下载selectpage.js,并引用js和css<link rel="stylesheet" href="/SelectPage/selectpage.css" type="text/css"></link><script type="text/javascript" src="/SelectPage/selectpage.js"></script>第二步:创建input,定义id为“selectPage”<input t.._js selectpage

Python爬虫工具:必会用的 6 款 Chrome 插件,2024年最新Python面试题及答案2024百度-程序员宅基地

文章浏览阅读883次,点赞7次,收藏11次。EditThisCookie 是一个 Cookie 管理器,可以很方便的添加,删除,编辑,搜索,锁定和屏蔽 Cookies。可以将登录后的 Cookies 先保存到本地,借助 cookielib 库,直接爬取登录后的数据。编写 Xpath 之后会实时显示匹配的数目和对应的位置,方便我们判断语句是否编写正确。它支持复杂的网站结构,数据支持文本、连接、数据块、下拉加载数据块等各种数据类型。操作简单,只需要鼠标点击和简单的配置,就能快速的爬取 Web 端的数据。此外,还能将爬取的数据导出到 CSV 文件中。

关于python启动illustrator程序后调用jsx脚本-程序员宅基地

文章浏览阅读123次。2.arguments为python传过来的参数,是一个数组,这里的arguments不能改成其他名字,否则接收不到python传过来的参数。2.arguments为传递进jsx脚本中的参数,类型是一个数组。在jsx是取参数为固定的变量名,这个变量名是无法自定义名字的。1.ret用来接收jsx脚本return的结果。1.main为自定义函数。

boost::interprocess::managed_shared_memory(2)(std::deque)-程序员宅基地

文章浏览阅读179次。struct shareDataEx : shareData{ int index; int total_size;};typedef managed_shared_memory::segment_manager segment_manager_t; //段管理器typedef allocator&lt;shareDataEx, segmen..._boost::interprocess deque

XStream 组件漏洞修复_xstream < 1.4.15 任意文件删除漏洞-程序员宅基地

文章浏览阅读2.1k次。风险描述XStream 是Java 类库中的常用组件,可将Java 对象序列化为XML,反之可将Java 对象和XML 文档相互转换。XStream 官方发布安全公告,披露多个反序列化漏洞,包括:1、拒绝服务漏洞(CVE-2021-21341/21348)攻击者可利用该漏洞操纵已处理的输入流并替换或注入对象,执行恶意正则表达式的计算,从而造成拒绝服务攻击。2、服务端请求伪造漏洞(CVE-2021-21342/21349)攻击者可利用该漏洞操纵已处理的输入流并替换或注入对象,从而伪造服务端请求。_xstream < 1.4.15 任意文件删除漏洞

C++ 在头文件中声明定义字符数组或指针变量_可以在头文件里放字符数组吗-程序员宅基地

文章浏览阅读5.1k次。C++ 在头文件中声明定义字符数组或指针变量_可以在头文件里放字符数组吗

随便推点

分布式任务调度,定时任务的处理方案_springcloud定时任务解决方案-程序员宅基地

文章浏览阅读1.8k次。4.复制配置文件,文件在xxl-job项目的 xxl-job-2.3.1\xxl-job-executor-samples\xxl-job-executor-sample-springboot\src\main\java\com\xxl\job\executor\core\config 的XxlJobConfig。这个命令将会创建一个名为 xxl-job-admin 的容器,并且将容器的 8080 端口映射到宿主机的 8080 端口,使得我们可以通过浏览器访问到 XXL-Job 的管理界面。_springcloud定时任务解决方案

启动Elasticsearch服务,提示如下错误信息:maybe these locations are not writable or multiple nodes were started_elasticsearch启动报错maybe these locations are not wri-程序员宅基地

文章浏览阅读5.2k次。Elasticsearch 服务启动,提示错误信息:[o.e.b.ElasticsearchUncaughtExceptionHandler] [node-1] uncaught exception in thread [main]org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: failed to obtain node locks, tried [[/path/to/data/my-app_elasticsearch启动报错maybe these locations are not writable or multiple node

HTML完整版,学HTML看这篇就够了-程序员宅基地

文章浏览阅读844次,点赞23次,收藏18次。什么是HTML?全称:Hyper Text Markup Language,超文本标记语言超文本:指的是网页中可以包含图片,链接,音频,视频等多媒体内容标记

用C语言编写程序,从键盘输入以下5个学生的学号、姓名,以及数学、语文和英语成绩,写到文本文件f2.txt中,再从文件中取出数据,计算每个学生的总成绩和平均分,并将结果显示在屏幕上。 提示:在文件读写的...-程序员宅基地

文章浏览阅读1.2k次。这是一个简单的C语言程序实现上述要求的示例:#include <stdio.h>struct student { int num; char name[20]; int math; int chinese; int english;};int main() { struct student s[5]; int i; ..._c语言从键盘输入以下5个学生的学号、姓名,以及数学、语文和英语成绩,写到文本文件

信息系统项目管理(第四版)(高级项目管理)考试重点整理 第15章 项目风险管理(一)-程序员宅基地

文章浏览阅读668次,点赞23次,收藏20次。每个项目都在两个层面上存在风险:一是每个项目都有会影响项目达成目标的单个风险;二是由单个风险和不确定性的其他来源联合导致的整体项目风险。项目风险管理过程同时兼顾这两个层面的风险。

Apollo_Lattice palnner_apollo lattice planner-程序员宅基地

文章浏览阅读4.1k次,点赞13次,收藏99次。Lattice并不简单,任何一个规划算法的工程实现都不是那么简单。顺带学习一下Frenet坐标系_apollo lattice planner

推荐文章

热门文章

相关标签