Every routine in this library follows the same structure. Understanding it once means you can read any routine file. The BLAS reference spec lives at netlib.org/blas.
The general flow
Validate arguments — requireGpuDevice and requireSameDevice, then the shared checks in validate, all before any GPU work; requireSameResidency also returns whether the operands are GPU-resident
Early return: empty output — m = 0 or n = 0 returns the output untouched
Validate shapes — each matrix's leading dimension and size (requireMatrixStorage, requireSquareStorage)
Early return: output unchanged — reference BLAS's quick return (e.g. alpha = 0 with beta = 1)
Row-major view — a column-major operand is read as its transpose (layoutOf, flipTrans)
Compile — the WGSL shader is compiled into a compute pipeline by name, cached per device (getPipeline(device, "saxpy"))
Upload — inside the try: operandBuffer takes a GpuVector's or GpuMatrix's own buffer or uploads a host array (f64 split into hi/lo f32 halves); scalars and sizes go into a uniform buffer via createParamsBuffer
Bind — buffers are grouped into a bind group with createBindGroup, in @binding order
Dispatch — runComputePass (or runComputePasses, one command encoder) dispatches enough workgroups to cover the work; with benchmarking on, timestamp writes bracket the pass(es)
Read back — stageReadback copies the output into a pooled MAP_READ buffer and extractResult maps it (f64: both halves through one map, stageReadbackPair); extractTimestamp gives gpuTimeMs. Skipped for a GPU-resident output
Destroy — temporary buffers are freed in the finally via destroyBuffers; GPU-resident operands are left alone
All routines are async because GPU operations — dispatching and mapping — are asynchronous by nature.
Naming
This follows the BLAS naming convention:
s — single precision (f32)
d — double precision, emulated with double-double arithmetic (WGSL has no native f64)
c — single-precision complex (cscal so far)
z — double-precision complex (not implemented yet)
i — returns an index (isamax, idamax)
Inputs
Routines accept either host typed arrays — Float32Array for s, Float64Array for d, Complex32Array for c — or GPU-resident operands: GpuVector for vectors and GpuMatrix for matrices, wrappers that keep buffers alive on the GPU between calls. Use the GPU-resident forms when calling routines repeatedly on the same data, to avoid re-uploading on every call; their results stay on the GPU too.
Utility functions
The boilerplate — creating buffers, bind groups, pipelines, and compute passes — is factored into devdocs/utility-functions. Each step maps to one utility function rather than raw WebGPU objects inline.
BLAS Routine Patterns
Every routine in this library follows the same structure. Understanding it once means you can read any routine file. The BLAS reference spec lives at netlib.org/blas.
The general flow
requireGpuDeviceandrequireSameDevice, then the shared checks invalidate, all before any GPU work;requireSameResidencyalso returns whether the operands are GPU-residentm = 0orn = 0returns the output untouchedrequireMatrixStorage,requireSquareStorage)alpha = 0withbeta = 1)layoutOf,flipTrans)getPipeline(device, "saxpy"))try:operandBuffertakes aGpuVector's orGpuMatrix's own buffer or uploads a host array (f64 split into hi/lof32halves); scalars and sizes go into a uniform buffer viacreateParamsBuffercreateBindGroup, in@bindingorderrunComputePass(orrunComputePasses, one command encoder) dispatches enough workgroups to cover the work; with benchmarking on, timestamp writes bracket the pass(es)stageReadbackcopies the output into a pooledMAP_READbuffer andextractResultmaps it (f64: both halves through one map,stageReadbackPair);extractTimestampgivesgpuTimeMs. Skipped for a GPU-resident outputfinallyviadestroyBuffers; GPU-resident operands are left aloneAll routines are
asyncbecause GPU operations — dispatching and mapping — are asynchronous by nature.Naming
This follows the BLAS naming convention:
s— single precision (f32)d— double precision, emulated with double-double arithmetic (WGSL has no native f64)c— single-precision complex (cscalso far)z— double-precision complex (not implemented yet)i— returns an index (isamax,idamax)Inputs
Routines accept either host typed arrays —
Float32Arrayfors,Float64Arrayford,Complex32Arrayforc— or GPU-resident operands:GpuVectorfor vectors andGpuMatrixfor matrices, wrappers that keep buffers alive on the GPU between calls. Use the GPU-resident forms when calling routines repeatedly on the same data, to avoid re-uploading on every call; their results stay on the GPU too.Utility functions
The boilerplate — creating buffers, bind groups, pipelines, and compute passes — is factored into devdocs/utility-functions. Each step maps to one utility function rather than raw WebGPU objects inline.