AP Computer Science Principles · Computer systems and networks
Parallel and distributed computing
1Sequential and parallel computing
- Sequential computing: operations run one at a time, in order.
- Parallel computing: a program is broken into smaller parts, some of which run at the same time on multiple processors.
- Distributed computing: multiple devices (often in different places) work together on a problem, communicating over a network.
Parallel and distributed computing let us solve larger problems faster, or solve problems too big for one computer (e.g. weather modelling, search engines, scientific data).
Check yourself
Running parts of a program at the same time on several processors is:
Show the answer
Parallel computing
Simultaneous tasks.
2Calculating run time
- Sequential time = the sum of the times of all tasks.
- Parallel time = the time of any sequential part plus the time of the longest parallel portion (the slowest processor determines when the parallel part ends).
- Speedup = sequential time ÷ parallel time.
three tasks of 40, 30 and 50 seconds, two processors, all tasks independent.
- Sequential: 40 + 30 + 50 = 120 s.
- Best parallel split: processor 1 runs 50; processor 2 runs 40 + 30 = 70. Parallel time = 70 s (the longer processor).
- Speedup = 120 ÷ 70 ≈ 1.71.
Running independent tasks on two processors: time is set by the longer branch
Check yourself
Tasks of 20, 20 and 20 s run on three processors in parallel. The parallel time is:
Show the answer
20 s
Longest parallel portion.
3Limits of parallelism
Speedup is limited because:
- some steps must run sequentially (one depends on another's result);
- the time is set by the slowest processor;
- processors must communicate and coordinate, which adds overhead.
So adding more processors gives diminishing returns: doubling processors rarely halves the time.
a program has a 10 s sequential part and 40 s of work that can be split evenly.
- 1 processor: 50 s. 2 processors: 10 + 20 = 30 s. 4 processors: 10 + 10 = 20 s. 8 processors: 10 + 5 = 15 s.
- The time can never drop below 10 s.
Check yourself
Why does adding more processors eventually stop reducing run time much?
Show the answer
The sequential parts and coordination overhead cannot be shortened by more processors
Diminishing returns.
4Distributed computing
Distributed systems spread work across many computers:
- Search engines index the web using thousands of servers.
- Scientific projects use volunteers' computers to analyse data.
- Cloud services store and process data in many data centres.
Benefits: handle problems too large for one machine (in time or storage), plus fault tolerance. Challenges: network communication delays and coordinating results.
Check yourself
A key advantage of distributed computing is:
Show the answer
Solving problems too large for a single computer
Scale.
5Going deeper: comparing run times and calculating speedup
Three models
- Sequential computing: operations are performed one at a time, in order. Total time = the sum of all the steps.
- Parallel computing: a program is broken into smaller operations, some of which run at the same time on multiple processors. There is usually a sequential part and a parallel part.
- Distributed computing: multiple devices (often many computers connected by a network) work together on one program.
Time for a parallel solution = the time of the sequential portion + the longest of the parallel portions (they finish only when the slowest one finishes).
Speedup = sequential time ÷ parallel time.
Worked example (two processors): three independent tasks take 40, 50 and 60 seconds.
- Sequentially: 40 + 50 + 60 = 150 s.
- With two processors, share the tasks to make the loads as even as possible: one runs the 60 s task; the other runs 40 + 50 = 90 s.
- Parallel time = the longer of the two = 90 s.
- Speedup = 150 ÷ 90 = 1.67.
- Another split (60 + 40 = 100 against 50) would take 100 s: worse. Choose the split that minimizes the larger load.
Worked example (three processors): the same tasks, one per processor.
- Time = the longest task = 60 s.
- Speedup = 150 ÷ 60 = 2.5.
- A fourth processor would not help: the 60 s task cannot be divided.
Worked example (a sequential step): a program has a setup step of 20 s that cannot be parallelized, followed by four independent steps of 30 s each.
- Sequentially: 20 + 120 = 140 s.
- With two processors: 20 + (two steps each = 60) = 80 s. Speedup 1.75.
- With four processors: 20 + 30 = 50 s. Speedup 2.8.
- With a hundred processors: still 50 s. The speedup can never exceed 140 ÷ 20 = 7 even if the parallel part took no time at all.
The limit: the benefit of adding processors is limited by the sequential portion. As more processors are added, the parallel part shrinks but the sequential part does not, so each extra processor helps less.
Tasks that depend on each other cannot run at the same time: if step B needs the result of step A, they must be done in order.
Why parallelism?
- Solutions scale better: as problems grow, more processors can be added.
- Processor speeds are no longer rising as they once did; modern chips gain power by having several cores.
- Common in graphics, video encoding, scientific simulation and training machine-learning models.
Why distributed computing?
- It allows problems to be solved that could not be solved on a single computer, because of the time or the storage needed.
- It is how web search, weather forecasting, large-scale data analysis and cloud services work.
- Volunteer projects have used the idle time of millions of home computers to analyze protein folding and to search radio signals.
- A system spread over many machines can also be fault-tolerant: if one fails, others continue.
Costs and difficulties
- Overhead: dividing the work, communicating and combining the results all take time. For a small problem this can outweigh the gain.
- Coordination: processes must not interfere with each other when using shared data.
- Not every problem divides neatly.
- More hardware, power and complexity; harder testing and debugging.
Comparing efficiency: sequential solutions are compared by their total number of steps; parallel solutions by the time to complete on a given number of processors.
Worked example (deciding): a photo-editing program applies the same filter to each of 1000 separate images. A second program computes each value from the previous one in a long chain.
- The first parallelizes almost perfectly: the images are independent.
- The second cannot: each step needs the one before.
Common mistakes
- Adding the parallel portions together (take the longest).
- Forgetting the sequential portion.
- Assuming that doubling the processors halves the time.
- Dividing the wrong way round when computing speedup.
- Confusing parallel (several processors, often in one machine) with distributed (several machines).
Check yourself
A sequential program takes 80 seconds. A parallel version takes 20 seconds. What is the speedup?
Show the answer
4
Speedup is sequential time divided by parallel time.
6For top marks
- Show working for time calculations: sequential sum, then parallel = sequential part + longest parallel branch.
- Assign tasks to processors to balance the load and minimise the longest branch.
- Explain why speedup is limited: dependencies, the slowest processor, communication.
- Distinguish parallel (multiple processors, usually one machine) from distributed (multiple machines on a network).
"With tasks 30, 30, 20 and 20 s on two processors, pairing 30 + 20 on each gives 50 s, better than 30 + 30 and 20 + 20 (60 s)."
Check yourself
Tasks of 45, 25, 20 and 10 s run on two processors. What is the minimum parallel time?
Show the answer
55 s
Best split: 45 + 10 and 25 + 20, so the longer branch is 55 s.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera