Transcript ALU

Department of Computer and IT Engineering
University of Kurdistan
Computer Architecture
Pipeline Processing
By: Dr. Alireza Abdollahpouri
‫مفهوم پردازش خط لوله اي‬
‫‪D‬‬
‫‪C‬‬
‫‪B‬‬
‫‪A‬‬
‫• مثال‪ :‬شستن لباسها‬
‫• ‪Ali, Bahram, Cathy, Dara‬‬
‫هر كدام مقداري لباس دارند كه مي خواهند بشورند‪ ،‬خشك كنند و اتو كنند‪.‬‬
‫• عمل شستن ‪ 30‬دقيقه طول مي كشد‪.‬‬
‫• عمل خشك كردن ‪ 40‬دقيقه طول مي كشد‪.‬‬
‫• اتو زدن ‪ 20‬دقيقه طول مي كشد‪.‬‬
‫‪2‬‬
‫لباسشويي به صورت متوالي‬
‫‪Midnight‬‬
‫‪11‬‬
‫‪10‬‬
‫‪9‬‬
‫‪8‬‬
‫‪7‬‬
‫‪6 PM‬‬
‫‪Time‬‬
‫‪30 40 20 30 40 20 30 40 20 30 40 20‬‬
‫‪A‬‬
‫‪T‬‬
‫‪a‬‬
‫‪s‬‬
‫‪k‬‬
‫‪B‬‬
‫‪C‬‬
‫‪D‬‬
‫• در انجام متوالي اعمال مذكور ‪ 6‬ساعت براي كار چهار نفر طول ميكشد‪.‬‬
‫‪3‬‬
‫‪O‬‬
‫‪r‬‬
‫‪d‬‬
‫‪e‬‬
‫‪r‬‬
‫لباسشويي به صورت خط لوله اي‬
‫‪Midnight‬‬
‫‪11‬‬
‫‪10‬‬
‫‪9‬‬
‫‪7‬‬
‫‪8‬‬
‫‪6 PM‬‬
‫‪Time‬‬
‫‪40 20‬‬
‫‪40‬‬
‫‪40‬‬
‫‪30 40‬‬
‫‪A‬‬
‫‪T‬‬
‫‪a‬‬
‫‪s‬‬
‫‪k‬‬
‫‪B‬‬
‫‪C‬‬
‫‪D‬‬
‫• انجام اعمال مذكور به صورت خط لوله اي ‪ 3.5‬ساعت براي كار چهار نفر طول ميكشد‪.‬‬
‫‪4‬‬
‫‪O‬‬
‫‪r‬‬
‫‪d‬‬
‫‪e‬‬
‫‪r‬‬
‫مفهوم پايه‬
‫• خط لوله‪ :‬چندین دستورالعمل به طور همزمان در حال اجر هستند‪.‬‬
‫• خط لوله به بخش ها یا قطعات تقسیم می شود‪.‬‬
‫• چرخه ماشین )‪:(Machine Cycle‬‬
‫– زمان مورد نیاز برای گذر از یک مرحله‬
‫– چرخه ماشین بوسیله کندترین مرحله خط لوله معین می گردد‪.‬‬
‫– معموال چرخه ماشین = پالس ساعت‬
‫‪5‬‬
‫خط لوله ای کردن‬
‫• اگر ‪ n‬تکلیف )‪ (Task‬داشته باشیم که زمان اجرای هر کدام از آن ها برابر‬
‫با ‪ tn‬باشد (زمان انجام کل تکاالیف = ‪ ،)n*tn‬باا رارا اینکاه تعاداد قطعاات‬
‫خط لوله ‪ k‬باشد و هر قطعه در ‪ tp‬انجام پذیر باشد (پالس ساعت = ‪:)tp‬‬
‫• ‪ Task‬اول در ‪ k‬پالس )‪ (k*tp‬انجام می پذیرد‪.‬‬
‫• ‪ Task‬های دیگر هر کدام در پالس زمانی بعدی (یا پاالس زماانی) تکمیال‬
‫خواهند شاد‪ ،‬پاس زماان زم بارای انجاام )‪ (n-1‬تکلیاف دیگار برابار باا ‪(n-‬‬
‫‪ 1)*tp‬خواهد شد‪.‬‬
‫• در نتیجه ارزایش سرعت پردازش خط لوله نسبت به پردازش غیر خط لولاه‬
‫‪S = ntn / (k + n - 1)tp‬‬
‫ای از ررمول زیر محاسبه می گردد‪:‬‬
‫‪6‬‬
‫نكاتي در مورد پردازش خط لوله اي‬
‫• پردازش خط لوله اي يك كار خاص را سريعتر نميكند‪،‬‬
‫بلكه توان عملياتي كل را بهبود ميبخشد‪.‬‬
‫• سرعت خط لوله توسط كندترين مرحله محدود‬
‫ميگردد‪.‬‬
‫‪9‬‬
‫‪7‬‬
‫‪8‬‬
‫‪6 PM‬‬
‫‪Time‬‬
‫‪40 20‬‬
‫‪40‬‬
‫‪40‬‬
‫‪30 40‬‬
‫• با استفاده از منابع مختلف چند كار همزمان باهم‬
‫اجرا ميشوند‪.‬‬
‫‪A‬‬
‫• در حالت ايده آل‪ ،‬تسريع به تعداد مراحل خط لوله‬
‫است‪.‬‬
‫‪B‬‬
‫• مراحل نامتعادل (با زمان اجراي نامساوي) سرعت و‬
‫كارايي خط لوله را كاهش ميدهد‪.‬‬
‫‪C‬‬
‫• زماني كه براي پر كردن و خالي كردن خط لوله صرف‬
‫ميشود نيز باعث كاهش سرعت خط لوله ميگردد‪.‬‬
‫‪D‬‬
‫‪7‬‬
‫‪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
8
DLX ‫پنج مرحله مسير داده پردازنده‬
Instruction
Fetch
Instr. Decode
Reg. Fetch
Execute
Addr. Calc
Next SEQ PC
Adder
4
Zero?
RS1
L
M
D
MUX
Data
Memory
ALU
Imm
MUX MUX
RD
Reg File
Inst
Memory
Address
RS2
Write
Back
MUX
Next PC
Memory
Access
Sign
Extend
WB Data
9
DLX ‫پنج مرحله مسير داده پردازنده‬
Next SEQ PC
Next SEQ PC
Adder
MUX
MEM/WB
Data
Memory
EX/MEM
ALU
MUX MUX
ID/EX
Reg File
IF/ID
Memory
Address
Imm
Write
Back
Zero?
RS1
RS2
Memory
Access
MUX
Next PC
4
Execute
Addr. Calc
Instr. Decode
Reg. Fetch
WB Data
Instruction
Fetch
Sign
Extend
RD
RD
RD
10
‫تجسم خط لوله‬
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
11
‫مشكالتي كه در پردازش خط لوله اي بوجود مي آيد‬
• 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).
12
One Memory Port/ Structural Hazards
Time (clock cycles)
Reg
DMem
Reg
DMem
Reg
DMem
Reg
ALU
Instr 4
Ifetch
ALU
Instr 3
DMem
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
Ifetch
Ifetch
Reg
Ifetch
Reg
Reg
Reg
DMem
Reg
13
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
14
Data Hazard on r1
add r1, r2, r3
sub r4, r1, r3
and r6, r1, r7
or
r8, r1, r9
xor r10, r1, r11
15
‫وابستگيهاي رو به عقب در زمان‬
Data Hazard on r1:
xor r10,r1,r11
Dm
Reg
Dm
Im
Reg
Dm
Im
Reg
Dm
Im
Reg
ALU
or r8,r1,r9
W
B
Reg
ALU
and r6,r1,r7
Im
ME
M
ALU
sub r4,r1,r3
E
X
ALU
O
r
d
e
r
add r1,r2,r3
ID/R
FReg
ALU
I
n
s
t
r.
Time (clock cycles)
I
F
Im
Reg
Reg
Reg
Dm
Reg
16
Data Hazard ‫ براي حل مشكل‬Forwarding ‫روش‬
‫•نتيجه يك مرحله را به محض آماده شدن به جلو برانيم‬
xor r10,r1,r11
Dm
Reg
Dm
Im
Reg
Dm
Im
Reg
Dm
Im
Reg
ALU
or r8,r1,r9
W
B
Reg
ALU
and r6,r1,r7
Im
ME
M
ALU
sub r4,r1,r3
E
X
ALU
O
r
d
e
r
add r1,r2,r3
ID/R
FReg
ALU
I
n
s
t
r.
Time (clock cycles)
I
F
Im
Reg
Reg
Reg
Dm
Reg
17
Forwarding‫تغييرات سخت افزاري براي پشتيباني از‬
NextPC
mux
MEM/WR
EX/MEM
ALU
mux
ID/EX
Registers
mux
Immediate
Data
Memory
18
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.
19
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”.
20
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”.
21
Forwarding ‫ حتي با استفاده از‬Data Hazard
and r6,r1,r7
or
r8,r1,r9
DMem
Ifetch
Reg
DMem
Reg
Ifetch
Ifetch
Reg
Reg
Reg
DMem
ALU
O
r
d
e
r
sub r4,r1,r6
Reg
ALU
lw r1, 0(r2) Ifetch
ALU
I
n
s
t
r.
ALU
Time (clock cycles)
Reg
DMem
Reg
22
Forwarding ‫ حتي با استفاده از‬Data Hazard
and r6,r1,r7
or r8,r1,r9
Reg
DMem
Ifetch
Reg
Bubble
Ifetch
Bubble
Reg
Bubble
Ifetch
Reg
DMem
Reg
Reg
DMem
ALU
sub r4,r1,r6
Ifetch
ALU
O
r
d
e
r
lw r1, 0(r2)
ALU
I
n
s
t
r.
ALU
Time (clock cycles)
Reg
DMem
23
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
24
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
25
Reg
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
26
Four Branch Hazard Alternatives
#1: Stall until branch direction is clear
#2: Predict Branch Not Taken
#3: Predict Branch Taken
#4: Delayed Branch
27
‫پردازش ابر عددي يا ‪Superscalar‬‬
‫استفاده از چند خط لوله به‬
‫صورت موازي‬
‫‪28‬‬
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
29