SpawnDev.ILGPU

GPU compute in C#, in the browser or on the desktop.

License: MIT, plus ILGPU code under NCSAnet10.0

SpawnDev.ILGPU extends ILGPU, the .NET compiler that turns C# methods into GPU code, with three browser backends (WebGPU, WebGL and WebAssembly) and a SIMD CPU backend (Velocity) for the desktop. A kernel written for CUDA or OpenCL runs unchanged in a Blazor WebAssembly app and on the desktop, and the application picks the best available backend at run time. It ships as one NuGet package that brings the forked ILGPU compiler and SpawnDev.SpawnJS for browser interop.

Runs on

WebGPUWebGLWasmCUDAOpenCLVelocity (SIMD CPU)CPU

Highlights

  • One kernel, every backend: WebGPU, WebGL and Wasm in the browser; CUDA, OpenCL, Velocity (SIMD CPU) and the plain CPU accelerator on the desktop.
  • Existing ILGPU code runs as it is: ArrayView, Index1D/2D/3D, shared memory, atomics, Group.Barrier(), lambdas and the ILGPU Algorithms (radix sort, scan, reduce, histogram) work on the browser backends.
  • Automatic backend selection with CreatePreferredAcceleratorAsync(): WebGPU, then WebGL, then Wasm in the browser; the discrete GPU before the CPU on the desktop.
  • Sub-word and low-precision types: Int8/16, Half, BFloat16, FP8 (Float8E4M3, Float8E5M2), FP4 (Float4E2M1) and packed 4-bit integers (QInt4, QUInt4).
  • Zero-copy browser I/O: write a JavaScript TypedArray straight into a GPU buffer, stream a Blob or OPFS file into a buffer in bounded chunks, present a buffer on a canvas.
  • WebGPU performance tools: batched dispatch submission, dispatch-plan capture and replay, device-memory budgeting, and shader precompilation with a persisted cache.
  • Trim and AOT safe (the IL must be kept for Blazor WebAssembly AOT) and no native dependencies.
Program.cs
// Registers every backend the environment offers:
// browser = WebGPU, WebGL, Wasm; desktop = CUDA, OpenCL, Velocity
using var context = await Context.CreateAsync(builder => builder.AllAcceleratorsAsync());

// Browser: WebGPU, then WebGL, then Wasm.
// Desktop: CUDA, then a discrete OpenCL GPU, then other OpenCL devices, then Velocity.
using var accelerator = await context.CreatePreferredAcceleratorAsync();

int length = 256;
using var a = accelerator.Allocate1D(Enumerable.Range(0, length).Select(i => (float)i).ToArray());
using var b = accelerator.Allocate1D(Enumerable.Range(0, length).Select(i => (float)i * 2f).ToArray());
using var c = accelerator.Allocate1D<float>(length);

var kernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<float>, ArrayView<float>, ArrayView<float>>(VectorAdd);
kernel((Index1D)length, a.View, b.View, c.View);

await accelerator.SynchronizeAsync();               // wait for the GPU
float[] results = await c.CopyToHostAsync<float>(); // the one way to read results back in the browser

static void VectorAdd(Index1D i, ArrayView<float> a, ArrayView<float> b, ArrayView<float> c) => c[i] = a[i] + b[i];

Install

dotnet add package SpawnDev.ILGPU

Building on SpawnDev.ILGPU?

Private tickets, response targets and a direct line to the engineer who wrote it.

See support plans
An unhandled error has occurred. Reload x