Task 1 of 6

The Sum So Far

A prefix sum — a scan — is a running total. Give it [3, 1, 4, 1] and it answers [3, 4, 8, 9]: cell i holds everything from the start up to and including element i. A reduction collapses an array to a single number; a scan keeps every partial answer along the way, which turns out to be far more useful.

In JavaScript it is two lines, and the shape of those two lines is the whole problem:

out[0] = x[0];
out[i] = out[i - 1] + x[i];

Look at what cell i needs: not its neighbour's input, but its neighbour's answer. Every thread on a GPU starts at the same instant, so when thread 7 reaches for out[6] nobody has computed it yet — and nobody will, because thread 6 is waiting on thread 5. That is a serial dependency chain as long as the array, and it cannot be a kernel. Write it here in plain JavaScript first; the rest of this module is five ways around it.

Goal: fill running so that running[i] is the total rainfall of days 0 … i, then log the array and the season total.

Requirements

Hint 1 — seed the chain

Cell 0 has nothing before it, so it is the only cell that does not read running[i - 1]. Set it first, then loop from i = 1.

Hint 2 — the loop
running[0] = rainfall[0];
for (let i = 1; i < rainfall.length; i++) {
  running[i] = running[i - 1] + rainfall[i];
}

The season total is the last cell — an inclusive scan ends with the reduction already done.

Same idea elsewhere

Every serious GPU platform ships a scan primitive precisely because you cannot write one by accident: CUDA has thrust::inclusive_scan and CUB's DeviceScan, ROCm has rocPRIM's inclusive_scan, Metal Shading Language has simd_prefix_inclusive_sum, and WGSL's subgroup extension has subgroupInclusiveAdd. All of them exist to break the chain you are about to feel.

All tasks in Prefix Sums (Scan)

  1. The Sum So Far
  2. Everyone Sums Their Own Prefix
  3. The Doubling Ladder
  4. Inclusive, Exclusive, and Why It Matters
  5. Work-Efficient: Upsweep, Downsweep
  6. Payoff: Offsets Place the Data

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.