Average CPI from an Instruction Mix
Different kinds of instructions take different numbers of cycles, so a program's CPI is a weighted average: each class's CPI, weighted by how often that class shows up. The class worth speeding up is the one with the biggest share , not simply the slowest one.
What the symbols mean
- Frequency of class i
The fraction of executed instructions in class i (loads, branches, ...). The fractions add to 1.
- CPI of class i
Cycles one instruction of that class takes.
- Count of class i
How many instructions of class i the program executes.
What the symbols mean
- Share of cycles
The fraction of all clock cycles spent on class i.
Explanation
The average CPI is simply the number of cycles a typical instruction takes, once you account for how common each kind of instruction is. Programs mix cheap instructions (an add takes one cycle) with expensive ones (a load from memory might take five). In , each term is one class of instruction: (what fraction of the instructions are in that class) times (how many cycles each one costs). Add the terms up and you have the average. It's the same weighted average you'd use for a course grade where homework is 20% and the final is 40%.
Where it comes from. Count cycles class by class. If a program runs instructions of class , each costing cycles, then the total is
The average CPI is total cycles divided by total instructions. Divide each term by , and is just , the fraction of instructions in class :
It's an expected value. Pick an instruction from the running program at random. The number of cycles it takes is a random variable that equals with probability , and its mean is . That's the expected value from statistics, applied to a processor.
Which class to speed up. Suppose half the instructions are ALU operations at 1 cycle, 20% are loads at 5 cycles, 10% are stores at 3 and 20% are branches at 2. Then
Look at the pieces, not just the total. Loads are only a fifth of the instructions, but they contribute of the cycles, about of the running time. ALU operations are the most common class but contribute only . The term is what matters: shaving one cycle off loads saves cycles per instruction, the same as shaving one cycle off branches, but loads have more cycles to shave.
A common mistake. It's tempting to average the class CPIs directly: . That treats every class as equally common, which they aren't, so the answer is wrong whenever the mix is uneven. The other slip is aiming at the class with the biggest CPI. A rare instruction that takes 40 cycles but is of the mix contributes only to CPI; making it free barely moves the total.
Next lesson, Amdahl's law turns "the share of the time" into an exact speedup formula.
See it
Worked examples
A program's instructions are ALU operations (CPI ), loads (CPI ), stores (CPI ) and branches (CPI ). Find the average CPI and the fraction of the running time spent on loads.
- 1
First check the fractions add to 1: . Good, every instruction is counted once.
For the same mix, a designer can either (A) cut the load CPI from to , or (B) cut the branch CPI from to . Which gives the bigger speedup, and how big is it?
- 1
Only one class changes in each option, so only that class's term changes. Start from .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A workload is ALU instructions (CPI ), loads (CPI ), stores (CPI ) and branches (CPI ). What is its average CPI?
A workload is ALU instructions (CPI ), loads (CPI ), stores (CPI ) and branches (CPI ), for an average CPI of . What percent of the running time is spent on loads? Give one decimal place.
A profiler counts ALU instructions (CPI ), loads (CPI ) and branches (CPI ). What is the average CPI?
A profiler counts ALU instructions (CPI ), loads (CPI ) and branches (CPI ). The hardware team can either make loads take cycles instead of , or make ALU instructions take cycles on average (by issuing two per cycle). The clock stays the same. What speedup does the better option give?
A student finds the average CPI of a mix with class CPIs , , and by computing . When is this right?
A workload of ALU instructions (CPI ), loads (CPI ), stores (CPI ) and branches (CPI ) has an average CPI of . It has instructions and runs on a GHz processor. What is its CPU time, in seconds?
A design target is an average CPI of . The mix is ALU (CPI ), loads and branches (CPI ). How many cycles can a load take, at most, to hit the target?
The hardest one here. Get it, and you've got this lesson.
A GHz processor runs a mix of ALU (CPI ), loads (CPI ), stores (CPI ) and branches (CPI ). A redesign cuts the load CPI to and the branch CPI to , but the extra hardware forces the clock down to GHz. The instruction count doesn't change. What is the speedup of the redesign? Give 3 decimal places.
Where it's used
Chip architects decide where to spend transistors by running real programs through a simulator that counts instruction classes and their cycles, then computing exactly these weighted sums. If loads hold of the cycles, a bigger cache or a smarter prefetcher gets the budget; if branches do, the branch predictor does. Compiler writers use the same table in reverse, choosing instruction sequences whose mix has the lowest .