wgblas
    Preparing search index...

    Function ssyr2

    • Performs the symmetric rank-2 update $$A \leftarrow \alpha x y^{T} + \alpha y x^{T} + A$$

      A is an n×n symmetric matrix stored in row-major order, updated in place. Only the triangle specified by uplo is referenced and updated; the other triangle is left untouched (implied by symmetry).

      import { init, cleanup } from "wgblas";
      import { ssyr2 } from "wgblas/ssyr2";

      const device = await init();

      // Symmetric rank-2 update A = alpha*x*y^T + alpha*y*x^T + A. With y all ones,
      // entry (i,j) is simply x[i] + x[j]. Only the upper triangle is written.
      const n = 3,
      lda = n;
      const x = new Float32Array([1, 2, 3]);
      const y = new Float32Array([1, 1, 1]);
      const A = new Float32Array(n * lda); // all zeros

      console.log("x =", x);
      console.log("y =", y);

      const { A: result } = await ssyr2(device, "upper", n, 1, x, 1, y, 1, A, lda);
      console.log("A = x*y^T + y*x^T (upper triangle) =");
      console.table([result.slice(0, 3), result.slice(3, 6), result.slice(6, 9)]); // [[2,3,4],[0,4,5],[0,0,6]]

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

      Browser (standalone HTML):

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

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

      // With y all ones, entry (i,j) of x*y^T + y*x^T is simply x[i] + x[j].
      const n = 3, lda = n;
      const x = new Float32Array([1, 2, 3]);
      const y = new Float32Array([1, 1, 1]);
      const A = new Float32Array(n * lda);

      const { A: result } = await ssyr2(device, "upper", n, 1, x, 1, y, 1, A, lda);

      document.getElementById("out").textContent = [
      "x = [" + [...x].join(", ") + "]",
      "y = [" + [...y].join(", ") + "]",
      "A = x*y^T + y*x^T (upper triangle) =",
      " [" + [...result.subarray(0, 3)].join(", ") + "]",
      " [" + [...result.subarray(3, 6)].join(", ") + "]",
      " [" + [...result.subarray(6, 9)].join(", ") + "]",
      ].join("\n");

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • uplo: "lower" | "upper"

        'lower' to use the lower triangle, 'upper' to use the upper triangle

      • n: number

        order of the matrix A (number of rows and columns)

      • alpha: number

        scalar multiplier for xy^T + yx^T

      • x: Float32Array

        Float32Array input vector, length at least (n-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 (n-1)*lda+n elements

      • lda: number

        leading dimension of A (>= n either way — A is square)

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

        storage layout of A (default: 'row-major'); for a symmetric matrix, column-major storage just means the other triangle is the one physically referenced for a given uplo

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

    • Performs the symmetric rank-2 update $$A \leftarrow \alpha x y^{T} + \alpha y x^{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 { ssyr2 } from "wgblas/ssyr2";
      import { GpuVector } from "wgblas/classes/GpuVector";
      import { GpuMatrix } from "wgblas/classes/GpuMatrix";

      const device = await init();

      // With y all ones, entry (i,j) of x*y^T + y*x^T is simply x[i] + x[j].
      const n = 3;
      const x = new Float32Array([1, 2, 3]);
      const y = new Float32Array([1, 1, 1]);

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

      console.log("x =", x);
      console.log("y =", y);

      await ssyr2(device, "upper", n, 1, xGpu, 1, yGpu, 1, AGpu, AGpu.lda);
      const result = await AGpu.read();
      console.log("A = x*y^T + y*x^T (upper triangle) =");
      console.table([result.slice(0, 3), result.slice(3, 6), result.slice(6, 9)]); // [[2,3,4],[0,4,5],[0,0,6]]

      xGpu.destroy();
      yGpu.destroy();
      AGpu.destroy();
      if (typeof process !== "undefined") cleanup();

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • uplo: "lower" | "upper"

        'lower' to use the lower triangle, 'upper' to use the upper triangle

      • n: number

        order of the matrix A

      • alpha: number

        scalar multiplier for xy^T + yx^T

      • x: GpuVector

        GpuVector input vector (not mutated), Float32-backed

      • incx: number

        stride for x (must be a positive integer)

      • y: GpuVector

        GpuVector input vector (not mutated), Float32-backed

      • incy: number

        stride for y (must be a positive integer)

      • A: GpuMatrix

        GpuMatrix, row-major, mutated in place, Float32-backed

      • lda: number

        leading dimension of A (must equal A.lda)

      Returns Promise<{ gpuTimeMs?: number }>