wgblas
    Preparing search index...

    Function sger

    • Performs the rank-1 update A = alpha * x * y^T + A

      A is an m×n matrix stored in row-major order, updated in place. lda is the leading dimension (number of floats between the start of consecutive rows — must be >= n).

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

      // Reshapes a flat row-major array into rows for console.table's 2D grid view.
      function toMatrix(A, rows, cols, lda = cols) {
      const out = [];
      for (let r = 0; r < rows; r++)
      out.push(Array.from(A.subarray(r * lda, r * lda + cols), (v) => +v.toFixed(4)));
      return out;
      }

      const device = await init();

      // 4×5 rank-1 update; lda = n
      const m = 4, n = 5, lda = n;
      const alpha = 1.0;
      const x = randomFloat32Array(m, -10, 10);
      const y = randomFloat32Array(n, -10, 10);
      const A = randomFloat32Array(m * lda, -10, 10);

      console.log("x:", x);
      console.log("y:", y);
      console.log("A (before):");
      console.table(toMatrix(A, m, n, lda));
      const { A: result } = await sger(device, m, n, alpha, x, 1, y, 1, A, lda, "row-major");
      console.log("A (after):");
      console.table(toMatrix(result, m, n, lda));
      if (typeof process !== "undefined") cleanup();

      Browser (standalone HTML):

      <!doctype html>
      <html lang="en">
      <head>
      <meta charset="UTF-8" />
      <title>sger — 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, sger, randomFloat32Array, cleanup } = window.wgblas;

      // Reshapes a flat row-major array into a newline-per-row grid for display.
      function matrixText(A, rows, cols, lda) {
      const lines = [];
      for (let r = 0; r < rows; r++) {
      const row = [];
      for (let c = 0; c < cols; c++) row.push(A[r * lda + c].toFixed(4));
      lines.push(row.join(", "));
      }
      return lines.join("\n");
      }

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

      const m = 4, n = 5, lda = n;
      const alpha = 1.0;
      const x = randomFloat32Array(m, -10, 10);
      const y = randomFloat32Array(n, -10, 10);
      const A = randomFloat32Array(m * lda, -10, 10);

      const { A: result } = await sger(device, m, n, alpha, x, 1, y, 1, A, lda, "row-major");

      document.getElementById("out").textContent =
      "x: " + Array.from(x).map(v => v.toFixed(4)).join(", ") +
      "\ny: " + Array.from(y).map(v => v.toFixed(4)).join(", ") +
      "\nA (before):\n" + matrixText(A, m, n, lda) +
      "\nA (after):\n" + matrixText(result, m, n, lda);

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • m: number

        number of rows in A (length of x)

      • n: number

        number of columns in A (length of y)

      • alpha: number

        scalar multiplier for x*y^T

      • x: Float32Array

        Float32Array input vector, length at least (m-1)*incx+1

      • incx: number

        stride for x (must be a positive integer)

      • y: Float32Array

        Float32Array input vector, length at least (n-1)*incy+1

      • incy: number

        stride for y (must be a positive integer)

      • A: Float32Array

        Float32Array, row-major or column-major (see layout), at least (m-1)*lda+n elements for row-major or (n-1)*lda+m elements for column-major

      • lda: number

        leading dimension of A (>= n for row-major, >= m for column-major)

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

        storage layout of A (default: 'row-major')

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

    • Performs the rank-1 update A = alpha * x * y^T + A

      A is kept GPU-resident; x and y are CPU Float32Arrays. A's own layout (set at GpuMatrix.from time) determines the operation — there is no separate layout argument here.

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • m: number

        number of rows in A

      • n: number

        number of columns in A

      • alpha: number

        scalar multiplier for x*y^T

      • x: Float32Array

        Float32Array input vector

      • incx: number

        stride for x (must be a positive integer)

      • y: Float32Array

        Float32Array input vector

      • incy: number

        stride for y (must be a positive integer)

      • A: GpuMatrix

        GpuMatrix, GPU-resident

      • lda: number

        leading dimension of A (must equal A.lda)

      Returns Promise<{ gpuTimeMs?: number }>

    • Performs the rank-1 update A = alpha * x * y^T + A

      x, y, and A are all kept resident on the GPU. A's own layout (set at GpuMatrix.from time) determines the operation — there is no separate layout argument here.

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

      // Reshapes a flat row-major array into rows for console.table's 2D grid view.
      function toMatrix(A, rows, cols, lda = cols) {
      const out = [];
      for (let r = 0; r < rows; r++)
      out.push(Array.from(A.subarray(r * lda, r * lda + cols), (v) => +v.toFixed(4)));
      return out;
      }

      const device = await init();

      const m = 4, n = 5;
      const x = randomFloat32Array(m, -10, 10);
      const y = randomFloat32Array(n, -10, 10);
      const A = randomFloat32Array(m * n, -10, 10);

      const xGpu = GpuVector.from(x);
      const yGpu = GpuVector.from(y);
      const AGpu = GpuMatrix.from(A, m, n, n, "row-major");

      console.log("x:", x);
      console.log("y:", y);
      console.log("A (before):");
      console.table(toMatrix(A, m, n));

      // results stay on the GPU between steps
      await sger(device, m, n, 1.0, xGpu, 1, yGpu, 1, AGpu, AGpu.lda); // A += x*y^T
      await sger(device, m, n, 1.0, xGpu, 1, yGpu, 1, AGpu, AGpu.lda); // A += x*y^T again

      // single readback (GpuMatrix.read() is already dense — no lda padding to strip)
      const result = await AGpu.read();
      console.log("A (after two rank-1 updates):");
      console.table(toMatrix(result, m, n));

      xGpu.destroy();
      yGpu.destroy();
      AGpu.destroy();

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • m: number

        number of rows in A

      • n: number

        number of columns in A

      • alpha: number

        scalar multiplier for x*y^T

      • x: GpuVector

        GpuVector input vector (not mutated)

      • incx: number

        stride for x (must be a positive integer)

      • y: GpuVector

        GpuVector input vector (not mutated)

      • incy: number

        stride for y (must be a positive integer)

      • A: GpuMatrix

        GpuMatrix, mutated in place

      • lda: number

        leading dimension of A (must equal A.lda)

      Returns Promise<{ gpuTimeMs?: number }>