GPUDevice from init()
number of elements (must be a positive integer)
Float64Array input vector
stride for x (must be a positive integer)
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 { idamax } from "wgblas/idamax";
import { GpuVector } from "wgblas/classes/GpuVector";
const device = await init();
const n = 5;
const x = new Float64Array([3, -7, 2, 5, -1]);
const xGpu = GpuVector.from(x);
console.log("x: ", x);
const { index } = await idamax(device, n, xGpu, 1);
console.log("index: ", index);
xGpu.destroy();
if (typeof process !== "undefined") cleanup();
GPUDevice from init()
number of elements (must be a positive integer)
Float64Array-backed GpuVector input vector
stride for x (must be a positive integer)
0-based index of max |x[i]|
Returns the 0-based index of the element with the largest absolute value, for a vector of doubles: $$\text{index} = \arg\max_{i} |x_i|$$ Each element of
xis split into a (hi, lo) double-double f32 pair (seesplitDoubleDouble/f64.mjs) since WGSL has no f64 type; comparisons use the double-double pair directly (hi, falling back to lo on an exact tie), giving ~48 bits of discriminating precision — more than a single f32 (24 bits) but less than true f64 (52 bits). 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 returns0. This matches CBLAS except whenx[0]itself is NaN — CBLAS seeds its running maximum fromx[0], and since no comparison against NaN succeeds it returns0however large the later elements are, whereas this returns the index of the largest non-NaN element.+-Infinitycompares normally and is selected as the maximum.Browser (standalone HTML):