1 / 38

CS1104: Computer Organisation comp.nus.sg/~cs1104

CS1104: Computer Organisation http://www.comp.nus.edu.sg/~cs1104. School of Computing National University of Singapore. PII Lecture 7: Pipelining. Pipelining Analogy Pipelining Instruction Execution Hazards Advanced Pipelining Concepts by Analogy. PII Lecture 7: Pipelining. Reading:

Download Presentation

CS1104: Computer Organisation comp.nus.sg/~cs1104

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1104: Computer Organisation http://www.comp.nus.edu.sg/~cs1104 School of Computing National University of Singapore

  2. PII Lecture 7: Pipelining • Pipelining Analogy • Pipelining Instruction Execution • Hazards • Advanced Pipelining Concepts by Analogy Pipelining

  3. PII Lecture 7: Pipelining • Reading: • Chapter 10 of textbook, which is Chapter 8 in “Computer Organization” by Hamacher, Vranesic and Zaky. • Optional reading: Chapter 5 in “Computer Organization & Design” by Patterson and Hennessy. Pipelining

  4. rd instruction memory PC registers rs Data memory rt ALU +4 imm 1. Instruction Fetch 2. Decode/ Register Read 3. Execute 4. Memory 5. Reg. Write Recap: Datapath Pipelining

  5. A B C D Gotta Do Laundry • Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, fold, and put away. • Washer takes 30 minutes. • Dryer takes 30 minutes. • “Folder” takes 30 minutes. • “Stasher” takes 30 minutes to put clothes into drawers. Pipelining

  6. 6 PM 7 8 9 10 11 12 1 2 AM Time 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 A B C D Sequential Laundry • Sequential laundry takes 8 hours for 4 loads. T a s k O r d e r Pipelining

  7. 6 PM 7 8 9 10 11 12 1 2 AM Time 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 T a s k O r d e r A B C D Pipelined Laundry • Pipelined laundry takes 3.5 hours for 4 loads. Pipelining

  8. General Definitions • Latency: time to completely execute a certain task. • For example, time to read a sector from disk is disk access time or disk latency. • Throughput: amount of work that can be done over a period of time. Pipelining

  9. 6 PM 7 8 9 Time T a s k O r d e r 30 30 30 30 30 30 30 A B C D Pipelining Lessons • Pipelining doesn’t help latency of single task, it helps throughput of entire workload. • Multiple tasks operating simultaneously using different resources. • Potential speedup = number of pipe stages. • Time to “fill” pipeline and time to “drain” it reduces speedup: 2.3 times versus 4 times (optimal) in this example. Pipelining

  10. 6 PM 7 8 9 Time T a s k O r d e r 30 30 30 30 30 30 30 A B C D Pipelining Lessons (2) • Suppose new Washer takes 20 minutes, new Stasher takes 20 minutes. How much faster is the pipeline? • Pipeline rate limited by slowest pipeline stage. • Unbalanced lengths of pipe stages also reduces speedup. Pipelining

  11. 2 4 6 8 10 12 14 16 18 Program execution order Time lw $1, 100($0) Instruction fetch Instruction fetch ALU ALU Data access Data access Reg Reg Reg Reg lw $2, 200($0) lw $3, 300($0) 8 ns Instruction fetch 8 ns … 8 ns 2 4 6 8 10 12 14 Time Program execution order lw $1, 100($0) Instruction fetch lw $2, 200($0) ALU Data access Instruction fetch Reg Reg lw $3, 300($0) 2 ns ALU ALU Data access Data access Instruction fetch Reg Reg Reg Reg 2 ns 2 ns 2 ns 2 ns 2 ns 2 ns Pipelining Lessons (3) • Improve performance by increasing instruction throughput. Ideal speedup = number of stages Pipelining

  12. Steps in Executing MIPS Instructions • IFetch: Fetch instruction, increment PC. • Decode instruction, read registers. • Execute: Memory reference (calculate address), perform arithmetic/logic operation. • Memory: Load (read data from memory), Store (write data to memory). • Write back: Write data to register. Pipelining

  13. Time IFtch Dcd Exec Mem WB IFtch Dcd Exec Mem WB IFtch Dcd Exec Mem WB IFtch Dcd Exec Mem WB IFtch Dcd Exec Mem WB IFtch Dcd Exec Mem WB Pipelined Execution Representation • Every instruction must take same number of steps, also called pipeline “stages”, so some will go idle sometimes. Pipelining

  14. Why cache is needed • Pipelining achieves its best effect if each stage of the pipeline takes the same amount of time (one clock cycle). • However, the first stage, instruction fetch, involves accessing the memory, which is slow. • How do we improve the time? By using cache. (To be covered later.) Pipelining

  15. IFtch Dcd Exec Mem WB D$ I$ Reg Reg ALU Datapath Figures • Use datapath figures to represent pipeline. Pipelining

  16. Time I$ Reg ALU D$ Reg Load I$ ALU D$ Reg Reg Add I$ Reg ALU D$ Reg Store D$ Reg ALU I$ Reg Sub I$ D$ Reg ALU Reg Or Graphical Pipeline Representation • In Reg, right-half is read, left-half write. I n s t r. o r d e r Pipelining

  17. Example • Suppose 2 ns for memory access, 2 ns for ALU operation, and 1 ns for register file read or write. • Non-pipelined execution: • lw : IF + Read Reg + ALU + Memory + Write Reg = 2 + 1 + 2 + 2 + 1 = 8 ns • add: IF + Read Reg + ALU + Write Reg = 2 + 1 + 2 + 1 = 6 ns • Pipelined execution: • Max(IF,Read Reg,ALU,Memory,Write Reg) = 2 ns Pipelining

  18. Pipelining • What makes it easy? • All instructions are the same length • Just a few instruction formats • Memory operands appear only in loads and stores • What makes it hard? • Structural hazards: suppose we had only one memory. • Control hazards: need to worry about branch instructions. • Data hazards: an instruction depends on a previous instruction. Pipelining

  19. 2 AM 12 6 PM 1 8 7 11 10 9 Time 30 30 30 30 30 30 30 T a s k O r d e r A bubble B C D E F Pipeline Hazard: Matching socks in later load A depends on D; stall since “folder” tied up. Pipelining

  20. Problems for Computers • Limits to pipelining:Hazards prevent next instruction from executing during its designated clock cycle. • Structural hazards: Hardware cannot support this combination of instructions (single person to fold and put clothes away). • Control hazards: Pipelining of branches and other instructions stall the pipeline until the hazard “bubbles” in the pipeline. • Data hazards: Instruction depends on result of prior instruction still in the pipeline (missing sock). Pipelining

  21. Data Hazard • Data hazard: an instruction depends on the results of a previous instruction still in the pipeline. • Data dependencies: sub $2, $1, $3# Reg $2 written by sub and $12, $2, $5# 1st operand($2) depends on sub or $13, $6, $2# 2nd operand($2) depends on sub and $14, $2, $2# 1st($2) and 2nd($2) depend on sub sw $15, 100($2)# Base ($2) depends on sub Pipelining

  22. I$ ALU add D$ Reg Reg bubble bubble bubble ALU D$ Reg Reg sub I$ Data Hazard (2) • Data hazard could stall the pipeline. • The add instruction does not write its result until the fifth stage, meaning that we would have to add three bubbles to the pipelines. add $s0, $t0, $t1 sub $12, $s0, $t3 Memory bus data lines Pipelining

  23. I$ ALU D$ Reg Reg ALU D$ Reg Reg I$ Data Hazard (3) • Solution: Forwarding • We do not need to wait for the instruction to complete before trying to resolve the data hazard. As soon as the ALU creates the sum for the add, we can supply it as input for the subtract. add sub Pipelining

  24. I$ ALU D$ Reg Reg bubble ALU D$ Reg Reg I$ Data Hazard (4) • However, forwarding cannot solve all data hazards. • Suppose the first instruction is a load instead. lw $s0, 20($t1) sub $12, $s0, $t3 Even with forwarding, we would have to stall one stage. lw sub Pipelining

  25. Time Load I$ Reg ALU D$ Reg Instr 1 I$ ALU D$ Reg Reg Instr 2 I$ Reg ALU D$ Reg Instr 3 D$ Reg ALU I$ Reg Instr 4 I$ D$ Reg ALU Reg Structural Hazard #1: Single Memory Read some memory twice in the same clock cycle. I n s t r. o r d e r Pipelining

  26. Structural Hazard #1: Single Memory (2) • Solution: • infeasible and inefficient to create second memory • so simulate this by having two level 1 caches • have both an L1 instruction cache and an L1 data cache • need more complex hardware to control when both caches miss Pipelining

  27. Time Load I$ Reg ALU D$ Reg Instr 1 I$ ALU D$ Reg Reg Instr 2 I$ Reg ALU D$ Reg Instr 3 D$ Reg ALU I$ Reg Instr 4 I$ D$ Reg ALU Reg Structural Hazard #2: Register Cannot read and write to register simultaneously. I n s t r. o r d e r Pipelining

  28. Structural Hazard #2: Register (2) • Fact: Register access is VERY fast: takes less than half the time of ALU stage. • Solution: introduce convention • Always Write to Registers during first half of each clock cycle. • Always Read from Registers during second half of each clock cycle. • Result: can perform Read and Write during same clock cycle. Pipelining

  29. Control Hazard: Branching • Suppose we put branch decision-making hardware in ALU stage • then two more instructions after the branch will always be fetched, whether or not the branch is taken • Desired functionality of a branch • if we do not take the branch, don’t waste any time and continue executing normally • if we take the branch, don’t execute any instructions after the branch, just go to the desired label Pipelining

  30. Control Hazard: Branching (2) • Initial Solution: Stall until decision is made • Insert “no-op” instructions: those that accomplish nothing, just take time. • Drawback: branches take 3 clock cycles each (assuming comparator is put in ALU stage). Pipelining

  31. Control Hazard: Branching (3) • Optimization #1: • Move comparator up to stage 2. • As soon as instruction is decoded (opcode identified as a branch), immediately make a decision and set the value of the PC (if necessary). • Benefit: since branch is complete in stage 2, only one unnecessary instruction is fetched, so only one no-op is needed. • Side note: This means that branches are idle in stages 3, 4 and 5. Pipelining

  32. I$ ALU D$ Reg Reg Add I$ ALU D$ Reg Reg Beq bubble ALU Load D$ Reg Reg I$ Control Hazard: Branching (4) • Insert a single no-op (bubble) Time I n s t r. o r d e r • Impact: 2 clock cycles per branch instructionslow Pipelining

  33. Control Hazard: Branching (5) • Optimization #2: • Old definition: if we take the branch, none of the instructions after the branch get executed by accident. • New definition: whether or not we take the branch, the single instruction immediately following the branch gets executed (called the branch-delay slot). Pipelining

  34. Control Hazard: Branching (6) • Branch-Delay Slot • Worst-Case Scenario: Always put a no-op in the branch-delay slot. • Better Case: Find an instruction preceding the branch which can be placed in the branch-delay slot without affecting flow of the program • re-ordering instructions is a common method of speeding up programs • compiler must be very smart in order to find instructions to do this • usually can find such an instruction at least 50% of the time Pipelining

  35. or $8, $9 ,$10 add $1 ,$2,$3 sub $4, $5,$6 add $1 ,$2,$3 beq $1, $4, Exit sub $4, $5,$6 beq $1, $4, Exit or $8, $9 ,$10 xor $10, $1,$11 xor $10, $1,$11 Exit: Exit: Non-delayed vs. Delayed Branch Delayed branch Non-delayed branch Pipelining

  36. Things To Remember • Optimal Pipeline • Each stage is executing part of an instruction in each clock cycle. • One instruction finishes during each clock cycle. • On average, execute far more quickly. • What makes this work? • Similarities between instructions allow us to use same stages for all instructions (generally). • Each stage takes about the same amount of time as all others: little wasted time. Pipelining

  37. Things To Remember (2) • Pipelining a Big Idea: widely used concept • What makes it less than perfect? • Structural hazards: suppose we had only one cache?  Need more HW resources • Control hazards: need to worry about branch instructions?  Delayed branch • Data hazards: an instruction depends on a previous instruction? Pipelining

  38. End of file

More Related