Skip to content

compute.kernels.axpy

Summary

compute.kernels.axpy computes alpha * x + y at the broadcast shape. Use it to apply a scaled correction in one call. When y already has the result shape and is C-contiguous, pass { out: y } to update it in place.

Syntax

axpy(x: GPUndarray, y: GPUndarray, alpha: KernelScalar, opts?: ElementwiseOptions): GPUndarray
const updated = kernels.axpy(x, y, 0.25, { out: y });

Parameters

Name Type Required Description
x, y GPUndarray Yes Packed or strided inputs with broadcastable shapes and the same f32, u32 or c64 dtype.
alpha KernelScalar Yes Valid scalar for the input dtype, with the same rules as scl.
opts ElementwiseOptions No Kernel output and dispatch settings.

Returns

GPUndarray - Result with the broadcast shape and input dtype. Unsigned arithmetic wraps modulo 2 ** 32. To update y, pass out: y if it is C-contiguous and already has the full broadcast shape.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const x = nd.fromArray("f32", [2], [4, 8], { residency: "gpu" });
const y = nd.fromArray("f32", [2], [10, 20], { residency: "gpu" });
kernels.axpy(x, y, 0.25, { out: y });
const cpu = await nd.toCPU(y);
console.log(cpu.data()); // [11, 22]
cpu.destroy();
y.destroy();
x.destroy();

See Also