← Blog
✎post del blog

One spider ran 57 times faster on the CPU, and the GPU won only in crowds

A small matte grey resin spider on a brushed steel plate beside a tall tower of dense steel blocks, in teal and steel with a warm amber rim light, the AMARBARO mark and wordmark centred over it.

One spider took 35.2 ms per frame on the GPU wave and 0.62 ms on the host CPU, but the GPU wave beats the host somewhere above 256 animals, and our sweep does not place that crossover exactly.

One spider, on one frame, took 35.2 milliseconds on the GPU and 0.62 milliseconds on the CPU. That is 57 times faster on the CPU. A frame is one update of the simulation, and at 60 frames per second a frame has about 16.7 ms to fit in.

One animal is the CPU's best case. The same physics in a crowd of 1,024 animals is the GPU's best case. The backend we ship follows that split.

One animal favours the CPU

The timing ran at 640 by 360 pixels, with K=10 and BRAIN=2. Our reading is that K=10 means ten physics steps per frame. Our log does not spell that out, so we treat it as an assumption.

The GPU wave's cost per animal and step falls as the crowd grows: 3,553 microseconds at one animal, 232 at 16, 62 at 64, 21 at 256 and 9.9 at 1,024. That shape looks like a large fixed cost that more animals spread out. The host falls too, to 10 at 256 animals, then climbs to 43 at 1,024. The single-animal row gives 54 microseconds for the host and 3,553 for the wave, a ratio of about 66. That is a second route to the same direction as the 57 from the frame times.

The crowd picks the backend

Animals Host (us per animal-step) GPU wave GPU thread
1 54 3,553 4,912
16 37 232 330
64 12 62 90
256 10 21 24
1,024 43 9.9 7.1

At 256 animals the host is still ahead, 10 against 21. At 1,024 the GPU wave is ahead, 9.9 against 43. The host's cost jumps between those two sizes, and our log ties that jump to the 96 MB L3 cache, the fast memory on the processor.

The auto backend uses the host up to 256 animals, the GPU wave below 1,024, and the GPU thread from 1,024. The 256 threshold is a pick from five sweep points. It is not a fitted crossover, and no point between 256 and 1,024 was measured. A mixed CPU and GPU split was rejected. Our log says the GPU frame time stayed flat below 1,024.

What the frame times mean for a viewer

At 35.2 ms per frame, one spider's GPU physics allows about 28 frames per second. At 0.62 ms, the host physics allows about 1,600. Both figures are physics only. Rendering is a separate cost, and the eyes cannot run on the CPU build at all.

Two things that went wrong

The first is the one that could have made the timing a lie. CPU builds with BRAIN set to 2 or 3 never received the device brain's controls, so the spider stood still. A standing spider is very cheap to simulate, and a timing taken then would have looked like a win. The fix landed in the same commit as the timing. The A/B harness now compares the root positions of the GPU and host arms and requires them to agree within 1 percent of travel.

The second was a stale cache. Mesh folders written before vertex normals existed, at 7,368 bytes for one file against 22,104 for the new format, crashed new builds. The cache keyed only one file. The stale folders were deleted on 2026-10-10, and the code now rebuilds them.

What we still do not know

  • The eyes cannot run on the CPU build. The host path is not a full substitute while the viewer needs eyes. A viewer with eyes still runs on the GPU.
  • Where the GPU wave overtakes the host, between 256 and 1,024 animals.
  • Whether the frame figures and the per-step sweep share the ten-step reading. We have not confirmed it.

How to prove this wrong

Run the A/B harness on one build for 300 frames in both arms. Check that the single-animal ratio stays near 57. Then add a 512-animal point. If the GPU wave beats the host there, the auto backend's 256 threshold is wrong, and the post should say the crossover sits lower.

Provenance

  • ~/Brain/fly-connectome/2026-10-10-spider-cpu-physics.md: the 35.2 ms and 0.62 ms timings, the sweep table, the 96 MB L3 note, the controls bug, and the mesh-cache gotcha.
  • ~/Projects/science/fly-connectome/agent/body_engine/spider_cpu_ab.py: the A/B harness at 640 by 360 with K=10 and BRAIN=2, GPU arm against CPU arm.

Commenti

Ancora nessun commento.

Accedi per commentare.