Task 1 of 4

The Laplacian: Ask Your Neighbors

Diffusion is gossip: every cell drifts toward the average of its neighbors. The operator that measures "how far am I from my neighbors' average" is the Laplacian, and on a grid it's a five-read gather: left + right + up + down − 4·center. Positive means the neighbors are higher and stuff will flow in; negative means it flows out.

One wrinkle: simulations hate edges. Instead of clamping like the filters in Convolution & Filters, we wrap around — the left neighbor of column 0 is column 31. The world becomes a torus and every cell has exactly four neighbors, no special cases.

five reads, one number — how far am i from my neighbors' average?
Goal: complete the gather kernel so it returns the 5-point Laplacian of field with wrap-around edges.

Requirements

Hint 1 — the wrap is an if

Same trick as clamping, different else:

let xr = this.thread.x + 1;
if (xr > this.constants.size - 1) xr = 0;

The starter already wrote xl for you — mirror it three times.

Hint 2 — five reads

The neighbors sit at field[y][xl], field[y][xr], field[yd][x] and field[yu][x] — only ever vary one coordinate at a time. The center is field[y][x].

Hint 3 — the whole return
return field[y][xl] + field[y][xr] + field[yd][x]
  + field[yu][x] - 4 * field[y][x];

Same idea elsewhere

The 5-point Laplacian stencil is the beating heart of PDE solvers on every platform — heat, waves, pressure projection in fluids. On big CUDA/ROCm clusters the wrap you just wrote becomes a halo exchange: each GPU ships its border rows to the neighbor that needs them before every step.

All tasks in Reaction–Diffusion

  1. The Laplacian: Ask Your Neighbors
  2. One Step of Gray–Scott
  3. Feed It Back: 100 Steps
  4. Paint the Pattern

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