wgblas
    Preparing search index...

    Function srot

    • Applies a Givens plane rotation to vectors x and y: x = cx + sy y = -sx + cy

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

      const device = await init();

      const n = 10;
      const x = randomFloat32Array(n, -10, 10);
      const y = randomFloat32Array(n, -10, 10);

      // 45-degree rotation: c = cos(π/4), s = sin(π/4)
      const angle = Math.PI / 4;
      const c = Math.cos(angle);
      const s = Math.sin(angle);

      console.log("x (before):", x);
      console.log("y (before):", y);

      const { x: xOut, y: yOut } = await srot(device, n, x, 1, y, 1, c, s);

      console.log("x (after): ", xOut);
      console.log("y (after): ", yOut);

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

      Browser (standalone HTML):

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

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

      const n = 10;
      const x = randomFloat32Array(n, -10, 10);
      const y = randomFloat32Array(n, -10, 10);

      // 45-degree rotation: c = cos(π/4), s = sin(π/4)
      const angle = Math.PI / 4;
      const c = Math.cos(angle);
      const s = Math.sin(angle);

      const xBefore = Array.from(x).map(v => v.toFixed(4)).join(", ");
      const yBefore = Array.from(y).map(v => v.toFixed(4)).join(", ");

      const { x: xOut, y: yOut } = await srot(device, n, x, 1, y, 1, c, s);

      document.getElementById("out").textContent =
      "x (before): " + xBefore +
      "\ny (before): " + yBefore +
      "\nx (after): " + Array.from(xOut).map(v => v.toFixed(4)).join(", ") +
      "\ny (after): " + Array.from(yOut).map(v => v.toFixed(4)).join(", ");

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • n: number

        number of elements (must be a positive integer)

      • x: Float32Array

        Float32Array input/output vector

      • incx: number

        stride for x (must be a positive integer)

      • y: Float32Array

        Float32Array input/output vector

      • incy: number

        stride for y (must be a positive integer)

      • c: number

        cosine of rotation angle

      • s: number

        sine of rotation angle

      Returns Promise<
          | { x: Float32Array; y: Float32Array }
          | { gpuTimeMs: number; x: Float32Array; y: Float32Array },
      >

    • Applies a Givens plane rotation to vectors x and y: x = cx + sy y = -sx + cy

      import { init, cleanup } from "wgblas";
      import { srot } from "wgblas/srot";
      import { sscal } from "wgblas/sscal";
      import { GpuVector } from "wgblas/classes/GpuVector";
      import { randomFloat32Array } from "wgblas/random";

      const device = await init();

      const n = 10;
      const xCpu = randomFloat32Array(n, -10, 10);
      const yCpu = randomFloat32Array(n, -10, 10);

      const xGpu = GpuVector.from(xCpu);
      const yGpu = GpuVector.from(yCpu);

      // 45-degree rotation
      const c = Math.cos(Math.PI / 4);
      const s = Math.sin(Math.PI / 4);

      console.log("x (cpu): ", xCpu);
      console.log("y (cpu): ", yCpu);

      // scale x by 2 on GPU, then rotate both vectors
      await sscal(device, n, 2.0, xGpu, 1);
      await srot(device, n, xGpu, 1, yGpu, 1, c, s);

      console.log("x (after): ", await xGpu.read());
      console.log("y (after): ", await yGpu.read());

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

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • n: number

        number of elements (must be a positive integer)

      • x: GpuVector

        GpuVector input/output vector (mutated in place)

      • incx: number

        stride for x (must be a positive integer)

      • y: GpuVector

        GpuVector input/output vector (mutated in place)

      • incy: number

        stride for y (must be a positive integer)

      • c: number

        cosine of rotation angle

      • s: number

        sine of rotation angle

      Returns Promise<{} | { gpuTimeMs: number }>