Single-cycle

Download Report

Transcript Single-cycle

Department of Computer and IT Engineering
University of Kurdistan
Pipelining (Multi-Cycle)
By: Dr. Alireza Abdollahpouri
Pipelined MIPS processor
Any instruction set can be implemented in
many different ways
MIPS ISA
Single Cycle
Short CPI
Long CCT
Multi-Cycle
Long CPI
Short CCT
Pipelined
Short CPI
Short CCT
2
Getting the Best of Both Datapaths
Pipelined:
Clock rate = 500 MHz
CPI  1
Single-cycle:
Multicycle:
Clock rate = 125 MHz
CPI = 1
Clock rate = 500 MHz
CPI  4
3
‫مفهوم پردازش خط لوله اي‬
‫‪D‬‬
‫‪C‬‬
‫‪B‬‬
‫‪A‬‬
‫‪‬مثال‪ :‬شستن لباسها‬
‫‪Ali, Bahram, Cathy, Dara ‬‬
‫هر كدام مقداري لباس دارند كه مي خواهند بشورند‪ ،‬خشك كنند و اتو كنند‪.‬‬
‫‪‬عمل شستن ‪ 30‬دقيقه طول مي كشد‪.‬‬
‫‪‬عمل خشك كردن ‪ 40‬دقيقه طول مي كشد‪.‬‬
‫‪‬اتو زدن ‪ 20‬دقيقه طول مي كشد‪.‬‬
‫‪4‬‬
‫لباسشويي به صورت متوالي‬
‫‪Midnight‬‬
‫‪20‬‬
‫‪11‬‬
‫‪40‬‬
‫‪10‬‬
‫‪20 30‬‬
‫‪40‬‬
‫‪9‬‬
‫‪Time‬‬
‫‪20 30‬‬
‫‪8‬‬
‫‪40‬‬
‫‪6 PM‬‬
‫‪7‬‬
‫‪20 30‬‬
‫‪40‬‬
‫‪30‬‬
‫‪A‬‬
‫‪T‬‬
‫‪a‬‬
‫‪s‬‬
‫‪k‬‬
‫‪B‬‬
‫‪C‬‬
‫‪D‬‬
‫در انجام متوالي اعمال مذكور ‪ 6‬ساعت براي كار چهار نفر طول ميكشد‪.‬‬
‫‪5‬‬
‫‪O‬‬
‫‪r‬‬
‫‪d‬‬
‫‪e‬‬
‫‪r‬‬
‫لباسشويي به صورت خط لوله اي‬
‫‪Midnight‬‬
‫‪11‬‬
‫‪10‬‬
‫‪9‬‬
‫‪8‬‬
‫‪6 PM‬‬
‫‪7‬‬
‫‪Time‬‬
‫‪20‬‬
‫‪40‬‬
‫‪40‬‬
‫‪40‬‬
‫‪40‬‬
‫‪30‬‬
‫‪A‬‬
‫‪T‬‬
‫‪a‬‬
‫‪s‬‬
‫‪k‬‬
‫‪B‬‬
‫‪C‬‬
‫‪D‬‬
‫‪6‬‬
‫انجام اعمال مذكور به صورت خط لوله اي ‪ 3.5‬ساعت براي كار چهار‬
‫نفر طول ميكشد‪.‬‬
‫‪O‬‬
‫‪r‬‬
‫‪d‬‬
‫‪e‬‬
‫‪r‬‬
‫مفهوم پايه‬
‫‪ ‬خط لوله‪ :‬چندین دستورالعمل به طور همزمان در حال اجر هستند‪.‬‬
‫‪ ‬خط لوله به بخش ها یا قطعات تقسیم می شود‪.‬‬
‫‪ ‬چرخه ماشین بوسیله کندترین مرحله خط لوله معین می گردد‪.‬‬
‫‪ ‬معموال چرخه ماشین = پالس ساعت‬
‫‪7‬‬
‫خط لوله ای کردن‬
‫‪‬اگ ررر ‪ n‬تکلی ر )‪ (Task‬داش ررته باش رریم ک رره زم رران اج ررار ه ررر ک رردام از ن ه ررا براب ررر ب ررا ‪tn‬‬
‫باش ررد (زم رران انج ررام ل ررل تک ررالی = ‪ ،)n*tn‬ب ررا ررره اینا رره اع ررداد قطع ررات خ ررط لول رره ‪k‬‬
‫باشد و هر قطعه در ‪ tp‬انجام پذیر باشد (پالس ساعت = ‪:)tp‬‬
‫‪ Task‬اول در ‪ k‬پالس )‪ (k*tp‬انجام می پذیرد‪.‬‬
‫‪ Task‬هار دیگر هر کدام در پالس زمانی بعدر (یک پرالس زمرانی) تامیرل خواهنرد شرد‪،‬‬
‫پس زمان الزم برار انجام )‪ (n-1‬تکلی دیگر برابر با ‪ (n-1)*tp‬خواهد شد‪.‬‬
‫‪‬در نتیج رره ا ر رزاپش س رررعت پ ررردازه خ ررط لول رره نس ررات ب رره پ ررردازه یر ر خ ررط لول رره ار از‬
‫رمول زیر محاسبه می گردد‪S = ntn / (k + n - 1)tp :‬‬
‫‪8‬‬
‫نكاتي در مورد پردازش خط لوله اي‬
‫‪‬پردازه خط لوله اي يك كار خاص را سريعت‬
‫نميكند‪ ،‬بلكه توان عمليااي كل را بهبود ميبخشد‪.‬‬
‫‪‬سرعت خط لوله توسط كندترين مرحله محدود‬
‫ميگردد‪.‬‬
‫‪9‬‬
‫‪Time‬‬
‫‪40 20‬‬
‫‪8‬‬
‫‪40‬‬
‫‪7‬‬
‫‪40‬‬
‫‪6 PM‬‬
‫‪30 40‬‬
‫‪ ‬با استفاده از منابع مختل چند كار همزمان باهم‬
‫اجرا ميشوند‪.‬‬
‫‪A‬‬
‫‪ ‬در حالت ايده ل‪ ،‬اسريع به اعداد مراحل خط‬
‫لوله است‪.‬‬
‫‪B‬‬
‫‪‬مراحل نامتعادل (با زمان اجراي نامساوي) سرعت‬
‫و كارايي خط لوله را كاهش ميدهد‪.‬‬
‫‪C‬‬
‫‪‬زماني كه براي پر كردن و خالي كردن خط لوله‬
‫صرف ميشود نیز باعث كاهش سرعت خط لوله‬
‫ميگردد‪.‬‬
‫‪9‬‬
‫‪D‬‬
‫‪T‬‬
‫‪a‬‬
‫‪s‬‬
‫‪k‬‬
‫‪O‬‬
‫‪r‬‬
‫‪d‬‬
‫‪e‬‬
‫‪r‬‬
‫پنج مرحله سيكل دستورالعمل‬
lw
Cycle 1
Cycle 2
Cycle 3
Ifetch
Reg/Dec
Exec
Cycle 4
Mem
Cycle 5
Wr
Ifetch: Instruction Fetch
Fetch the instruction from the Instruction
Memory
Reg/Dec: Registers Fetch and Instruction Decode
Exec: Calculate the memory address
Mem: Read the data from the Data Memory
Wr: Write the data back to the register file
10
MIPS ‫پنج مرحله مسير داده پردازنده‬
IF: Instruction
Fetch
ID Instr. Decode
Reg. Read
EX: Execute
Addr. Calc
MEM:Memory
Access
WB: Write
Back
‫رجیسترهای بين مراحل خط لوله‬
 Need registers between stages
 To hold information produced in previous cycle
‫تجسم خط لوله‬
Time (clock cycles)
Reg
DMem
Ifetch
Reg
DMem
Reg
ALU
DMem
Reg
ALU
O
r
d
e
r
Ifetch
ALU
I
n
s
t
r.
ALU
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7
Ifetch
Ifetch
Reg
Reg
Reg
DMem
Reg
13
‫مشكالاي كه در پردازه خط لوله اي بوجود مي يد‬
 Limits to pipelining: Hazards prevent next instruction
from executing during its designated clock cycle
 Structural hazards: HW cannot support this
combination of instructions
 Data hazards: Instruction depends on result of prior
instruction still in the pipeline
 Control hazards: Caused by delay between the
fetching of instructions and decisions about
changes in control flow (branches and jumps).
14
One Memory Port/ Structural Hazards
Time (clock cycles)
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7
Ifetch
Reg
DMem
Reg
ALU
DMem
Reg
ALU
DMem
Reg
ALU
Instr 3
DMem
ALU
O
r
d
e
r
Instr 2
Reading data from
memory
Reg
ALU
I Load
n
s
Instr 1
t
r.
Ifetch
Ifetch
Ifetch
Reading instruction
Instr 4 from memory
Reg
Ifetch
Reg
Reg
Reg
DMem
Reg
15
One Memory Port/ Structural Hazards
Time (clock cycles)
Stall
Instr 3
DMem
Ifetch
Reg
DMem
Reg
ALU
Ifetch
Bubble
Reg
Reg
DMem
Bubble Bubble
Ifetch
Reg
Reg
Bubble
ALU
O
r
d
e
r
Instr 2
Reg
ALU
I Load Ifetch
n
s
Instr 1
t
r.
ALU
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7
Bubble
DMem
Reg
16
Data Hazard on $1
add $1,$3,$0
sub $4,$1,$5
and $6,$1,$7
or
$8,$1,$9
xor $4,$1,$5
17
‫وابستگيهاي رو به عقب در زمان‬
Time (clock cycles)
xor $10,$1,$11
Reg
Dm
Im
Reg
Dm
Im
Reg
Dm
Im
Reg
ALU
or r$,$1,$9
Im
ALU
and $6,$1,$7
Dm
ALU
sub $4,$1,$3
Reg
ALU
O
r
d
e
r
Im
MEM WB
ALU
I
n
s
t
r.
add $1,$3,$0
IF ID/RF EX
Reg
Reg
Reg
Reg
Dm
Reg
18
Data Hazard ‫ براي حل مشكل‬Forwarding ‫روه‬
‫نتيجه يك مرحله را به محض آماده شدن به جلو برانيم‬
Time (clock cycles)
xor $10,$1,$11
Reg
Dm
Im
Reg
Dm
Im
Reg
Dm
Im
Reg
ALU
or r$,$1,$9
Im
ALU
and $6,$1,$7
Dm
ALU
sub $4,$1,$3
Reg
ALU
O
r
d
e
r
add $1,$3,$0
Im
MEM WB
ALU
I
n
s
t
r.
IF ID/RF EX
Reg
Reg
Reg
Dm
Reg
19
Forwarding ‫اغيی ات سخت ا زاري براي پشتيباني از‬
NextPC
mux
MEM/WR
EX/MEM
ALU
mux
ID/EX
Registers
mux
Immediate
Data
Memory
20
Data Hazard ‫سه نوع‬
 Read After Write (RAW)
InstrJ tries to read operand before InstrI
writes it
I: add r1,r2,r3
J: sub r4,r1,r3
 Caused by a “Dependence” (in compiler
nomenclature). This hazard results from
an actual need for communication.
21
Data Hazard ‫سه نوع‬
 Write After Read (WAR)
InstrJ writes operand before InstrI reads it
I: sub r4,r1,r3
J: add r1,r2,r3
K: mul r6,r1,r7
 Called an “anti-dependence” by compiler
writers.
This results from reuse of the name “r1”.
22
Data Hazard ‫سه نوع‬
 Write After Write (WAW)
InstrJ writes operand before InstrI writes it.
I: sub r1,r4,r3
J: add r1,r2,r3
K: mul r6,r1,r7
 Called an “output dependence” by compiler
writers
This also results from the reuse of name “r1”.
23
Forwarding ‫ حتي با استفاده از‬Data Hazard
24
Forwarding ‫ حتي با استفاده از‬Data Hazard
25
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:
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
26
22: add r8,r1,r9
36: xor r10,r1,r11
Reg
DMem
Ifetch
Reg
DMem
Ifetch
Reg
DMem
Ifetch
Reg
ALU
r6,r1,r7
Ifetch
DMem
ALU
18: or
Reg
ALU
14: and r2,r3,r5
Ifetch
ALU
10: beq r1,r3,36
ALU
Control Hazard on Branches - Three Stage Stall
Reg
Reg
Reg
Reg
DMem
Reg
27
Branch Stall Impact
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
28
Four Branch Hazard Alternatives
1: Stall until branch direction is clear
2: Predict Branch Not Taken
3: Predict Branch Taken
4: Delayed Branch
29
‫پردازش ابر عددي يا ‪Superscalar‬‬
‫استفاده از چند خط لوله به‬
‫صورت موازي‬
‫‪30‬‬
Summary : Control and Pipelining
 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
31
Single Cycle, Mult-Cycle, vs. Pipeline
Single Cycle Implementation:
Cycle 1
Cycle 2
Clk
lw
sw
Waste
Multiple Cycle Implementation:
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9Cycle 10
Clk
lw
IFetch
sw
Dec
Exec
Mem
WB
IFetch
R-type
Dec
Pipeline Implementation:
lw
IFetch
sw
Dec
Exec
Mem
WB
IFetch
Dec
Exec
Mem
WB
Dec
Exec
Mem
R-type IFetch
WB
Exec
Mem
IFetch
Questions
33