compute.kernels.histogram¶
Summary¶
compute.kernels.histogram counts u32 keys into bins indexed by key value. Use it to tally integer labels. With an existing output and clear: false, successive calls accumulate counts across batches.
Syntax¶
histogram(keys: GPUndarray, opts: HistogramOptions): GPUndarray
const counts = kernels.histogram(keys, { bins: 3 });
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
keys |
GPUndarray |
Yes | Rank-one u32 input. Keys at or above bins are skipped. |
opts |
HistogramOptions |
Yes | Bin count, clearing policy and kernel output and dispatch settings. |
Returns¶
GPUndarray - u32 array of shape [bins]. Bin counts wrap modulo 2 ** 32.
Type Details¶
bins is a non-negative integer fitting u32. Zero bins yield an empty result. A fresh result starts at zero. With an existing out, clear defaults to true and clears it even if the key vector is empty. Use clear: false to accumulate into the current bin counts. Storage-only outputs can be cleared without COPY_DST.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const keys = nd.fromArray("u32", [4], [1, 0, 1, 3], { residency: "gpu" });
const counts = kernels.histogram(keys, { bins: 3 });
const cpu = await nd.toCPU(counts);
console.log(cpu.data()); // [1, 2, 0]
cpu.destroy();
counts.destroy();
keys.destroy();