C841 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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,

CPI=CPIbase+B(1−a)p

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 αCV2f, 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:

S=1(1−p)+p/n

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
  1. Branches per run. The branch executes 4 times.
  2. The 1-bit predictor. It misses 2, an accuracy of 50%.
  3. The 2-bit predictor. It misses 1, an accuracy of 75%.
  4. 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
  1. Power. Power scales as V2f per core: 2×0.852×0.8=1.16 times the single core's.
  2. Throughput. 2×0.8=1.6 times.
  3. 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
  1. Amdahl's law. S=10.1+0.9/8=10.2125=4.71
  2. Efficiency. 4.718=59%
  3. The ceiling. Even with infinitely many cores the speedup can't pass 1/0.1=10. 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?

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.