wgblas
    Preparing search index...

    Function sscal

    • Scales a single-precision vector by a constant: x = alpha * x

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

      const device = await init();

      const n = 10;
      const alpha = 2.0;
      const x = randomFloat32Array(n, -10, 10);

      console.log("before:", x);
      const result = await sscal(device, n, alpha, x, 1);
      console.log("after: ", result);
      if (typeof process !== "undefined") cleanup();

      Browser (standalone HTML):

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

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

      const n = 10;
      const alpha = 2.0;
      const x = randomFloat32Array(n, -10, 10);

      console.log("before:", x);
      const result = await sscal(device, n, alpha, x, 1);
      console.log("after: ", result);

      document.getElementById("out").textContent =
      "before: " + Array.from(x).map(v => v.toFixed(4)).join(", ") +
      "\nafter: " + Array.from(result).map(v => v.toFixed(4)).join(", ");

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • n: number

        number of elements to scale (must be a positive integer)

      • alpha: number

        scalar multiplier

      • x: Float32Array

        Float32Array input/output vector

      • incx: number

        stride for x (must be a positive integer)

      Returns Promise<
          | Float32Array<ArrayBufferLike>
          | { gpuTimeMs: number; result: Float32Array },
      >

    • Scales a single-precision vector by a constant: x = alpha * x

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

      const device = await init();

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

      const xGpu = GpuVector.from(x);
      const yGpu = GpuVector.from(y);

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

      await sscal(device, n, alpha, xGpu, 1);
      await saxpy(device, n, 1.0, xGpu, 1, yGpu, 1);

      const result = await yGpu.read();
      console.log("result (2x + y):", result);

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • n: number

        number of elements to scale (must be a positive integer)

      • alpha: number

        scalar multiplier

      • x: GpuVector

        GpuVector input/output vector (mutated in place)

      • incx: number

        stride for x (must be a positive integer)

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