Symmetric rank-k update (SYRK)

C := α op(A) op(A)ᵀ + β C on one triangle of C.

  • syrk! — BLAS on the CPU, vendor SYRK on GPUs.
  • syrk_batched! — many independent updates.
  • SYRK_KERNEL! — a portable tiled KernelAbstractions SYRK for backends or element types no vendor library covers (e.g. Float16).
  • recsyrk! — recursive SYRK; its mixed-precision method is under Mixed-precision BLAS.
NextLA.syrk! — Method
syrk!(uplo, trans, alpha, A, beta, C)

Perform a symmetric rank-k update and store the result in C:

C := alpha * op(A) * transpose(op(A)) + beta * C

where op(A) is determined by trans. SYRK uses a transpose, not an adjoint, also for complex A.

uplo selects which triangle of C is updated:

  • 'U': upper triangle
  • 'L': lower triangle

trans may be:

  • 'N': no transpose
  • 'T': transpose
  • 'C': for real types only, where it means 'T'

Notes

  • On the CPU, syrk! is a thin wrapper around LinearAlgebra.BLAS.syrk!.
  • GPU backends may dispatch to native SYRK kernels when available.

Examples

julia> using LinearAlgebra

julia> A = rand(1024, 512); C = zeros(1024, 1024);

julia> syrk!('L', 'N', 1.0, A, 0.0, C);   # lower triangle of A * Aᵀ

julia> LowerTriangular(C) ≈ LowerTriangular(A * A')
true
source
NextLA.syrk_batched! — Method
syrk_batched!(uplo, trans, alpha, A, beta, C)

Perform a batched symmetric rank-k update.

Two storage forms are supported:

  • AbstractArray{T,3} for strided batches
  • AbstractVector of matrices for pointer batches

Each batch entry applies

C[i] := alpha * op(A[i]) * transpose(op(A[i])) + beta * C[i]

where op(A) is determined by trans.

This is currently an internal NextLA API and is not part of the primary exported surface.

Notes

  • If a backend does not provide a native batched SYRK kernel, syrk_batched! falls back to a loop of single-matrix syrk! calls. Metal is the one exception: both its strided and its vector paths go through gemm_batched! instead. On Metal that takes only the 3-D form, so the vector form throws ArgumentError.
  • When any fallback is used, syrk_batched! emits @warn naming the layout.

Examples

julia> using LinearAlgebra

julia> A = rand(1024, 512, 4); C = zeros(1024, 1024, 4);

julia> NextLA.syrk_batched!('L', 'N', 1.0, A, 0.0, C);   # on the CPU this loops syrk! and says so
┌ Warning: syrk_batched! has no native batched SYRK here; looping syrk!
│   backend = "Array{Float64, 3}"
│   layout = :strided
└ @ NextLA src/dense/blas/syrk/syrk_batched.jl

julia> all(LowerTriangular(C[:, :, i]) ≈ LowerTriangular(A[:, :, i] * A[:, :, i]') for i in 1:4)
true
source
NextLA.SYRK_KERNEL! — Method
SYRK_KERNEL!(uplo, trans, alpha, A, beta, C)

Symmetric rank-k update C := alpha * op(A) * transpose(op(A)) + beta * C, computed with KernelAbstractions rather than a vendor BLAS.

Only the uplo triangle of C is written; the other is left exactly as it was. trans selects op(A) = A ('N') or op(A) = Aᵀ ('T'). The flags are accepted in either case. beta = 0 does not read C, and Float16 is summed in Float32 and rounded once.

Unlike syrk! this needs no backend library, so it is the path to use on a backend with no native SYRK, or for an element type the vendor does not cover. It does not synchronize; do that through the backend if the result is read on the host.

source
NextLA.recsyrk! — Function
recsyrk!(alpha, A, beta, C, threshold=256; uplo='L')

Performs an in-place, nested recursive block symmetric rank-k update (C = alpha*A*Aᵀ + beta*C), writing the uplo triangle, 'L' or 'U'. Falls back to standard hardware routines using the dispatch helper at the specified base case threshold.

Examples

julia> using LinearAlgebra

julia> A = rand(1024, 512); C = zeros(1024, 1024);

julia> recsyrk!(1.0, A, 0.0, C; uplo='U');

julia> UpperTriangular(C) ≈ UpperTriangular(A * A')
true
source

Usage examples

With trans = 'T' and beta = 1, syrk! adds AᵀA into the chosen triangle of an existing C and leaves the other triangle exactly as it was.

using NextLA, LinearAlgebra

A = rand(4096, 2048)
C = rand(2048, 2048)
C0 = copy(C)

syrk!('U', 'T', 1.0, A, 1.0, C)          # upper triangle: C += Aᵀ A
UpperTriangular(C) ≈ UpperTriangular(transpose(A) * A + C0) && tril(C, -1) == tril(C0, -1)

# output

true

recsyrk! gives the same triangle as syrk!, and Symmetric turns that triangle into the full product without copying.

using NextLA, LinearAlgebra

A = rand(2048, 1024)
C1 = zeros(2048, 2048)
C2 = zeros(2048, 2048)

syrk!('L', 'N', 1.0, A, 0.0, C1)
recsyrk!(1.0, A, 0.0, C2)                 # same update, blocked recursively
LowerTriangular(C1) ≈ LowerTriangular(C2) && Symmetric(C1, :L) ≈ A * A'

# output

true

recsyrk! also takes Float16 storage with a Float32 result, accumulating in Float32 where BLAS has no half-precision SYRK.

using NextLA, LinearAlgebra

A = rand(Float16, 2048, 1024)
C = zeros(Float32, 2048, 2048)

recsyrk!(1.0, A, 0.0, C)                 # lower triangle
isapprox(LowerTriangular(C), LowerTriangular(Float64.(A) * Float64.(A)'); rtol = 1e-4)

# output

true

On the GPU, syrk! calls cuBLAS SYRK, and recsyrk! covers Float16, which cuBLAS SYRK does not. This block is not run when the documentation is built.

using NextLA, CUDA, LinearAlgebra

A = CUDA.rand(Float32, 4096, 2048)
C = CUDA.zeros(Float32, 4096, 4096)
syrk!('L', 'N', 1f0, A, 0f0, C)                 # cuBLAS SYRK

# cuBLAS has no Float16 SYRK; recsyrk! accumulates in Float32 through GEMMEx
A16 = CUDA.rand(Float16, 4096, 2048)
C32 = CUDA.zeros(Float32, 4096, 4096)
recsyrk!(1f0, A16, 0f0, C32)

isapprox(tril(C), tril(A * A'); rtol = 1e-4) &&
    isapprox(tril(C32), tril(Float32.(A16) * Float32.(A16)'); rtol = 1e-4)