By the end of this topic you will learn
- Amdahl’s law, an important piece of common-sense mathematics for computer scientists
- pipelining, a form of parallelism that enables advanced computer graphics
- concurrency and how it’s managed to let the many apps on your phone run at the same time
Flash Cards
When multiple workers can split up a problem without adding much extra coordination overhead, computer scientists sometimes call the problem embarrassingly parallel.
Code to solve the problem
const timeToCutTheDoghIntoTriangles = 2
const timeToSpreadFillingOntoTriangles = 4
const timeToRollFilledTrianglesIntoCrescents = 10
const timeToPlacePastriesIntoTheOven = 20
const timeToRemovePastriesFromTheOven = 4
const timeToLeftThePastriesCool = 10
const sum = (...args) => args.reduce((sum, item) => sum + item, 0)
const workers = 2
const parallelWork = sum(
timeToCutTheDoghIntoTriangles,
timeToSpreadFillingOntoTriangles,
timeToRollFilledTrianglesIntoCrescents,
timeToRemovePastriesFromTheOven,
) //?
const notParallelWork = sum(
timeToLeftThePastriesCool,
timeToPlacePastriesIntoTheOven
) //?
const pierreTotalTime = sum(
timeToCutTheDoghIntoTriangles / workers,
timeToSpreadFillingOntoTriangles / workers,
timeToRollFilledTrianglesIntoCrescents / workers,
timeToPlacePastriesIntoTheOven,
timeToRemovePastriesFromTheOven / workers,
timeToLeftThePastriesCool,
) //?
const bakeryTime = sum(
parallelWork / 16,
notParallelWork
) //?
const desiredTime = 35
parallelWork
workers
notParallelWork
const workersNeededFor5MinutesGain = (
parallelWork / - (notParallelWork - (40 - 5))
) //?
70 - 90 //?
const pierreAverageProductionPerMinute = (
200 / 20
) //?
const averagePasteriesForMinute = (
200 / 30
) //?
Not all tasks can be done in Parallel
When you think about parallel and non-parallel tasks it should be obvious that adding more workers will only make you faster at the parts of a task that can be done in parallel. Amdahl’s law says that we cannot make a task faster just by putting more agents into the task. This conclusion came after a paper from Gene Amdahl in 1967
Pipelining
Pipelining is a very common form of parallelism when you think about making cookies or bread. When you want to make a huge amount of food you often will nee to create a pipeline for making this tasks parallel for. For example, let’s say you’re baking 2 cakes, and your oven can only fit one. In order to optimize your cake-per-minute ratio you can prepare another cake while your other cake is in the oven.
This form of parallelism is called Pipelining. By working on different branches of your work in parallel.
Pipeline is also used in computer generated graphics to predict which chunks it will need to render next. This concept is called speculative execution.
Concurency
Concurency is the name for situations where multiple agents each want a turn at having exclusive access to the same resource. It sometimes show up in similar places as parallelism, but concurrency occurs when there’s no way to plan in advance for the order in which things will happen.
One method for dealing with Concurrency is called the Bakery Algorithm and it was devised by Leslie Lamport
This Quiz
- The most common kind of parallelism is splitting up a problem and assigning it to different workers
- If you can’t use parallelism on every part of the problem, adding more and more workers may not help you very much
- When you need to work on different problems at the same time, it can be useful to use pipeline parallelism
- When different tasks need exclusive access to a single shared resource in an unpredictable order, you are dealing with concurrency
- Concurrency and parallelism show up in many of the same places, but they are different ideas.
Flash Cards