C8 · Lesson 41 · Branch prediction and parallel processors
Guessing branches, issuing several instructions, and using several cores
A pipeline has to fetch the next instruction before it knows where a branch goes. Guessing "not taken" every time throws away two cycles on every loop iteration, and a fast processor with a 15-stage pipeline would throw away 15. So processors predict, remembering what each branch did last time, and the best predictors are right more than 95% of the time. With prediction in place, a processor can go further and issue several instructions each cycle, and when one core can't go faster for reasons of power, a chip gets several cores instead. Each step brings a new limit, and each limit is a number you can work out.
- The curve is the CPI against the misprediction penalty in cycles, for a processor that issues w instructions a cycle, with branches a fraction B of the instructions and predicted with accuracy a.
- Start at w = 1 and a = 0.9: at a 15-cycle penalty the CPI is 1.3. Raise w to 4: the base CPI falls to 0.25, but mispredictions now take more than half the time.
- Raise a toward 0.99 and watch a wide, deep machine come back to life. The dashed line is a 99% predictor.
Predict
A loop runs 10 iterations, and the whole loop is run many times. A 1-bit predictor remembers only whether the branch was taken last time. How many times does it mispredict the loop's closing branch per run of the loop?
2: on the last iteration (it predicts taken, but the loop exits), and again on the first iteration of the next run (it remembers the exit and predicts not taken). A 2-bit predictor needs two wrong guesses in a row to change its mind, so it only misses the exit: 1 per run.
Name it
Branch prediction
- Static: fixed rules. "Backward taken, forward not taken" gets loops right, since a loop's closing branch jumps backward.
- Dynamic: a branch history table of small counters, indexed by low bits of the branch's address. A 1-bit counter predicts whatever happened last time. A 2-bit saturating counter counts up when taken and down when not, between 0 and 3, and predicts taken at 2 or 3. One odd outcome doesn't flip it.
- A branch target buffer remembers each taken branch's target address, so fetch can go there in the very next cycle.
For a loop of n iterations run repeatedly, a 1-bit predictor misses 2 of every n executions of its closing branch and a 2-bit predictor 1. With branches a fraction B of instructions, accuracy a and a penalty of p cycles,
Issuing several instructions
A superscalar processor fetches and executes w instructions per cycle, so its ideal CPI is 1/w (an IPC, instructions per cycle, of w). Out-of-order execution lets later independent instructions run while one waits for memory, with register renaming removing false dependencies on reused register names. Real code rarely has more than 2 to 4 independent instructions ready at a time, so widths beyond that pay off less and less.
Several cores
Dynamic power is , and a higher f needs a higher V, so power grows much faster than speed. Since about 2005, chips have added cores instead of clock speed. A program speeds up on n cores only in the part that runs in parallel:
Worked example
A loop of 4 iterations is run many times. What accuracy do a 1-bit and a 2-bit predictor achieve on its closing branch?
Show the solution
- Branches per run. The branch executes 4 times.
- The 1-bit predictor. It misses 2, an accuracy of 50%.
- The 2-bit predictor. It misses 1, an accuracy of 75%.
- Longer loops. For a 100-iteration loop both are better, 98% and 99%, which is why short loops are where predictors differ most.
Answer 50% for the 1-bit predictor and 75% for the 2-bit one.
Worked example
One core runs at f and voltage V. Two cores run at 0.8f and 0.85V. Compare power and throughput on perfectly parallel work.
Show the solution
- Power. Power scales as per core: times the single core's.
- Throughput. times.
- Compare. That's 60% more work for 16% more power, if the program is parallel.
Answer 1.6 times the throughput for 1.16 times the power.
Worked example
90% of a program runs in parallel. What speedup do 8 cores give, and how efficiently are they used?
Show the solution
- Amdahl's law.
- Efficiency.
- The ceiling. Even with infinitely many cores the speedup can't pass . The serial 10% sets the ceiling, so parallel programmers spend most of their effort shrinking it.
Answer a speedup of 4.71, using the cores at 59% efficiency.
Check
A loop of 8 iterations is run 100 times; count only the runs after the first.
(a)
How many times does a 1-bit predictor mispredict its closing branch in those runs?
(b)
What accuracy does a 2-bit predictor achieve on it?
Check
Branches are 0.2 of the instructions, predicted with accuracy 0.85, and a misprediction costs 20 cycles. The base CPI is 1.
(a)
What is the CPI?
(b)
What is the IPC?
Check
A 2-wide superscalar processor loses 0.1 cycles per instruction to stalls and mispredictions.
(a)
What is its CPI?
(b)
What IPC does it achieve?
Check
A fraction 0.9 of a program's work runs in parallel on 4 cores.
(a)
What is the speedup over one core?
(b)
What fraction of the cores' capacity is used, as a percentage?
Check
A chip replaces one core with 2 cores, each at 0.6 of the clock rate and 0.9 of the voltage.
(a)
What is the dynamic power relative to the single core?
(b)
What is the throughput on perfectly parallel work, relative to the single core?
Check
A processor with no history uses "backward taken, forward not taken". Which branch does it predict correctly most of the time?
Check
90% of a program runs in parallel on 4 cores.
(a)
What's the speedup?
(b)
How efficiently are the cores used?
Check
A loop of 5 iterations is run many times.
(a)
What accuracy does a 1-bit predictor get on its closing branch?
(b)
And a 2-bit predictor?
Explain why
Why does a wider, deeper processor need a more accurate branch predictor?
Every misprediction throws away all the instructions fetched since the branch. A deeper pipeline decides branches later, so more cycles of fetch are wasted each time, and a wider processor fetches several instructions per cycle, so each wasted cycle throws away more work. Meanwhile the useful part of each cycle shrinks: a 4-wide machine's ideal CPI is 0.25. A misprediction cost that was a small addition to a CPI of 1 becomes most of the running time, so the accuracy has to rise from around 90% toward 99% for the extra width and depth to pay off.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.