AP Computer Science Principles · Study guide

Parallel and Distributed Computing

AP® Computer Science Principles · Big Idea 4 · Computer Systems and Networks

Parallel and Distributed Computing

How sequential, parallel, and distributed computing differ, how to work out the time and speedup of each, and why adding processors eventually stops helping.

1Three ways to run a program

ModelWhat happensClue words
Sequential computingOperations are performed in order, one at a timeone processor, one step after another
Parallel computingThe program is broken into smaller sequential operations, some of which run at the same timeseveral processors (cores) in one computer
Distributed computingMultiple devices are used to run a programseveral computers connected by a network

The question behind every parallel plan: can these tasks run independently, or does one need another's result first? Independent tasks can overlap. A task that needs another task's result has to wait, and that waiting limits how fast the whole job can go.

2Timing the work

Compare solutions by the time each takes to do the same task. Two rules give that time:

  • A sequential solution takes as long as the sum of all its steps.
  • A parallel solution takes as long as its sequential tasks plus the longest of its parallel tasks.

Here a video app does a 10-second setup, then renders three clips that do not depend on each other:

Figure 1. Sequential time adds every step. Parallel time adds the setup to the longest clip, because clips B and C finish while A is still running.

Sequential: 10 + 30 + 20 + 20 = 80 seconds. Parallel: the setup must finish before any clip starts, so it is added; the clips overlap, so only the longest counts: 10 + 30 = 40 seconds. For each step, decide: does it overlap with others (take the longest) or run alone (add it)?

3Speedup

The speedup of a parallel solution is the sequential time divided by the parallel time:

speedup = sequential time ÷ parallel time

For the clip job, 80 ÷ 40 = 2. Three processors gave a speedup of 2, not 3, for two reasons you can see in Figure 1: the setup ran in both versions, and the clips were unequal, so two processors sat idle while clip A finished. A faster parallel solution always has a speedup greater than 1. If you get 0.5, you divided the wrong way.

4Splitting tasks among processors

When there are more tasks than processors, each processor runs its tasks one after another, and the job is done when the busiest processor finishes.

SituationSequential timeBest parallel time (2 processors)Speedup
Four independent 20 s tasks80 s40 s: two tasks each2
Tasks J 10 s, K 30 s, L 45 s, M 90 s175 s90 s: M alone; J, K, L on the other (85 s)1.94
Steps 1 (10 s), 2 (20 s), 3 (30 s), 4 (15 s); step 2 needs step 1, step 4 needs step 375 s45 s: steps 1 then 2 on one (30 s), steps 3 then 4 on the other (45 s)1.67

An even split gives the ideal speedup. With uneven tasks, the longest task (M, 90 s) sets a floor no matter how the others are arranged. With dependencies, each chain of steps must run in order, and the longest chain sets the floor.

To split tasks well by hand: give the largest task to one processor, then hand each remaining task, largest first, to whichever processor has less work so far. Then check whether any swap lowers the busiest processor's total.

5The sequential limit

Every parallel solution has a parallel portion (pieces that can run at the same time) and a sequential portion (work that cannot be split: reading the input, setting up, combining the answers). More processors shrink only the parallel portion. Take a job with 100 seconds of parallel work and 20 seconds of sequential work:

ProcessorsTimeSpeedup
1100 ÷ 1 + 20 = 120 s1
2100 ÷ 2 + 20 = 70 s1.71
4100 ÷ 4 + 20 = 45 s2.67
10100 ÷ 10 + 20 = 30 s4
100100 ÷ 100 + 20 = 21 s5.71
Figure 2. Processors only shrink the parallel portion. The 20-second sequential portion stays, so the time can never drop below it and the speedup can never reach 6.

Going from 1 to 4 processors saves 75 seconds. Going from 10 to 100 saves only 9. However many processors you add, the time stays above 20 seconds, so the speedup stays below 120 ÷ 20 = 6. This is why the efficiency of a parallel solution is still limited by its sequential portion: at some point, adding more parallel processing no longer meaningfully helps.

The key idea

Sequential time is the sum of the steps. Parallel time is the sequential steps plus the longest parallel task. Speedup is sequential time divided by parallel time. Adding processors shrinks only the parallel portion, so the sequential portion limits how fast the solution can get.

6Distributed computing

Distributed computing uses the same timing arithmetic, with one extra piece: the machines are separate, so sending the work out and collecting the results back takes real time, and that time is sequential.

Figure 3. The searching overlaps; the splitting and combining do not, so they are added like any sequential step.

The four searches overlap, but the split and the combine do not: 2 + 30 + 3 = 35 seconds instead of 120, a speedup of about 3.43, not 4.

Speed is only half the reason to distribute. Some problems cannot be solved on one computer at all, because the data would not fit in its storage or the processing would take far too long. Weather forecasts, search engines, and scientific projects that analyze huge data sets spread both the data and the work across many machines. Distributed computing lets much larger problems be solved, and solved faster, than a single computer could manage.

7Benefits and challenges

BenefitThe challenge that comes with it
Parallel solutions finish faster by running independent pieces at onceSpeedup is limited by the sequential portion; it falls short of the number of processors whenever there is a sequential portion
Parallel solutions scale better than sequential ones as problems growBeyond some point, adding more parallel processing no longer meaningfully helps
Distributed computing solves problems too large for one computer's storage or timeSplitting, sending, and combining across a network adds sequential time, and more machines mean more of it
Distributed systems can keep going if one machine failsMore machines and more communication mean more places for something to go wrong

8Worked examples

Worked Example 1 Compute times

Setup, then three tasks

A job has a 6-second setup, then tasks of 12, 9, 4 seconds that can run at the same time. Find the sequential time, the parallel time with enough processors, and the speedup.

  1. 1
    Sequential: 6 + 12 + 9 + 4 = 31 s.
  2. 2
    Parallel: the setup is added; the tasks overlap, so take the longest: 6 + 12 = 18 s.
  3. 3
    Speedup: 31 ÷ 18 ≈ 1.72.
Answer: 31 s, 18 s, speedup about 1.72. Answering 31 s for the parallel time means the tasks were added instead of overlapped.
Worked Example 2 Split the tasks

Two processors, four tasks

Independent tasks take A 15 s, B 25 s, C 40 s, and D 20 s. Split them between two processors to finish as soon as possible.

  1. 1
    Total work is 100 s, so no split can beat 50 s, and C alone is 40 s.
  2. 2
    If C runs alone, the other processor gets 60 s. If C shares, the best partner is the shortest task: C + A = 55 s.
  3. 3
    Then B + D = 45 s on the other processor. Checking every possible split confirms nothing finishes sooner.
Answer: C and A on one processor, B and D on the other: 55 s, a speedup of 1.82.
Worked Example 3 Find the limit

Many processors

A program has 60 s of parallel work and 10 s of sequential work. What is the time with 3 processors? With as many processors as you like?

  1. 1
    3 processors: 60 ÷ 3 + 10 = 30 s.
  2. 2
    With more and more processors the parallel part shrinks toward 0, but the 10 s remain.
Answer: 30 s, and never less than 10 s.
Worked Example 4 Evaluate a claim

“Four processors, four times faster”

A job is a 4-unit setup followed by 48 units of work that can be split evenly. A student claims 4 processors make it 4 times faster. Check the claim.

  1. 1
    1 processor: 4 + 48 = 52 units.
  2. 2
    4 processors: 4 + 48 ÷ 4 = 16 units.
  3. 3
    Speedup: 52 ÷ 16 = 3.25.
Answer: False. The speedup is 3.25, because the setup still runs once.
Worked Example 5 Choose a model

Too big for one computer

A research group needs to analyze 2 petabytes of telescope images, far more than any single computer they own can store. Which model fits, and why?

  1. 1
    Sequential on one computer fails on storage before speed even matters.
  2. 2
    Parallel computing inside one computer still has only that computer's storage.
  3. 3
    Distributed computing spreads the images and the work across many machines.
Answer: Distributed computing: it solves problems a single computer cannot, because of storage or processing time.

9Common mistakes

Common Mistake 1 · Adding the parallel tasks

Tasks that run at the same time count once, as the longest of them. Adding them gives the sequential time.

Common Mistake 2 · Dividing the wrong way

Speedup is sequential ÷ parallel. An answer below 1 means you flipped it.

Common Mistake 3 · Assuming N processors means N times faster

The sequential portion runs no matter how many processors there are, so the speedup is always less than the number of processors once there is any sequential work.

Common Mistake 4 · Forgetting the overhead in distributed computing

Splitting the work and combining the results across a network take time that does not overlap. Add it.

Compare solutions like this

“The sequential solution takes 6 + 12 + 9 + 4 = 31 seconds. The parallel solution takes the 6-second setup plus the longest parallel task, 12 seconds, for 18 seconds. The speedup is 31 ÷ 18 ≈ 1.72; it is not 3 because the setup cannot run in parallel.”

Show both times, the speedup, and what limits it.