Task 5 of 5

The Race

Everything is wired: the Jacobi sweep, both halves of the red-black sweep, and the residual. Same plate, same starting guess of zero, same finish line — an RMS residual below 0.0005. Count sweeps.

"The same work per sweep" is the claim worth being careful about. One Jacobi sweep updates all 900 interior cells once. One red-black sweep updates all 900 once too, in two halves — the same averages, split across two kernel launches instead of one, with half the threads in each launch copying themselves. Sweeps are what we are comparing; the extra launch is what it costs. (A production kernel launches only the cells of one colour and pays nothing for the copies.)

The residual is sampled every 5 sweeps rather than every sweep: the read-back is the expensive part of this loop, and five sweeps barely move the number.

same finish line, same work per sweep — 170 sweeps against 275
Goal: fill in the red-black loop, and read the two sweep counts off the console — Jacobi should need about 275 sweeps and red-black about 170.

Requirements

Hint 1 — the helper already does the counting

sweepsToTolerance takes one argument: a function that turns a grid into the next grid. Because a kernel call is awaited, that function is async — Jacobi's is async u => await sweep(u), and the helper awaits whatever it returns. Red-black's is one sweep — both halves — expressed the same way.

Hint 2 — the one-liner
const redBlackSweeps = await sweepsToTolerance(
  async u => await black(await red(u))
);

Both halves inside one call, so the helper counts a full sweep each time it runs it. Both awaits matter: the outer one hands the helper a grid, and the inner one is what makes the black half read the red half's output instead of a Promise.

Same idea elsewhere

The shape of this measurement is the one that transfers, more than the numbers: an iterative solver is judged on iterations-to-tolerance, and a GPU implementation is judged on that times the cost of an iteration. Red-black buys fewer sweeps for one extra dispatch, which is a trade you make on nearly every platform; the same ledger decides whether SOR's relaxation factor, a Chebyshev acceleration or a full multigrid V-cycle is worth its complexity. Multigrid is where this ends up — and its inner smoother is the red-black sweep you just wrote.

All tasks in Iterative Linear Solvers

  1. One Sweep of Jacobi
  2. Watch the Residual Fall
  3. Colour the Board
  4. Two Halves Make a Sweep
  5. The Race

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