GPU roadmap

lib/gpu/ runs std.quantum.Circuit on Vulkan 1.0 compute queues with SPIR-V it emits itself (Hardware Vulkan execution). gpu.vulkan lends a Job to a closure (Device.with_job). The job’s methods are safe. view(T) borrows the job, so the borrow checker keeps host reads of GPU memory away from submissions. This page lists the work that would come next, roughly in order.

1. Verify on hardware

The safe job API (with_job, write, view, typed dispatch) passed the unit, fail and example suites on a machine without Vulkan. It has not yet run on a GPU. Run scripts/gpu_check.sh on a discrete or integrated GPU, and compare its results and timings with the ones recorded for step 3b of the roadmap (AMD RX 9060 XT, 24 qubits in about 9 ms of compute).

2. Test in CI without a GPU

  • Accept software devices (Lavapipe) behind an opt-in variable such as JIHOO_GPU_ALLOW_SOFTWARE=1. Selection keeps rejecting them by default. Then CI’s full job can run examples/quantum_gpu.jh against the CPU simulator on every change.
  • Validate the emitted shaders with spirv-val (Debian’s spirv-tools). The tests in lib/gpu/spirv.jh check the module’s structure only.
  • Run the Vulkan validation layers in that job, so that synchronization mistakes fail the build.

3. Spend less time outside the shader

At 20 qubits, compute was 0.35 ms of a 4.33 ms run. Setup, upload and readback took the rest.

  • Initialize the state on the GPU. vkCmdFillBuffer zeroes it and vkCmdUpdateBuffer writes the first amplitude. Today write clears and copies the whole staging buffer, 128 MiB at 24 qubits.
  • Reduce on the GPU. Leakage checks and probabilities() read back the whole state. A reduction shader could return one sum per checked wire, or only the requested probabilities.
  • Fuse gates. Consecutive single-qubit gates on one target (and diagonal gates generally) combine into one matrix, which saves a pass over the state per fused gate.
  • Overlap work. execute waits on a fence after every chunk. Two command buffers would let the CPU record one chunk while the GPU runs the other.
  • Reuse jobs. Gpu.run builds a pipeline per run. A job cached per precision and size would remove most of setup_ns from repeated runs.

4. Generalize gpu.vulkan

A Job has one storage buffer and one push block, which fits the pair shader and little else. Before more shaders use it:

  • Several bindings per job, with typed buffers (Buffer(T: Scalar)) and views for each.
  • Writes and downloads of a range instead of the whole buffer.
  • A small SPIR-V builder in place of the fixed module in spirv.jh, so that new kernels need no hand-assigned ids.
  • Decide the library’s scope. Either gpu.vulkan becomes a general compute library with quantum as one user, or the directory says it is a quantum backend.

5. Safety follow-ups

  • Constrain dispatch’s push block to plain data. It copies size_of(P) bytes and checks only the size, so a pointer or a padding byte reaches the shader unnoticed.
  • Diagnostics: a view returned from the with_job closure is reported inside with_job (“job goes away here while it is still borrowed”), not at the closure. The error should point at the user’s code.
  • The #[view] rule says that raw pointers in a view type point to what the type borrows. A view type that owns the memory behind a raw pointer (as the old Job did) therefore gets slices that borrow the wrong thing, and no error says so. Document the pitfall in Coming from Rust and Borrowing, or warn about it.

6. Concurrency

  • Several queues or jobs at once. with_job holds the device mutably, which serializes all GPU work in a program.
  • Combine with std.thread, for example CPU threads that record command buffers for separate jobs, once jobs can share a device safely.
Esc