wgblas
    Preparing search index...

    Function snrm2

    • Computes the Euclidean norm of a vector: $$\text{result} = \sqrt{\sum_{i} x_i^2}$$

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

      const device = await init();

      // 2^2 + 3^2 + 6^2 = 49 = 7^2, so the norm comes out to a clean 7.
      const n = 3;
      const x = new Float32Array([2, 3, 6]);

      console.log("x: ", x);
      const { nrm2 } = await snrm2(device, n, x, 1);
      console.log("nrm2: ", nrm2);
      if (typeof process !== "undefined") cleanup();

      Browser (standalone HTML):

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

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

      // 2^2 + 3^2 + 6^2 = 49 = 7^2, so the norm comes out to a clean 7.
      const n = 3;
      const x = new Float32Array([2, 3, 6]);

      const { nrm2 } = await snrm2(device, n, x, 1);

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

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

      Parameters

      • device: GPUDevice

        GPUDevice from init()

      • n: number

        number of elements (must be a positive integer)

      • x: Float32Array

        Float32Array input vector

      • incx: number

        stride for x (must be a positive integer)

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

      Euclidean norm scalar — always a CPU readback, even for GpuVector inputs

    • Computes the Euclidean norm of a vector: $$\text{result} = \sqrt{\sum_{i} x_i^2}$$

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

      const device = await init();

      // 2^2 + 3^2 + 6^2 = 49 = 7^2, so the norm comes out to a clean 7 (14 once scaled).
      const n = 3;
      const scale = 2.0;
      const x = new Float32Array([2, 3, 6]);

      const xGpu = GpuVector.from(x);

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

      await sscal(device, n, scale, xGpu, 1);
      const { nrm2 } = await snrm2(device, n, xGpu, 1);
      console.log("nrm2 (of 2x): ", nrm2);

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

      • incx: number

        stride for x (must be a positive integer)

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

      Euclidean norm scalar — always a CPU readback, even for GpuVector inputs