Skip to content

compute.kernels.dot

Summary

compute.kernels.dot reduces two equal-length GPU vectors to a scalar sum of paired products. Complex products use the components as supplied. For a Hermitian inner product, pass an already conjugated first vector.

Syntax

dot(a: GPUndarray, b: GPUndarray, opts?: ElementwiseOptions): GPUndarray
const scalar = kernels.dot(a, b);

Parameters

Name Type Required Description
a, b GPUndarray Yes Rank-one arrays of equal length and the same f32, u32 or c64 dtype. Strided vectors are accepted.
opts ElementwiseOptions No Kernel output and dispatch settings.

Returns

GPUndarray - Scalar of shape [] and the input dtype. Empty vectors produce zero, and unsigned arithmetic wraps modulo 2 ** 32.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const a = nd.fromArray("f32", [2], [3, 4], { residency: "gpu" });
const scalar = kernels.dot(a, a);
const cpu = await nd.toCPU(scalar);
console.log(cpu.get()); // 25
cpu.destroy();
scalar.destroy();
a.destroy();

See Also