The Cycles Per Instruction (CPI) in a multi-cycle processor is calculated as the average number of clock cycles required to execute a single instruction. Since different instructions take different numbers of cycles in a multi-cycle design, CPI depends on the instruction mix (the proportion of each instruction type in a program). Here's how it is computed:
Formula for CPI in a Multi-Cycle Processor
[
\text{CPI} = \frac{\text{Total Cycles for All Instructions}}{\text{Total Number of Instructions}} = \sum \left( \text{Frequency of Instruction Type}_i \times \text{Cycles per Instruction}_i \right)
]
Where:
- Frequency of Instruction Type (i): The fraction of instructions in the program that are of type (i) (e.g., 30% loads, 20% stores).
- Cycles per Instruction (i): The number of cycles required to execute instruction type (i).
Steps to Calculate CPI
-
Determine Cycles per Instruction Type:
- Break down each instruction type into its constituent cycles. For example:
- R-type (e.g.,
add, sub): 4 cycles (fetch, decode, ALU, write-back).
- Load (
lw): 5 cycles (fetch, decode, ALU, memory access, write-back).
- Store (
sw): 4 cycles (fetch, decode, ALU, memory access).
- Branch (
beq): 3 cycles (fetch, decode, ALU/compare).
-
Profile the Program:
- Measure the frequency of each instruction type in the program (e.g., 30% R-type, 25% loads, 20% stores, 25% branches).
-
Compute Weighted Average:
- Multiply the frequency of each instruction type by its cycle count and sum the results.
Example Calculation
Suppose a program has the following instruction mix:
- 30% R-type (4 cycles),
- 25% loads (
lw, 5 cycles),
- 20% stores (
sw, 4 cycles),
- 25% branches (
beq, 3 cycles).
[
\text{CPI} = (0.3 \times 4) + (0.25 \times 5) + (0.2 \times 4) + (0.25 \times 3) = 1.2 + 1.25 + 0.8 + 0.75 = 4.0
]
Key Notes
- Multi-Cycle vs. Single-Cycle: In a single-cycle processor, all instructions take 1 cycle (CPI = 1). In multi-cycle, CPI > 1 because instructions take variable cycles.
- Performance Context: CPI alone does not determine total execution time. The clock period (cycle time) also matters. Total time = ( \text{CPI} \times \text{Number of Instructions} \times \text{Clock Period} ).
Summary
CPI in a multi-cycle processor is the average cycles per instruction, calculated by weighting the cycle count of each instruction type by its frequency in the program. This metric helps evaluate the efficiency of the processor design relative to the workload.