wgblas
    Preparing search index...

    Function ddot

    • Computes the dot product of two vectors of doubles in extended precision: $$\text{result} = \sum_{i} x_i y_i$$ Each element of x and y is split into a (hi, lo) double-double f32 pair (see splitDoubleDouble/f64.mjs) since WGSL has no f64 type; the elementwise products and their accumulation both use 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 { ddot } from "wgblas/ddot";

      const device = await init();

      const n = 5;
      // 1e-9 is far below what f32 can hold beside a value of 10 — single precision
      // drops it entirely and returns 10. The extended-precision path keeps it.
      const x = new Float64Array([1, 1e-9, 2, 3, 4]);
      const y = new Float64Array([1, 1, 1, 1, 1]);

      console.log("x: ", x);
      console.log("y: ", y);
      const { dot } = await ddot(device, n, x, 1, y, 1);
      console.log("dot: ", dot); // 10.000000001, not 10
      if (typeof process !== "undefined") cleanup();

      Browser (standalone HTML):

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

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

      const n = 5;
      // 1e-9 is far below what f32 can hold beside a value of 10 — single
      // precision drops it and returns 10. ddot keeps it.
      const x = new Float64Array([1, 1e-9, 2, 3, 4]);
      const y = new Float64Array([1, 1, 1, 1, 1]);

      const { dot } = await ddot(device, n, x, 1, y, 1);

      document.getElementById("out").textContent =
      "x: " + Array.from(x).join(", ") +
      "\ny: " + Array.from(y).join(", ") +
      "\ndot: " + dot;

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • n: number

        number of elements (must be a positive integer)

      • x: Float64Array

        Float64Array input vector

      • incx: number

        stride for x (must be a positive integer)

      • y: Float64Array

        Float64Array input vector

      • incy: number

        stride for y (must be a positive integer)

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

      dot product scalar — always a CPU readback, even for GpuVector inputs

    • Computes the dot product of two vectors of doubles in extended precision: $$\text{result} = \sum_{i} x_i y_i$$ Accumulation uses Dekker's double-double algorithm (see shaders/f64/), giving ~48 bits of mantissa.

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

      const device = await init();

      const n = 5;
      const x = new Float64Array([1, 1e-9, 2, 3, 4]);
      const y = new Float64Array([1, 1, 1, 1, 1]);

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

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

      const { dot } = await ddot(device, n, xGpu, 1, yGpu, 1);
      console.log("dot: ", dot); // 10.000000001, not 10

      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

        Float64Array-backed GpuVector input vector

      • incx: number

        stride for x (must be a positive integer)

      • y: GpuVector

        Float64Array-backed GpuVector input vector

      • incy: number

        stride for y (must be a positive integer)

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

      dot product scalar — always a CPU readback, even for GpuVector inputs