Sub topic 14
Effective CPI and speedup with stalls
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB). It also assumes you can recognize RAW hazards, load-use stalls and branch penalties, and that you are comfortable with ratio arithmetic. Here we combine those pieces into a single number, the effective CPI, and use it to find the real speedup.
Ideal CPI versus effective CPI
CPI means cycles per instruction. A pipeline that never stalls finishes one instruction per cycle once it is full, so its ideal CPI is 1. Every stall cycle adds time without finishing an extra instruction.
The effective CPI is the ideal CPI plus the average number of stall cycles per instruction:
CPI_eff = CPI_ideal + Σ (frequency of event × stall cycles per event)The frequency is measured per instruction in the whole program, not per instruction of the affected type. If 20% of instructions are branches, a branch with a 2-cycle penalty adds 0.20 × 2 = 0.40 to the CPI.
The same idea works on totals. For n instructions in a k-stage pipeline with S total stall cycles:
cycles = k + n - 1 + S
CPI_eff = cycles / nFor n = 10, k = 5 and S = 3, the program takes 17 cycles, so the CPI is 1.7. As n grows, the fill cost k − 1 is spread over many instructions, and CPI_eff approaches 1 + S/n.
Speedup with stalls
Speedup compares execution time, which is instruction count × CPI × clock period. The instruction count is the same on both machines, so it cancels:
Speedup = (CPI_unpipelined × clock_unpipelined) / (CPI_eff × clock_pipelined)Two cases come up in practice:
- Same clock period. The unpipelined machine takes k cycles per instruction, so Speedup = k / CPIeff. With CPIeff = 1.5 and k = 5, this gives 3.33.
- Different clock periods. The unpipelined clock is the sum of the stage delays. The pipelined clock is the largest stage delay plus the latch (pipeline register) delay. Use the full formula above.
Stalls and clock imbalance both reduce the speedup, and the two losses multiply. Throughput is the number of instructions finished per unit time, 1 / (CPI_eff × clock). Efficiency is the speedup divided by k.
Worked example in code
This program uses the stage delays from the earlier timing chapter: 60, 80, 70, 90 and 50 ns, with a 10 ns latch delay. It adds 20% branches with a 2-cycle penalty and 10% load-use stalls of 1 cycle.
def effective_cpi(base_cpi, stall_events):
# stall_events: list of (frequency per instruction, stall cycles per event)
return base_cpi + sum(freq * penalty for freq, penalty in stall_events)
delays = [60, 80, 70, 90, 50] # ns per stage
latch = 10 # ns
k = len(delays)
pipe_clock = max(delays) + latch # 100 ns
unpipe_clock = sum(delays) # 350 ns
cpi = effective_cpi(1.0, [(0.20, 2), (0.10, 1)]) # 1.5
speedup = (1.0 * unpipe_clock) / (cpi * pipe_clock)
throughput = 1000 / (cpi * pipe_clock) # instructions per microsecond
efficiency = speedup / k
print(cpi) # 1.5
print(round(speedup, 2)) # 2.33
print(round(throughput, 2)) # 6.67
print(round(efficiency, 3)) # 0.467The ideal speedup here is 350 / 100 = 3.5, because the 90 ns stage and the latch set the clock. The stalls cut it to 2.33, so the real gain is well below the stage count of 5.
Computing the branch term
The branch term depends on the strategy. Assume 20% branches, a 2-cycle penalty whenever the fetched path is wrong, and 60% of branches taken.
| Strategy | When the penalty applies | Branch stalls per instruction | CPI from branches alone |
|---|---|---|---|
| Stall or flush on every branch | Every branch | 0.20 × 2 = 0.40 | 1.40 |
| Predict not taken | Only taken branches | 0.20 × 0.60 × 2 = 0.24 | 1.24 |
| Dynamic prediction, 90% accurate | Only mispredictions | 0.20 × 0.10 × 2 = 0.04 | 1.04 |
The general form is branch frequency × (fraction that is penalized) × penalty. A fixed rule like predict-not-taken penalizes the taken fraction. A predictor penalizes the misprediction rate. For a predict-taken scheme, the penalty applies to the not-taken fraction instead, and may be smaller if the target is known early.
Combining several stall sources
Stall terms add. Do not average them or multiply them together. A load-use stall and a branch penalty are separate events and each contributes its own term. One instruction can in principle cause both, but GATE problems almost always treat the frequencies as independent and add them as stated.
Check what each percentage refers to before you multiply:
- "30% of instructions are loads, and 40% of loads are followed by a dependent instruction" gives a load-use frequency of 0.30 × 0.40 = 0.12 per instruction.
- "20% of branches are mispredicted" is a fraction of branches. Multiply it by the branch frequency to get a per-instruction figure.
- "A stall of 2 cycles for 15% of instructions" is already per instruction.
Also check whether a hazard exists under the stated design. With forwarding, an ALU-to-ALU dependency costs 0 stalls, and only load-use costs 1. Without forwarding, the same dependency may cost 2 stalls, or 3 if the register file does not write in the first half of the cycle. The problem statement says which case applies.
Common errors
- Using the stage count as the speedup. A k-stage pipeline reaches a speedup of k only with equal stage delays, no latch overhead, no stalls and a very large n.
- Forgetting the latch delay. The pipelined clock is the maximum stage delay plus the latch delay, not the maximum alone.
- Dividing by the wrong CPI. The unpipelined machine usually has CPI 1 at the long clock. Do not set it to k and also use the long clock, because that counts the stage count twice.
- Applying a branch penalty to every instruction. Multiply by the branch frequency.
- Mixing fractions. A misprediction rate of 10% among branches is not 10% of all instructions.
- Ignoring fill time on small n. For small n use cycles = k + n − 1 + S. The CPI formula 1 + S/n then holds only approximately, because it drops the (k − 1)/n fill term.
Systematic method for numerical questions
- Find the pipelined clock period: the maximum stage delay plus the latch delay.
- Find the unpipelined time per instruction: the sum of the stage delays, with no latches.
- Read each stall source and convert it to a per-instruction frequency.
- Multiply each frequency by its penalty and sum the terms. Add the sum to 1.
- Compute the speedup, throughput or efficiency that the question asks for.
- Sanity-check the result. The speedup should be below k, and the CPI should be at least 1.
Exercise
A 5-stage pipeline has equal stage delays and no latch overhead. 20% of instructions are branches, 70% of those are taken, and the machine uses predict-not-taken with a 3-cycle penalty on a taken branch. In addition, 15% of all instructions suffer a 1-cycle load-use stall. Find the effective CPI and the speedup over an unpipelined machine that runs at the same clock period and takes 5 cycles per instruction.
Show answer
Branch term = 0.20 × 0.70 × 3 = 0.42. Load-use term = 0.15 × 1 = 0.15. CPI_eff = 1 + 0.42 + 0.15 = 1.57. Speedup = 5 / 1.57 ≈ 3.18.
Exercise
Ten instructions run through a 5-stage pipeline and lose 3 cycles in total to stalls. How many cycles does the program take, and what is the effective CPI?
Show answer
Cycles = k + n − 1 + S = 5 + 10 − 1 + 3 = 17. CPI_eff = 17 / 10 = 1.7.
Exercise
A pipeline has stage delays of 40, 70, 50 and 60 ns and a latch delay of 5 ns. The unpipelined clock is the sum of the stage delays. Stalls bring the effective CPI to 1.25. What is the speedup, and what is the efficiency?
Show answer
Pipelined clock = 70 + 5 = 75 ns. Unpipelined time per instruction = 40 + 70 + 50 + 60 = 220 ns. Speedup = 220 / (1.25 × 75) = 220 / 93.75 ≈ 2.35. Efficiency = 2.35 / 4 ≈ 0.59.