Unit 4: Algorithms · Parallel and distributed computing

Article

Parallel computing

6 min read

Four friends must clean a classroom. If one person does everything, it takes an hour. If all four work at the same time, it takes about fifteen minutes. Computers can use the same trick. It is one of the main reasons why your phone can do so much so quickly.

One step at a time, or many at once

In sequential computing, a program's steps are done in order, one at a time. Each step starts when the one before it has finished. Everything so far in this course has been sequential.

In parallel computing, the program is broken into smaller parts (called tasks). Some of the tasks run at the same time, each on a different processor. A processor (or core) is the part of a computer that carries out instructions. Most modern phones and laptops have several.

Here is a small job: add a filter to a photo. We cut the photo into four strips. Filtering one strip takes 3 seconds.

  • Sequential: one processor does strip 1, then 2, then 3, then 4. That takes 3 + 3 + 3 + 3 = 12 seconds.
  • Parallel on 2 processors: each processor does two strips. Both run at the same time. That takes 3 + 3 = 6 seconds.

The photo filter on 2 processors (time goes from left to right)

  1. Processor 1

    Strip 1 (3 s)Strip 2 (3 s)
  2. Processor 2

    Strip 3 (3 s)Strip 4 (3 s)
Both processors work at once. The whole job takes 6 seconds, not 12.

Before you read on, work this out: how long would the job take on 4 processors? (Each processor gets one strip.) The answer is 3 seconds.

Check your understanding

Which statement best describes parallel computing?

Some steps must wait

Not every task can run at the same time as the others. Sometimes a task needs the result of another task. It must wait until that task has finished. Look at this calculation:

(1 + 2) × (3 + 4)

The two additions do not need each other, so they can run on two processors at the same time. The multiplication needs both results, so it must wait.

(1 + 2) × (3 + 4) on 2 processors

  1. Processor 1

    1 + 2 = 33 × 7 = 21
  2. Processor 2

    3 + 4 = 7(waits, nothing to do)
The multiplication cannot start until both additions are done. Only the additions run in parallel.

A step that cannot be split up, or that has to wait for others, is called the sequential part of a parallel program. It limits how much faster the program can get. The next article shows how much.

Check your understanding

A program has three steps.

AP pseudocode
x ← 6 + 2
y ← 5 + 4
z ← x * y

Which statement is correct?

Worked example: a photo app

The app does three things. It must load the photo first (2 seconds). Then it filters the top half and the bottom half (4 seconds each). Last, it saves the photo (1 second). The app has 2 processors.

  1. Load the photo. Nothing else can start without it, so only one processor works. 2 seconds.
  2. Filter both halves. Each processor takes one half, at the same time. 4 seconds.
  3. Save the photo. It needs both filtered halves, so one processor works alone. 1 second.

The photo app on 2 processors

  1. Processor 1

    Load photo (2 s)Filter top (4 s)Save photo (1 s)
  2. Processor 2

    waits (2 s)Filter bottom (4 s)waits (1 s)
Parallel time is 2 + 4 + 1 = 7 seconds. Run one after another, the same tasks take 2 + 4 + 4 + 1 = 11 seconds.

The result is exactly the same photo either way. Parallel computing changes how long the job takes, not what the answer is.

Is parallel always better?

No. Parallel computing helps when a job can be split into tasks that do not depend on each other, such as filtering a big photo, running a game's graphics, or checking many files. It helps less when:

  • most of the steps depend on one another, so there is little to run at the same time;
  • the work of splitting the job up and putting the results together costs more than it saves;
  • the program is small and quick already.

Parallel programs are also harder to write correctly, because the programmer must decide which tasks can safely run together.

Common mistakes

  • "Two processors always finish in half the time." Only if the work splits evenly and nothing has to wait.
  • "All the tasks run at the same time." Tasks that need another task's result must wait.
  • "Parallel computing gives a different answer." It gives the same answer, in less time.
  • "Parallel means many computers." Parallel computing uses several processors, usually inside one computer. Using many separate computers connected by a network is distributed computing (a later article).
  • "A computer with several processors is automatically parallel." The program must be split into tasks. Otherwise the extra processors have nothing to do.

On the exam

You may be asked to tell sequential from parallel computing, or to say which tasks in a list can run at the same time and which must wait. The next article shows how to calculate times. A key sentence to remember: in a parallel solution, some tasks run at the same time, and the tasks that depend on others limit the speed.

Key words

Sequential computing: doing a program's steps in order, one at a time.

Parallel computing: breaking a program into tasks and running some of them at the same time on several processors.

Processor (core): the part of a computer that carries out instructions.

Task: one part of a program that can be given to a processor.

Sequential part: a part of a parallel program that must run alone, because it cannot be split or must wait for other tasks.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.