Log inStart free
LESSON 3.12 · 5 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Parallel reduction and prefix scan

Watch 8 numbers combine in pairs at once, then every running total appear.


Pro lesson. Stage 1 is free to watch. Pro opens all 5 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 3.12 · SCAN BAY1 WORKER · 7 OPS · 7 STEPS
01
Stage 1 of 5 · One worker
One worker keeps a running total. Each step waits for the step before: 7 steps for 8 numbers.
Explain it simply

One worker, like one person with a calculator, adds the numbers from top to bottom. Each new result needs the result before it. So the steps form a chain down the diagonal, and 8 numbers take 7 steps. Every result along the way is kept.

The column on the left holds the 8 inputs: 3, 1, 4, 5, 8, 4, 2, 9. One worker combines them in 7 steps, one per column. Its running totals are 3, 4, 8, 13, 21, 25, 27, 36.

input, partial valueworker, operationresultwrong result, idle workeridentity element
[0][1][2][3][4][5][6][7]inputstep 1s + x[1]1 opstep 2s + x[2]1 opstep 3s + x[3]1 opstep 4s + x[4]1 opstep 5s + x[5]1 opstep 6s + x[6]1 opstep 7s + x[7]1 op31458429481321252736[0][1][2][3][4][5][6][7]inputstep 1s + x[1]1 opstep 2s + x[2]1 opstep 3s + x[3]1 opstep 4s + x[4]1 opstep 5s + x[5]1 opstep 6s + x[6]1 opstep 7s + x[7]1 op31458429481321252736
Toy: 8 seeded numbers · 1 to 8 workers · one operation per worker per step
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
sk=sk−1⊕xk,T=n−1\sy{s}{out}{s_k} = \sy{s}{out}{s_{k-1}} \oplus \sy{x}{data}{x_k},\qquad \sy{T}{param}{T} = n - 1
s7=27+9=36,T=7s_7 = 27 + 9 = 36,\quad T = 7

Each running result combines the last result with the next number.

Common mix-ups

MYTHA parallel scan is always faster than one worker.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With max, does the tree find the same result as one worker?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 5
Unlock the full explainer
  • 4 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.