wgblas
    Preparing search index...

    Class GpuMatrix

    Represents a Float32Array (or Float64Array) matrix stored in GPU memory, row-major or column-major.

    rows/cols always describe the logical shape regardless of layout. lda (leading dimension) is the stride between consecutive rows (row-major) or columns (column-major) — must be >= cols (row-major) or

    = rows (column-major). When lda equals that minimum the matrix is dense with no padding.

    Index
    _buf: GPUBuffer
    cols: number

    Number of columns (logical shape, independent of layout).

    dtype:
        | Float32ArrayConstructor
        | Float64ArrayConstructor
        | typeof Complex32Array
        | typeof Complex64Array

    Typed array (or complex array) constructor used when reading data back from the GPU.

    layout: "column-major" | "row-major"

    Storage layout this matrix was created with — every routine that accepts a GpuMatrix reads this automatically.

    lda: number

    Leading dimension — stride between row starts (row-major) or column starts (column-major).

    rows: number

    Number of rows (logical shape, independent of layout).

    • Destroys the underlying GPU buffer. Call when the matrix is no longer needed to free GPU memory.

      import { init, cleanup, GpuMatrix } from "wgblas";

      await init();
      const mat = GpuMatrix.from(new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
      mat.destroy();
      console.log("GPU buffer released");
      if (typeof process !== "undefined") cleanup();

      Returns void

    • Downloads the matrix from GPU memory and returns a dense array of shape rows × cols, in the same layout and type it was created with. If lda exceeds the dense minimum, the leading-dimension padding is stripped so the returned array is always tightly packed.

      import { init, cleanup, GpuMatrix } from "wgblas";

      await init();
      const mat = GpuMatrix.from(new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
      const data = await mat.read();
      console.log(data); // Float32Array [1, 2, 3, 4, 5, 6]

      mat.destroy();
      if (typeof process !== "undefined") cleanup();

      Returns Promise<
          | Float32Array<ArrayBufferLike>
          | Complex32Array
          | Complex64Array
          | Float64Array<ArrayBufferLike>,
      >

    • Uploads a Float32Array, Float64Array, Complex32Array, or Complex64Array matrix to GPU memory, row-major or column-major. A Float64Array is split into a double-double (hi, lo) f32 pair per element (WGSL has no f64 type) and stored across two GPU buffers internally; read() reassembles doubles from these pairs. This gives ~48 bits of mantissa (vs. 24 for a single f32) but less than true f64 precision (52 bits), so round-tripped values are not always bit-exact with the original input. A Complex32Array is stored interleaved ([re0, im0, re1, im1, ...]) in one buffer; a Complex64Array gets the same double-double split applied independently to its real and imaginary components.

      rows/cols always describe the logical shape regardless of layout. lda defaults to cols (row-major) or rows (column-major) — dense, no padding. data must have at least rows * lda (row-major) or cols * lda (column-major) elements.

      Omitting the device falls back to the one from the last init call — the historical form, and fine for a single-GPU program. Pass a device explicitly (matching every routine's own (device, ...) convention) when driving more than one GPU at once, since a GpuMatrix is bound for life to whichever device created it.

      Parameters

      • data:
            | Float32Array<ArrayBufferLike>
            | Complex32Array
            | Complex64Array
            | Float64Array<ArrayBufferLike>

        matrix data, in the order matching layout

      • rows: number

        number of rows

      • cols: number

        number of columns

      • Optionallda: number

        leading dimension (default: cols for row-major, rows for column-major)

      • Optionallayout: "column-major" | "row-major"

        storage layout (default: 'row-major')

        import { init, cleanup, GpuMatrix } from "wgblas";

        await init();
        // 2×3 matrix: [[1,2,3],[4,5,6]]
        const mat = GpuMatrix.from(new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
        console.log(mat.rows, mat.cols, mat.lda); // 2 3 3

        // Same logical matrix, column-major storage
        const matCol = GpuMatrix.from(
        new Float32Array([1, 4, 2, 5, 3, 6]),
        2,
        3,
        undefined,
        "column-major",
        );

        mat.destroy();
        matCol.destroy();
        if (typeof process !== "undefined") cleanup();

        Explicit device (multi-GPU):

        import { init, cleanup, gpuName } from "wgblas";
        import { GpuMatrix } from "wgblas/classes/GpuMatrix";

        const dGpu = await init({ powerPreference: "high-performance" });
        const iGpu = await init({ powerPreference: "low-power" });
        console.log(gpuName(dGpu).description, "and", gpuName(iGpu).description);

        // A GpuMatrix is bound to whichever device created it — pass one explicitly
        // to keep each matrix resident on its own GPU.
        const dMat = GpuMatrix.from(dGpu, new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
        const iMat = GpuMatrix.from(iGpu, new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);

        const [a, b] = await Promise.all([dMat.read(), iMat.read()]);
        console.log("dGpu matrix:", a);
        console.log("iGpu matrix:", b);

        dMat.destroy();
        iMat.destroy();
        if (typeof process !== "undefined") cleanup(); // releases both

      Returns GpuMatrix

    • Parameters

      • device: GPUDevice

        GPUDevice from init() — the matrix is bound to this device for life

      • data:
            | Float32Array<ArrayBufferLike>
            | Complex32Array
            | Complex64Array
            | Float64Array<ArrayBufferLike>

        matrix data, in the order matching layout

      • rows: number

        number of rows

      • cols: number

        number of columns

      • Optionallda: number

        leading dimension (default: cols for row-major, rows for column-major)

      • Optionallayout: "column-major" | "row-major"

        storage layout (default: 'row-major')

      Returns GpuMatrix