GPUDevice from init()
'lower' to use the lower triangle, 'upper' to use the upper triangle
order of the matrix A (number of rows and columns)
scalar multiplier for xy^T + yx^T
Float32Array input vector, length at least (n-1)*incx+1
stride for x (must be a positive integer)
Float32Array input vector, length at least (n-1)*incy+1
stride for y (must be a positive integer)
Float32Array, row-major or column-major (see layout), at least (n-1)*lda+n elements
leading dimension of A (>= n either way — A is square)
Optionallayout: "column-major" | "row-major"storage layout of A (default: 'row-major'); for a symmetric
matrix, column-major storage just means the other triangle is the one
physically referenced for a given uplo
Performs the symmetric rank-2 update A = alpha * x * y^T + alpha * y * x^T + A
A is kept GPU-resident; x and y are CPU Float32Arrays. A's own layout
(set at GpuMatrix.from time) determines the operation — there is no
separate layout argument here.
GPUDevice from init()
'lower' to use the lower triangle, 'upper' to use the upper triangle
order of the matrix A
scalar multiplier for xy^T + yx^T
Float32Array input vector
stride for x (must be a positive integer)
Float32Array input vector
stride for y (must be a positive integer)
GpuMatrix, row-major, GPU-resident, Float32-backed
leading dimension of A (must equal A.lda)
Performs the symmetric rank-2 update A = alpha * x * y^T + alpha * y * x^T + A
x, y, and A are all kept resident on the GPU. A's own layout (set at
GpuMatrix.from time) determines the operation — there is no separate
layout argument here.
import { init, cleanup } from "wgblas";
import { ssyr2 } from "wgblas/ssyr2";
import { GpuVector } from "wgblas/classes/GpuVector";
import { GpuMatrix } from "wgblas/classes/GpuMatrix";
import { randomFloat32Array } from "wgblas/random";
// Reshapes a flat row-major array into rows for console.table's 2D grid view.
function toMatrix(A, rows, cols, lda = cols) {
const out = [];
for (let r = 0; r < rows; r++)
out.push(Array.from(A.subarray(r * lda, r * lda + cols), (v) => +v.toFixed(4)));
return out;
}
const device = await init();
const n = 4;
const x = randomFloat32Array(n, -10, 10);
const y = randomFloat32Array(n, -10, 10);
const A = randomFloat32Array(n * n, -10, 10);
const xGpu = GpuVector.from(x);
const yGpu = GpuVector.from(y);
const AGpu = GpuMatrix.from(A, n, n, n, "row-major");
console.log("x:", x);
console.log("y:", y);
console.log("A (before):");
console.table(toMatrix(A, n, n));
// results stay on the GPU between steps
await ssyr2(device, "lower", n, 1.0, xGpu, 1, yGpu, 1, AGpu, AGpu.lda); // A += x*y^T + y*x^T
await ssyr2(device, "lower", n, 1.0, xGpu, 1, yGpu, 1, AGpu, AGpu.lda); // A += x*y^T + y*x^T again
// single readback (GpuMatrix.read() is already dense — no lda padding to strip)
const result = await AGpu.read();
console.log("A (lower triangle, after two rank-2 updates):");
console.table(toMatrix(result, n, n));
xGpu.destroy();
yGpu.destroy();
AGpu.destroy();
if (typeof process !== "undefined") cleanup();
GPUDevice from init()
'lower' to use the lower triangle, 'upper' to use the upper triangle
order of the matrix A
scalar multiplier for xy^T + yx^T
GpuVector input vector (not mutated), Float32-backed
stride for x (must be a positive integer)
GpuVector input vector (not mutated), Float32-backed
stride for y (must be a positive integer)
GpuMatrix, row-major, mutated in place, Float32-backed
leading dimension of A (must equal A.lda)
Performs the symmetric rank-2 update A = alpha * x * y^T + alpha * y * x^T + A
A is an n×n symmetric matrix stored in row-major order, updated in place. Only the triangle specified by
uplois referenced and updated; the other triangle is left untouched (implied by symmetry).Browser (standalone HTML):