Search code examples
mipscpupipeline

Calculating the total clock cycles per instruction in a CPU


I was reading some university material, and I found that to calculate the CPI (clock cycles per instruction) of a CPU, we use the following formula:

CPI = Total execution cycles / executed instructions count

this is clear and does make sense, but for this example it says that n instructions have been executed:

instruction type   frequency     relative CPI
1                      50%            3
2                      20%            4
3                      30%            5

why is the total CPI equal to 3*0.5+4*0.2+5*0.3 = 3.8 and not 3.8/3 = 1.26 because following the above formula, there are a total of 3 executed instructions, or am I understanding the formula in a wrong way?


Solution

  • The formula is correct but you are not reading the table right.

    instruction type   frequency     relative CPI
    1                      50%            3
    2                      20%            4
    3                      30%            5
    

    The first line means you have an instruction that uses 3 CPI and this instruction has a frequency of 50% which basically means every second instruction in your program is this instruction.

    Instruction 2 needs 4 CPI to be executed but occurs only 20% in your program. Instruction 3 needs 5 CPI but occurs 30%.

    Therefore you calculate 0.5 * 3 + 0.2 * 4 + 0.3 + 5 = 3.8. It is basically the average CPI.


    Just imagine you have following program:

    INS_1   3 CPI
    INS_3   5 CPI
    INS_1   3 CPI
    INS_3   5 CPI
    INS_2   4 CPI
    INS_1   3 CPI
    INS_3   5 CPI
    INS_2   4 CPI
    INS_1   3 CPI
    INS_1   3 CPI 
    --------------
           38 CPI / 10 (Instructions) = 3.8