wgblas
    Preparing search index...

    Function sgemm

    • Performs the matrix-matrix operation $$C \leftarrow \alpha \mathrm{op}(A) \mathrm{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";

      const device = await init();

      // C = alpha*A*B + beta*C, with A 2x3 and B 3x2, so C is 2x2.
      const m = 2,
      n = 2,
      k = 3;
      const A = new Float32Array([1, 2, 3, 4, 5, 6]);
      const B = new Float32Array([1, 0, 0, 1, 1, 1]);
      const C = new Float32Array(m * n);

      console.log("A =");
      console.table([A.slice(0, 3), A.slice(3, 6)]);
      console.log("B =");
      console.table([B.slice(0, 2), B.slice(2, 4), B.slice(4, 6)]);

      const { C: result } = await sgemm(
      device,
      "no-transpose",
      "no-transpose",
      m,
      n,
      k,
      1,
      A,
      3,
      B,
      2,
      0,
      C,
      2,
      );
      console.log("C = A*B =");
      console.table([result.slice(0, 2), result.slice(2, 4)]);
      // B's columns pick out [a0+a2, a1+a2]: [[1+3, 2+3], [4+6, 5+6]] = [[4,5],[10,11]]

      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, cleanup } = window.wgblas;

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

      // C = A*B with A 2x3 and B 3x2, so C is 2x2.
      const m = 2, n = 2, k = 3;
      const A = new Float32Array([1, 2, 3,
      4, 5, 6]);
      const B = new Float32Array([1, 0,
      0, 1,
      1, 1]);
      const C = new Float32Array(m * n);

      const { C: result } = await sgemm(device, "no-transpose", "no-transpose", m, n, k, 1, A, 3, B, 2, 0, C, 2);

      document.getElementById("out").textContent = [
      "A =",
      " [" + A.slice(0, 3).join(", ") + "]",
      " [" + A.slice(3, 6).join(", ") + "]",
      "B =",
      " [" + B.slice(0, 2).join(", ") + "]",
      " [" + B.slice(2, 4).join(", ") + "]",
      " [" + B.slice(4, 6).join(", ") + "]",
      "C = A*B =",
      " [" + result.slice(0, 2).join(", ") + "]",
      " [" + result.slice(2, 4).join(", ") + "]",
      ].join("\n");

      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 \leftarrow \alpha \mathrm{op}(A) \mathrm{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";

      const device = await init();

      // C = A*B with A 2x3 and B 3x2, so C is 2x2.
      const m = 2,
      n = 2,
      k = 3;
      const A = new Float32Array([1, 2, 3, 4, 5, 6]);
      const B = new Float32Array([1, 0, 0, 1, 1, 1]);

      const AGpu = GpuMatrix.from(A, m, k, k, "row-major");
      const BGpu = GpuMatrix.from(B, k, n, n, "row-major");
      const CGpu = GpuMatrix.from(new Float32Array(m * n), m, n, n, "row-major");

      console.log("A =");
      console.table([A.slice(0, 3), A.slice(3, 6)]);

      await sgemm(
      device,
      "no-transpose",
      "no-transpose",
      m,
      n,
      k,
      1,
      AGpu,
      AGpu.lda,
      BGpu,
      BGpu.lda,
      0,
      CGpu,
      CGpu.lda,
      );

      const result = await CGpu.read();
      console.log("C = A*B =");
      console.table([result.slice(0, 2), result.slice(2, 4)]); // [[4,5],[10,11]]

      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)