Pipelining: Overlapping Instructions
A pipeline splits each instruction into short stages and starts a new instruction every cycle, the way an assembly line works. Each instruction takes just as long as before (or a little longer), but once the line is full one finishes every cycle, so instructions take cycles and the speedup approaches .
What the symbols mean
- Number of stages
How many steps each instruction is split into (5 in the classic design: IF, ID, EX, MEM, WB).
- Number of instructions
How many instructions go through the pipeline.
What the symbols mean
- Speedup
Unpipelined time (k cycles per instruction) divided by pipelined time.
What the symbols mean
- Stage delay
How long the logic in stage j takes.
- Register overhead
The time to latch results between stages, paid once per cycle.
Explanation
Pipelining is simply the assembly-line trick applied to instructions. Each instruction goes through the same steps: fetch it from memory (IF), decode it and read its registers (ID), execute it (EX), access data memory (MEM) and write the result back (WB). Instead of finishing one instruction completely before starting the next, the processor starts a new instruction every cycle, so up to five are in progress at once, each in a different stage. In , is the number of stages and is the number of instructions; the formula just counts clock ticks. It's the most important idea in processor design, and the math behind it is pleasantly small.
A laundry picture. Washing, drying and folding a load take 30 minutes each. Doing four loads one at a time takes minutes. But as soon as load 1 moves to the dryer, load 2 can go in the washer. Now a load comes out every 30 minutes once things get going, and four loads take minutes. Each load still spends 90 minutes in the system. The gain is that the machines are never idle.
Counting cycles. Give each stage one clock cycle. The first instruction needs cycles to go all the way through. After that, a new instruction comes out the end every cycle, because each one started a cycle after the one before it. So the remaining instructions add one cycle each:
The is the cost of filling the pipe (and draining it at the end), paid once.
The speedup approaches . Without pipelining, each instruction takes all cycles alone, so of them take . The speedup is
To see what happens for long programs, divide the top and bottom by :
A 5-stage pipeline running millions of instructions is almost exactly 5 times faster. There's an animation further down that shows instructions filling the pipe and then finishing one per cycle.
Throughput versus latency. Latency is how long one instruction takes from start to finish; throughput is how many finish per second. Pipelining doesn't shorten latency at all. It raises throughput by keeping every stage busy. In fact latency gets slightly worse, for two reasons. The clock has to fit the slowest stage, , so faster stages sit idle part of every cycle. And every cycle pays the register overhead , the time to save each stage's results for the next one. That's why designers try to balance the stages: the ideal times speedup assumes the work splits into equal pieces.
A common mistake. It's tempting to think pipelining makes each instruction run faster. It doesn't: one instruction still takes cycles, and with unbalanced stages and register overhead it takes more real time than before. Pipelining wins by overlap. A related slip is forgetting the fill time and using for a short run: for 10 instructions on 5 stages the speedup is , not 5.
In the performance equation, pipelining is what lets a processor have both a short clock period and a CPI near 1. The next lessons are about everything that pushes CPI back above 1.
See it
Worked examples
A -stage pipeline runs instructions, one stage per cycle. How many cycles does it take, and what is the speedup over an unpipelined machine that spends cycles of the same length on each instruction?
- 1
Identify the numbers: stages and instructions.
An unpipelined datapath has stage delays of , , , and ps. Pipelining it adds ps of register overhead per stage. Find the pipelined clock period, the latency of one instruction, and the speedup for a long program.
- 1
Unpipelined, an instruction runs through all the logic in one long cycle: ps per instruction.
Practice
Type your answer and check it. Solve them all to complete the lesson.
How many cycles does a -stage pipeline need to run instructions?
A -stage pipeline runs instructions. What is its speedup over an unpipelined machine taking cycles of the same length per instruction? Give 3 decimal places.
An -stage pipeline with perfectly balanced stages and no stalls runs a very long program. What speedup does it approach?
A 4-stage pipeline has stage delays of , , and ps, plus ps of register overhead per stage. What is its maximum clock rate, in GHz? Give 3 decimal places.
A 4-stage pipeline has stage delays of , , and ps, plus ps of register overhead per stage. What is the latency of one instruction, in ps?
What is the smallest number of instructions for which a -stage pipeline's speedup is greater than ?
A single-cycle processor has a ps clock and CPI . A 5-stage pipelined version has a ps clock and, ignoring hazards, CPI as well. A program executes instructions. What is its CPU time on the pipelined version, in milliseconds? (The fill time of 4 cycles is negligible.)
Which statement about pipelining is true?
The hardest one here. Get it, and you've got this lesson.
A designer splits ps of logic into equal stages, each with ps of register overhead, so ps. Deeper pipelines suffer more from hazards: CPI . Which whole number minimizes the average time per instruction, ?
Where it's used
Every modern processor core is pipelined, typically with 10 to 20 stages, and the same math runs GPU shader pipelines, network packet processors and video encoders. Outside computers, it's the arithmetic of any production line: a car plant with 40 stations and a car leaving every minute has each car in the plant for 40 minutes, but builds 60 cars an hour.