---
title: Branch prediction and parallel processors
heading: Guessing branches, issuing several instructions, and using several cores
eyebrow: C8 · Lesson 41 · Branch prediction and parallel processors
xp: 40
---

::: hook
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.
:::

::: play
- 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.
:::
:::

```sim plot
view: graph
x: p = 1..25
y: CPI = 0..3
slider: w = 1..4 step 1 = 1
slider: B = 0.05..0.3 step 0.01 = 0.2
slider: a = 0.5..1 step 0.01 = 0.9
plot: 1/w + B*(1 - a)*p | label: CPI
plot: 1/w + B*0.01*p | label: 99% accurate | dashed
readout: CPI at a 15-cycle penalty = 1/w + B*(1 - a)*15 | digits: 3
readout: instructions per cycle at 15 = 1/(1/w + B*(1 - a)*15) | digits: 3
```

::: 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?

- [ ] 0
- [ ] 1
- [x] 2
- [ ] 10

::: reveal
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.
:::
:::

::: nameit
## 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,

$$\text{CPI} = \text{CPI}_\text{base} + 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 $\alpha CV^2f$, 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 = \frac{1}{(1 - p) + p/n}$$
:::

::: 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?

::: 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.
:::
:::

::: 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.

::: solution
1. **Power.** Power scales as $V^2f$ per core:
   $$2 \times 0.85^2 \times 0.8 = 1.16$$
   times the single core's.
2. **Throughput.**
   $$2 \times 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.
:::
:::

::: example
## 90% of a program runs in parallel. What speedup do 8 cores give, and how efficiently are they used?

::: solution
1. **Amdahl's law.**
   $$\begin{aligned} S &= \frac{1}{0.1 + 0.9/8} \\ &= \frac{1}{0.2125} \\ &= 4.71 \end{aligned}$$
2. **Efficiency.**
   $$\frac{4.71}{8} = 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
kind: parts
given: n = 4 | 8 | 20; m = 100 | 1000

## A loop of {n} iterations is run {m} times; count only the runs after the first.

::: part
answer: 2*(m - 1)
tolerance: 0

### How many times does a 1-bit predictor mispredict its closing branch in those runs?

::: misses
- [m - 1] A 1-bit predictor also misses the first iteration, because it remembers the last exit.
- [any] Not yet. Two per run.
:::
:::

::: part
answer: (n - 1)/n*100
unit: %
tolerance: 0.5%

### What accuracy does a 2-bit predictor achieve on it?

::: misses
- [(n - 2)/n*100] That's the 1-bit predictor. The 2-bit one misses only the exit.
- [any] Not yet. One miss in {n}.
:::
:::
:::

::: check
kind: parts
given: B = 0.15 | 0.2 | 0.25; a = 0.85 | 0.93 | 0.97; p = 3 | 12 | 20

## Branches are {B} of the instructions, predicted with accuracy {a}, and a misprediction costs {p} cycles. The base CPI is 1.

::: part
answer: 1 + B*(1 - a)*p
tolerance: 0.5%

### What is the CPI?

::: misses
- [1 + B*a*p] The penalty is paid on the mispredictions, a fraction 1 − a.
- [1 + B*p] Only mispredicted branches pay the penalty.
- [any] Not yet. 1 + B(1 − a)p.
:::
:::

::: part
answer: 1/(1 + B*(1 - a)*p)
tolerance: 0.5%

### What is the IPC?

::: misses
- [any] One over the CPI.
:::
:::
:::

::: check
kind: parts
given: w = 2 | 4; s = 0.1 | 0.3

## A {w}-wide superscalar processor loses {s} cycles per instruction to stalls and mispredictions.

::: part
answer: 1/w + s
tolerance: 0.5%

### What is its CPI?

::: misses
- [1 + s] A {w}-wide machine's ideal CPI is 1/{w}.
- [any] Not yet. 1/w + stalls.
:::
:::

::: part
answer: 1/(1/w + s)
tolerance: 0.5%

### What IPC does it achieve?

::: misses
- [w] The stalls keep it below {w}.
- [any] Not yet. One over the CPI.
:::
:::
:::

::: check
kind: parts
given: p = 0.6 | 0.9 | 0.99; n = 4 | 16 | 64

## A fraction {p} of a program's work runs in parallel on {n} cores.

::: part
answer: 1/((1 - p) + p/n)
tolerance: 0.5%

### What is the speedup over one core?

::: misses
- [n] Only the parallel part speeds up.
- [any] Not yet. $1/((1 - p) + p/n)$.
:::
:::

::: part
answer: 1/((1 - p) + p/n)/n*100
unit: %
tolerance: 0.5%

### What fraction of the cores' capacity is used, as a percentage?

::: misses
- [any] Speedup over the number of cores.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 4; s = 0.6 | 0.75; v = 0.8 | 0.9

## A chip replaces one core with {k} cores, each at {s} of the clock rate and {v} of the voltage.

::: part
answer: k*v^2*s
tolerance: 0.5%

### What is the dynamic power relative to the single core?

::: misses
- [k*v*s] Power scales with the square of the voltage.
- [any] Not yet. k × v² × s.
:::
:::

::: part
answer: k*s
tolerance: 0.5%

### What is the throughput on perfectly parallel work, relative to the single core?

::: misses
- [any] k cores at {s} of the speed.
:::
:::
:::

::: check
kind: choice

## A processor with no history uses "backward taken, forward not taken". Which branch does it predict correctly most of the time?

- [x] The branch at the bottom of a loop that jumps back to the top
- [ ] A forward branch that is taken 90% of the time
- [ ] A branch whose outcome alternates every time
- [ ] Any branch, equally

::: misses
- [2] Forward branches are predicted not taken, so this one is wrong 90% of the time.
- [3] Alternation defeats every static rule half the time.
- [4] The rule depends on direction, which is the point.
:::
:::

::: check
kind: parts
given: p = 80 | 90 | 95; n = 4 | 8 | 16

## {p}% of a program runs in parallel on {n} cores.

::: part
answer: 1/((1 - p/100) + p/100/n)
tolerance: 0.5%

### What's the speedup?

::: misses
- [n] The serial part doesn't speed up.
- [any] Amdahl's law.
:::
:::

::: part
answer: 1/((1 - p/100) + p/100/n)/n*100
unit: %
tolerance: 0.5%

### How efficiently are the cores used?

::: misses
- [any] Speedup ÷ cores.
:::
:::
:::

::: check
kind: parts
given: n = 3 | 5 | 10 | 50

## A loop of {n} iterations is run many times.

::: part
answer: (n - 2)/n*100
unit: %
tolerance: 0.5%

### What accuracy does a 1-bit predictor get on its closing branch?

::: misses
- [(n - 1)/n*100] The 1-bit predictor also misses the first branch of the next run.
- [any] Two misses per run.
:::
:::

::: part
answer: (n - 1)/n*100
unit: %
tolerance: 0.5%

### And a 2-bit predictor?

::: misses
- [any] One miss per run.
:::
:::
:::

::: explain
## Why does a wider, deeper processor need a more accurate branch predictor?

::: model
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

::: check
given: n = 5 | 16 | 50
answer: (n - 2)/n*100
unit: %
tolerance: 0.5%

## What accuracy does a 1-bit predictor get on the closing branch of a {n}-iteration loop that is run repeatedly?

::: misses
- [any] Two misses per {n} executions.
:::
:::

::: check
given: B = 0.1 | 0.2; a = 0.9 | 0.95; p = 5 | 15
answer: 1 + B*(1 - a)*p
tolerance: 0.5%

## B = {B}, accuracy {a}, penalty {p} cycles, base CPI 1: what is the CPI?

::: misses
- [any] 1 + B(1 − a)p.
:::
:::

::: check
given: p = 0.75 | 0.95; n = 2 | 8
answer: 1/((1 - p) + p/n)
tolerance: 0.5%

## A fraction {p} parallel on {n} cores: what is the speedup?

::: misses
- [any] Amdahl's law.
:::
:::
:::
