/ concept-collection / math-webgpu-sandbox
Sign in
concept-collection / math-webgpu-sandbox
math-webgpu-sandbox / README.md
4.5 KBPreviewCodeBlameHistoryRaw

math-webgpu-sandbox#

Write a MATLAB script, run it on your GPU. The sandbox compiles MATLAB-syntax scripts to fused WebGPU compute kernels, times them with the script's own tic/toc, and — because everything you write is plain MATLAB — you can paste the same script into real MATLAB and compare the numbers. An optional in-browser CPU run through numbl's normal engine gives a third column without leaving the page.

Live page: https://concept-collection.github.io/math-webgpu-sandbox/

n = 8000000;
x = rand(n, 1);
y = zeros(n, 1);
tic;
for k = 1:600
  y = y + 0.1*sin(x + k) .* exp(-x) + x.^2;
end
toc
fprintf('checksum %.4f\n', mean(y));

That loop body is one GPU kernel, compiled once and replayed 600 times — about five billion element-updates, a few seconds on an integrated GPU.

How it works#

The script is parsed and lowered by numbl's JIT pipeline (reached through a numbl-src vite alias, the same arrangement turing-surface uses), which fixes every type and shape at compile time — n = 2048; A = rand(n); pins static shapes via exact-value propagation. The back end is this repo's: a planner maps the typed IR onto WebGPU, and an executor replays the resulting op sequence.

What is (and isn't) supported#

Supported: elementwise math on real arrays (including comparisons, &/|/~, two-arg max/min, mod/rem, .^), zeros/ones/eye/rand/randn/ linspace/ranges, A*B, transpose, the reductions above, A(:)/reshape, counted for loops, tic/toc, disp/fprintf, semicolon-suppressed or echoed assignments, and literal row/matrix constants.

Deliberately rejected, with a source-located error rather than a wrong answer: indexing/slicing beyond (:), if/while, complex numbers, user-defined functions, variables that change size, and printing inside for loops (the body has nowhere to run host I/O — it replays on the GPU).

The comparison is honest, with two caveats#

Development#

npm install        # numbl must be checked out as a sibling: ../../numbl
npm run dev
npm run test:node  # correctness suite on desktop WebGPU (Dawn)
npm run test:gpu   # same suite in headless Chrome (SwiftShader fallback)

The GPU compiler runs from numbl's TypeScript sources directly (through the numbl-src alias), so no numbl build is needed.

License#

Apache-2.0

moveopenescclose