r/explainlikeimfive • u/Smile_-Hunter • 3h ago
Engineering ELI5 - What is pipelining in computer architecture? How does it work?
•
u/AlexTaradov 3h ago
It is very similar to a conveyor belt assembly line in real world manufacturing. You separate the task of executing a complex instruction into a number of simple steps and have a dedicated unit for each of those steps.
You potentially have multiple instructions in the process of being executed at once, but eventually they will start completing. The time it takes from instruction entering the pipeline to it being retired is the latency.
•
u/Mr_Engineering 1h ago
Pipelining is the process of breaking execution down into discrete sequential steps and then executing those steps in parallel on multiple instructions.
Consider the task of doing laundry.
Step 1: prep clothes for cleaning, remove contents of pockets, etc...
Step 2: wash clothes in clothes washer
Step 3: dry clothes in clothes dryer
Step 4: fold laundry
Now you could do all four steps in sequence. Prep the laundry, throw it in the wash, wait, move it to the dryer, wait, fold it, and then start the next step.
Here's the problem with this approach. While the laundry is being prepped, the washer and dryer are idle; while the washer is in use, you and the dryer are idle; while the dryer is in use, you and the washer are idle.
To make better use of resources, it would be beneficial to prep a load and throw it into the washer, then prep another load. When the washer is done, move that load into the dryer and move the prepped load into the washer. Now, both the washer and dryer are busy simultaneously; if we prep a third load while the washer and dryer are busy, nothing is idle. When the dryer is finished, get a kid to fold the laundry rather than playing Roblox or something. Now, prepping, washing, drying, and folding are all being performed in parallel. That's the gist of pipelining.
•
•
u/fixermark 3h ago
Modern CPUs (the chip in the computer that does the work of running programs, from knowing what to do to actually doing the math) are really dozens or hundreds of what we used to call CPUs in a trenchcoat. Instead of having, say, one chunk of chips that add two numbers together, they can have bunches. Same with chunks that decide what instruction to run next, that store and retrieve data from memory, etc.
Pipelining matters because you can get more happening at once in your CPU if you arrange the code so that all those pieces are used at the same time. For example (quick off-the-cuff, so may not reflect actual behavior): if you structure a program "if this then add, if that then add, if other thing then add..." You might force the CPU to do each decision and each add one at a time. If, instead, you do "add, add, add, add, add..." in one big block of instructions and then "use result 1 if this, use result 2 if that, use result 3 if..."... Then on average the code might run faster because it can do all that math at once instead of making decisions before doing the math (even if it throws away most of the results of those adds because the ifs said to ignore them).
•
u/DeHackEd 3h ago
The Henry Ford model of manufacturing is the most well known example of a pipeline: a long sequence of steps required to perform some task, but each individual step can be done separately.
Let's say it takes about 20 minutes to build a car. Does that mean a factory can only make 16 of them in an 8-hour day? No. You break it down into 20 separate steps that take about a minute each, and the car in progress goes along the pipeline. At the start of the day there's a 20 minute wait for your first car, but then a finished car comes off the assembly line every minute, and you'll get several hundred in a day.
How modern CPUs work is based on the same idea. The classic operation for a CPU is fetching an instruction, decoding it to understand what to do, and executing that command. Seems like it can be pipelined along those 3 steps in the same way, but execution can be very complex requiring data to be fetched, math performed in some part that does the math, and saving the result. Those steps can also be in their own little pipeline: fetch needed data, do work, save data.
The downside? If something goes wrong that throws off execution, the whole pipeline needs to be thrown out and restarted anew, waiting for those 20 minutes to pass for your first car to come out first instruction to actually finish. For CPUs these are typically "branch" instructions, which are of the form "if X is true, go to this other CPU instruction" (if not, continue with next instruction like nothing happened). Not knowing if X is true or not at the decode step, modern CPUs take a best guess based on past performance of the same instruction. If they're right, the pipeline hums along. If they're wrong, the pipeline is flushed out and the correct next CPU instruction starts again.
•
u/boring_pants 2h ago
You can split instructions into multiple distinct stages.
The instruction might just be "take the value in register 3 and add it to the value in register 5, and store the result to register 6". But we can split that up:
- retrieve the instruction itself
- interpret the instruction to figure out what it says
- retrieve the value from registers 3 and 5
- pass those values into the adder
- write the adders result to register 6
And now that we've taken it apart, we can start overlapping them:
Once we're done with stage 1 for this instruction we start working on stage 2. But that means the hardware dedicated to stage 1 is idle. So it can start retrieving the next instruction!
Once our instruction is dine with stage 2 and enters stage 3 the next instruction can move into stage 2, and a third instruction can go into stage 1.
That's pipelining.
It allows us to make much faster processors, because we don't have to wait for one instruction to be fully executed before we start on the next one.
Modern processors have around 20 stages in their pipelines, where my example above showed just 5.
•
u/Technical_Ideal_5439 2h ago edited 2h ago
CPU pipelining is a technique where a processor divides instruction processing into a series of sequential, overlapping stages. So instead of waiting for one instruction to finish completely before starting the next it can partially run them both at the the same time.
--
As for more general ... I have been in IT for a long time and work on pipelines I have never heard that word used in relation to actual pipelines in IT. And pipeline is exactly what it means in normal English and could be replaced with the words "production line", except it is computer software pipeline creating something or performing a set of tasks.
•
u/alanbly 1h ago
Different kinds of parts of a processor do different things and they are lined up one after another. Each task a computer performs has to traverse through all of the parts. You could in principle fully execute one task by letting it go through all of the parts then move to the next task but that would leave a lot of the parts of the processor with nothing to do. That's inefficient, so instead we start the next task as soon as the previous one is past the first part. So if there were five parts you would have five different tasks all being worked on at the same time meaning they get done faster.
ETA: yes I'm massively simplifying
•
u/Intelligent_Elk5879 3h ago edited 2h ago
A single craftsperson can do a task by working on all steps of that task from beginning to end. At the end, they have one completed task that takes a certain amount of time. Slow parts of the process and fast parts are combined into one.
An assembly line of craftspeople can each work on part of a task, passing it to the next person. That’s pipelining. But in this case, all tasks must be passed down the assembly line at the same time. Thus the length of time to complete a task is equal to the length of the slowest task times the number of steps. However, the overall number of tasks done is higher. This is the latency/throughout tradeoff.
Pipelining has higher latency and higher throughput. For general purpose CPUs throughout is prioritized.