Performance: CPU Time and Amdahl's Law

An architect compares designs before they exist. Two small equations do most of that work. One gives the time of a program, and one limits every speed-up.

You Will Be Able To

  • Work out CPU time from the instruction count, the CPI and the clock period.
  • Compare two designs by the time of one program, not by one number.
  • Use Amdahl's law to find the worth of a speed-up, and its limit.
Lesson
S1.5
Time
22 min
Runs on
Your browser
Check
5 questions

A team makes one unit of its chip four times faster. The job that uses the unit finishes less than twice as fast. Nothing is broken.

Before You Start

You need the fetch, decode and execute loop from S0.6: a processor runs one instruction after another. You need the design flow of S1.4, where the architect decides first.

Five questions this page answers
Question
1Which three numbers give the time a program takes?
2Why is a faster clock not always a faster chip?
3What is a speed-up?
4If one part of a job gets faster, how much faster is the whole job?
5What is the most that any speed-up of one part can buy?

The Idea: Time Is the Measure

The architect of S1.4 must choose between designs that are still on paper. Which processor? How fast a clock? Is a special block worth building? Each choice needs a number.

For speed, that number is time: how long the chip takes to finish a real job. Everything else is a part of that time, and a part can mislead.

Two equations carry most of the load. The first splits the time of a program into three factors. The second says what happens when you improve only a part of a job. Both fit on one line.

New Words

New words on this page
WordMeaning
CPUCentral processing unit: another name for the processor.
CPU timeThe time the processor spends running one program.
Instruction countThe number of instructions the processor executes to finish the program.
CPICycles per instruction: the clock cycles of a program, divided by the instructions it executes.
Nanosecond (ns)One thousand-millionth of a second.
Megahertz (MHz)One million clock cycles in each second. A clock of 100 MHz has a period of 10 ns.
Speed-upThe old time divided by the new time. A speed-up of 2 means half the time.
Amdahl's lawThe rule that the untouched part of a job limits the speed-up of the whole job.

One more name: the time of one clock cycle is the clock period, as in S1.2.

One Picture: Where the Time Goes

Where the Time Goes, Before and AfterStep through the two runs of the lab. Only one part of the job gets faster.
  • Processor
  • Memory
  • Peripheral
Where the Time Goes, Before and AfterStep through the two runs of the lab. Only one part of the job gets faster. component Before: 260cycles the whole job, withthe slow unit component Copy: 100 part 1, one word ineach cycle component Sum: 160 part 2, four cyclesfor each word component After: 140cycles the whole job, withthe fast unit component Copy: 100 part 1, untouched component Sum: 40 part 2, one cycle foreach word part 1part 2part 1part 2
  1. component Before: 260 cyclesthe whole job, with the slow unit
    • part 1 → Copy: 100
  2. component Copy: 100part 1, one word in each cycle
    • part 2 → Sum: 160
  3. component Sum: 160part 2, four cycles for each word
  4. component After: 140 cyclesthe whole job, with the fast unit
    • part 1 → Copy: 100
  5. component Copy: 100part 1, untouched
    • part 2 → Sum: 40
  6. component Sum: 40part 2, one cycle for each word
  1. Before

    The job takes 260 cycles. The sum is the larger part: 160 of them.

  2. After

    The unit is 4 times faster, so the sum takes 40 cycles. The copy still takes 100.

  3. What is left

    The part that did not change is now the larger part. More speed in the unit would buy little.

Read the diagram as text
  • Before: 260 cycles: the whole job, with the slow unit
  • Copy: 100: part 1, one word in each cycle
  • Sum: 160: part 2, four cycles for each word
  • After: 140 cycles: the whole job, with the fast unit
  • Copy: 100: part 1, untouched
  • Sum: 40: part 2, one cycle for each word
  • Before: 260 cycles → Copy: 100: part 1
  • Copy: 100 → Sum: 160: part 2
  • After: 140 cycles → Copy: 100: part 1
  • Copy: 100 → Sum: 40: part 2

Built from the recorded cycle counts of the lab.

Keep this picture in mind for the second half of the page. First comes the equation for one program.

The Smallest Example, Worked by Hand: Three Numbers

A program executes 1000 instructions. On this processor an instruction takes 1.5 clock cycles on average. The clock of ap_soc runs at 100 MHz, so its period is 10 ns.

CPU time, step by step
StepSumResult
Clock cycles1000 instructions x 1.5 cycles each1500 cycles
CPU time1500 cycles x 10 ns15,000 ns

Now a designer offers a change. The clock period falls to 8 ns. To reach that, some instructions need an extra cycle, and the CPI rises to 2.

Two designs, one program
DesignInstructionsCPIPeriodCPU time
A10001.510 ns15,000 ns
B100028 ns16,000 ns

Exercise numbers. The 100 MHz clock is the fast clock of ap_soc.

Design B has the faster clock, 125 MHz against 100 MHz. It is also the slower chip. Its extra cycles cost more than its shorter period saves.

The Rules

  1. Rule 1: CPU time is instruction count, times CPI, times clock period.

    The program sets the first factor. The processor design sets the second. The circuit sets the third.

  2. Rule 2: Compare designs by the time of the same program.

    One factor alone can mislead. A higher clock rate with a higher CPI can be slower.

  3. Rule 3: Speed-up is the old time divided by the new time.

    It is a ratio of whole times, measured on the whole job.

  4. Rule 4: A faster part shrinks only its own share.

    New time is the untouched time, plus the improved time divided by its factor. This is Amdahl's law.

  5. Rule 5: The untouched part sets the limit.

    Even an endlessly fast part leaves the untouched time. The speed-up can never pass the old time divided by it.

Sources for These Rules

The CPU time equation and its three factors: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 1.6.

Amdahl's law, and the mistake of expecting one improved part to speed up the whole: the same book, section 1.10.

The 100 MHz clock of ap_soc is a choice of this course. The other numbers of the worked example are exercise numbers.

Now Run It: Count the Cycles

The lab at the end of this page runs a job in two parts and counts clock cycles. Part 1 copies 100 words, one in each cycle. Part 2 adds up 40 words in a unit that needs four cycles for each.

Recorded from the lab on Icarus, with STEPS set to 4.
part 1, copy: 100 cycles
part 2, sum:  160 cycles (40 words, 4 each)
sum=780
total: 260 cycles

Now the team makes the unit four times faster: one cycle for each word. Before running it, apply rule 4 by hand.

Amdahl's law, by hand
QuantitySumResult
Untouched partthe copy100 cycles
Improved part160 cycles / 440 cycles
New time100 + 40140 cycles
Speed-up260 / 140about 1.86
Limit260 / 1002.6
Recorded from the same lab, with STEPS set to 1.
part 1, copy: 100 cycles
part 2, sum:  40 cycles (40 words, 1 each)
sum=780
total: 140 cycles

Rule 4 at work: the lab counts 140 cycles, as predicted. A unit that is 4 times faster made the job about 1.86 times faster.

Rule 5 at work: the copy alone takes 100 cycles. No unit, however fast, brings this job under 100. The best possible speed-up is 2.6.

In real time, 260 cycles of a 10 ns clock are 2600 ns, and 140 cycles are 1400 ns. Rule 1 turns cycles into time.

The lab leaves things out. Its two parts are test code, not a processor running instructions. The cycle counts are exact for this model only.

Why It Is Built This Way

HabitThe alternativeWhat the habit buys, and costs
Measure whole jobsQuote one factor, such as the clock rateAn honest comparison. You need a real program to measure.
Improve the largest part firstImprove the part that is easiestThe most speed-up for the work. The largest part is often the hardest.
Work out the limit before buildingBuild, then measureA block that cannot pay back is never built. The estimate needs a measured split of the time.

The Trap: "Four Times Faster"

Return to the opening. The team reported "the unit is four times faster", and that was true. The customer heard "the chip is four times faster". The job improved by 1.86.

After the change the split has moved. The copy is now 100 cycles of 140. Doubling the speed of the unit again would save only 20 cycles.

The cure is two questions before any speed-up. What share of the time does this part take? And what will the largest part be afterwards?

At Scale

This page1 job, 2 parts
A real chipmany programs, many parts

This section looks ahead. The lab split a job into two parts by hand. On a real chip the split must be measured.

  1. The CPI is measured, not guessed

    Module S2 builds a processor and counts its cycles. S2.23 measures one program on three of them.

  2. The period comes from the circuit

    The longest path of logic sets the shortest clock period. S2.6 finds that path.

  3. The split comes from counters

    Chips carry counters that record where cycles go. S8.6 reads them.

  4. Standard programs

    Teams agree on a fixed set of programs to time, so that two chips can be compared fairly.

Try It

Run the lab. Check that the two parts add up to the total.

Set STEPS to 1. Work out the total with rule 4 before you run it.

Keep STEPS at 4 and change the copy to 20 words. Work out the new limit of the speed-up.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: part 1, copy: 100 cycles part 2, sum: 160 cycles (40 words, 4 each) sum=780 total: 260 cycles

Check Yourself

Predict the outputThe unit now needs two cycles for each word. What is the last line the lab prints?
// A job in two parts. Part 1 copies 100 words. Part 2 adds up 40 words in a slow unit.
// STEPS is the number of clock cycles the unit needs for one word. Change it and run again.
module tb;
  localparam STEPS = 2;
  logic clk = 0;
  always #5 clk = ~clk;

  int cycles = 0;                       // counts every rising clock edge
  always @(posedge clk) cycles++;

  logic [7:0]  from_mem [100];          // the words to copy
  logic [7:0]  to_mem   [100];
  logic [15:0] sum = 0;
  int part1, part2;

  initial begin
    for (int i = 0; i < 100; i++) from_mem[i] = i;
    @(posedge clk); #1 cycles = 0;
    // Part 1: one word is copied in each clock cycle.
    for (int i = 0; i < 100; i++) begin
      @(posedge clk); to_mem[i] = from_mem[i];
    end
    #1 part1 = cycles;
    // Part 2: the unit takes STEPS clock cycles for each word.
    for (int i = 0; i < 40; i++) begin
      repeat (STEPS) @(posedge clk);
      sum = sum + to_mem[i];
    end
    #1 part2 = cycles - part1;
    $display("part 1, copy: %0d cycles", part1);
    $display("part 2, sum:  %0d cycles (40 words, %0d each)", part2, STEPS);
    $display("sum=%0d", sum);
    $display("total: %0d cycles", cycles);
    $finish;                      // $finish ends the simulation
  end
endmodule
ConceptWhich factor of CPU time does the clock circuit set?
ConceptA job takes 100 s. A part that takes 60 s is made 3 times faster. What is the new time?
ConceptFor that job, what is the largest speed-up that any improvement of the 60 s part can reach?
ConceptDesign X has a period of 4 ns and a CPI of 3. Design Y has a period of 5 ns and a CPI of 2. Which runs a given program sooner?

Common Mistakes

Wrong Call the chip with the higher clock rate the faster chip.

A shorter period can come with more cycles for each instruction.

Fix Multiply all three factors, for the same program.

Wrong Quote the speed-up of a part as the speed-up of the job.

The rest of the job takes as long as before.

Fix Add the untouched time, then divide old time by new time.

Wrong Keep speeding up the same part.

After each gain it is a smaller share, and the limit is near.

Fix Find the largest part again after every change.

Remember

  • CPU time is instruction count, times CPI, times clock period.
  • Compare designs by whole times for the same program.
  • Speed-up is old time divided by new time.
  • A faster part shrinks only its own share of the time.
  • The untouched part limits every speed-up.

Practice

Report a problem with this page