Readonly Internal_Readonly Internal_Low (f32) halves of a Float64Array-backed matrix's double-double values; null otherwise.
ReadonlycolsNumber of columns (logical shape, independent of layout).
ReadonlydeviceThe GPUDevice this matrix's buffers live on; routines refuse operands from another device.
ReadonlydtypeTyped array (or complex array) constructor used when reading data back from the GPU.
ReadonlylayoutStorage layout this matrix was created with — every routine that accepts a GpuMatrix reads this automatically.
ReadonlyldaLeading dimension — stride between row starts (row-major) or column starts (column-major).
ReadonlyrowsNumber of rows (logical shape, independent of layout).
Static Readonly InternaltypeThe class's name for error messages, which survives bundling and minification (.name doesn't).
Destroys the underlying GPU buffer. Call when the matrix is no longer needed to free GPU memory.
import { init, cleanup, GpuMatrix } from "wgblas";
await init();
const mat = GpuMatrix.from(new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
mat.destroy();
console.log("GPU buffer released");
if (typeof process !== "undefined") cleanup();
Downloads the matrix from GPU memory and returns a dense array of shape
rows × cols, in the same layout and type it was created with. If lda
exceeds the dense minimum, the leading-dimension padding is stripped so
the returned array is always tightly packed.
import { init, cleanup, GpuMatrix } from "wgblas";
await init();
const mat = GpuMatrix.from(new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
const data = await mat.read();
console.log(data); // Float32Array [1, 2, 3, 4, 5, 6]
mat.destroy();
if (typeof process !== "undefined") cleanup();
StaticfromUploads a Float32Array, Float64Array, Complex32Array, or Complex64Array
matrix to GPU memory, row-major or column-major. A Float64Array is split
into a double-double (hi, lo) f32 pair per element (WGSL has no f64
type) and stored across two GPU buffers internally; read() reassembles
doubles from these pairs. This gives ~48 bits of mantissa (vs. 24 for a
single f32) but less than true f64 precision (52 bits), so
round-tripped values are not always bit-exact with the original input.
f32's exponent range applies too: a double beyond ±3.4e38 is stored as
±Infinity and one below ~1.2e-38 in magnitude as 0, so read() returns
those rather than the original value.
A Complex32Array is stored interleaved ([re0, im0, re1, im1, ...]) in
one buffer; a Complex64Array gets the same double-double split applied
independently to its real and imaginary components.
rows/cols always describe the logical shape regardless of layout.
lda defaults to cols (row-major) or rows (column-major) — dense, no
padding. data must have at least rows * lda (row-major) or
cols * lda (column-major) elements.
Omitting the device falls back to the one from the last init call
— the historical form, and fine for a single-GPU program. Pass a device
explicitly (matching every routine's own (device, ...) convention)
when driving more than one GPU at once, since a GpuMatrix is bound for
life to whichever device created it.
import { init, cleanup, GpuMatrix } from "wgblas";
await init();
// 2×3 matrix: [[1,2,3],[4,5,6]]
const mat = GpuMatrix.from(new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
console.log(mat.rows, mat.cols, mat.lda); // 2 3 3
// Same logical matrix, column-major storage
const matCol = GpuMatrix.from(
new Float32Array([1, 4, 2, 5, 3, 6]),
2,
3,
undefined,
"column-major",
);
mat.destroy();
matCol.destroy();
if (typeof process !== "undefined") cleanup();
Explicit device (multi-GPU):
import { init, cleanup, gpuName } from "wgblas";
import { GpuMatrix } from "wgblas/classes/GpuMatrix";
const dGpu = await init({ powerPreference: "high-performance" });
const iGpu = await init({ powerPreference: "low-power" });
console.log(gpuName(dGpu).description, "and", gpuName(iGpu).description);
// A GpuMatrix is bound to whichever device created it — pass one explicitly
// to keep each matrix resident on its own GPU.
const dMat = GpuMatrix.from(dGpu, new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
const iMat = GpuMatrix.from(iGpu, new Float32Array([1, 2, 3, 4, 5, 6]), 2, 3);
const [a, b] = await Promise.all([dMat.read(), iMat.read()]);
console.log("dGpu matrix:", a);
console.log("iGpu matrix:", b);
dMat.destroy();
iMat.destroy();
if (typeof process !== "undefined") cleanup(); // releases both
matrix data, in the order matching layout
number of rows
number of columns
Optionallda: number
leading dimension (default: cols for row-major, rows for column-major)
Optionallayout: "row-major" | "column-major"
storage layout (default: 'row-major')
GPUDevice from init() — the matrix is bound to this device for life
matrix data, in the order matching layout
number of rows
number of columns
Optionallda: number
leading dimension (default: cols for row-major, rows for column-major)
Optionallayout: "row-major" | "column-major"
storage layout (default: 'row-major')
Represents a Float32Array (or Float64Array) matrix stored in GPU memory, row-major or column-major.
rows/colsalways describe the logical shape regardless of layout.lda(leading dimension) is the stride between consecutive rows (row-major) or columns (column-major) — must be >=cols(row-major) orSee