Lecture2a PDF
Lecture2a PDF
and Caches
DAP.F96 1
Review, #1
• Designing to Last through Trends
Capacity Speed
Logic 2x in 3 years 2x in 3 years
DRAM 4x in 3 years 1.4x in 10 years
Disk 4x in 3 years 1.4x in 10 years
• Time to run the task
– Execution time, response time, latency
• Tasks per day, hour, week, sec, ns, …
– Throughput, bandwidth
• “X is n times faster than Y” means
ExTime(Y) Performance(X)
--------- = --------------
ExTime(X) Performance(Y)
DAP.F96 2
Review, #2
• Amdahl’s Law:
1
ExTimeold
Speedupoverall = =
(1 - Fractionenhanced) + Fractionenhanced
ExTimenew
• CPI Law: Speedupenhanced
0%
Mini W/S PC
5 4.7
3.8
4 3.5 Average Discount
1.5
1 Component Costs
0
Mini W/S PC DAP.F96 4
Pipelining: Its Natural!
• Laundry Example
• Ann, Brian, Cathy, Dave A B C D
each have one load of clothes
to wash, dry, and fold
• Washer takes 30 minutes
30 40 20 30 40 20 30 40 20 30 40 20
T
a A
s
k
B
O
r
d C
e
r D
• Sequential laundry takes 6 hours for 4 loads
• If they learned pipelining, how long would laundry take?
DAP.F96 6
Pipelined Laundry
Start work ASAP
6 PM 7 8 9 10 11 Midnight
Time
30 40 40 40 40 20
T
a A
s
k
B
O
r
d C
e
r D
DAP.F96 9
5 Steps of DLX Datapath
Figure 3.1, Page 130
IR
L
M
D
DAP.F96 10
Pipelined DLX Datapath
Figure 3.4, page 137
Instruction
Fetch Instr. Decode Execute
Reg. Fetch Addr. Calc. Write
Back
Memory
Access
I
n
s
t
r.
O
r
d
e
r
DAP.F96 12
Its Not That Easy for
Computers
• Limits to pipelining: Hazards prevent next
instruction from executing during its designated
clock cycle
– Structural hazards: HW cannot support this combination of
instructions (single person to fold and put clothes away)
– Data hazards: Instruction depends on result of prior
instruction still in the pipeline (missing sock)
– Control hazards: Pipelining of branches & other instructions
that change the PC (football uniform analogy)
• Common solution is to stall the pipeline until the
hazard is resolved, inserting one or more
“bubbles” in the pipeline
DAP.F96 13
One Memory Port/Structural Hazards
Figure 3.6, Page 142
Time (clock cycles)
Load
I
n
s
t Instr 1
r.
O Instr 2
r
d
e Instr 3
r
Instr 4
DAP.F96 14
One Memory Port/Structural Hazards
Figure 3.7, Page 143
Time (clock cycles)
Load
I
n
s
t Instr 1
r.
O Instr 2
r
d
e stall
r
Instr 3
DAP.F96 15
Speed Up Equation for
Pipelining
CPIpipelined = Ideal CPI + Pipeline stall clock cycles per instr
DAP.F96 16
Example: Dual-port vs. Single-port
• 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 DAP.F96 17
Data Hazard on R1
Figure 3.9, page 147
Time (clock cycles)
IF ID/RF EX MEM WB
I
n
add r1,r2,r3
s
t
r. sub r4,r1,r3
O
r and r6,r1,r7
d
e
r or r8,r1,r9
DAP.F96 19
Three Generic Data Hazards
InstrI followed by InstrJ
DAP.F96 20
Three Generic Data Hazards
InstrI followed by InstrJ
I
n
s
add r1,r2,r3
t
r.
sub r4,r1,r3
O
r
d and r6,r1,r7
e
r
or r8,r1,r9
xor r10,r1,r11
DAP.F96 23
HW Change for Forwarding
Figure 3.20, Page 161
DAP.F96 24
Data Hazard Even with Forwarding
Figure 3.12, Page 153
I lw r1, 0(r2)
n
s
t
r. sub r4,r1,r6
O
r and r6,r1,r7
d
e
r
or r8,r1,r9
DAP.F96 25
Data Hazard Even with Forwarding
Figure 3.13, Page 154
Time (clock cycles)
I
n
s lw r1, 0(r2)
t
r.
O
sub r4,r1,r6
r
d
e and r6,r1,r7
r
or r8,r1,r9
DAP.F96 26
Software Scheduling to Avoid
Load Hazards
Try producing fast code for
a = b + c;
d = e – f;
assuming a, b, c, d ,e, and f in memory.
Slow code: Fast code:
LW Rb,b LW Rb,b
LW Rc,c LW Rc,c
ADD Ra,Rb,Rc LW Re,e
SW a,Ra ADD Ra,Rb,Rc
LW Re,e LW Rf,f
LW Rf,f SW a,Ra
SUB Rd,Re,Rf SUB Rd,Re,Rf
SW d,Rd SW d,Rd DAP.F96 27
Control Hazard on Branches
Three Stage Stall
DAP.F96 28
Branch Stall Impact
DAP.F96 29
Pipelined DLX Datapath
Figure 3.22, page 163
DAP.F96 30
Four Branch Hazard Alternatives
#1: Stall until branch direction is clear
#2: Predict Branch Not Taken
– Execute successor instructions in sequence
– “Squash” instructions in pipeline if branch actually taken
– Advantage of late pipeline state update
– 47% DLX branches not taken on average
– PC+4 already calculated, so use it to get next instruction
#3: Predict Branch Taken
– 53% DLX branches taken on average
– But haven’t calculated branch target address in DLX
» DLX still incurs 1 cycle branch penalty
» Other machines: branch target known before outcome
DAP.F96 31
Four Branch Hazard Alternatives
branch instruction
sequential successor1
sequential successor2
........ Branch delay of length n
sequential successorn
branch target if taken
DAP.F96 32
Delayed Branch
DAP.F96 33
Evaluating Branch Alternatives
Pipeline speedup = Pipeline depth
1 + Branch frequency×Branch penalty
DAP.F96 34
Pipelining Summary
DAP.F96 35
Levels of the Memory Hierarchy
Capacity Upper Level
Access Time Staging
Cost Xfer Unit faster
CPU Registers
100s Bytes Registers
<10s ns
prog./compiler
Instr. Operands 1-8 bytes
Cache
K Bytes
10-100 ns
Cache
$.01-.001/bit cache cntl
Blocks 8-128 bytes
Main Memory
M Bytes Memory
100ns-1us
$.01-.001 OS
Pages 512-4K bytes
Disk
G Bytes
ms-3
Disk
-4
10 - 10 cents user/operator
Files Mbytes
Tape Larger
infinite
sec-min Tape Lower Level
10 -6
DAP.F96 36
The Principle of Locality
DAP.F96 37
Memory Hierarchy: Terminology
• Hit: data appears in some block in the upper level
(example: Block X)
– Hit Rate: the fraction of memory access found in the upper level
– Hit Time: Time to access the upper level which consists of
RAM access time + Time to determine hit/miss
• Miss: data needs to be retrieve from a block in the
lower level (Block Y)
– Miss Rate = 1 - (Hit Rate)
– Miss Penalty: Time to replace a block in the upper level +
Time to deliver the block the processor
• Hit Time << Miss Penalty Lower Level
To Processor Upper Level Memory
Memory
Blk X
From Processor Blk Y
DAP.F96 38
Cache Measures
31 9 4 0
Cache Tag Example: 0x50 Cache Index Byte Select
Ex: 0x01 Ex: 0x00
Stored as part
of the cache “state”
: :
Byte 31 Byte 1 Byte 0 0
0x50 Byte 63 Byte 33 Byte 32 1
2
3
: : :
:
Byte 1023 Byte 992 31
DAP.F96 41
Two-way Set Associative Cache
• N-way set associative: N entries for each Cache Index
– N direct mapped caches operates in parallel
• Example: Two-way set associative cache
– Cache Index selects a “set” from the cache
– The two tags in the set are compared in parallel
– Data is selected based on the tag result
Cache Index
Valid Cache Tag Cache Data Cache Data Cache Tag Valid
Cache Block 0 Cache Block 0
: : : : : :
Adr Tag
Compare Sel1 1 Mux 0 Sel0 Compare
OR
Cache Block DAP.F96 42
Hit
Disadvantage of Set Associative Cache
• N-way Set Associative Cache v. Direct Mapped Cache:
– N comparators vs. 1
– Extra MUX delay for the data
– Data comes AFTER Hit/Miss
• In a direct mapped cache, Cache Block is available
BEFORE Hit/Miss:
– Possible to assume a hit and continue. Recover later if miss.
Cache Index
Valid Cache Tag Cache Data Cache Data Cache Tag Valid
Cache Block 0 Cache Block 0
: : : : : :
Adr Tag
Compare Sel1 1 Mux 0 Sel0 Compare
OR
DAP.F96 43
Cache Block
Hit
4 Questions for Memory
Hierarchy
• Q1: Where can a block be placed in the upper
level? (Block placement)
• Q2: How is a block found if it is in the upper
level?
(Block identification)
• Q3: Which block should be replaced on a
miss?
(Block replacement)
• Q4: What happens on a write?
(Write strategy)
DAP.F96 44
Q1: Where can a block be
placed in the upper level?
• Block 12 placed in 8 block cache:
– Fully associative, direct mapped, 2-way set associative
– S.A. Mapping = Block Number Modulo Number Sets
DAP.F96 45
Q2: How is a block found if it is in
the upper level?
DAP.F96 46
Q3: Which block should be replaced
on a miss?
• Easy for Direct Mapped
• Set Associative or Fully Associative:
– Random
– LRU (Least Recently Used)
Associativity: 2-way 4-way 8-way
Size LRURandom LRURandom LRURandom
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%
DAP.F96 47
Q4: What happens on a write?
Write Buffer
DAP.F96 50
A Modern Memory Hierarchy
• By taking advantage of the principle of locality:
– Present the user with as much memory as is available in the
cheapest technology.
– Provide access at the speed offered by the fastest technology.
Processor
Control Tertiary
Secondary Storage
Storage
Second Main (Disk)
(Disk)
On-Chip
Registers
Level Memory
Cache
reg
pages
frame
Paging Organization
virtual and physical address space partitioned into blocks of equal size
page frames
pages DAP.F96 52
Address Map
V = {0, 1, . . . , n - 1} virtual address space n>m
M = {0, 1, . . . , m - 1} physical address space
Page Table
Page Table
Base Reg Access
V PA + actually, concatenation
index Rights
is more likely
into
page
table table located physical
in physical memory
memory DAP.F96 54
address
Virtual Address and a Cache
VA PA miss
Trans- Main
CPU Cache
lation Memory
hit
data
This makes cache access very expensive, and this is the "innermost
loop" that you want to go as fast as possible
software enforced alias boundary: same lsb of VA &PA > cache size
DAP.F96 55
TLBs
A way to speed up translation is to use a special cache of recently
used page table entries -- this has many names, but the most
frequently used is Translation Lookaside Buffer or TLB
DAP.F96 56
Translation Look-Aside Buffers
Just like any other cache, the TLB can be organized as fully associative,
set associative, or direct mapped
TLBs are usually small, typically not more than 128 - 256 entries even on
high end machines. This permits fully associative
lookup on these machines. Most mid-range machines use small
n-way set associative organizations.
hit
VA PA miss
TLB Main
CPU Cache
Lookup Memory
DAP.F96 58
Overlapped Cache & TLB Access
assoc index
32 TLB Cache 1K
lookup
4 bytes
10 2
00
PA Hit/ Data
20 12 PA Hit/
Miss Miss
page # disp
IF cache hit AND (cache tag = PA) then deliver data to CPU
ELSE IF cache miss and TLB hit THEN
access memory with the PA from the TLB
ELSE do standard VA translation
DAP.F96 59
Problems With Overlapped TLB Access
Overlapped access only works as long as the address bits used to
index into the cache do not change as the result of VA translation
This usually limits things to small caches, large page sizes, or high
n-way set associative caches if you want a large cache
11 2
cache
index 00
This bit is changed
by VA translation, but
20 12 is needed for cache
virt page # disp lookup
Solutions:
go to 8K byte page sizes;
go to 2 way set associative cache; or
SW guarantee VA[13]=PA[13]
DAP.F96 61
Summary #2: TLB, Virtual Memory
• Caches, TLBs, Virtual Memory all understood by
examining how they deal with 4 questions: 1) Where
can block be placed? 2) How is block found? 3) What
block is repalced on miss? 4) How are writes
handled?
• Page tables map virtual address to physical address
• TLBs are important for fast translation
• TLB misses are significant in processor
performance: (funny times, as most systems can’t
access all of 2nd level cache without TLB misses!)
DAP.F96 62
Summary #3: Memory Hierachy
• VIrtual memory was controversial at the time:
can SW automatically manage 64KB across many
programs?
– 1000X DRAM growth removed the controversy
• Today VM allows many processes to share single
memory without having to swap all processes to
disk; VM protection is more important than memory
hierarchy
• Today CPU time is a function of (ops, cache misses)
vs. just f(ops):
What does this mean to Compilers, Data structures,
Algorithms?
DAP.F96 63