wgblas
    Preparing search index...

    Function sgemm

    • Performs the matrix-matrix operation C = alpha * op(A) * op(B) + beta * C

      • transA/transB='no-transpose': op(A) = A (m×k), op(B) = B (k×n)
      • transA/transB='transpose': op(A) = A^T, op(B) = B^T

      A, B, C are row-major or column-major (see layout) — backed by one of two shared-memory-tiled, register-blocked kernels chosen by shape: sgemm_small.wgsl (BM=BN=32) below a 6x6 workgroup grid, sgemm_large.wgsl (BM=BN=64) above it.

      import { init, cleanup } from "wgblas";
      import { sgemm } from "wgblas/sgemm";
      import { randomFloat32Array } from "wgblas/random";

      const device = await init();

      // 4×4 row-major matrices: A (m×k), B (k×n), C (m×n)
      const m = 4, n = 4, k = 4;
      const lda = k, ldb = n, ldc = n;
      const alpha = 1.0, beta = 0.0;
      const A = randomFloat32Array(m * lda, -10, 10);
      const B = randomFloat32Array(k * ldb, -10, 10);
      const C = randomFloat32Array(m * ldc, -10, 10);

      console.log("A:", A);
      console.log("B:", B);
      console.log("C (input):", C);
      const { C: result } = await sgemm(device, "no-transpose", "no-transpose", m, n, k, alpha, A, lda, B, ldb, beta, C, ldc, "row-major");
      console.log("C (result):", result);
      if (typeof process !== "undefined") cleanup();

      Browser (standalone HTML):

      <!doctype html>
      <html lang="en">
      <head>
      <meta charset="UTF-8" />
      <title>sgemm — wgblas browser example</title>
      <script src="https://unpkg.com/wgblas/dist/wgblas.browser.js"></script>
      </head>
      <body>
      <pre id="out">Running…</pre>
      <script>
      const { init, sgemm, randomFloat32Array, cleanup } = window.wgblas;

      (async () => {
      const device = await init();

      const m = 4, n = 4, k = 4;
      const lda = k, ldb = n, ldc = n;
      const alpha = 1.0, beta = 0.0;
      const A = randomFloat32Array(m * lda, -10, 10);
      const B = randomFloat32Array(k * ldb, -10, 10);
      const C = randomFloat32Array(m * ldc, -10, 10);

      const { C: result } = await sgemm(device, "no-transpose", "no-transpose", m, n, k, alpha, A, lda, B, ldb, beta, C, ldc, "row-major");

      document.getElementById("out").textContent =
      "A: " + Array.from(A).map(v => v.toFixed(4)).join(", ") +
      "\nB: " + Array.from(B).map(v => v.toFixed(4)).join(", ") +
      "\nC: " + Array.from(result).map(v => v.toFixed(4)).join(", ");

      cleanup();
      })();
      </script>
      </body>
      </html>

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • transA: "no-transpose" | "transpose"

        'no-transpose' for A, 'transpose' for A^T

      • transB: "no-transpose" | "transpose"

        'no-transpose' for B, 'transpose' for B^T

      • m: number

        rows of op(A) and C

      • n: number

        columns of op(B) and C

      • k: number

        columns of op(A), rows of op(B)

      • alpha: number

        scalar multiplier for op(A)*op(B)

      • A: Float32Array

        Float32Array, row-major or column-major (see layout)

      • lda: number

        leading dimension of A as stored

      • B: Float32Array

        Float32Array, row-major or column-major (see layout)

      • ldb: number

        leading dimension of B as stored

      • beta: number

        scalar multiplier for C

      • C: Float32Array

        Float32Array input/output matrix, row-major or column-major

      • ldc: number

        leading dimension of C as stored

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

        storage layout shared by A/B/C when they're Float32Array (default: 'row-major'); column-major A/B flips the respective trans flag internally, column-major C computes C^T = op(B)^T*op(A)^T instead (same underlying bytes) — op(A)*op(B) stays what you asked for either way

      Returns Promise<{ C: Float32Array; gpuTimeMs?: number }>

      updated C as a Float32Array

    • Performs the matrix-matrix operation C = alpha * op(A) * op(B) + beta * C

      A, B, and C are all kept GPU-resident. Each matrix's own layout (set at GpuMatrix.from time) determines the operation — there is no separate layout argument here. A and B must be GpuMatrix whenever C is, and vice versa — mixing a GpuMatrix with a plain Float32Array is not supported.

      import { init, cleanup } from "wgblas";
      import { sgemm } from "wgblas/sgemm";
      import { GpuMatrix } from "wgblas/classes/GpuMatrix";
      import { randomFloat32Array } from "wgblas/random";

      const device = await init();

      const n = 4;
      const A = randomFloat32Array(n * n, -10, 10);
      const B = randomFloat32Array(n * n, -10, 10);
      const C = new Float32Array(n * n);

      const AGpu = GpuMatrix.from(A, n, n, n, "row-major");
      const BGpu = GpuMatrix.from(B, n, n, n, "row-major");
      const CGpu = GpuMatrix.from(C, n, n, n, "row-major");

      console.log("A:", A);
      console.log("B:", B);

      // results stay on the GPU between steps
      await sgemm(device, "no-transpose", "no-transpose", n, n, n, 1.0, AGpu, AGpu.lda, BGpu, BGpu.lda, 0.0, CGpu, CGpu.lda); // C = A*B
      await sgemm(device, "no-transpose", "no-transpose", n, n, n, 1.0, AGpu, AGpu.lda, CGpu, CGpu.lda, 0.0, BGpu, BGpu.lda); // B = A*C = A²*B

      // single readback
      const result = await BGpu.read();
      console.log("A²B:", result);

      AGpu.destroy();
      BGpu.destroy();
      CGpu.destroy();

      if (typeof process !== "undefined") cleanup();

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • transA: "no-transpose" | "transpose"

        'no-transpose' for A, 'transpose' for A^T

      • transB: "no-transpose" | "transpose"

        'no-transpose' for B, 'transpose' for B^T

      • m: number

        rows of op(A) and C

      • n: number

        columns of op(B) and C

      • k: number

        columns of op(A), rows of op(B)

      • alpha: number

        scalar multiplier for op(A)*op(B)

      • A: GpuMatrix

        GpuMatrix

      • lda: number

        leading dimension of A (must equal A.lda)

      • B: GpuMatrix

        GpuMatrix

      • ldb: number

        leading dimension of B (must equal B.lda)

      • beta: number

        scalar multiplier for C

      • C: GpuMatrix

        GpuMatrix (mutated in place)

      • ldc: number

        leading dimension of C (must equal C.lda)

      Returns Promise<{ gpuTimeMs?: number }>

      no C — it stays GPU-resident; call C.read() yourself for a CPU readback (see the example)