wgblas
    Preparing search index...

    Function isamax

    • Returns the 0-based index of the element with the largest absolute value: $$\text{index} = \arg\max_{i} |x_i|$$ Ties are broken in favour of the lower index, matching CBLAS behaviour.

      NaN handling. The search compares with >, which is false for NaN, so NaN elements are skipped rather than selected: a vector of all NaN returns 0. This matches CBLAS except when x[0] itself is NaN — CBLAS seeds its running maximum from x[0], and since no comparison against NaN succeeds it returns 0 however large the later elements are, whereas this returns the index of the largest non-NaN element. +-Infinity compares normally and is selected as the maximum.

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

      const device = await init();

      const n = 5;
      const x = new Float32Array([3, -7, 2, 5, -1]);

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

      Browser (standalone HTML):

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

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

      const n = 5;
      const x = new Float32Array([3, -7, 2, 5, -1]);

      const { index } = await isamax(device, n, x, 1);

      document.getElementById("out").textContent =
      "x: " + Array.from(x).map(v => v.toFixed(4)).join(", ") +
      "\nindex: " + index +
      "\nx[index]: " + x[index].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<{ index: number } | { gpuTimeMs: number; index: number }>

      0-based index of max |x[i]|

    • Returns the 0-based index of the element with the largest absolute value: $$\text{index} = \arg\max_{i} |x_i|$$ Ties are broken in favour of the lower index, matching CBLAS behaviour.

      NaN handling. The search compares with >, which is false for NaN, so NaN elements are skipped rather than selected: a vector of all NaN returns 0. This matches CBLAS except when x[0] itself is NaN — CBLAS seeds its running maximum from x[0], and since no comparison against NaN succeeds it returns 0 however large the later elements are, whereas this returns the index of the largest non-NaN element. +-Infinity compares normally and is selected as the maximum.

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

      const device = await init();

      const n = 5;
      const scale = 2.0;
      const x = new Float32Array([3, -7, 2, 5, -1]);

      const xGpu = GpuVector.from(x);

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

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

      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<{ index: number } | { gpuTimeMs: number; index: number }>

      0-based index of max |x[i]|