Benchmarks: Fixed Work, a Score and the Score per Megahertz

A benchmark is fixed work with a checked answer, timed between two reads of a counter. Its score counts iterations for each second. Divided by the clock, it counts cycles instead, and that hides something.

You Will Be Able To

  • Say what makes a program a benchmark, and turn a timed run into a score.
  • Compute a score per MHz, and say what it hides about the clock.
  • Split the cycles of a run by class of instruction, and relate CPI and IPC to the score.
Lesson
S8.14
Time
26 min
Runs on
Your browser
Check
5 questions

Two builds of the same core run the same loop. The table of scores per MHz says that the simple build wins by a quarter.

Put both in a product, and the other one finishes first. Which table lied?

Before You Start

You need Performance Counters: a window, read before and after, and CPI.

You need Single-Cycle, Multicycle or Pipelined: the two builds of ap_rv32, and why a taken branch costs the five-stage core two cycles.

It helps to have read Performance: CPU Time and Amdahl's Law: time is instructions, times CPI, times the clock period.

Five questions this page answers
Question
1What makes a program a benchmark?
2How does a timed run become a score, and a score per MHz?
3What does a score per MHz hide?
4How are CPI and IPC related, and where did the five-stage core lose its cycles?
5The run has 20 iterations, not 10. What is the five-stage score per MHz?

The Idea: Same Work, Same Answer, Then Count

To compare two cores, run the same work on both and measure the time. That work must be fixed: one program and one input. Its answer must be checked, or a broken core could look fast.

Such a program is a benchmark. Ours repeats one small loop several times. One pass of that loop is an iteration. The run is timed in a window, between two reads of the cycle counter.

The result is turned into a score: iterations for each second. A bigger score means a faster run. Vendors often quote the score divided by the clock, in MHz. That number counts iterations for each million cycles.

Cycles depend on two things. One is the number of instructions in an iteration. The other is how many cycles an instruction takes on average. Split the cycles by class of instruction, and you see where a core loses time.

New Words

New words on this page
WordMeaning
BenchmarkA program chosen to compare the performance of computers. It does fixed work on a fixed input, and checks its answer.
WorkloadThe set of programs that a user really runs. A benchmark stands in for it.
Benchmark kernelA small loop that does the heavy part of a job. Our benchmark is one such kernel: a checksum.
IterationOne pass of the work of a benchmark. Ours is the checksum of eight words.
Score of a benchmarkIterations for each second. A bigger score means a faster run.
Score per MHzThe score divided by the clock in megahertz. It equals the iterations for each million cycles.
IPCInstructions per cycle: the instructions of a window divided by its cycles. It is one divided by the CPI.
mcycle, minstretTwo counters of ap_rv32, read by a CSR instruction. mcycle counts clock cycles, minstret retired instructions.
Turn left by one bitEvery bit moves one place up, and the top bit comes in at the bottom. The checksum does this.
DhrystoneA small benchmark from 1984 for integer work, much used for embedded cores.
CoreMarkA small benchmark for embedded cores. It walks lists, works on a matrix, runs a state machine and computes a CRC (cyclic redundancy check).
SPEC CPUA suite of large real programs, such as a compiler and a chess program, for computers in general. SPEC (System Performance Evaluation Cooperative) is the vendor group that makes it.

One Picture: From Fixed Work to a Score

One Benchmark Run, from Fixed Work to a ScoreStep through the picture. It follows one run of the checksum kernel and ends with two ways of quoting its result.
  • Memory
  • Clock / reset
  • Processor
  • Control / FSM
  • Peripheral
One Benchmark Run, From Fixed Work to a ScoreStep through the picture. It follows one run of the checksum kernel and ends with two ways of quoting its result. component Fixed work one program, oneinput of eight words component Read mcycle the window opens component The loop ROUNDS iterationsof the same work component Read mcycle again the window closes component Check the answer a wrong checksummakes the runworthless component Score iterations for eachsecond component Score per MHz iterations for eachmillion cycles startcount cyclesstopanswercycles and clockdivide by the clock
  1. component Fixed workone program, one input of eight words
    • start → Read mcycle
  2. component Read mcyclethe window opens
    • count cycles → The loop
  3. component The loopROUNDS iterations of the same work
    • stop → Read mcycle again
  4. component Read mcycle againthe window closes
    • answer → Check the answer
  5. component Check the answera wrong checksum makes the run worthless
    • cycles and clock → Score
  6. component Scoreiterations for each second
    • divide by the clock → Score per MHz
  7. component Score per MHziterations for each million cycles
  1. Fixed work

    Every core runs the same program on the same input. Only then can two runs be compared.

  2. A window

    The program reads the cycle counter before the loop and after it. The difference is the time of the work.

  3. A checked answer

    The run counts only if the checksum is right. A fast wrong answer is no result.

  4. A score

    Iterations divided by seconds. A bigger score means a faster run.

  5. Per megahertz

    Divide the score by the clock in MHz. What is left counts cycles, not seconds.

Read the diagram as text
  • Fixed work: one program, one input of eight words
  • Read mcycle: the window opens
  • The loop: ROUNDS iterations of the same work
  • Read mcycle again: the window closes
  • Check the answer: a wrong checksum makes the run worthless
  • Score: iterations for each second
  • Score per MHz: iterations for each million cycles
  • Fixed work → Read mcycle: start
  • Read mcycle → The loop: count cycles
  • The loop → Read mcycle again: stop
  • Read mcycle again → Check the answer: answer
  • Check the answer → Score: cycles and clock
  • Score → Score per MHz: divide by the clock

The run of the lab on this page. The cycle counter is mcycle, the one that S8.6 reads in a window.

The kernel of this page, in C. The test reads mcycle just before the outer loop, and again just after it.

uint32_t sum = 0;
for (int r = 0; r < ROUNDS; r++)            // one round is one iteration of the benchmark
  for (int i = 0; i < 8; i++)
    sum = ((sum << 1) | (sum >> 31)) ^ buf[i];   // turn left by one bit, then mix in a word

The Smallest Example, Worked by Hand

One iteration in machine code has 60 instructions. Two set up the inner loop. Each of the eight words takes seven: a load, four steps of the checksum, a step of the address, and the branch. Two more close the outer loop.

The single-cycle core takes one cycle for each instruction: 60 cycles. The five-stage core takes one for each instruction too. Each taken branch costs it 2 more. In one iteration 8 branches are taken: 7 of the inner loop and 1 of the outer. So it needs 60 + 16 = 76 cycles.

One iteration, in the middle of a long run
Single-cycleFive-stage
Cycles for each iteration6076
CPI, and IPC1.00, and 1.001.27, and 0.79
Score per MHz: 1,000,000 / cycles16,66613,157
Clock (exercise values)65 MHz100 MHz
Score: iterations for each second1,083,3331,315,789

Read the table twice. Per MHz, the single-cycle core wins: it needs fewer cycles. Per second, the five-stage core wins: its cycles are shorter.

Where do the clocks come from? S2.6 and S2.24 measured the longest paths: 122 levels of logic and 80. At an assumed 0.125 ns for each level, that gives about 65 MHz and 100 MHz.

The Rules

  1. Rule 1: A benchmark is fixed work with a checked answer.

    Same program, same input on every core. A run with a wrong answer does not count.

  2. Rule 2: Time it in a window, and make the window long.

    The reads of the counter and the start of the loop cost cycles. A long run makes them small.

  3. Rule 3: The score is iterations for each second.

    It is the inverse of a time, so a bigger score is better.

  4. Rule 4: The score per MHz is iterations for each million cycles.

    Score = iterations × clock / cycles. Divide by the clock in MHz, and the clock is gone.

  5. Rule 5: A score per MHz hides the clock.

    Multiply it by the clock that the core really reaches before you compare.

  6. Rule 6: Cycles for each iteration = instructions for each iteration × CPI.

    IPC is one divided by CPI. Split the cycles by class to see where they went.

Sources for These Rules

Benchmark, workload and SPEC CPU2006: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 1.9.

Fixed code, fixed data and one proper answer as the usual rules, and SPECrate with many copies: the same book, section 6.10.

Time as instructions, times CPI, times the clock period: the same book, section 1.6. Section 1.10 warns against using part of it.

Dhrystone: from 1984, for embedded use, and not very representative. CoreMark: matrix work, linked lists, state machines and a CRC. Both are small. Source: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 7.2.

The same section of that book calls SPEC programs real programs, and says that CPI is one divided by IPC.

The score per MHz as a common way to quote CoreMark is common practice. No source in our library defines it. The run rules of CoreMark and of Dhrystone are not checked here.

The kernel, its input and the clocks are choices of this lab. The cost of a taken branch and of a CSR instruction on the five-stage core: docs/soc/ap_soc-facts.md, section 20.

Now Run It

The lab at the end of this page runs 10 iterations of the kernel. It walks the instructions of the window and counts cycles for both builds. A probe ran the same program on the real cores. Both read the same numbers from mcycle.

Printed by the lab on Icarus. Every count equals the window that the real ap_rv32 read from mcycle, on both builds.
window | 602 instructions | 10 iterations | alu 430 | load 80 | store 0 | branch 90 | csr 2
five-stage cycles by class | alu 430 | load 80 | store 0 | branch 248 | csr 8
five-stage extra cycles | taken branches 158 | load-use 0 | waits 0 | counter reads 6
single-cycle | cycles 602 | CPI 1.00 | IPC 1.00 | per iteration 60.2 | per MHz 16611 | at 65 MHz 1079734 per second
five-stage | cycles 766 | CPI 1.27 | IPC 0.79 | per iteration 76.6 | per MHz 13054 | at 100 MHz 1305483 per second

Rule 1: the window holds 602 instructions for 10 iterations. That is 60 for each, plus the 2 counter reads.

Rule 2, in the last two rows: 60.2 and 76.6 cycles for each iteration, not 60 and 76. The counter reads and the last pass of the loop are in the window too. With 20 iterations the five-stage core shows 76.3.

Rules 4 and 5, in the same rows. Per MHz: 16611 against 13054, so the single-cycle core leads by a quarter. Per second: 1079734 against 1305483, so the five-stage core leads by a fifth.

Rule 6, in the second and third rows. Of 766 cycles, 602 are one for each instruction. Taken branches add 158, and the counter reads add 6.

One run of 10 iterations: the time split by class of instruction
ClassInstructionsSingle-cycle cyclesFive-stage cyclesWhy the five-stage core needs more
Arithmetic430430430Nothing: one cycle each
Load808080Nothing here: no instruction uses a load at once
Branch909024879 taken branches, 2 more cycles each
Counter read (CSR)2283 more cycles for each CSR instruction
All602602766

Every cell comes from the first three rows that the lab prints. A taken branch throws away the two words fetched behind it.

The run on a real chip measures the same window. A short window flatters nobody: with one iteration, the five-stage core read 82 cycles from mcycle, not 76. Its score per MHz would be 12195, seven percent low.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Score as iterations for each secondQuote the time of a runBigger is better, and runs of any length compare. The number of iterations must be stated.
Quote the score per MHzQuote the score at one clockOne number for a core that ships at many clocks. It hides the clock that the core can reach.
A small kernelA suite of real programs, as SPEC CPU doesEasy to run on a small chip. It fits in any cache and tests little of the memory.

The last row matters most. Our kernel reads eight words. Dhrystone and CoreMark are small too, so a small cache holds them whole. A real workload may not fit.

The Trap: The Winner per Megahertz

A team chose a core from a table of scores per MHz. The simple core led by a quarter. In the product it was slower, because its clock could not go as high.

That is our lab. The single-cycle core packs a whole instruction into one long cycle. Its cycle count is low and its clock is low. A score per MHz shows only the first half.

The second trap is the short run. The same core read 82 cycles for one iteration and 76 for each iteration of a long run. A quoted score must say how long the run was.

The third is a fast wrong answer. A compiler that drops work whose result nobody checks makes a loop look very fast. A benchmark checks its answer for that reason.

At Scale

This page1 kernel, 8 words
SPEC CPU200612 integer programs, 17 floating-point

This section looks ahead. A large chip is measured with larger work, by several cores at once.

  1. Real programs

    SPEC CPU uses large real programs. Each result is the time of a reference computer divided by the measured time. The results of a suite are joined by a geometric mean.

  2. Cores that finish more than one instruction

    A wider core can reach an IPC above 1. See Out-of-Order Execution.

  3. Several cores

    A throughput score runs one copy of the work on each core at once. That measures the shared memory as much as the cores.

  4. Energy

    A score for each watt matters as much as a score for each second in a phone. See Power, Area and Cost.

The next lesson, What Moves a Score, keeps the core fixed. It changes the compiler and the memory, and then reads a vendor's number.

Try It

Run the lab. Find the two rows that end in "per second". Which core has the higher score per MHz, and which the higher score?

Set ROUNDS to 1, then to 100. Watch the score per MHz of the five-stage core. Why does it rise?

Set MHZ_SINGLE to 100. Which core wins now, and does the clock of a real single-cycle core allow it?

Names Printed by the Lab
NameMeaning
windowThe instructions between the two reads of mcycle: how many, and how many of each class.
alu, load, store, branch, csrThe classes: arithmetic and logic, loads, stores, branches, and CSR instructions such as the counter reads.
cycles by classThe five-stage cycles charged to each class. A taken branch is charged its two lost cycles.
extra cyclesThe five-stage cycles above one for each instruction, by cause.
per iterationCycles of the window divided by the iterations.
per MHzIterations for each million cycles, cut down to a whole number.
at .. MHzIterations for each second at that clock, cut down to a whole number.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: window | 602 instructions | 10 iterations | alu 430 | load 80 | store 0 | branch 90 | csr 2 five-stage cycles by class | alu 430 | load 80 | store 0 | branch 248 | csr 8 five-stage extra cycles | taken branches 158 | load-use 0 | waits 0 | counter reads 6 single-cycle | cycles 602 | CPI 1.00 | IPC 1.00 | per iteration 60.2 | per MHz 16611 | at 65 MHz 1079734 per second five-stage | cycles 766 | CPI 1.27 | IPC 0.79 | per iteration 76.6 | per MHz 13054 | at 100 MHz 1305483 per second

Check Yourself

Predict the outputIn this run ROUNDS is 20. What is the last row? Its form is: five-stage | cycles .... | CPI .... | IPC .... | per iteration .... | per MHz ..... | at 100 MHz ....... per second
// A benchmark run on two builds of ap_rv32
// Test code, not the core. It walks the instructions of the measured window and adds up cycles with the
// cost rules of the two builds of ap_rv32. A probe ran the same program on the real cores and read the
// same counts from mcycle. The checksum kernel, one version.
// Clocks: exercise values. 65 MHz and 100 MHz follow the logic depths of the two builds.
module tb;
  localparam int ROUNDS = 20;           // iterations of the benchmark
  localparam int DATA_WAITS = 0;       // wait cycles of each load and store (five-stage core)
  localparam int MHZ_SINGLE = 65, MHZ_FIVE = 100;
  localparam int ALU = 0, LOAD = 1, STORE = 2, BRANCH = 3, CSR = 4;
  localparam int ROWS = 13;
  // One row for each instruction of the window: its kind, the register it writes, the registers it reads,
  // and for a branch the row it goes back to and the trips of its loop.
  int kind [0:ROWS-1], rd [0:ROWS-1], rs1 [0:ROWS-1], rs2 [0:ROWS-1], target [0:ROWS-1], trips [0:ROWS-1];
  task automatic row(input int i, input int k, input int d, input int s1, input int s2, input int t, input int p);
    kind[i] = k;  rd[i] = d;  rs1[i] = s1;  rs2[i] = s2;  target[i] = t;  trips[i] = p;
  endtask
  // Walk the window from row "from" to row "to" - 1 ("at" is the row now running), and add up the cycles.
  // left[r] counts down the trips of the loop that the branch in row r closes.
  int left [0:ROWS-1];
  int n [0:4], five [0:4];
  int instructions, cycles, taken, stalls, waits, reads, at, c, prev_rd;
  bit prev_load;
  task automatic walk(input int from, input int to);
    for (int r = 0; r < ROWS; r++) left[r] = trips[r];
    for (int k = 0; k < 5; k++) begin n[k] = 0; five[k] = 0; end
    taken = 0;  stalls = 0;  waits = 0;  reads = 0;  prev_load = 0;  prev_rd = 0;
    at = from;
    while (at < to) begin
      c = 1;                                                           // every instruction: one cycle
      if (prev_load && prev_rd != 0 && (rs1[at] == prev_rd || rs2[at] == prev_rd)) begin
        c += 1;  stalls++;                                             // it needs the load just before it
      end
      if (kind[at] == LOAD || kind[at] == STORE) begin c += DATA_WAITS;  waits += DATA_WAITS; end
      if (kind[at] == CSR) begin c += 3;  reads += 3; end              // a CSR instruction leaves from MEM
      n[kind[at]]++;
      prev_load = (kind[at] == LOAD);  prev_rd = rd[at];
      if (kind[at] == BRANCH) left[at] = left[at] - 1;
      if (kind[at] == BRANCH && left[at] > 0) begin
        c += 2;  taken++;                                               // taken: two fetched words are thrown away
        five[kind[at]] += c;
        at = target[at];
      end else begin
        if (kind[at] == BRANCH) left[at] = trips[at];               // the loop is done: ready for its next pass
        five[kind[at]] += c;
        at++;
      end
    end
    instructions = n[0] + n[1] + n[2] + n[3] + n[4];
    cycles = five[0] + five[1] + five[2] + five[3] + five[4];
  endtask
  // a / b with two decimals, and with one decimal, rounded half up
  function automatic string dec2(input longint a, input longint b);
    longint v = (a * 200 + b) / (2 * b);
    return $sformatf("%0d.%02d", v / 100, v % 100);
  endfunction
  function automatic string dec1(input longint a, input longint b);
    longint v = (a * 20 + b) / (2 * b);
    return $sformatf("%0d.%0d", v / 10, v % 10);
  endfunction
  task automatic score(input string name, input longint cyc, input longint mhz);
    $display("%s | cycles %0d | CPI %s | IPC %s | per iteration %s | per MHz %0d | at %0d MHz %0d per second", name, cyc,
      dec2(cyc, instructions), dec2(instructions, cyc), dec1(cyc, ROUNDS), longint'(ROUNDS) * 1000000 / cyc, mhz,
      longint'(ROUNDS) * mhz * 1000000 / cyc);
  endtask
  initial begin
    //  row  kind   rd  rs1 rs2 to  trips
    row(0,  CSR,   18, 0,  0,  0,  0);      // csrr  s2, mcycle
    row(1,  CSR,   19, 0,  0,  0,  0);      // csrr  s3, minstret
    row(2,  ALU,   5,  8,  0,  0,  0);      // round: addi  t0, s0, 0
    row(3,  ALU,   6,  8,  0,  0,  0);      // addi  t1, s0, 32
    row(4,  LOAD,  7,  5,  0,  0,  0);      // word: lw    t2, 0(t0)
    row(5,  ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(6,  ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(7,  ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(8,  ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(9,  ALU,   5,  5,  0,  0,  0);      // addi  t0, t0, 4
    row(10, BRANCH,0,  5,  6,  4,  8);      // bne   t0, t1, word
    row(11, ALU,   9,  9,  0,  0,  0);      // addi  s1, s1, -1
    row(12, BRANCH,0,  9,  0,  2,  ROUNDS); // bne   s1, x0, round
    walk(0, ROWS);
    $display("window | %0d instructions | %0d iterations | alu %0d | load %0d | store %0d | branch %0d | csr %0d",
      instructions, ROUNDS, n[ALU], n[LOAD], n[STORE], n[BRANCH], n[CSR]);
    $display("five-stage cycles by class | alu %0d | load %0d | store %0d | branch %0d | csr %0d",
      five[ALU], five[LOAD], five[STORE], five[BRANCH], five[CSR]);
    $display("five-stage extra cycles | taken branches %0d | load-use %0d | waits %0d | counter reads %0d",
      2 * taken, stalls, waits, reads);
    score("single-cycle", instructions, MHZ_SINGLE);              // one cycle for each instruction
    score("five-stage", cycles, MHZ_FIVE);
  end
endmodule
ConceptWhat makes a program a benchmark?
ConceptWhat does a score per MHz hide?
ConceptA window has an IPC of 0.79. What is its CPI?
ConceptWhere did the five-stage core lose most of its extra cycles in the lab?

Common Mistakes

Wrong Compare two cores by their scores per MHz alone.

It hides the clock, and the clock may differ a lot.

Fix Multiply by the clock that each core reaches, then compare.

Wrong Time one short iteration and scale it up.

The counter reads and the start of the loop are in the window.

Fix Run many iterations, and state how many.

Wrong Read a low CPI as a fast core.

Time is instructions, times CPI, times the clock period.

Fix Look at all three before you rank two cores.

Remember

  • A benchmark is fixed work with a checked answer, timed in a long window.
  • Score: iterations for each second. Score per MHz: iterations for each million cycles.
  • A score per MHz hides the clock: the simpler core can win per MHz and lose per second.
  • Cycles for each iteration = instructions × CPI. Split them by class to find the lost cycles.

Practice

Report a problem with this page