/ concept-collection / math-webgpu-sandbox
Sign in
concept-collection / math-webgpu-sandbox
math-webgpu-sandbox / CLAUDE.md
4.6 KBPreviewCodeBlameHistoryRaw

CLAUDE.md#

Tips for future agents working in this repo.

Architecture#

src/mgpu/compile.ts   MATLAB source -> numbl IR: parseMFile + lowerProgram
                      (whole-script lowering, NOT turing-surface's per-function
                      specialization), numbl's inlinePass, then fuse.ts.
src/mgpu/patches.ts   Type-rule patches applied to numbl's JIT builtin
                      registry (registerBuiltin overwrites by name): precise
                      shapes for comparisons, tensor &/|/~, two-arg max/min,
                      matrix rand, and randn registered from scratch. Only
                      types — numbl's emitters never run here.
src/mgpu/fuse.ts      Folds single-use _mtoc2_* temps numbl's inline pass
                      declines (tensor-producing Calls like sin(x), logicals),
                      so one source line = one kernel.
src/mgpu/wgsl.ts      Fused elementwise emitter -> one WGSL kernel per Assign.
                      Also: inline generators (rand/randn/linspace/ranges/eye
                      computed from the linear index), loop-var uniforms,
                      runtime scalars as 1-element buffers, exact-value
                      folding at every node.
src/mgpu/kernels.ts   Non-elementwise WGSL: tiled column-major GEMM, tiled
                      transpose, 2-pass full reduction + per-column reduction,
                      all with shapes baked in as constants.
src/mgpu/plan.ts      IR statements -> static op sequence: buffers, pipelines
                      (cached by WGSL text), bind groups, aliasing (X(:) etc.
                      as views when the source has exactly one assignment),
                      scratch+copy-back for in-place updates, `for` bodies
                      planned once with per-iteration dynamic-offset uniform
                      slots (256 B each), host ops (tic/toc/disp/fprintf/echo).
src/mgpu/run.ts       Executor: streams ops into command encoders; tic/toc
                      flush + await onSubmittedWorkDone (that's what makes toc
                      MATLAB-comparable); readbacks for printing.
src/cpu/cpuRunner.ts  Optional CPU column via createNumblSession
                      (numbl/browser) — numbl's own worker, f64, lazy-loaded
                      because it's a ~3 MB chunk.
test/cases.ts         One suite, two harnesses: scripts/test-node.ts (Dawn via
                      the `webgpu` npm package) and test/test-page.ts +
                      scripts/test-gpu.mjs (headless Chrome, SwiftShader
                      fallback).

Key gotchas#

Testing#

moveopenescclose