Sub topic 14
Effective CPI and Speedup with Stalls
Assume you know CPU registers, memory, clock cycles, and simple instructions such as add, lw, and conditional branches. You should also know how to calculate ratios. This chapter uses the ideal five-stage pipeline model: IF, ID, EX, MEM, and WB.
Ideal CPI and the effect of stalls
CPI means cycles per instruction. In an ideal pipeline, one instruction completes every clock cycle after the pipeline fills, so the ideal CPI is 1. This does not mean that each instruction takes only one cycle. An individual instruction still passes through all five stages.
A stall is an extra cycle in which an instruction cannot advance. Stalls can result from data hazards, branch penalties, or resource conflicts. The effective CPI includes these extra cycles:
\[ \text{Effective CPI}=\text{Base CPI}+\text{Average stall cycles per instruction} \]
For an ideal pipeline:
\[ \text{Effective CPI}=1+\sum(\text{event frequency}\times\text{stall penalty}) \]
| Event | Frequency per instruction | Penalty | CPI contribution |
|---|---|---|---|
| Branch misprediction | 20% | 2 cycles | 0.40 |
| Load-use hazard | 10% | 1 cycle | 0.10 |
The effective CPI in this example is \(1+0.40+0.10=1.50\).
Calculating pipeline speedup
For a pipeline with \(k\) stages and \(n\) instructions, the ideal number of cycles is:
\[ k+n-1 \]
If the pipeline has \(s\) additional stall cycles, use:
\[ \text{Pipeline cycles}=k+n-1+s \]
The clock period is determined by the slowest stage plus the pipeline-register or latch delay:
\[ T{\text{clock}}=\max(\text{stage delays})+T{\text{latch}} \]
The following example compares a pipelined processor with a non-pipelined processor.
def pipelined_time(stage_delays, latch_delay, n, stall_cycles=0):
clock = max(stage_delays) + latch_delay
k = len(stage_delays)
return (k + n - 1 + stall_cycles) * clock
def non_pipelined_time(stage_delays, n):
return n * sum(stage_delays)
delays = [60, 80, 70, 90, 50] # nanoseconds
n = 100
pipelined = pipelined_time(delays, 10, n)
non_pipelined = non_pipelined_time(delays, n)
print(pipelined) # 10400
print(non_pipelined) # 35000
print(round(non_pipelined / pipelined, 2)) # 3.37The slowest stage takes 90 ns. With a 10 ns latch delay, the clock period is 100 ns. The pipeline takes \((5+100-1)\times100=10{,}400\) ns. The non-pipelined design takes \(100\times350=35{,}000\) ns. Its speedup is about 3.37, not 5, because startup time, latch overhead, and unequal stage delays reduce the ideal result.
A systematic method
- Identify the number of stages and the stage delays.
- Compute the clock period from the slowest stage.
- Find the ideal pipeline cycles, \(k+n-1\).
- Add every stall or flush penalty.
- Multiply cycles by the clock period.
- Compare with the requested baseline using the same time units.
Exercise
A five-stage pipeline executes 100 instructions. It has 12 branch mispredictions costing 2 cycles each and 8 load-use hazards costing 1 cycle each. Ignoring other stalls, how many total cycles are required?
Show answer
Ideal cycles are 5 + 100 - 1 = 104. Stall cycles are 12 × 2 + 8 × 1 = 32. Total cycles are 104 + 32 = 136.