Fallback API β
cuda.fallback β
zig
/// Force the CPU fallback backend regardless of GPU presence.
/// Must be called before cuda.init().
pub fn force(enabled: bool) void
/// Return true if the CPU fallback is currently active.
pub fn isActive() boolfallback/dispatch.zig β
The dispatch layer sits between the public API and the CUDA runtime. On each call it checks whether the GPU backend is active:
zig
pub fn memcpy(dst: anytype, src: anytype, bytes: usize, kind: MemcpyKind) !void {
if (fallback.isActive()) {
return cpu_backend.memcpy(dst, src, bytes);
}
return runtime.cudaMemcpy(dst, src, bytes, @intFromEnum(kind));
}CPU Backend Implementations β
| GPU operation | CPU fallback |
|---|---|
cudaMalloc | allocator.alloc |
cudaFree | allocator.free |
cudaMemcpy | @memcpy |
cudaMemset | @memset |
cudaStreamCreate | No-op handle |
cudaStreamSynchronize | No-op |
cudaEventCreate | Stores std.time.Instant |
cudaEventElapsedTime | Monotonic clock delta |
| Tensor elementwise | Host scalar loop |
| Tensor reductions | Host scalar loop |
| Tensor matmul | NaΓ―ve O(nΒ³) host loop |
Environment Variable β
sh
CUDA_ZIG_FORCE_FALLBACK=1 ./your-binarySetting this environment variable forces fallback mode without changing source code.