Task 5 of 5

Put It Together: 128 Bodies

Everything from this module, running as one machine. The three kernels below are your last three tasks — softened O(n²) acceleration, the velocity tick, the position tick. What's missing is the conductor: a JavaScript loop that runs ten full ticks, feeding each kernel's output into the next and carrying the new state into the next tick.

Notice who does what: JavaScript never touches a single interaction — it just passes arrays around. The GPU grinds through 128 × 128 = 16,384 interactions per tick, 163,840 across the run. Swap 128 for 100,000 and this exact structure is a galaxy simulator; the loop you're about to write wouldn't change by a character.

Goal: write the simulation loop — ten ticks of accel → stepVel → stepPos, carrying the new arrays forward each time.

Requirements

Hint 1 — the shape of one tick

Inside the loop: await accel, unpack its pairs into ax, ay arrays (the unpack helper is right there), await stepVel, unpack, await stepPos, unpack. One at a time — the next call needs the previous one's numbers.

Hint 2 — carrying the state

End every tick by overwriting the state:

vx = newVx;
vy = newVy;
px = newPx;
py = newPy;

— next tick's accel must see the moved bodies, or time never advances.

Hint 3 — the whole loop
for (let step = 0; step < STEPS; step++) {
  const [ax, ay] = unpack(await accel(px, py, mass, SOFT));
  const [nvx, nvy] = unpack(await stepVel(vx, vy, ax, ay, DT));
  const [npx, npy] = unpack(await stepPos(px, py, nvx, nvy, DT));
  px = npx; py = npy; vx = nvx; vy = nvy;
}

Same idea elsewhere

A host loop launching device kernels in sequence is the universal skeleton of GPU simulation: CUDA streams queueing kernel after kernel per timestep, WebGPU building one command encoder per frame, Metal committing a command buffer per tick. Production codes differ mainly in never reading the arrays back between passes — that's what the textures in Pipelines & Textures are for.

All tasks in N-Body Gravity

  1. The Pull of One Star
  2. Every Body Pulls on Every Body
  3. Softening the Singularity
  4. One Tick of the Clock
  5. Put It Together: 128 Bodies

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