Triangular solve and multiply

  • trsm! — triangular solve in BLAS argument order, op(A) X = α B or X op(A) = α B.
  • trmm! — triangular multiply in BLAS argument order.
  • unified_rectrxm! — the recursive engine behind both, selected with func = 'S' (solve) or 'M' (multiply). Its mixed-precision method is under Mixed-precision BLAS.
  • trsm_batched! — many independent solves.
  • LeftLowerTRSM!, RightUpperTRMM!, ... — the KernelAbstractions base-case kernels.

Recursive TRSM / TRMM

NextLA.unified_rec — Method
unified_rec(func, side, uplo, A, n, B, threshold; unitdiag=false)

Recursive kernel for unified triangular matrix operations.

This function implements the divide-and-conquer recursive algorithm that partitions matrices into 2x2 block structure and applies the appropriate sequence of operations.

Arguments

  • func::Char: Operation type ('S' for solve, 'M' for multiply)
  • side::Char: Operation side ('L' for left, 'R' for right)
  • uplo::Char: Triangular part ('U' for upper, 'L' for lower)
  • A::AbstractMatrix{T}: Triangular coefficient matrix
  • n::Int: Matrix dimension to process
  • B::AbstractMatrix{T}: Target matrix (modified in-place)
  • threshold::Int: Recursion base case threshold (default: 256)

Algorithm

The recursion follows different orderings based on the operation type:

  1. For forward substitution: A11 → GEMM → A22
  2. For backward substitution: A22 → GEMM → A11

This ensures numerical stability and correctness of the triangular solve.

source
NextLA.unified_rectrxm! — Method
unified_rectrxm!(side, uplo, trans, diag, alpha, func, A, B)

As above, with diag selecting whether A has a unit diagonal.

  • diag::Char: 'N' reads the stored diagonal, 'U' treats it as all ones and never reads it, matching the BLAS convention.

The seven-argument form delegates here with 'N', so existing callers are unaffected.

source
NextLA.unified_rectrxm! — Method
unified_rectrxm!(side, uplo, trans, alpha, func, A, B)

Unified recursive function for triangular matrix solve (TRSM) and multiply (TRMM) operations.

This function supports both solving triangular systems of equations and performing triangular matrix multiplications using recursive algorithms that are cache-friendly and numerically stable.

Arguments

  • side::Char: Specifies the side of the operation ('L' for left, 'R' for right)
    • 'L': Left multiplication (A * B or inv(A) * B)
    • 'R': Right multiplication (B * A or B * inv(A))
  • uplo::Char: Specifies the triangular part of the matrix to reference
    • 'U': Use the upper triangle
    • 'L': Use the lower triangle
  • trans::Char: Specifies the transposition operation
    • 'N': No transpose
    • 'T': Transpose
    • 'C': Conjugate transpose
  • alpha::Number: Scalar multiplier applied to the operation
  • func::Char: Specifies the function type
    • 'S': Solve (TRSM, A * X = alpha * B)
    • 'M': Multiply (TRMM, Update B = alpha * A * B or alpha * B * A)
  • A::AbstractMatrix: The triangular matrix
  • B::AbstractMatrix: The matrix to multiply or solve for (modified in-place)

Returns

  • Updated matrix B after performing the specified operation

Algorithm

Uses recursive divide-and-conquer approach that:

  1. Partitions matrices into 2x2 block structure
  2. Applies operations recursively on subblocks
  3. Handles base cases with optimized kernel functions
  4. Maintains numerical stability through careful ordering

Implementation Notes

  • The function modifies B in place for efficiency
  • Uses different thresholds for TRSM (256) vs TRMM (16) operations
  • Automatically handles transpose operations by adjusting matrix views
  • Recursive partitioning adapts to matrix size for optimal performance

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> A = rand(n, n) + n * I;

julia> B = rand(n, 256);

julia> C = unified_rectrxm!('L', 'U', 'N', 1.0, 'M', A, copy(B));

julia> unified_rectrxm!('L', 'U', 'N', 1.0, 'S', A, C) ≈ B
true
source

BLAS-style entry points and kernels

NextLA.LeftLowerTRSM! — Function
LeftLowerTRSM!(A, B, unitdiag=false) -> nothing

Solve A * X = B in place for a lower triangular A (left side): B is overwritten with X. Only the lower triangle of A is read. With unitdiag = true the diagonal is taken as one and not read.

This is one GPU kernel launch, with no alpha and no transpose. A must be k×k, where k is the number of rows of B; otherwise it throws DimensionMismatch. k may be at most 1024; a larger triangle throws ArgumentError. Use trsm! for larger triangles. Returns nothing and does not synchronize.

source
NextLA.LeftUpperTRSM! — Function
LeftUpperTRSM!(A, B, unitdiag=false) -> nothing

Solve A * X = B in place for a upper triangular A (left side): B is overwritten with X. Only the upper triangle of A is read. With unitdiag = true the diagonal is taken as one and not read.

This is one GPU kernel launch, with no alpha and no transpose. A must be k×k, where k is the number of rows of B; otherwise it throws DimensionMismatch. k may be at most 1024; a larger triangle throws ArgumentError. Use trsm! for larger triangles. Returns nothing and does not synchronize.

source
NextLA.RightLowerTRSM! — Function
RightLowerTRSM!(A, B, unitdiag=false) -> nothing

Solve X * A = B in place for a lower triangular A (right side): B is overwritten with X. Only the lower triangle of A is read. With unitdiag = true the diagonal is taken as one and not read.

This is one GPU kernel launch, with no alpha and no transpose. A must be k×k, where k is the number of columns of B; otherwise it throws DimensionMismatch. k may be at most 1024; a larger triangle throws ArgumentError. Use trsm! for larger triangles. Returns nothing and does not synchronize.

source
NextLA.RightUpperTRSM! — Function
RightUpperTRSM!(A, B, unitdiag=false) -> nothing

Solve X * A = B in place for a upper triangular A (right side): B is overwritten with X. Only the upper triangle of A is read. With unitdiag = true the diagonal is taken as one and not read.

This is one GPU kernel launch, with no alpha and no transpose. A must be k×k, where k is the number of columns of B; otherwise it throws DimensionMismatch. k may be at most 1024; a larger triangle throws ArgumentError. Use trsm! for larger triangles. Returns nothing and does not synchronize.

source
NextLA.trsm — Function
trsm(side, uplo, transa, diag, A, B, alpha=1.0) -> B

Short form of trsm!: the arguments are forwarded unchanged, with alpha last.

Arguments

  • 'side':
    • 'L': solve op(A)X = alphaB
    • 'R': solve Xop(A) = alphaB
  • 'uplo':
    • 'U': A is upper triangular
    • 'L': A is lower triangular
  • 'transa': operation on A
    • 'N': op(A) = A
    • 'T': op(A) = A^T
    • 'C': op(A) = A^H
  • 'diag': diagonal type
    • 'N': non-unit diagonal
    • 'U': unit diagonal
  • 'A': triangular matrix
  • 'B': right-hand side matrix (will be overwritten with solution)
  • 'alpha': scalar multiplier (default: one(eltype(A)))

Returns

  • Updated matrix B containing the solution

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> A = complex.(triu(randn(n, n)), triu(randn(n, n))) + n*I; B = complex.(randn(n, 3), randn(n, 3)); B0 = copy(B);

julia> NextLA.trsm('L', 'U', 'N', 'N', A, B);   # B = UpperTriangular(A) \ B

julia> UpperTriangular(A) * B ≈ B0
true
source
NextLA.trsm! — Method
trsm!(side, uplo, transa, diag, alpha, A, B) -> B

Triangular solve in the BLAS argument order, in place: B is overwritten with the X that solves op(A) * X = alpha * B (side = 'L') or X * op(A) = alpha * B (side = 'R'), where op(A) is A, transpose(A) or adjoint(A) for transa = 'N', 'T' or 'C', A is the uplo triangle and diag = 'U' takes its diagonal as ones without reading it.

A thin entry point over unified_rectrxm! with func = 'S', so it runs wherever that does, for real and complex element types. On CuMatrix and ROCMatrix of Float32, Float64, ComplexF32 or ComplexF64 it reaches cuBLAS / rocBLAS trsm! through that function's extension methods; on the CPU, oneAPI, Metal and Float16 it runs the portable recursive kernels. CUDA views go to cuBLAS too; AMDGPU views take the portable path. LinearAlgebra.BLAS.trsm! takes the same arguments but is CPU BLAS only.

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> A = rand(n, n) + n * I;

julia> B = rand(n, 256);

julia> X = trsm!('L', 'L', 'N', 'N', 1.0, A, copy(B));

julia> LowerTriangular(A) * X ≈ B
true
source
NextLA.LeftLowerTRMM! — Function
LeftLowerTRMM!(A, B, unitdiag=false; n_threads=(16,16))

Perform left-sided lower triangular matrix multiplication: B := A * B

Arguments

  • A::AbstractMatrix: N×N lower triangular coefficient matrix
  • B::AbstractMatrix: N×M target matrix (modified in-place)
  • unitdiag::Bool: if true, the diagonal of A is taken as one (default: false)
  • n_threads::Tuple: Thread block dimensions (default: (16,16))

A may have at most n_threads[1] (16) rows; a larger A throws ArgumentError. Use trmm! for larger triangles.

Implementation Notes

  • Uses GPU acceleration with optimized kernel
  • Thread block size should not exceed hardware limits
  • NDRange is padded to handle boundary conditions
source
NextLA.LeftLowerTRMM_kernel! — Method
LeftLowerTRMM_kernel!(A, B, ::Val{U}, ::Val{BANK}=Val(1))

GPU kernel for left-sided lower triangular matrix multiplication.

Performs the operation B := A * B where A is lower triangular. Uses shared memory tiling with configurable bank offset to avoid conflicts.

Arguments

  • A::AbstractMatrix: N×N lower triangular coefficient matrix
  • B::AbstractMatrix: N×M target matrix (modified in-place)
  • U::Bool: if true, the diagonal of A is taken as one (unit diagonal)
  • BANK::Int: Memory bank offset to avoid conflicts (default: 1)

Implementation Notes

  • Tile size limited to 16×16 due to shared memory constraints
  • Uses private variables for accumulation to enable vectorization
  • Includes bounds checking for non-square input matrices
  • Synchronization points ensure correct shared memory access patterns
source
NextLA.LeftUpperTRMM! — Function
LeftUpperTRMM!(A, B, unitdiag=false; n_threads=(16,16))

Perform left-sided upper triangular matrix multiplication: B := A * B

Arguments

  • A::AbstractMatrix: N×N upper triangular coefficient matrix
  • B::AbstractMatrix: N×M target matrix (modified in-place)
  • unitdiag::Bool: if true, the diagonal of A is taken as one (default: false)
  • n_threads::Tuple: Thread block dimensions (default: (16,16))

A may have at most n_threads[1] (16) rows; a larger A throws ArgumentError. Use trmm! for larger triangles.

source
NextLA.RightLowerTRMM! — Function
RightLowerTRMM!(A, B, unitdiag=false; n_threads=(16,16))

Perform right-sided lower triangular matrix multiplication: B := B * A

Arguments

  • A::AbstractMatrix: N×N lower triangular coefficient matrix
  • B::AbstractMatrix: M×N target matrix (modified in-place)
  • unitdiag::Bool: if true, the diagonal of A is taken as one (default: false)
  • n_threads::Tuple: Thread block dimensions (default: (16,16))

A may have at most n_threads[1] (16) rows; a larger A throws ArgumentError. Use trmm! for larger triangles.

source
NextLA.RightUpperTRMM! — Function
RightUpperTRMM!(A, B, unitdiag=false; n_threads=(16,16))

Perform right-sided upper triangular matrix multiplication: B := B * A

Arguments

  • A::AbstractMatrix: N×N upper triangular coefficient matrix
  • B::AbstractMatrix: M×N target matrix (modified in-place)
  • unitdiag::Bool: if true, the diagonal of A is taken as one (default: false)
  • n_threads::Tuple: Thread block dimensions (default: (16,16))

A may have at most n_threads[1] (16) rows; a larger A throws ArgumentError. Use trmm! for larger triangles.

source
NextLA.trmm — Function
trmm(side, uplo, transa, diag, A, B, alpha=1.0) -> B

Short form of trmm!: the arguments are forwarded unchanged, with alpha last.

Arguments

  • side::Char: Operation side
    • 'L': B := alphaop(A)B (left multiplication)
    • 'R': B := alphaBop(A) (right multiplication)
  • uplo::Char: Triangular part specification
    • 'U': A is upper triangular
    • 'L': A is lower triangular
  • transa::Char: Operation on matrix A
    • 'N': op(A) = A (no transpose)
    • 'T': op(A) = A^T (transpose)
    • 'C': op(A) = A^H (conjugate transpose)
  • diag::Char: Diagonal type
    • 'N': non-unit diagonal
    • 'U': unit diagonal
  • A::AbstractMatrix: Triangular coefficient matrix
  • B::AbstractMatrix: Target matrix (modified in-place)
  • alpha: Scalar multiplier (default: one(eltype(A)))

Returns

  • Updated matrix B (same as input B, modified in-place)

Examples

julia> using LinearAlgebra

julia> n = 16;

julia> A = complex.(triu(randn(n, n)), triu(randn(n, n))); B = complex.(randn(n, 3), randn(n, 3)); B0 = copy(B);

julia> NextLA.trmm('L', 'U', 'N', 'N', A, B);   # B = UpperTriangular(A) * B

julia> B ≈ UpperTriangular(A) * B0
true

Implementation Notes

  • The same as trmm!(side, uplo, transa, diag, alpha, A, B), which it calls
source
NextLA.trmm! — Method
trmm!(side, uplo, transa, diag, alpha, A, B, C) -> C

Triangular multiply into a separate output: C := alpha * op(A) * B (side = 'L') or C := alpha * B * op(A) (side = 'R'), leaving B as it was. This is the argument list of cuBLAS / rocBLAS trmm!; passing C = B gives the in-place form. B is copied into C and multiplied there by the in-place method, so it runs wherever that does.

source
NextLA.trmm! — Method
trmm!(side, uplo, transa, diag, alpha, A, B) -> B

Triangular multiply in the BLAS argument order, in place: B := alpha * op(A) * B (side = 'L') or B := alpha * B * op(A) (side = 'R'), with op, uplo and diag as for trsm!.

A thin entry point over unified_rectrxm! with func = 'M': cuBLAS / rocBLAS trmm! on CuMatrix / ROCMatrix of Float32, Float64, ComplexF32 or ComplexF64, the portable recursive kernels everywhere else.

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> A = rand(n, n);

julia> B = rand(n, 256);

julia> C = trmm!('L', 'L', 'T', 'N', 2.0, A, copy(B));

julia> C ≈ 2 * transpose(LowerTriangular(A)) * B
true
source

Batched TRSM

NextLA.trsm_batched! — Function
trsm_batched!(side, uplo, transa, diag, A, B, alpha=one(eltype(A)))

Solve a batch of triangular systems in place.

A and B must be three-dimensional arrays with equal batch sizes, or matching vectors of matrices. Each B[:, :, b] is overwritten by the solution of the corresponding triangular system defined by A[:, :, b].

This entry point is reserved for backend batched-library dispatch. CPU arrays fall back to a BLAS loop, CUDA/AMDGPU/oneAPI extensions override this with native batched kernels, and Metal currently throws an error.

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> A = rand(n, n, 4) .+ n * Matrix(I, n, n);

julia> B = rand(n, 64, 4);

julia> X = trsm_batched!('L', 'L', 'N', 'N', A, copy(B));

julia> all(LowerTriangular(A[:, :, k]) * X[:, :, k] ≈ B[:, :, k] for k in 1:4)
true
source

Usage examples

Right-side solve with the conjugate transpose

side = 'R' with transa = 'C' solves X * U' = α B for a complex upper triangle U.

using NextLA, LinearAlgebra

n = 2048
A = rand(ComplexF64, n, n) + n * I
B = rand(ComplexF64, 512, n)

X = trsm!('R', 'U', 'C', 'N', 0.5, A, copy(B))    # X * U' = 0.5 * B
X * UpperTriangular(A)' ≈ 0.5 * B

# output

true

Unit diagonal

With diag = 'U' the stored diagonal is never read, so here it holds NaN; small off-diagonal entries keep the unit triangle well conditioned.

using NextLA, LinearAlgebra

n = 2048
A = rand(n, n) / n
A[diagind(A)] .= NaN
B = rand(n, 256)

X = trsm!('L', 'L', 'T', 'U', 1.0, A, copy(B))    # transpose(L) * X = B, L unit lower
transpose(UnitLowerTriangular(A)) * X ≈ B

# output

true

Multiply into a separate output

The eight-argument trmm! writes alpha * op(A) * B into C and leaves B unchanged.

using NextLA, LinearAlgebra

n = 2048
A = rand(n, n)
B = rand(n, 256)
C = similar(B)

trmm!('L', 'U', 'N', 'N', 1.0, A, B, C)           # C = U * B
C ≈ UpperTriangular(A) * B

# output

true

Batched solve over a vector of matrices

trsm_batched! also takes vectors of matrices, and an optional alpha last.

using NextLA, LinearAlgebra

n = 1024
A = [rand(n, n) + n * I for _ in 1:4]
B = [rand(n, 128) for _ in 1:4]

X = trsm_batched!('L', 'U', 'N', 'N', A, deepcopy(B), 2.0)
all(UpperTriangular(A[k]) * X[k] ≈ 2 * B[k] for k in 1:4)

# output

true

On the GPU

The same calls on CuArrays reach cuBLAS trsm! and trmm! (not run when the documentation is built).

using NextLA, LinearAlgebra, CUDA

n = 4096
A = CUDA.rand(Float32, n, n) + n * I
B = CUDA.rand(Float32, n, 512)

X = trsm!('L', 'L', 'N', 'N', 1.0f0, A, copy(B))  # solve L * X = B
C = trmm!('L', 'L', 'N', 'N', 1.0f0, A, copy(X))  # multiply back: C = L * X
C ≈ B                                             # true