wgblas
    Preparing search index...

    Function dscal

    • Scales a double-precision vector by a constant: $$x \leftarrow \alpha x$$

      x and alpha are each split into a (hi, lo) double-double f32 pair (see splitDoubleDouble/f64.mjs) since WGSL has no f64 type; the multiply uses Dekker's double-double algorithm (see shaders/f64/), giving ~48 bits of mantissa — more than a single f32 (24 bits) but less than true f64 (52 bits), so results are not bit-exact with a CPU double.

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

      const device = await init();

      const n = 5;
      const alpha = 3;
      const x = new Float64Array([1, 2, 3, 4, 5]);

      console.log("before:", x);
      const { x: result } = await dscal(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>dscal — 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, dscal, cleanup } = window.wgblas;

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

      const n = 5;
      const alpha = 3;
      const x = new Float64Array([1, 2, 3, 4, 5]);

      console.log("before:", x);
      const { x: result } = await dscal(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: Float64Array

        Float64Array input/output vector

      • incx: number

        stride for x (must be a positive integer)

      Returns Promise<{ x: Float64Array } | { gpuTimeMs: number; x: Float64Array }>

    • Scales a double-precision vector by a constant: $$x \leftarrow \alpha x$$

      import { init, cleanup } from "wgblas";
      import { dscal } from "wgblas/dscal";
      import { GpuVector } from "wgblas/classes/GpuVector";

      const device = await init();

      const n = 5;
      const alpha = 3;
      const x = new Float64Array([1, 2, 3, 4, 5]);

      const xGpu = GpuVector.from(x);

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

      await dscal(device, n, alpha, xGpu, 1);

      const result = await xGpu.read();
      console.log("result:", result);

      xGpu.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

        Float64Array-backed GpuVector input/output vector (mutated in place)

      • incx: number

        stride for x (must be a positive integer)

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