TactivergeLite

Lite / Independent Engineering Research / Week 20

Pipelining: an assembly line for instructions

A laundry takes 30 minutes to wash, 40 to dry and 20 to fold. How long do four loads take if you never wait for one to finish before starting the next?

About 45 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Pipelining is why one processor core retires billions of instructions per second. It is also a general engineering pattern: video codecs, network routers and factory lines all overlap stages. Understanding its limits stops you expecting a 20-stage pipeline to be 20 times faster.

Predict

5 instructions on an ideal 5-stage pipeline (one cycle per stage, no hazards). How many cycles?

  1. 5
  2. 9
  3. 25
  4. 10
Guess first, then reveal

The first instruction takes 5 cycles to go through; each later one finishes one cycle after the previous: 5 + 4 = 9. Unpipelined (5 cycles each) it would be 25.

Discover

Choose 'Independent ALU ops', turn forwarding on and choose 'Perfect predictor'. Read the diagonal staircase and check the cycle count against k + n − 1. Then pick the other programs to see the ideal break: that is next lesson's topic.

What the interactive shows (text version)

The pipeline table has one row per instruction and one column per clock cycle; each instruction's IF, ID, EX, MEM and WB cells shift one column to the right of the previous instruction's, forming a staircase. For 5 independent instructions it ends at cycle 9.

You can also open the interactive on the full site when you have a faster connection.

Understand

Throughput versus latency

Pipelining does not make one instruction faster: it still passes through every stage, and register overhead makes it slightly slower. What improves is throughput: once full, the pipeline completes one instruction every cycle. Ideal CPI approaches 1 for long runs.

The classic five stages

IF fetches the instruction, ID decodes it and reads registers, EX uses the ALU, MEM accesses data memory, WB writes the result back. Registers between stages hold each instruction's state so the stages can work on different instructions at once.

Balance and overhead

The clock must suit the slowest stage, and every stage boundary adds a register's setup and clock-to-output delay. Deeper pipelines get diminishing returns and, as you will see, pay more for every hazard. Modern designs settle on roughly 10–20 stages as a compromise.

Explain it another way

Intuitive

A sandwich bar with one person doing everything makes a sandwich every 4 minutes. Four people, each doing one step, make one every minute once the line is going, though each sandwich still takes 4 minutes.

Technical

A k-stage pipeline has fill time (k − 1 cycles) and drain time; steady-state throughput is one instruction per cycle. Cycle time = max stage delay + register overhead (setup + clock-to-Q + clock skew).

Mathematical

S(n) = nT / ((k + n − 1)(T/k + o)). For n = 100, k = 5, o = 0: S = 500/104 ≈ 4.81. Efficiency S/k → 1 only as n → ∞.

Applied

A GPU is a deep pipeline in both senses: thousands of pixels flow through vertex, raster and shading stages. Its latency for one pixel is long; its throughput is enormous.

Derivation: How much faster can a pipeline be?
  1. Unpipelined: each instruction takes T, so n instructions take n·T.

  2. Split into k stages. If perfectly balanced, each stage is T/k, but each needs a pipeline register adding overhead o. Cycle = T/k + o.

  3. Pipelined time for n instructions = (k + n − 1)(T/k + o).

  4. Speed-up S = n·T / ((k + n − 1)(T/k + o)). As n → ∞, S → T/(T/k + o) = k / (1 + k·o/T).

  5. With no overhead the limit is k. With o = 0.1 T, a 5-stage pipe tops out at 5/1.5 ≈ 3.3×; a 20-stage pipe at 20/3 ≈ 6.7×, not 20×.

  6. Unbalanced stages are worse: the cycle is set by the slowest stage, so S ≤ T / max(stage delay).

Worked example 1

Stages of 200, 150, 250, 300 and 100 ps, register overhead 20 ps. Find the cycle time and the speed-up for many instructions versus the 1000 ps unpipelined design.

Cycle = slowest stage + overhead = 300 + 20 = 320 ps. Limit speed-up = 1000/320 ≈ 3.1×, well below 5× because the stages are unbalanced.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

cycles

×

Practice 3. Compared with the unpipelined design, the latency of a single instruction in a pipeline is…

cycles

×

Apply it

Sketch a 4-stage pipeline for a robot's sensor loop: read sensor, filter, decide, actuate. Give each stage a realistic time, find the cycle time and throughput, then argue whether the pipeline's extra latency is acceptable for a control loop.

Reflect

Where else have you seen a pipeline (in software, in a kitchen, in a factory)? What was its slowest stage?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Single-cycle, multi-cycle and the Iron LawHazards, forwarding and branch prediction →