wgblas
    Preparing search index...

    Module devdocs/blas-routines

    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.

    1. 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
    2. Early return: empty output — m = 0 or n = 0 returns the output untouched
    3. Validate shapes — each matrix's leading dimension and size (requireMatrixStorage, requireSquareStorage)
    4. Early return: output unchanged — reference BLAS's quick return (e.g. alpha = 0 with beta = 1)
    5. Row-major view — a column-major operand is read as its transpose (layoutOf, flipTrans)
    6. Compile — the WGSL shader is compiled into a compute pipeline by name, cached per device (getPipeline(device, "saxpy"))
    7. 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
    8. Bind — buffers are grouped into a bind group with createBindGroup, in @binding order
    9. Dispatch — runComputePass (or runComputePasses, one command encoder) dispatches enough workgroups to cover the work; with benchmarking on, timestamp writes bracket the pass(es)
    10. 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
    11. 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.

    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)

    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.

    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.