Single-Cycle, Multicycle or Pipelined: CPI and One Program on Three Cores
The single-cycle core makes every instruction as slow as a load. There are two ways out. Both cut an instruction into steps. This page runs one program on all three cores, and compares the time.
You Will Be Able To
- Describe a multicycle core as a state machine, and count the cycles of an instruction on it.
- Work out the CPI of a program, and its time from the instruction count, the CPI and the clock period.
- Compare a single-cycle, a multicycle and a pipelined core for one program.
- Lesson
- S2.23
- Time
- 28 min
- Runs on
- Your browser
- Check
- 6 questions
An add was ready after 96 levels and waited until 124. The cure seems plain: cut the long cycle into short steps, and let an add take fewer of them. A core built that way has a clock almost twice as fast. And it loses the race.
Before You Start
You need the critical path of S2.6: a load needs 123 levels, so the single-cycle clock period is 124.
You need a simple state machine: states, and a rule for the next state.
| Question | |
|---|---|
| 1 | How does a multicycle core run an instruction? |
| 2 | What does CPI stand for, and what is it for a single-cycle core? |
| 3 | Which three numbers give the time of a program? |
| 4 | What sets the clock period of a multicycle core? |
| 5 | Why can a core with a faster clock be the slower core? |
The Idea: Three Numbers Make the Time
A multicycle core cuts each instruction into steps: fetch, decode, execute, memory, write back. Each step takes one short clock cycle. An instruction uses only the steps it needs. A branch is done after three, a load after five.
So instructions now take different numbers of cycles. The average over a program is its CPI: cycles per instruction. For the single-cycle core the CPI is exactly 1.
The time of a program is a product of three numbers. It is the number of instructions, times the CPI, times the clock period.
A pipelined core cuts the instruction into the same steps. But it does not wait for one instruction to finish. Each cycle, a new instruction starts, and the others move one step on. Five instructions are in the core at once.
So the pipelined core has the short clock of the multicycle core, and a CPI near 1. That is why almost every real core is pipelined. The next lessons build one.
New Words
| Word | Meaning |
|---|---|
| Multicycle | A processor that takes several short clock cycles for each instruction: one for each step. |
| Step of an instruction | One part of the work of an instruction: fetch, decode, execute, memory or write back. |
| CPI | Cycles per instruction: the clock cycles of a program, divided by the instructions it executes. |
| Instruction count | The number of instructions that a program executes. An instruction in a loop counts each time. |
| Execution time | The time a program takes: instruction count times CPI times clock period. |
| Pipelined | A processor that starts a new instruction each cycle, while older ones are still in later steps. |
| Pipeline stage | One step of a pipelined processor, with the hardware that does it. |
One Picture: The Steps of Each Kind of Instruction
- Control / FSM
- An add: four steps
Fetch, decode, execute, write back. An add has nothing to do in the memory step, so it skips it.
- A load: five steps
A load is the only kind that needs every step. After the ALU has the address, the memory is read, and then the register is written.
- A store: four steps
A store ends in the memory step. It writes no register.
- A branch: three steps
A branch is decided in the execute step. Then the next instruction is fetched, from the target or from the next address.
Read the diagram as text
- Fetch: read the word at the PC
- Decode: decode, and read two registers
- Execute: the ALU, the compare, the target
- Memory: read or write the data memory
- Write back: write the register rd
- Fetch → Decode: always
- Decode → Execute: always
- Execute → Memory: load, store
- Execute → Write back: arithmetic, jal
- Execute → Fetch: branch: done
- Memory → Write back: load
- Memory → Fetch: store: done
- Write back → Fetch: done
The steps for each kind of instruction are a choice of this page. A real multicycle core may cut them differently.
| Step | What happens | Levels | Used by |
|---|---|---|---|
| Fetch | the instruction memory is read | 20 | every instruction |
| Decode | the decoder works, and two registers are read | 11 | every instruction |
| Execute | the ALU, the branch compare and the target adder work | 65 | every instruction |
| Memory | the data memory is read or written | 20 | load, store |
| Write back | the load unit, and the write of rd | 7 | arithmetic, load, jal |
The levels are those of the lab of S2.6. The longest step is execute: the clock period of the multicycle core is 66.
The Smallest Example, Worked by Hand
Take five instructions: addi, slli, lw, addi, add. Count their cycles on each core.
| Core | Cycles | Clock period | Time, in levels |
|---|---|---|---|
| Single-cycle | 5: one each | 124 | 620 |
| Multicycle | 21: four each, and five for the lw | 66 | 1386 |
| Pipelined | 9: five, and 4 to fill | 66 | 594 |
Time is cycles times period. In the pipelined core, the first instruction needs 5 cycles, and each later one ends a cycle after it.
The multicycle core has the shorter clock, and takes more than twice the time. It needs 21 cycles where the single-cycle core needs 5.
The Rules
Rule 1: Time is instruction count, times CPI, times clock period.
A core is faster only if this product is smaller.
Rule 2: A single-cycle core has a CPI of 1.
Its clock period fits the longest instruction.
Rule 3: A multicycle core takes one cycle for each step of an instruction.
Its clock period fits the longest step.
Rule 4: The CPI of a program depends on its mix of instructions.
On a multicycle core, many loads raise it and many branches lower it.
Rule 5: A pipelined core completes one instruction in each cycle, once it is full.
It loses cycles when it must throw work away, as after a taken branch.
Rule 6: Never compare two cores by clock period alone, or by CPI alone.
Only the product counts.
Sources for These Rules
Two things on this page are not measured on a real core. The first is the multicycle core. The core ap_rv32 has no multicycle build, so the lab holds a small state machine. Its number of steps for each kind of instruction is a choice of this page, close to what textbooks commonly show. The second is the clock periods. They follow from the levels of the lab of S2.6, where two delays are a choice. The plan of this track points to Patterson and Hennessy, sections 4.5 and 4.14. It also points to Harris and Harris, sections 7.2 and 7.4. Those places are not checked. The pipelined core is counted in the lab, and its costs are measured. The five-stage build of ap_rv32 was measured on its pipeline view, with memories that answer at once. A taken branch or a jump loses 2 cycles. A load with its reader right behind loses 1. A program with no such event takes its instructions plus 4 cycles. The measurement is in docs/soc/ap_rv32-blueprint.md, section 14.2, and S2.7 to S2.10 show it.
Now Run It
The lab at the end of this page takes the program of S2.21. It executes 15 instructions: nine arithmetic ones, one load, one store, three branches and one jal. The real core gave that list.
The lab first shows the first five instructions on each core. Then it counts the whole program.
single-cycle | addi | one cycle of 124 levels
single-cycle | slli | one cycle of 124 levels
single-cycle | lw | one cycle of 124 levels
single-cycle | addi | one cycle of 124 levels
single-cycle | add | one cycle of 124 levels
multicycle | addi | F D E W
multicycle | slli | F D E W
multicycle | lw | F D E M W
multicycle | addi | F D E W
multicycle | add | F D E W
pipelined | addi | F D E M W
pipelined | slli | . F D E M W
pipelined | lw | . . F D E M W
pipelined | addi | . . . F D E M W
pipelined | add | . . . . F D E M W
single-cycle | 15 instructions | 15 cycles | CPI 1.00 | period 124 | time 1860
multicycle | 15 instructions | 58 cycles | CPI 3.87 | period 66 | time 3828
pipelined | 15 instructions | 25 cycles | CPI 1.67 | period 66 | time 1650Rule 3 at work: the five rows of the multicycle core. The lw has five letters, the others four.
Rule 5 at work: the five rows of the pipelined core. Each instruction starts one cycle after the one before. In the fifth cycle, all five are in the core, each in another step.
Now the three totals. The multicycle core needs 58 cycles: 9 times 4, then 5, 4, 3 times 3, and 4. Its CPI is 58 divided by 15.
Rule 6 at work: the multicycle core has the short clock, and the worst time: 3828 against 1860.
The pipelined core needs 25 cycles. Fifteen are for the instructions, and 4 to fill. Two branches are taken and one jal jumps: each costs 2 cycles.
Its time is 1650. That is the best of the three, though not by much. The program is short, so the 4 cycles to fill weigh a lot.
Why It Is Built This Way
| Core | What it buys | What it costs |
|---|---|---|
| Single-cycle | The simplest control: one table, no state. | A clock period as long as the longest instruction. Every instruction pays it. |
| Multicycle | A short clock. One ALU and one memory port can be used in several steps. | A state machine, and registers between the steps. Several cycles for every instruction. |
| Pipelined | A short clock and a CPI near 1. | Registers between the stages, and logic for instructions that depend on each other: the next four lessons. |
The multicycle core is rare in new designs. It is still worth knowing. The steps of a pipeline are its steps, and a core that waits for a slow memory behaves like one.
The Trap: The Faster Clock That Lost
Two cores are on the table. One runs at a period of 124, the other at 66. Many people would call the second one nearly twice as fast. It is the multicycle core, and it needs twice the time.
Why is it so bad? Look at one load. Its five steps need 20, 11, 65, 20 and 7 levels. That is 123 in all. The multicycle core gives each step a full period of 66. Five periods are 330.
The steps are far from equal. The period fits the ALU, and the other four steps waste most of their cycle. What if the ALU had only 20 levels?
single-cycle | 15 instructions | 15 cycles | CPI 1.00 | period 79 | time 1185
multicycle | 15 instructions | 58 cycles | CPI 3.87 | period 21 | time 1218
pipelined | 15 instructions | 25 cycles | CPI 1.67 | period 21 | time 525Now the steps are nearly equal, and the period drops to 21. The multicycle core comes close: 1218 against 1185. It still does not win. Every step is rounded up to a full period.
The pipelined core gains the most. It is now more than twice as fast as the single-cycle core. A short clock pays only when the CPI stays low.
So the question about a clock is always: how much work does one cycle do?
At Scale
This section looks ahead. Every processor is compared by this product of three numbers.
Long programs
The 4 cycles to fill a pipeline vanish in a long run. What remains are the cycles lost at branches: S2.10 to S2.12.
CPI below 1
A core that starts two or more instructions in each cycle can have a CPI under 1: S2.15.
Memory in the CPI
A load that waits for a slow memory adds cycles. In real systems that part of the CPI is often the largest: S3.
Counting on the chip
A core counts its own cycles and instructions in two registers. Software divides them to get the CPI: S8.6.
Try It
Run the lab. Check the 58 cycles of the multicycle core by hand, from the mix of the program.
Give a branch only two steps in the state machine. How does the CPI of the multicycle core change?
Set T_MEMORY to 80. Which numbers of the three totals change, and which core is hit hardest?
single-cycle | addi | one cycle of 124 levels single-cycle | slli | one cycle of 124 levels single-cycle | lw | one cycle of 124 levels single-cycle | addi | one cycle of 124 levels single-cycle | add | one cycle of 124 levels multicycle | addi | F D E W multicycle | slli | F D E W multicycle | lw | F D E M W multicycle | addi | F D E W multicycle | add | F D E W pipelined | addi | F D E M W pipelined | slli | . F D E M W pipelined | lw | . . F D E M W pipelined | addi | . . . F D E M W pipelined | add | . . . . F D E M W single-cycle | 15 instructions | 15 cycles | CPI 1.00 | period 124 | time 1860 multicycle | 15 instructions | 58 cycles | CPI 3.87 | period 66 | time 3828 pipelined | 15 instructions | 25 cycles | CPI 1.67 | period 66 | time 1650Check Yourself
Common Mistakes
Wrong Call the core with the shortest clock period the fastest.
A short period with many cycles for each instruction can take longer in all.
Fix Multiply: instruction count, CPI and clock period.
Wrong Give the multicycle core the average of the step delays as its period.
One clock serves every step. It must fit the longest one.
Fix Take the longest step. The other steps waste the rest of their cycle.
Wrong Count a pipelined core as one cycle for each instruction, and nothing more.
It needs cycles to fill, and it loses cycles after a taken branch or jump.
Fix Add the cycles to fill and the cycles lost.
Remember
- Time is instruction count times CPI times clock period. Only the product compares two cores.
- A single-cycle core has a CPI of 1 and a long period. A multicycle core has a short period and a CPI of 3 to 5.
- A multicycle period fits the longest step, so unequal steps waste time.
- A pipelined core has the short period and a CPI near 1. It pays with cycles to fill and cycles lost at taken branches.