Experimental GPU simulator

An experimental GPU port of RocketSim, not used for training yet.

9 min read

gpu-sim/ is an experimental port of RocketSim's physics to the GPU, written in NVIDIA Warp. It steps many independent arenas in parallel and is checked against RocketSim with our own scenarios and physlab compare. Training doesn't use it yet: it can't drive a car on the ground, and there is no GPU training environment. It's an optional research track that builds on the CPU simulator rather than replacing it.

The work is split into stages named by what they add: B for the ball, C for cars, E for the RL environment (What each stage covers).

The goal isn't "close enough". Every stage so far reproduces RocketSim 0.2.11 + patch P1 bit for bit, so the GPU can eventually stand in for the CPU env without changing what a policy learns.

B1 · Ball + arena

done 51 scenarios (41 shared + 10 GPU-owned edge cases), 29,865 / 29,865 samples bit-identical, events included. Throughput with contact is about the same as 28 CPU threads.

C1 · Car free flight

done 29 scenarios, 3,871 / 3,871 car-ticks bit-identical. About 0.95 G car-ticks/s, ~18× RocketSim on 28 threads.

C2 · Car on flat ground

next Wheel raycasts, suspension, tire friction, ground jumps. Its throughput gate decides whether the GPU path continues.

Where it lives#

gpu-sim/ is its own uv project (Python 3.12, warp-lang==1.18.0, numpy==2.5.4). It is not part of the root pyproject.toml, and it doesn't need a CUDA toolkit, because Warp bundles NVRTC. Rust is needed once, to build the oracle.

    • pyproject.toml separate uv project; gpu-sim console script
      • tick.py the ball megakernel: one thread per arena, n ticks per launch
      • carsim.py C1 car free-flight kernel, one thread per car
      • contact.py sphere vs. triangle/plane contacts, persistent manifolds
      • solver.py Bullet's sequential-impulse solver, ported line by line
      • wpmath.py glam's SSE2 operation order, written out by hand
      • trig.py CPU-libm-exact sin/cos (--trig crt)
      • arena.py / cmf.py arena geometry from the oracle dump, checked against the .cmf files
      • oracle.py / consts.py / scenario.py oracle build and lookup, RocketSim constants, scenario loader
      • sim.py BatchBallSim Python API
      • runner.py / trace.py batch runs, trace writer, physlab compare integration
      • bench.py / carbench.py / sweep.py throughput benchmarks and bound sizing
      • cli.py gpu-sim info | cache | run | batch | crosscheck | bench | …
      • make_oracle.py copies vendor/rocketsim and widens visibility only
      • oracle/ gpusim-oracle: dumps geometry, manifolds, trig table, CPU baseline
      • car-oracle/ gpusim-car-oracle: random-car crosscheck and CPU baseline
    • scenarios/ 24 GPU-owned edge cases (gpu-ball-*, gpu-car-*)
    • tests/ bit-exactness, determinism, batch invariance, crosscheck
    • reports/ fidelity-round1.md (independent round-1 validation)
    • .cache/ oracle build, geometry, trig table, reference traces, bin/physlab.exe (frozen reference physlab, sha256 465b832790b4d40f), bench/, car-oracle/ (gitignored)
    • out/ batch and benchmark output (gitignored)

How it stays bit-exact#

Getting bit-identical output from a GPU port of a Bullet-derived physics engine needed four things.

  1. Exact data from RocketSim itself (the oracle). tools/make_oracle.py copies vendor/rocketsim and applies 9 visibility-only substitutions, making some internal modules and fields pub. No logic changes. The oracle then dumps what would be hard to rederive:

    • Bullet's local triangle points and normals;

    • internal-edge info;

    • BVH leaf visit order;

    • broadphase proxy AABBs;

    • the full broadphase grid with every cell's ordered handle list.

    ArenaGeometry re-verifies the dump against the .cmf files bit for bit.

  2. Replaying RocketSim's broadphase exactly. Contact order affects the sequential-impulse solver. The GPU therefore replays RocketSim's grid cell lists, not a "body order" approximation. It visits the statics listed in the cell of the ball box's min corner, in insertion order, and leaves a manifold frozen when RocketSim would.

  3. Line-by-line ports with fixed float semantics.

    • Contacts, manifolds, internal-edge correction and the solver are ported from the Rust sources, with glam's operation order written out by hand.

    • Every module sets fuse_fp=False (no FMA contraction) and fast_math=False. Every calling module needs this too, because Warp compiles a @wp.func into the caller's module.

  4. The CPU's libm, reproduced. RocketSim calls Rust's f32 sin/cos, which on Windows are UCRT sinf/cosf, and those aren't correctly rounded everywhere.

    • --trig crt (the default) evaluates in f64, rounds to f32, and applies an exhaustive exception table. The oracle enumerates all 2.16e9 f32 inputs with |x| ≤ π and finds 89,808 sin and 103,142 cos inputs that differ.

    • The table is keyed by the hash of ucrtbase.dll.

    • Without it, 31 ticks in two scenarios end up 1 ulp off.

A few hard-won lessons from building it:

  • Warp scalar unary minus loses the sign of zero. In Warp 1.18, -x on a scalar float32 gives +0 for x = +0. The car kernel uses wpmath.neg(x) (x·−1) wherever a zero's sign can reach the output.

  • The quaternion is not persistent state. RocketSim re-derives it from the rotation matrix every tick, so the GPU does the same.

  • Event slots are a memory trade-off. Traces use E = 32 events per env per tick. The API and benchmark default of 8 drops events in reachable states, up to 93 in one tick for random states. Events never feed back into physics.

What each stage covers#

StageScopeStatus
B1 Ball + arenaClamp, quantize (P1), damping, plane and mesh contacts, persistent manifolds, internal-edge correction, the ball-only solver, integration, eventsdone 2026-10-15
B2 Ball performanceSplit integrate/contact kernels, compaction of contacting envs, fewer registers. Target ≥ 1.5× B1 at 256k envsoptional, not started
C1 Car free flightJump, double jump, flip impulse and torque, flip cancel, z-damp, pitch lock, boost, air throttle, air control; a conservative contact-free validity windowdone 2026-10-15
C2 Car on flat ground4 wheel raycasts on the floor plane, suspension, friction curves, sticky force, ground jumps, landingsnext
C3 Car vs arena meshWheel rays against the mesh BVH, hitbox vs planes and mesh, wall driving, auto-flipplanned
C4 Car–ballSphere vs car OBB, shared solver island, RocketSim's extra hit impulseplanned
C5 Car–carBox–box contacts, bumps and demos, respawnplanned
E1 RL environmentTorch-native batched env with the task-JSON layout, on-device auto-reset, matching the CPU BatchEnvafter C2

The C1 validity window. Cars are only judged while no contact of any kind is possible. In Soccar, every car origin must be inside |x| ≤ 3350, |y| ≤ 3200, 195 ≤ z ≤ 1920 uu, and more than 205 uu from the ball and from every other car. The 195 uu floor clears the boost-pad pickup cylinders. The predicate is conservative: in every scenario where it fires, it fires before RocketSim's first divergence from free flight. Traces record c1_valid_until_tick.

Performance so far#

All numbers are from an RTX 4070 next to an i7-14700K (28 threads). The GPU was shared with a running Rocket League client, so GPU numbers are lower bounds.

WorkloadGPUCPU RocketSim, 28 threadsRatio
Ball, random states with contact (1M envs)~74–75 M env-ticks/s70–82 M (depends on scheduling)≈ 1×
Ball, resting/rolling on the floor (1M envs)54.6 M46.8 M1.2×
Ball, free flight in the_void1.29 G150 M8.6×
Car free flight (≥ 65k cars)~0.95–0.98 G car-ticks/s53 M~18×

What the numbers say:

  • These are physics-only numbers. Training doesn't use the GPU simulator, so none of this makes training faster today.

  • Integration is cheap on the GPU. Contact is the bottleneck. The B1 megakernel has a large register footprint and a 348-byte local stack, so occupancy is low. Splitting the kernel and compacting contacting envs is B2 work.

  • C1 flatters the GPU, because free flight has no contacts. The real question is decided at C2/C3, where wheel rays and arena contacts come in.

  • Long launches matter. step(n) loops ticks inside one kernel launch. Short step(8) launches lose about 30% for the ball and 10–20% for cars.

  • Memory favours the GPU. About 160 B per car, against ~815 KB per one-car RocketSim arena.

Why not Bullet3 OpenCL?#

Bullet3's GPU pipeline was built and measured before Warp was chosen. It was 6–30× slower than one RocketSim CPU thread, non-deterministic, and wrong out of the box (restitution hard-coded to 0). See Alternatives considered.

Using it#

sh
just gpu-sync        # create the gpu-sim venv (add --extra torch for zero-copy torch views)
just gpu-cache       # build the oracle, dump geometry + libm trig table, size the bounds
just gpu-test        # pytest gpu-sim/tests (146 tests)
just gpu-run scenarios/ball/ball-drop-center-500.json recordings/ball-drop-center-500.gpu-warp.jsonl
just gpu-batch       # ~80 scenarios as one GPU batch + RocketSim references + compare
just gpu-validate    # gpu-batch, then random ball arenas and random cars bit for bit
just gpu-bench --envs 65536,1048576 --readback --cpu-baseline
just gpu-car-bench --cpu-baseline --with-ball

gpu-batch writes gpu-sim/out/gpu/summary.{md,json}. RocketSim references come from a physlab binary, looked up in this order:

  1. --physlab;

  2. $PHYSLAB_EXE;

  3. a frozen copy at gpu-sim/.cache/bin/physlab.exe (present: the 06:52 build, sha256 465b8327…, that all B1 round-2 and C1 results used);

  4. target/release/physlab.exe.

The reference cache is keyed by that binary's sha256, so a rebuild with different vendor patches can't silently reuse stale references. The current target/release build (12:19, sha256 bb5d684d0ffa05c0) hasn't been run against the GPU suite. Against the real game, GPU-vs-real error equals RocketSim-vs-real exactly on the 37 scenarios with real captures. Details: Using gpu-sim.

From Python:

python
from gpusim.sim import BatchBallSim
from gpusim.carsim import BatchCarSim

sim = BatchBallSim(n_envs, device="cuda:0", trig="crt")
sim.set_state(env_ids, pos_uu, vel_uu_s, angvel_rad_s, mode)  # mode 1 = soccar, 0 = the_void
sim.step(1200)                                                # ONE kernel launch looping 1200 ticks
state = sim.get_state()

cars = BatchCarSim(n_cars, device="cuda:0", trig="crt")
cars.set_controls(axes, buttons, every=8)  # axes [rows,N,5]; buttons bit0 jump, 1 boost, 2 handbrake
cars.step(1200)

Open questions#

  1. Throughput with contact. B1 matches 28 CPU threads but doesn't beat them. Re-measure on an idle GPU before trying the B2 ideas.

  2. atan2 has no exact table (it has a 2-D domain). It's only used by the internal-edge correction, and no suite tick has shown an effect.

  3. C1 coverage gaps. Ported code that only runs after contact (is_jumping, auto-flip continuation, on_ground branches) is unexercised until C2.

  4. Thread mapping for C4. One thread per car, or one per arena once cars can touch the ball? Running the ball kernel separately already costs about a third of car throughput.