Starting…
HPC · GPU compute

GPU vs CPU, on two classic workloads

Gravity between thousands of bodies, and heat spreading across a metal plate, each run on an Apple M2 Pro's graphics cores and on all of its CPU cores. One workload ends up twenty times faster on the GPU and the other barely twice as fast, and the reason is the most useful idea in GPU programming.

The results are in data/native.js; if this message stays, that file is missing. Run cd bench && make run.

Why the gap is so different

What the two kernels do

Gravity: for each body, add up G·m / (r² + ε²)3/2 times the direction to every other body, then nudge its velocity and position. ε (the "softening") keeps two bodies that pass very close from producing an infinite force.

Heat: new = old + 0.24 × (left + right + up + down − 4 × old). The 0.24 sits just under 0.25, the largest value for which this update stays stable. The edges are held cold.

Reading the live numbers

"Run the same on one CPU core", at the top, is JavaScript on one core in a background worker, so its gap to the GPU is bigger than the native comparison above, which uses every core. Browser timings include handing work to the GPU and waiting for it, and they move with whatever else the machine is doing.

Source: hpc/metal-gpu-bench in open-concept-lab. Native numbers: cd bench && make run on a Mac. It compiles the project's Metal shaders from source at runtime, so only the Command Line Tools are needed, writes results/bench-<date>.json, and export.py bakes it into site/data/native.js for this page.