Skip to content

compute.kernels.scaleHistogram

Summary

compute.kernels.scaleHistogram counts finite GPU scalars in equal-width bins over a numeric range. Out-of-range values contribute to the nearest edge bin. Reuse an output with clear: false to accumulate chunks measured against the same range.

Syntax

scaleHistogram(values: GPUndarray, opts: ScaleHistogramOptions): GPUndarray
const bins = kernels.scaleHistogram(values, { bins: 16, minValue: 0, maxValue: 1 });

Parameters

Name Type Required Description
values GPUndarray Yes Rank-one f32 values. NaN and infinities are skipped.
opts ScaleHistogramOptions Yes Bin count and range, clearing policy and kernel output and dispatch settings.

Returns

GPUndarray - u32 vector of length bins. Counts wrap modulo 2 ** 32.

Type Details

type ScaleHistogramOptions = KernelOutputOptions & {
    bins: number;
    minValue: number;
    maxValue: number;
    clear?: boolean;
};

bins is a non-negative integer fitting u32. Zero bins yield an empty result. Accumulation requires finite bounds with maxValue > minValue. Invalid ranges and empty inputs add no counts. clear defaults to true, including when no values are added. With clear: false, a supplied output keeps its counts and a fresh output starts at zero.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const values = nd.fromArray("f32", [4], [-1, 0.25, 0.75, 2], { residency: "gpu" });
const counts = kernels.scaleHistogram(values, { bins: 2, minValue: 0, maxValue: 1 });
const cpu = await nd.toCPU(counts);
console.log(cpu.data()); // [2, 2]
for (const array of [cpu, counts, values]) array.destroy();

See Also