---
title: Performance evaluation and simulation
heading: Measurement, analytical models and simulation, statistical sampling, CPI stacks and summarizing results
eyebrow: S4 · Lesson 46 · Performance evaluation and simulation
xp: 50
---

::: hook
Architects judge an idea before building it, and they have three tools: measure an existing machine, model it with equations like the ones in this course, or simulate the new design in software. Detailed simulators are honest but slow, often 10,000 to 100,000 times slower than real hardware, so running a whole benchmark in detail can take weeks. Statistical sampling fixes that: simulate a few thousand short, randomly chosen slices in detail, fast-forward through the rest, and report the answer with a confidence interval. Knowing how to sample, how to break performance into its causes, and how to summarize many results without fooling yourself is as much a part of architecture as designing caches.
:::

::: play
- The relative error of a sampled CPI estimate against the number of samples n: $\varepsilon = \frac{z\,\text{CoV}}{\sqrt n}$, where CoV is the coefficient of variation of CPI between samples and z sets the confidence (1.96 for 95%, 3 for 99.7%).
- Halving the error takes four times the samples. With a CoV of 0.5, 2500 samples give ±3% at 99.7% confidence.
- Each sample is a short detailed simulation (say 1000 instructions) after a warm-up, so the total detailed work stays tiny compared with the whole program.
:::
:::

```sim plot
view: graph
x: n = 10..100000 log
y: e = 0.1..100 % log
slider: c = 0.1..2 = 0.5
slider: z = 1..3 = 3
plot: 100*z*c/sqrt(n) | samples: 300 | label: relative error, %
readout: samples for ±3% = (z*c/0.03)^2
```

::: predict
## Two programs each do $10^9$ floating-point operations, one at 100 MFLOPS and one at 400 MFLOPS. What's the average rate over running both?

- [ ] 250 MFLOPS
- [x] 160 MFLOPS
- [ ] 200 MFLOPS
- [ ] 400 MFLOPS

::: reveal
160 MFLOPS: the pair does $2 \times 10^9$ operations in 10 s + 2.5 s = 12.5 s. That's the harmonic mean of the two rates, $\frac{2}{1/100 + 1/400}$. The arithmetic mean, 250, describes no real run. Rates for equal amounts of work average harmonically; times average arithmetically; ratios to a reference use the geometric mean (lesson 1).
:::
:::

::: nameit
## Three ways to evaluate

- **Measurement** on real hardware with performance counters: fast and exact, but only for machines that exist.
- **Analytical models** (the iron law, Amdahl, Little, AMAT, roofline): instant and insightful, but they leave out interactions.
- **Simulation:** functional simulators (correct results, around 10 to 100 million instructions a second) and cycle-accurate timing simulators (tens of thousands to a few million).

## Sampling

- **SimPoint** finds a few representative phases of a program and simulates each in detail, weighting the results.
- **SMARTS** simulates many short random samples in detail, with functional warming of caches and predictors in between. With n samples of a quantity whose coefficient of variation is CoV, the relative error at confidence z is $\varepsilon = z\,\text{CoV}/\sqrt n$; the samples needed for a target ε are

$$n = \left(\frac{z\,\text{CoV}}{\varepsilon}\right)^2$$

## CPI stacks

Performance counters (or a simulator) split the CPI into a base plus stall components: $\text{CPI} = \text{CPI}_\text{base} + \text{CPI}_\text{branch} + \text{CPI}_\text{L2} + \text{CPI}_\text{memory} + \dots$, which shows where the time goes and which improvement would matter.

## Summarizing

- Times: arithmetic mean (weighted by how often each program runs).
- Rates for equal work: harmonic mean, $\frac{n}{\sum 1/r_i}$.
- Normalized ratios: geometric mean.
:::

::: example
## A program runs $10^{11}$ instructions. A detailed simulator manages 100,000 instructions a second, and the CPI's coefficient of variation across samples is 0.5. How long would full detailed simulation take, and how much detail does SMARTS need for ±3% at 99.7%, with 1000-instruction samples?

::: solution
1. **Full detailed simulation.**
   $$10^{11}/10^5 = 10^6\ \text{s}$$
   which is 11.6 days.
2. **Samples needed.**
   $$\begin{aligned} n &= (3 \times 0.5/0.03)^2 \\ &= 2500\ \text{samples} \end{aligned}$$
3. **Detail.** At 1000 instructions each, that's 2.5 million detailed instructions, 25 seconds of detailed simulation.
4. **The rest.** Fast-forwarding and warming the rest runs at functional speed, so the whole study takes hours instead of days.

**Answer:** full detailed simulation would take 11.6 days; SMARTS needs 2500 samples, 2.5 million detailed instructions or 25 seconds of detailed simulation.
:::
:::

::: example
## A core's CPI stack is base 0.6, branch mispredictions 0.15, L2 hits 0.2 and memory 0.85. What fraction is memory, and what's the speedup if prefetching halves the memory part?

::: solution
1. **Total CPI.**
   $$0.6 + 0.15 + 0.2 + 0.85 = 1.8$$
2. **Memory share.**
   $$0.85/1.8 = 47\%$$
3. **Halving the memory part.** The CPI falls to 1.375:
   $$1.8/1.375 = 1.31$$
4. **Removing branch stalls instead.** Even removing every branch stall would give only
   $$1.8/1.65 = 1.09$$
   so prefetching is the better target.

**Answer:** memory is 47% of the CPI, and halving it gives a speedup of 1.31.
:::
:::

::: example
## Two programs do the same amount of work, one at 100 MIPS and one at 400. What is the machine's average rate over the pair?

::: solution
1. **Total time.** If each program does W instructions:
   $$t = W/100 + W/400$$
2. **Average rate.** Two programs' worth of work over that time:
   $$\frac{2}{1/100 + 1/400} = 160\ \text{MIPS}$$
   the harmonic mean.
3. **The arithmetic mean.** It would give 250, overstating the rate, because the slow program takes four times as long and dominates the time.

**Answer:** the average rate over the pair is 160 MIPS.
:::
:::

::: check
given: c = 0.3 | 0.8 | 1.5; z = 1.96 | 3; e = 0.01 | 0.02 | 0.05
answer: (z*c/e)^2
tolerance: 1%

## The CPI's coefficient of variation across samples is {c}. How many samples give a relative error of {e} at confidence z = {z}?

::: right
Right. $(z\,\text{CoV}/\varepsilon)^2$ = {answer} samples.
:::

::: misses
- [z*c/e] The error falls as $1/\sqrt n$: square it.
- [any] Not yet. Solve $\varepsilon = z\,\text{CoV}/\sqrt n$ for n.
:::
:::

::: check
given: c = 0.4 | 1; z = 1.96 | 3; n = 100 | 1000 | 10000
answer: 100*z*c/sqrt(n)
unit: %
tolerance: 0.5%

## With {n} samples, a coefficient of variation of {c} and z = {z}, what's the relative error?

::: right
Right. $z\,\text{CoV}/\sqrt n$ = {answer}%.
:::

::: misses
- [100*z*c/n] The error shrinks as the square root of n.
- [any] Not yet. $z\,\text{CoV}/\sqrt n$.
:::
:::

::: check
given: N = 1 | 50 | 500; k = 20 | 100 | 2000
answer: N*1e9/(k*1000)/3600
unit: h
tolerance: 0.5%

## How many hours does a simulator running at {k} thousand instructions a second take for {N} billion instructions?

::: right
Right. {answer} hours.
:::

::: misses
- [N*1e9/(k*1000)] Convert seconds to hours.
- [any] Not yet. Instructions ÷ rate.
:::
:::

::: check
given: a = 50 | 200; b = 300 | 800; c = 100 | 400
answer: 3/(1/a + 1/b + 1/c)
tolerance: 0.5%

## Three programs each do the same amount of work, at {a}, {b} and {c} MFLOPS. What's the overall rate across all three?

::: right
Right. The harmonic mean: {answer} MFLOPS.
:::

::: misses
- [(a + b + c)/3] Equal work means unequal times: the slow program dominates.
- [any] Not yet. Total work over total time.
:::
:::

::: check
given: b = 0.5 | 0.8; r = 0.1 | 0.2; l = 0.15 | 0.3; m = 0.4 | 1.2; f = 0.3 | 0.5
answer: (b + r + l + m)/(b + r + l + m*f)
tolerance: 0.5%

## A CPI stack is base {b}, branches {r}, L2 hits {l} and memory {m}. An optimization cuts the memory component to {f} of its size. What's the speedup?

::: right
Right. {answer}.
:::

::: misses
- [1/f] Only the memory part shrinks.
- [any] Not yet. Total CPI before over total CPI after.
:::
:::

::: check
given: b = 0.5 | 0.9; r = 0.1 | 0.25; l = 0.1 | 0.3; m = 0.3 | 1
answer: 100*m/(b + r + l + m)
unit: %
tolerance: 0.5%

## A CPI stack is base {b}, branches {r}, L2 {l} and memory {m}. What fraction of the time goes to memory stalls?

::: right
Right. {answer}%.
:::

::: misses
- [100*m/b] Divide by the total CPI.
- [any] Not yet. Memory component over total.
:::
:::

::: check
kind: parts
given: C = 0.3 | 0.8; e = 0.01 | 0.03; z = 2 | 3; U = 1000 | 10000

## SMARTS sampling must estimate CPI within {e} at confidence z = {z}; the CPI's coefficient of variation across samples is {C}, and each sample simulates {U} instructions in detail at 100 000 instructions a second.

::: part
answer: (z*C/e)^2
tolerance: 0.5%

### How many samples does it need?

::: misses
- [z*C/e] Square it.
- [any] $(z\,\text{CoV}/\varepsilon)^2$.
:::
:::

::: part
answer: (z*C/e)^2*U/1e5
unit: s
tolerance: 0.5%

### How long does the detailed simulation take?

::: misses
- [any] Samples × sample length ÷ rate.
:::
:::
:::

::: check
kind: parts
given: b = 0.5 | 0.8; r = 0.1 | 0.3; l = 0.1 | 0.2; m = 0.5 | 1.2

## A core's CPI stack is base {b}, branch mispredictions {r}, L2 hits {l} and memory {m}.

::: part
answer: m/(b + r + l + m)*100
unit: %
tolerance: 0.5%

### What fraction of the time goes to memory?

::: misses
- [any] Memory over the total CPI.
:::
:::

::: part
answer: (b + r + l + m)/(b + r + l + m/2)
tolerance: 0.5%

### What speedup does halving the memory part give?

::: misses
- [2] Only the memory part halves.
- [any] Old CPI over new CPI.
:::
:::
:::

::: check
kind: derivation
vars: a = 10..1000, b = 10..1000

## Two programs of equal work run at rates a and b.

::: step
answer: 2/(1/a + 1/b)

### What is the average rate over both?
:::

::: step
answer: (a + b)/2 - 2/(1/a + 1/b)

### By how much does the arithmetic mean overstate it?
:::

::: right
Right. For rates over equal work, the harmonic mean is the one that matches total time.
:::
:::

::: explain
## Why can a few thousand short samples predict a whole program's CPI accurately?

::: model
A program's CPI varies from one stretch of execution to another, but it varies around a stable average, and samples chosen at random across the run are a fair cross-section of that variation. By the central limit theorem the average of n such samples has an error that shrinks in proportion to one over the square root of n, scaled by how much the CPI varies between samples, so the error can be computed and controlled from the samples themselves. A few thousand samples bring it to a few percent with high confidence, however long the program is. The samples need warmed caches and predictors so each one sees realistic state, which fast functional simulation between samples provides.
:::
:::

::: practice

::: check
given: c = 0.5 | 1.2; z = 1.96 | 3; e = 0.02 | 0.04
answer: (z*c/e)^2
tolerance: 1%

## Samples for error {e}, CoV {c}, z = {z}?

::: misses
- [any] $(z\,\text{CoV}/\varepsilon)^2$.
:::
:::

::: check
given: a = 80 | 250; b = 400 | 900
answer: 2/(1/a + 1/b)
tolerance: 0.5%

## Overall rate of two equal-work programs at {a} and {b} MFLOPS?

::: misses
- [any] Harmonic mean.
:::
:::

::: check
given: b = 0.6 | 1; r = 0.1 | 0.2; l = 0.2; m = 0.5 | 1.5; f = 0.4 | 0.7
answer: (b + r + l + m)/(b + r + l + m*f)
tolerance: 0.5%

## Speedup from cutting memory to {f}: stack {b}, {r}, {l}, {m}?

::: misses
- [any] Total CPI ratio.
:::
:::
:::
