Skip to content

compute.kernels.scl

Summary

compute.kernels.scl multiplies a GPU ndarray by a single scalar and preserves its shape and dtype. Use it to apply a uniform gain or multiply complex elements by one complex coefficient. Supply a shape-matched contiguous out to reuse storage.

Syntax

scl(x: GPUndarray, alpha: KernelScalar, opts?: ElementwiseOptions): GPUndarray
const scaled = kernels.scl(x, 0.5);

Parameters

Name Type Required Description
x GPUndarray Yes Live f32, u32 or c64 array in any supported layout.
alpha KernelScalar Yes Number for real types, in the u32 integer range for unsigned input. For c64, a number supplies only the real part and a [re, im] pair supplies both parts.
opts ElementwiseOptions No Kernel output and dispatch settings.

Returns

GPUndarray - Array with the same shape and dtype. Unsigned multiplication wraps modulo 2 ** 32. Set out: x to scale a C-contiguous array in place.

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" });
kernels.scl(x, 0.5, { out: x });
const cpu = await nd.toCPU(x);
console.log(cpu.data()); // [2, 4]
cpu.destroy();
x.destroy();

See Also