计算机组织与系统结构

Download Report

Transcript 计算机组织与系统结构

高等计算机系统结构
复 习
程 旭
2011年2月28日
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
复习
性能、成本
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
什么是性能(Performance)?
• 完成任务的时间 (执行时间)
• 执行时间(execution time), 响应时间(response time), 延迟
(latency)
• 单位时间(每天、小时、星期、秒、纳秒…等等)内完成的任务数
量(性能)
• 吞吐率(throughput), 带宽(bandwidth)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
性能的定义
• Performance is in units of things per sec
• Bigger is better
• If we are primarily concerned with responds time
1
Performace( X ) 
ExTime( X )
“X 比 Y 快 n 倍” (“ X is n times faster than Y” )
是指
ExTime( Y ) Performance( X )

ExTime( X ) Performance( Y )
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
影响CPU性能的几个方面
秒数 指令数 时钟数 秒数
CPU时 间 



程序
程序
指令 周期
指令总数
程序
编译器
指令系统体系结构
组成
工艺技术
CPI
时钟频率
X
X
X
X
X
X
X
X
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
选择基准程序集(Benchmark Sets)
°Toy benchmarks
°Kernels
°Synthetic benchmarks
• e.g. Whetstones and Dhrystones
°Simplified programs
°Real programs
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
SPEC:
System Performance Evaluation
Cooperative
第一版 1989
10个程序(6Fp+4Int)产生单一数值(SPECmarks)
第二版 1992
SPECInt92 (6Int) 和 SPECfp92 (14Fp)
 不限制编译器的开关. DEC 4000 Model 610在93年3月:
spice: unix.c:/def=(sysv,has_bcopy,攂copy(a,b,c)=memcpy(b,a,c)
wave5: /ali=(all,dcom=nat)/ag=a/ur=4/ur=200
nasa7: /norecu/ag=a/ur=4/ur2=200/lc=blas
第三版 1995
一组新的程序: SPECint95 (8Int) 和 SPECfp95 (10Fp)
揵有效期 三年 ?
对所有程序使用同一开关设置: SPECint_base95, SPECfp_base95
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
第一版SPEC
 1989年,第一版; 10 个程序, 用单一数值来总结性能
(6Fp+4Int), 相对于VAX 11/780
 其中有一个程序: 99%的时间耗费在该程序的单一一行代码上
 新型前端编译器可以非常显著地改进它的性能
800
700
SPEC Perf
600
500
400
300
200
100
tomcatv
fpppp
matrix300
eqntott
li
nasa7
doduc
spice
epresso
gcc
0
B ench mark
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
SPEC95
Benchmark
go
m88ksim
gcc
compress
li
ijpeg
perl
vortex
tomcatv
swim
su2cor
hydro2d
mgrid
applu
trub3d
apsi
fpppp
wave5
Description
Artificial intelligence; plays the game of Go
Motorola 88k chip simulator; runs test program
The Gnu C compiler generating SPARC code
Compresses and decompresses file in memory
Lisp interpreter
Graphic compression and decompression
Manipulates strings and prime numbers in the special-purpose programming language Perl
A database program
A mesh generation program
Shallow water model with 513 x 513 grid
quantum physics; Monte Carlo simulation
Astrophysics; Hydrodynamic Naiver Stokes equations
Multigrid solver in 3-D potential field
Parabolic/elliptic partial differential equations
Simulates isotropic, homogeneous turbulence in a cube
Solves problems regarding temperature, wind velocity, and distribution of pollutant
Quantum chemistry
Plasma physics; electromagnetic particle simulation
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
如何总结性能
 算术平均值 (或者加权算术平均值) 追踪 执行时间: SUM(Ti)/n
或者 SUM(Wi*Ti)
 比率(例如MFLOPS) 的 调和平均值 (或者加权调和平均值)
追踪 执行时间:
n/SUM(1/Ri) 或者 n/SUM(Wi/Ri)
 为了按比例伸缩性能,规格化执行时间 是 非常便捷的!
例如, 参照机器的时间  被评测机器的时间
 注意,不可使用规格化的执行时间的算术平均值,而应该使用几
何平均值!
 几何平均值平等对待所有的改进情况:
与
A 程序 的执行
B 程序 的执行
从
从
2 秒
2000 秒
减少到
减少到
1 秒
1000 秒
同等重要!
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
为什么对规格化数值要进行几何平均?
Time on A Time on B Normalized to A
Normalized to B
A
B
A
B
Program 1
1
10
1
10
0.1
1
Program 2
1000
100
1
0.1
10
1
Arithmetic mean
500.5
55
1
5.05
5.05
1
Geometric mean
31.6
31.6
1
1
1
1
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
性能评测
 or better or worse, benchmarks shape a field
 Good products created when have:
Good benchmarks
Good ways to summarize performance
 Given sales is a function in part of performance relative to
competition, investment in improving product as reported
by performance summary
 If benchmarks/summary inadequate, then choose between
improving product for real programs vs. improving
product to get more sales;
Sales almost always wins!
 Execution time is the measure of computer performance!
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
功耗问题——能耗有效性
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
功耗问题
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
性能成本比 Cost/Performance
What is Relationship of Cost to Price?
 Component Costs
 Direct Costs (add 25% to 40%) recurring costs: labor,
purchasing, scrap, warranty
 Gross Margin (add 82% to 186%) nonrecurring costs:
R&D, marketing, sales, equipment maintenance, rental, financing
cost, pretax profits, taxes
 Average Discount to get List Price (add 33% to 66%):
volume discounts and/or retailer markup
List Price
Avg. Selling Price
Average
Discount
Gross
Margin
25% to 40%
34% to 39%
6% to 8%
15% to 33%
Direct Cost
Component
Cost
北京大学计算机科学技术系 [email protected]
北京大学微处理器研究开发中心
人是如何思考(Thinking)的?
简单的模型?
感知:
眼、耳等
命令:
行为
判断
记忆
大脑
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
现代计算机的理论基础
1936 Alan Turing 阿伦∙图灵
On Computable Numbers
Turing Machine
ACE - EDSAC
1945 John von Neumann 冯∙诺依曼
First Draft of a Report on the EDVAC
感知:
眼、耳等
命令:
行为
判断
记忆
大脑
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
数字逻辑基础回顾
同步时序电路
感知:
眼、耳等
有穷状态自动机FSM
数据通路+控制 = 数字系统
Combinational
组合逻辑
logic
xn
y1
...
yr
Clock
时钟
大脑
zm
Yr
Memory
存储
记忆
z1
...
...
x1
...
命令:
行为
判断
Y1
控制
状态信息
状态
控制
信号输出
输入信号
数据通路
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
有穷状态自动机
°显式描述出系统的状态
°状态间的迁移用“有向边+输入”描述
°输出可以表示为状态的一部分也可以表现为输出边的
一部分
1
“Mod 3 Machine”
Input (MSB first)
Mod 3
106
1
Alpha/
0
0
1
Beta/
1
0
1 1 0101 0
Delta/
100122 1
1
0
2
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
“Moore Machine”
“Mealey Machine”
Latch
Combinational
Logic
有穷状态自动机的实现:组合逻辑+锁存器
1/0
Alpha/
0/0
Beta/
1/1
0
1
0/2
Delta/
1/2
0/1
2
Input
State old
State new
Out
0
0
0
1
1
1
00
01
10
00
01
10
00
10
01
01
00
10
0
2
1
1
0
2
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
冯诺依曼结构-存储程序的概念
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
存储程序的概念
 计算机由运算器、控制器、存储器和输入设备、输出设备
五大部件组成
 指令和数据以同等的地位存放于存储器中,并按地址访存
 指令和数据都用二进制数表示
 指令由操作码和地址码组成,操作码用来表示操作的性质
,地址码用来表示操作数存在存储器中的位置
 指令是顺序存放、顺序执行的,并可根据运算结果或设定
的条件改变执行顺序
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
计算机的简单模型IAS
程序计数器:Program
Counter (PC)
累加器ACC和路
存储地址寄存器MAR
存储数据寄存器
MDR/MBR
指令寄存器IR
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
微程序控制器
°State machine in which part of state is a “micro-pc”.
• Explicit circuitry for incrementing or changing PC
°Includes a ROM with “microinstructions”.
+ 1
ROM
(Instructions)
State w/ Address
Addr
Control
Branch
PC
MUX
Combinational Logic/
Controlled Machine
• Controlled logic implements at least branches and jumps
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
复习
指令系统、流水线、Cache
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
典型 RISC
 32位固定格式指令(3种格式)
 32个32位GPR (R0恒为0, 双精成对使用)
 3地址、reg-reg算术指令
 load/store使用单一寻址方式:
base + displacement
无间接寻址
 简单的转移条件(基于寄存器数值)
 延迟转移
参见: SPARC, MIPS, HP PA-Risc, DEC Alpha, IBM PowerPC,
CDC 6600, CDC 7600, Cray-1, Cray-2, Cray-3
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
例如: MIPS
Register-Register
31
26 25
Op
21 20
Rs1
16 15
Rs2
11 10
Rd
6 5
0
Opx
Register-Immediate
31
26 25
Op
21 20
Rs1
16 15
0
immediate
Rd
Branch
31
26 25
Op
Rs1
21 20
16 15
Rs2/Opx
0
immediate
Jump / Call
31
26 25
Op
0
target
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
串行洗衣店
6 PM
7
8
9
10
11
12
1
2 AM
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
任
务
次
序
A
工作时间
B
C
D
 串行洗衣店需要8个小时完成4个工作量
 如果他们了解流水技术,那么需要多长时间完成上述工
作呢?
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
流水技术性质
6 PM
任
务
顺
序
7
8
 流水技术无助于减少单个任务的
处理延迟(latency) ,但有助于提
高整体工作负载 的吞吐率
9
时间
30 30 30 30 30 30 30
多个不同任务同时操作, 使用不同
资源
A
 潜在加速比 = 流水线级数
B
 流水线的速率受限于最慢的流水段
C
 流水段的执行时间如果不均衡,那
么加速比就会降低
D
 开始填充流水线的时间 和 最后排
放流水线的时间 降低加速比
 相关将导致流水线暂停
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
流水线可以看为随时间偏移的一系列数据通路
Figure 3.3, Page 133 时间 (时钟周期)
Inst 4
Im
Reg
Dm
Reg
Im
Reg
Dm
Reg
Im
Reg
Dm
Reg
Reg
Dm
Reg
Im
Reg
ALU
Inst 3
CC5
ALU
Inst 2
CC4
ALU
Inst 1
CC3
ALU
指
令
执
行
次
序
CC2
ALU
Inst 0
CC1
Dm
Im
CC6
CC7
CC8
CC9
Reg
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Stage
IF
ID
Any Instruction
IF/ID.IR  Mem[PC];
IF/ID.NPC, PC (if EX/MEM.cond {EX/MEM.ALUOutput} else {PC+4});
ID/EX.A Regs[IF/ID.IR6..10]; ID/EX.B Regs[IF/ID.IR11..15];
ID/EX.NPCIF/ID.NPC; ID/EX.IRIF/ID.IR;
16
EX
MEM
WB
ID/EX.Imm(IR16) ##IR16..31;
ALU Instruction
Load or store instruction
EX/MEM.IRID/EX.IR;
EX/MEM.ALUOutput 
EX/MEM.IR  ID/EX.IR
ID/EX.A func ID/EX.B;
or EX/MEM.ALUOutput 
EX/MEM.ALUOutput 
ID/EX.A +ID/EX.Imm;
ID/EX.A op ID/EX.Imm;
EX/MEM.cond  0;
EX/MEM.cond0;
EX/MEM.BID/EX.B;
MEM/WB.IREX/MEM.IR
MEM/WB.IR  EX/MEM.IR;
MEM/WB.ALUOutput 
MEM/WB.LMD 
EX/MEM.LUOutput;
Mem[EX/MEM.ALUOutput];
Or
Mem[EX/MEM.ALUOutput] 
EX/MEM.B;
Regs[MEM/WB.IR16..20]  Regs[MEM/WB.IR11..15] 
MEM/WB.ALUOutput;
MEM/WB.LMD;
Or
Regs[MEM/WB.IR] 11..15] 
MEM/WB.ALUOutput;
Branch instruction
EX/MEM.ALUOutput 
ID/EX.NPC + ID.EX.Imm
EX/MEM.cond 
(ID/EX.A op 0)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
常规流水化执行的表示
时间
IFetch Dcd
Exec
IFetch Dcd
Mem
WB
Exec
Mem
WB
Exec
Mem
WB
Exec
Mem
WB
Exec
Mem
WB
Exec
Mem
IFetch Dcd
IFetch Dcd
IFetch Dcd
程序流
IFetch Dcd
WB
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
计算机设计的法则
Make the Common Case Fast
Amdahl定律
加速比增加E  
1
1  FE   FE
SE
The CPU Performance Equation
 CPI
 CPU time = (IC  CPI) / f
Principle of Locality
 Cache
 Memory Hierarchy
Take Advantage of Parallelism
Piplelining
Instruction-Level Parallelism
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
流水技术的主要障碍
流水线冒险(Pipeline Hazards)
结构冒险(structural hazards): 试图同时以两
种不同的方式使用同一资源;
 原因:硬件无法支持同时重叠执行的所有可
能的指令组合
数据冒险(data hazards): 在产生数据之前,就
试图使用它们
 原因:流水线中的重叠执行的指令依赖于前
面指令的结果
控制冒险(control hazards):在判定转移条件之
前,就试图决策转移方向
 原因:可能改变PC的指令的流水
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
单口存储器是一种结构冒险
Figure 3.6, Page 142
Reg
Reg
Mem
Reg
Mem
Reg
Mem
Reg
Mem
Reg
ALU
Mem
Mem
Reg
ALU
Mem
Mem
ALU
Instr 1
Reg
ALU
令
Mem
ALU
指
Load
时间 (时钟周期)
次
序
Instr 2
Instr 3
Instr 4
Reg
Mem
Reg
此例中,检测非常容易!
Mem
读
Mem
写
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
增加空泡(暂停)解决单存储器结构冒险
Figure 3.7, Page 143
Mem
Mem
Reg
Reg
Mem
Reg
Mem
Reg
ALU
Instr 1
Reg
ALU
令
Mem
ALU
指
Load
时间 (时钟周期)
Mem
Reg
bubble
bubble bubble
次
序
Instr 2
(stall)
bubble
bubble
Instr 3
Mem
读
Reg
Mem
ALU
Mem
写
Mem
Reg
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
流水线加速比计算公式
Average_ Instruction _ Tim e_ Unpipelined
SpeedupFrom Pipelining 
Average_ Instruction _ Tim e_ pipelined
CPI _ Unpipelined  Clock _ cycle _ unpipelined

CPI _ pipelined Clock _ cycle _ pipelined
CPI _ unpipelined Clock _ cycle _ unpipelined


CPI _ pipelined
Clock _ cycle _ pipelined
CPI _ pipelined  Ideal_ CPI  Pipeline_ stall _ clock _ cycles _ per _ instruction
 1  Pipeline_ stall _ clock _ cycles _ per _ instruction
Ignoring cycle time overhead of pipelining and assuming stages are perfectly balanced
Speedup
CPI _ unpipelined
1  Pipeline_ stall _ cycles _ per _ instruction
All instructions take the same number of cycles
Speedup
Pipeline_ depth
1  Pipeline_ stall _ cycles _ per _ instruction
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
流水线加速比计算公式(续)
CPI _ unpipelined Clock _ cycle _ unpipelined

CPI _ pipelined
Clock _ cycle _ pipelined
1
Clock _ cycle _ unpipelined


1  Pipeline_ stall _ cycles _ per _ instruction Clock _ cycle _ pipelined
SpeedupFrom Pipelining 
Clock _ cycle _ unpipelined
Clock _ cycle _ pipelined 
Pipeline_ depth
Clock _ cycle _ unpipelined
Pipeline_ depth 
Clock _ cycle _ pipelined
1
Clock _ cycle _ unpipelined

1  Pipeline_ stall _ cycles _ per _ instruction Clock _ cycle _ pipelined
1

 Pipeline_ depth
1  Pipeline_ stall _ cycles _ per _ instruction
SpeedupFrom Pipelining 
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
示例: 双口 与 单口 存储器
Machine A: Dual ported memory
Machine B: Single ported memory, but its pipelined
implementation has a 1.05 times faster clock rate
Ideal CPI = 1 for both
Loads are 40% of instructions executed
SpeedUpA = Pipeline Depth/(1 + 0) x (clockunpipe/clockpipe)
= Pipeline Depth
SpeedUpB = Pipeline Depth/(1 + 0.4 x 1)
x (clockunpipe/(clockunpipe / 1.05)
= (Pipeline Depth/1.4) x 1.05
= 0.75 x Pipeline Depth
SpeedUpA / SpeedUpB = Pipeline Depth/(0.75 x Pipeline Depth) = 1.33
Machine A is 1.33 times faster
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
关于r1的数据冒险
Figure 3.9, page 147
add r1,r2,r3
Reg
MEM
DM
Reg
WB
Reg
DM
Reg
DM
Reg
ALU
IM
EX
ALU
sub r4,r1,r3
IM
ID/RF
ALU
指
IF
ALU
时间 (时钟周期)
令
次
序
and r6,r1,r7
or r8,r1,r9
Reg
IM
Mem
Reg
Reg
DM
ALU
xor r10,r1,r11
IM
Reg
Mem
Reg
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
三种普通的数据相关
InstrI followed by InstrJ
Read After Write
I: add r1,r2,r3
J: sub r4,r1,r3
(RAW,先写后读相关、写后读相关)
InstrJ tries to read operand before Instr I
writes it
Caused by a “Dependence” (in compiler
nomenclature). This hazard results from an
actual need for communication.
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
三种普通的数据相关(续一)
InstrI followed by InstrJ
Write After Read
(WAR,先读后写相关、读后写相关)
I: sub r4,r1,r3
J: add r1,r2,r3
K: mul r6,r1,r7
InstrJ tries to write operand before InstrI reads i
Gets wrong operand
Called an “anti-dependence” by compiler writers.
This results from reuse of the name “r1”.
Can’t happen in DLX 5 stage pipeline because:
 All instructions take 5 stages, and
 Reads are always in stage 2, and
 Writes are always in stage 5
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
三种普通的数据相关(续二)
InstrI followed by InstrJ
I: sub r1,r4,r3
J: add r1,r2,r3
K: mul r6,r1,r7
 Write After Write (WAW,写后写相关)
InstrJ tries to write operand before InstrI writes it
 Leaves wrong result ( InstrI not InstrJ )
 Called an “output dependence” by compiler writers
This also results from the reuse of name “r1”.
 Can‘t happen in DLX 5 stage pipeline because:
 All instructions take 5 stages, and
 Writes are always in stage 5
 Will see WAR and WAW in later more complicated pipes
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
数据冒险解决策略 (Fig3.10
Page149)
• 从某流水段向其他流水段前递(forward)结果
add r1,r2,r3
Reg
MEM
DM
Reg
IM
WB
Reg
DM
Reg
Reg
DM
IM
Reg
ALU
IM
EX
ALU
sub r4,r1,r3
IM
ID/RF
ALU
指
IF
ALU
时间 (时钟周期)
Mem
Reg
令
次
or r8,r1,r9
xor r10,r1,r11
Reg
DM
Reg
ALU
序
and r6,r1,r7
Mem
Reg
• 如果适当定义读/写次序,就可能正常工作
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
装入指令:不能通过前递解决的数据相关
Figure 3.12 Page 153
IF
ID/RF
EX
MEM
ALU
时间 (时钟周期)
Dm
WB
指
令
lw r1,0(r2)
IM
Reg
序
sub r4,r1,r3
IM
Reg
ALU
次
Reg
DM
Reg
• 不能通过前递技术解决
• 需要增加流水线互锁(pipeline interlock)
硬件,进行检测
• 必须延迟/暂停与装入指令相关的指令
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
通过编译调度解决的数据相关
a=b+c;
A=B+C
LW R1,B
LW R2,C
ADD R3,R1,R2
SW A,R3
d=e-f;
IF
ID
IF
EX
ID
IF
MEM
EXE
ID
IF
WB
MEM WB
stall EX MEM WB
stall
ID
EX MEM WB
assuming a, b, c, d ,e, and f in memory.
Slow code:
LW
LW
ADD
SW
LW
LW
SUB
SW
Rb,b
Rc,c
Ra,Rb,Rc
a,Ra
Re,e
Rf,f
Rd,Re,Rf
d,Rd
Fast code:
LW
LW
LW
ADD
LW
SW
SUB
SW
Rb,b
Rc,c
Re,e
Ra,Rb,Rc
Rf,f
a,Ra
Rd,Re,Rf
d,Rd
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
支持数据前递的硬件改变
Figure 3.20, Page 161
ZERO?
Data
ALU
Memory
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
控制冒险
Figure 3.22, Page 163
ADD
IF/ID
1
EX/MEM
MEM/WB
Zero?
Mux
ALU
+4
ID/EX
0
Ra
Reg File
Address
Instruction
Memory
0
busW busB
Sign
Extend
Mux
Instruciton
Rw
1
Address
Read
Data
Data
Memory
1
Mux
P
C
busA
ALU
Rb
0
Wr
Data
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
22: add r8,r1,r9
36: xor r10,r1,r11
Ifetch
Reg
DMem
Ifetch
Reg
DMem
Ifetch
Reg
DMem
Ifetch
Reg
ALU
r6,r1,r7
DMem
ALU
18: or
Reg
ALU
14: and r2,r3,r5
Ifetch
ALU
10: beq r1,r3,36
ALU
3级暂停转移的控制冒险
Reg
Reg
Reg
Reg
DMem
Reg
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
转移暂停的影响
°If CPI = 1, 30% branch,
Stall 3 cycles => new CPI = 1.9!
°Two part solution:
• Determine branch taken or not sooner, AND
• Compute taken branch address earlier
°DLX branch tests if register = 0 or  0
°DLX Solution:
• Move Zero test to ID/RF stage
• Adder to calculate new PC in ID/RF stage
• 1 clock cycle penalty for branch versus 3
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
控制冒险
Figure 3.22, Page 163
ADD
IF/ID
1
EX/MEM
MEM/WB
Zero?
Mux
ALU
+4
ID/EX
0
Ra
Reg File
Address
Instruction
Memory
0
busW busB
Sign
Extend
Mux
Instruciton
Rw
1
Address
Read
Data
Data
Memory
1
Mux
P
C
busA
ALU
Rb
0
Wr
Data
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
可改变PC的指令的出现频率
Figure 3.24, Page 165
1%
1%2%
su2cor
0%
mdljdp0%
0%
hydro2d
9%
2%
10%
4%
4%
ear
2%
2%
doduc
4%
gcc
3%4%
1%
6%
Backward
conditional
branches
Forward
conditional
branches
6%
li
espresso
Unconditional
branches
4%
8%
11%
12%
11%
2%
2%
equntott
22%
3%
3%
compress
0%
5%
11%
10%
15%
20%
25%
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
前跳和后转的转移占总转移的67%
Figure 3.25, Page 166
63%
61%
53%
51%
44%
35%
38%
34%
37%
26%
25%
22%
21%
21%
16%
13%
14%
8%
Forward Taken
ea
hy
r
dr
o2
md d
lj
dp
su
2c
or
c
du
do
li
gc
c
so
re
s
tt
nt
o
es
p
3%
ss
pr
e
co
m
78%
eq
u
80%
70%
60%
50%
40%
30%
20%
10%
0%
Backward Taken
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
减小流水线中转移的损失
1) Simplest scheme: Freeze the pipeline
2) Prediction-not-taken
Untaken branch inst
Instruction I+1
Instruction I+2
Instruction I+3
Instruction I+4
IF
Taken branch inst
Instruction I+1
Branch Target
Branch Target+1
Branch Target+2
IF
ID
IF
ID
IF
EX
ID
IF
EX
idle
IF
MEM
EX
ID
IF
MEM
idle
ID
IF
WB
MEM
EXE
ID
IF
WB
MEM
stall
stall
WB
EX
ID
MEM
EX
WB
MEM
WB
WB
idle
EXE
ID
IF
idle
MEM
stall
stall
WB
EX
ID
MEM
EX
WB
MEM
WB
3) Prediction-taken
4) Delayed Branch
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
对转移延迟槽的调度
(a) From before
(b) From target
ADD R1,R2,R3
SUB R4,R5,R6
ADD R1,R2,R3
IF R1==0 THEN
IF R2==0 THEN
ADD R1,R2,R3
Delay slot
(c) From fall through
IF R1==0 THEN
Delay slot
SUB R4,R5,R6
Delay slot
IF R2==0 THEN
ADD R1,R2,R3
IF R1==0 THEN
ADD R1,R2,R3
ADD R1,R2,R3
SUB R4,R5,R6
IF R1==0 THEN
SUB R4,R5,R6
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
延迟和删除的转移的性能(Fig.3.32 P172)
50%
45%
40%
35%
30%
25%
20%
15%
10%
5%
0%
12%
4%
26%
4%
13%
10%
34%
30%
24%
17%
16%
20%
37%
10%
15%
Empty slot
7%
1%
7%
e
hy ar
dr
o2
md d
lj
dp
su
2c
or
du
c
do
li
gc
c
so
re
s
tt
es
p
nt
o
ss
1%
eq
u
pr
e
co
m
6%
Canceled delay slot
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
延迟转移
°Where to get instructions to fill branch delay slot?
• Before branch instruction
• From the target address: only valuable when branch taken
• From fall through: only valuable when branch not taken
• Cancelling branches allow more slots to be filled
°Compiler effectiveness for single branch delay slot:
• Fills about 60% of branch delay slots
• About 80% of instructions executed in branch delay slots
useful in computation
• About 50% (60% x 80%) of slots usefully filled
°Delayed Branch downside: 7-8 stage pipelines, multiple
instructions issued per clock (superscalar)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
评测不同的转移处理策略
Pipeline speedup =
Scheduling
scheme
Pipeline depth
1 +Branch frequency Branch penalty
Branch
penalty
CPI
speedup v.
unpipelined
speedup v.
stall
Stall pipeline
3
1.42
3.5
1.0
Predict taken
1
1.14
4.4
1.26
Predict not taken
1
1.09
4.5
1.29
0.5
1.07
4.6
1.31
Delayed branch
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
小结 1:
• 设计中必须考虑技术的发展
Capacity
Speed
Logic
2x in 3 years
2x in 3 years
DRAM
4x in 3 years
2x in 10 years
Disk
4x in 3 years
2x in 10 years
• 执行任务所需时间
– Execution time, response time, latency
• 每天、每小时、每周、每秒完成的任务
– Throughput, bandwidth
• X比Y快 n 倍 是指:
ExTime(Y)
--------ExTime(X)
=
Performance(X)
-------------Performance(Y)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
小结 2:
 Amdahl定律:
Speedupoverall =
ExTimeold
ExTimenew
1
=
(1 - Fractionenhanced) + Fractionenhanced
Speedupenhanced
 CPI:
CPU time
= Seconds
Program
= Instructions x
Program
Cycles
x Seconds
Instruction
Cycle
 执行时间 是 计算机系统的 真正 测度!
 创造成功产品需要:好基准程序、正确总结方法
 晶模成本约为晶模面积的四次方
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
小结3:控制 与 流水
°Just overlap tasks; easy if tasks are independent
°Speed Up  Pipeline Depth; if ideal CPI is 1, then:
Cycle Time unpipeline d
Pipeline depth
Speedup 

1  Pipeline stall CPI
Cycle Time pipelined
°Hazards limit performance on computers:
• Structural: need more HW resources
• Data (RAW,WAR,WAW): need forwarding, compiler
scheduling
• Control: delayed branch, prediction
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
存储层次
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
微处理器-DRAM 的延迟差距
Performance
1000
CPU
Proc
60%/yr.
Processor-Memory
Performance Gap:
(grows 50% / year)
DRAM
DRAM 7%/yr.
100
10
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
1
Time
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
容量
访问时间
成本
CPU寄存器
100s Bytes
<10s ns
Cache
K Bytes
10~100 ns
1~0.1cent/bit
主存
M Bytes
200ns-500ns
0.0001~.00001 cent/bit
磁盘
G Bytes
10毫秒
10-5~10-6 cent/bit
磁带
无限
秒~分
10-8
存储层次的级别
寄存器堆
指令操作数
Cache
块(Blocks)
存储器
页(Pages)
磁盘
文件(Files)
磁带
较高级别
分段传
输单位
较快
程序/编译器
1-8 字节
Cache控制
8-128字节
操作系统
512-4K字节
用户/操作员
Mbytes
较大
较低级别
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
局部性原理
访问的可能性
°局部性原理:
0
地址空间
2
• 程序在一定的时间段内通常只会访问地址空间较小的部分。
• 例如:10%的代码将耗费90%的程序执行时间。
°两种不同类型的局部性:
• 时间局部性(Temporal Locality):如果一个信息项正在被访问,那
么在近期她很可能还会被再次访问。
• 空间局部性(Spatial Locality):在最近的将来将用到的信息很可能
与现在正在使用的信息在空间地址上是临近的。
• 在过去的十五年,计算机硬件主要依赖局部性来改进性能
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
存储层次:术语
°命中(Hit): 数据在较高层的某一块中出现(例如X块)
• 命中率(Hit Rate):在较高层发现存储访问的比率
• 命中时间(Hit Time):访问在较高层命中数据的时间
RAM访问时间 + 确定命中/失效的时间
°失效(Miss):需要从较低层中的块中找回数据(Y块)
• 失效率(Miss Rate) = 1 - (命中率)
• 失效损失(Miss Penalty):替换较高层存储的一个数据块的时间 + 将该
块交付给处理器的时间
°命中时间 << 失效损失(Alpha 21264中约需500条指令处理时间!)
至处理器
较高级
存储器
较低级
存储器
X块
来自处理器
Y块
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Cache度量
°Hit rate: fraction found in that level
• So high that usually talk about
Miss rate
• Miss rate fallacy: as MIPS to CPU performance,
miss rate to average memory access time in memory
°Average memory-access time
= Hit time + Miss rate x Miss penalty
(ns or clocks)
°Miss penalty: time to replace a block from lower
level, including time to replace in CPU
• access time: time to lower level
= f(latency to lower level)
• transfer time: time to transfer block
=f(BW between upper & lower levels)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
最简单的Cache: 直接映射Cache
主存地址
0
1
2
3
4
5
6
主存
4 字节直接映射Cache
Cache索引(Index)
0
1
2
3
7
8
9
A
B
C
D
E
位置 0 可以被来自以下位置的数据占用:
主存位置0、4、8, ... 等等
一般情况:任何地址最低两位为0的主存位置
地址<1:0>  cache索引
我们应该将哪一个放置在cache中?
我们如何知道到底哪一个在cache中呢?
F
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
示例: 32B数据块的1KB直接映射Cache
对于 2 N 字节cache:
最高的 (32 - N)位总是Cache标签
最低的 M位是字节选择 (块大小 = 2M)
31
9
Cache标签 示例: 0x50
4
Cache 索引
示例: 0x01
0
字节选择
示例: 0x00
作为Cache状态
的一部分存储
Cache 标签
0x50
Cache 数据
Byte 31
Byte 1
Byte 63
Byte 33 Byte 32 1
: :
有效位
Byte 0
0
2
3
:
:
Byte 1023
:
:
Byte 992
31
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
两路组相联Cache
N路组相联: 对每个Cache索引有N个数据块(entries)
N个直接映射cache并行工作
示例:两路组相联cache
Cache索引来选择cache中的某一组
该组种的两个标签进行并行比较
根据标签比较的结果来选择数据
有效位
Cache 标签
Cache 数据
Cache 索引
Cache 数据
Cache 块 0
:
:
地址标签
:
Sel1 1
有效位
Cache 块 0
:
比较
Cache 标签
Mux
0 Sel0
:
:
比较
或
Hit
Cache 块
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
组相联Cache缺点
N路组相联Cache 与 直接映射Cache:
N个比较器 与 1个比较器
在输出数据时,增加了额外的多路选择延迟
在判断命中/失效时候,才可以得到数据
在直接映射Cache中, Cache块在命中/失效之前就可以得到:
可以先假设命中并继续执行,如果以后发现失效再恢复。
有效位
Cache 标签
Cache 数据
Cache 索引
Cache 数据
Cache 块 0
:
:
地址标签
:
Sel1 1
有效位
Cache 块 0
:
比较
Cache 标签
Mux
0 Sel0
:
:
比较
或
Hit
Cache 块
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
另一个极端的示例: 全相联
全相联Cache -- 将组相联的思想推广到极限!
完全不要Cache索引
并行地比较所有的 Cache标签
示例:块大小 = 32 字节, 需要N个 27位的比较器
根据定义: 对于全相联cache,冲突失效 = 0
31
4
0
字节选择
示例: 0x01
Cache标签 (27位长)
Cache 标签
有效位
: :
X
Cache 数据
字节 31
字节 1 字节 0
字节 63
字节 33 字节 32
X
X
X
X
:
:
:
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Cache的设计空间
一些相互影响的维
Cache Size
Cache大小
块大小
相联度
替换策略
写穿透 与 写返回
写分配
Associativity
Block Size
最佳选择是进行折衷
根据访问特性
 工作负载
 使用 (I-cache, D-cache, TLB)
依赖于技术工艺/成本
“简洁”常胜
Bad
Good
Factor A
Less
Factor B
More
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
存储层次设计的四个问题
Q1: 信息块可以放在高层的哪里? (Block placement)
全相联、组相联、直接映射
Q2: 如果信息块在高层,那么如何找到它? (Block
identification)
标记/信息块
Q3: 在失效时,应该替换掉哪个信息块?(Block
replacement)
随机、 LRU
Q4: 在写操作时,会发生什么情况 (Write strategy)
回写(Write Back)或 直写(Write Through) (使用写缓
冲器)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Q1:信息块可以放在高层的哪里?
第12块 放到 8块的cache:
全相联、直接映射、2路组相联
S.A. Mapping = Block Number Modulo Number Sets
全相联
直接映射
(12 mod 8) = 4
2路组相联
(12 mod 4) = 0
01234567
01234567
01234567
Cache
1111111111222222222233
01234567890123456789012345678901
Memory
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Q2:如果信息块在高层,那么如何找到它?
Tag on each block
No need to check index or block offset
Increasing associativity shrinks index,
expands tag
Block Address
Tag
Index
Block
Offset
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Q3: 在失效时,应该替换掉哪个信息块?
直接映射 很简单!
组相联 或 全相联
随机
LRU (Least Recently Used)
Assoc:
2-way
4-way
LRU
Ran
8-way
Size
LRU
Ran
LRU
Ran
16 KB
5.2%
5.7% 4.7% 5.3% 4.4%
5.0%
64 KB
1.9%
2.0% 1.5% 1.7% 1.4%
1.5%
256 KB 1.15% 1.17% 1.13% 1.13% 1.12% 1.12%
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Q4: 在写操作时,会发生什么情况?
直写(Write through)— 信息不仅被写入CACHE,而且
还要写到底层的存储中
写返回(Write back)— 信息仅被写入到CACHE中。被
修改的cache块只有在被替换时才会回写到主存
块 干净 还是 脏?
各自利弊?
WT: read misses cannot result in writes
WB: no repeated writes to same location
WT 总是与 “写缓冲器(write buffers)”结合,从
而无需等待低级存储
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
写穿透的写缓冲器
Cache
Processor
DRAM
Write Buffer
在Cache和存储器之间需要一个写缓冲器
处理器:将数据写入cache和写缓冲器
存储器控制器:将缓冲器中的内容写入存储器
写缓冲器就像一个先进先出栈(FIFO):
典型数据项的数目:4
可以很好地工作,如果: 存储操作的频率 (写时间) << 1
/ DRAM写周期
存储器设计人员最害怕的事情:
存储频率(写时间)
写缓冲器饱和

1 / DRAM写操作
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
现代计算机的存储层次体系
通过利用程序局部性原理的优点:
向用户提供最廉价工艺所可能提供的最多的存储空间
向用户提供最快速工艺所可能提供的最快访问速度
Processor
Control
Speed (ns): 1s
Size (bytes): 100s
On-Chip
Cache
Registers
Datapath
Second
Level
Cache
(SRAM)
Main
Memory
(DRAM)
10s
100s
Ks
Ms
Secondary
Storage
(Disk)
10,000,000s
(10s ms)
Gs
Tertiary
Storage
(Disk/Tape)
10,000,000,000s
(10s sec)
Ts
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
虚拟存储系统设计的基本问题
•
•
•
•
从二级存贮器向主存传输信息块的大小
如果某信息块需要引入主存M,但是M已经装满,那么M中的一些区域就必
须释放,以为新的信息块腾出空间  替换策略(replacement policy)
M中的哪个区域将安放新的信息块 放置策略(placement policy )
失效的信息项只有在出现页面失效(fault)时,才从第二级存储器中取出 
读取/装入策略(fetch/load policy)
cache
mem
disk
reg
pages
frame
分页组织
虚拟 和 物理 地址被划分为同等大小的信息块
页帧(page frames)
页(pages)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
地址映射
V = {0, 1, . . . , n - 1} 虚拟地址空间
M = {0, 1, . . . , m - 1} 物理地址空间
n>m
映射: V  M U {0} 地址映射函数
MAP(a) = a‘ 如果在虚拟地址 a 的数据出现在物理地址 a’ ,并且 a‘ 在 M中
= 0 如果在虚拟地址 a 的数据不出现在M中
a
失效信息项故障
名字空间 V
缺页处理
处理器
a
地址变换
机制
0
主存
二级存储
a'
物理地址
OS 完成这一传输
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
分页组织
物理地址
0
1024
页帧
0
1
7
7168
1K
1K
地址
变换
映射
1K
0
1024
页 0
1
1K
1K
映射单位
也是从虚拟存
储器到物理存
储器的传输单
位
物理存储器
31744
31
1K
虚拟存储器
地址映射
虚拟地址
10
偏移量
页号
页表
页表基址
寄存器
到页表
的索引
V
访问
权限
PA
位于物理
存储器的表
+
实际上,经常采用串联方式
物理存
储器地址
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
虚拟地址与Cache访问
VA
CPU
PA
Translation
data
Cache
miss
Main
Memory
hit
•需要一次额外的存储访问来将虚拟地址变换成物理地址
•这使得cache访问的代价非常高,而且这也是我们希望尽可能快得执
行的“最内层循环”
•思考:为什么总是用物理地址来访问cache? 虚拟地址cache有一个
很难对付的问题!
•同义问题(synonym problem):两个不同的虚拟地址可以映
射到同一物理地址 => 两个不同的cache表项包含对同一物理地
址的数据!
•对于更新:必须更新所有具有同一物理地址的cache表项 或者 允
许 存储器不一致
•确定上述情况需要大量的硬件,特别是需要对物理地址标签进行
相联查找以确定是否出现多次命中
•或者通过软件强制实现:alias boundary:即 VA和PA相同的低
位空间 > cache大小
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
快表(TLB)
加速地址变换的一种方式是使用一种关于最近使用页表表项的特殊
cache  它有很多名字,但最经常使用的名字是变换旁视缓冲
器(Translation Lookaside Buffer:TLB)或快表!
虚拟地址
物理地址
脏位 访问 有效位 访问方式
TLB访问时间与Cache访问时间是可比的(但要小一些)
(也比主存访问时间要小得多)
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
变换旁视缓冲器
• 就像其他的cache, TLB可以是全相联的、组相联的,也可以是直
接映射的
• TLB一般较小,通常即使在高性能机器中也只有128 - 256个表项。
这样就可以实现全相联查找。大多数中等性能的机器使用n路组相
联的组成。
hit
PA
VA
CPU
使用TLB
的地址变换
miss
TLB
Lookup
miss
Cache
Main
Memory
hit
Translation
data
1/2 t
t
20 t
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
减少变换时间
具有TLB的机器可以进一步降低 时钟
周期数/ Cache访问
可以将Cache访问与TLB访问重叠
由于使用虚拟地址中的高位来查找
TLB,而用低位来作为访问Cache的
索引,因而上述策略可以工作
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
Cache和TLB访问重叠
32
TLB
相联查找
index
Cache
1K
4 bytes
10
2
00
PA
Hit/
Miss
20
page #
PA
12
disp
Data
Hit/
Miss
=
IF cache命中 AND (cache标签 = 物理地址)
THEN 将数据提交给CPU
ELSE IF [cache失效 OR (cache标签 = 物理地址)] AND TLB命中
THEN 用TLB中的物理地址来访问存储器
ELSE 做正常的虚拟地址变换
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心
重叠TLB访问的问题
• 重叠的访问只能当用于对cache进行索引的地址位 不会
作为虚拟地址变换的结果而改变时,才能正常工作
• 这通常只适用于小 cache和大页面大小 或者 高n路组相联
的大cache
• 例如:假设cache的容量从4K增长到8K,而其他都不变
20
虚拟页号
11
2
cache
索引
00
这一位被虚拟地址变换所改
变,但是cache查找时也需要
12
偏移量
解决方案:
变成8K字节的页面大小
变成2路祖相联cache (仍然使用10位索引)
1K
10
4
4
2 路组相联cache
北京大学计算机科学技术系 [email protected] 北京大学微处理器研究开发中心