Task 1 of 6

The One-Thread Trap

Meet the reduction: many values in, one value out — sum, min, max, mean. It's the awkward case in GPU land, because a kernel thread writes exactly one output cell. 4,096 inputs collapsing to 1 output means output: [1]… a single thread.

You can do it — kernels may loop, as long as the bound is known at compile time, which is exactly what this.constants is for. But one thread grinding through 4,096 additions while thousands of its neighbours sit idle is the slowest possible way to use a GPU. Write it anyway: it's the baseline the rest of this module tears down.

Goal: make the single thread loop over all of data (bound: this.constants.n) and return the total.

Requirements

Hint 1 — an accumulator

Declare let sum = 0; before the loop, add to it inside the loop, and return sum; after. Plain JavaScript — the transpiler handles it.

Hint 2 — the loop body

One statement: sum += data[i];

Same idea elsewhere

This wall exists on every platform: a single CUDA thread summing a whole buffer is the textbook example of what not to do, and a naive WebGPU compute shader with one invocation hits it just the same. Everyone's escape route is the trick you build next — split the work, then combine.

All tasks in Reductions

  1. The One-Thread Trap
  2. Partial Sums: Divide the Work
  3. One Rung of the Ladder
  4. Ride the Ladder Down
  5. Min and Max: Change the Operator
  6. Payoff: Mean and RMS, Fused

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