Cholesky

Factorizations A = L Lᵀ or A = Uᵀ U of symmetric positive definite matrices.

RoutineAlgorithm
potrf!single call to LAPACK / the vendor solver
potrf_recursive!recursive 2×2 blocking over potrf!, TRSM and SYRK
potrf_batched!many independent matrices
cholesky_blocked!blocked KernelAbstractions factorization
cholesky_lower!single-kernel KernelAbstractions factorization

The mixed-precision method of potrf_recursive! for SymmMixedPrec is under Mixed-precision LAPACK.

Factorizations

NextLA.potrf! — Method
potrf!(uplo, A) -> (A, info)

Compute the Cholesky factorization of the symmetric positive definite matrix A, in place, and return (A, info).

uplo selects which triangle is read and overwritten with the factor:

  • 'U': A = Uᵀ * U, the upper triangle
  • 'L': A = L * Lᵀ, the lower triangle

The opposite triangle is neither read nor written, so it keeps whatever it held on entry. info follows LAPACK: 0 on success, and k > 0 when the leading minor of order k is not positive definite, at which point the factorization is incomplete.

potrf!(A) -> (A, info)

Convenience form, equivalent to potrf!('L', A).

Notes

  • On the CPU, potrf! is a thin wrapper around LinearAlgebra.LAPACK.potrf!.
  • GPU backends dispatch to native Cholesky routines when available; see ext/{cuda,amdgpu}/potrf.jl. Metal's wrapper throws, and oneAPI has none. There is no generic device fallback, so a backend without a working wrapper raises rather than silently copying to the host.
  • This is the single-matrix entry point. For a batch of independent matrices use potrf_batched!, which has its own backend wrappers.
  • Unlike LinearAlgebra.cholesky, a non-positive-definite input is reported through info rather than raising PosDefException.

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> M = rand(n, n);

julia> S = M * M' + n * I;

julia> F, info = potrf!('L', copy(S));

julia> L = LowerTriangular(F);

julia> info == 0 && L * L' ≈ S
true
source
NextLA.potrf_recursive! — Function
potrf_recursive!(A, block_size=POTRF_BLOCK_SIZE; uplo='L')

Perform an in-place, nested recursive Cholesky factorization of the symmetric positive definite matrix A: A = L * Lᵀ with L written into the lower triangle for uplo = 'L', or A = Uᵀ * U with U in the upper triangle for 'U'.

The recursion splits A into a 2x2 block scheme and stops once a sub-block is block_size or smaller, at which point it falls back to potrf!.

The panel solve goes through trsm! (one vendor call on CUDA, the portable recursive unified_rectrxm! elsewhere); Float16 takes unified_rectrxm! directly, as no vendor library has a Float16 TRSM. The rank-k update uses syrk!, which reaches BLAS or the vendor library; syrk! has no Float16 method, so half precision takes recsyrk!.

The other triangle is not referenced and is left as it was on entry. A matrix that is not positive definite throws PosDefException(k), k the order of the first leading minor that is not, as LinearAlgebra.cholesky! does; A is then partly factored. The recursion splits n at the largest power of two below it (at n ÷ 2 when n is a power of two).

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> M = rand(n, n);

julia> S = M * M' + n * I;

julia> L = LowerTriangular(potrf_recursive!(copy(S), 256));

julia> L * L' ≈ S
true
source
NextLA.potrf_batched! — Method
potrf_batched!(uplo, A)

Compute an in-place batched Cholesky factorization of A.

A must be a three-dimensional array whose slices A[:, :, b] are factored independently.

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

The return value follows the underlying backend wrapper as closely as practical: CPU, CUDA, and AMDGPU return (A, status), oneAPI returns A, and Metal throws.

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> S = stack(M * M' + n * I for M in (rand(n, n) for _ in 1:4));

julia> F, status = potrf_batched!('L', copy(S));

julia> L = LowerTriangular(F[:, :, 4]);

julia> all(iszero, status) && L * L' ≈ S[:, :, 4]
true
source
NextLA.cholesky_blocked! — Method
cholesky_blocked!(A; block = 64, threads = 512, kernel = :shared) -> A

In-place left-looking blocked Cholesky, A = L * Lᵀ, writing L into the lower triangle of A. The strict upper triangle outside the diagonal blocks is left untouched. Inside each diagonal block after the first, the GEMM also updates the strict upper triangle, so those entries no longer hold their input values.

Each pass works on one block column. It first updates the whole block column (the diagonal block and the panel below it) with one GEMM against the columns already factored, then factors the diagonal block with a kernel, then solves for the panel below it with a triangular solve. There is no trailing update. block must not exceed 64, the width both kernels are sized for.

kernel picks how a diagonal block is factored, and only that – the panel solve and the trailing update are the same either way:

  • :shared stages the block in shared memory (chol_kernel_shared!), and takes threads as given.
  • :register keeps each thread's four columns in registers and shares only the active column (chol_kernel_register!). Its thread mapping is fixed, so it ignores threads and launches CHOL_REG_THREADS of them in one workgroup; the backend has to allow a workgroup that large.

GPU only: the kernel is declared cpu=false on the branch this came from, and the CPU backend cannot run it. Use potrf! or potrf_recursive! there.

A matrix that is not positive definite throws PosDefException(k) at the first pivot that is not positive, with A partly factored (NaN from that column on).

Examples

GPU only, so this example is not run as a doctest:

using NextLA, LinearAlgebra, CUDA

n = 1024
M = CUDA.rand(Float64, n, n)
S = M * M' + n * I
L = tril(cholesky_blocked!(copy(S)))
L * L' ≈ S      # true
source
NextLA.cholesky_lower! — Method
cholesky_lower!(A) -> A

Factor the symmetric positive definite matrix A in place as A = L * Lᵀ, overwriting the lower triangle with L and explicitly zeroing the upper triangle.

Unblocked and confined to a single workgroup, so it is intended for a diagonal block rather than a full matrix. For a one-shot factorization of a general matrix prefer potrf!, which dispatches to the vendor library.

Unlike potrf! this zeroes the opposite triangle. A matrix that is not positive definite throws PosDefException(k) at the first pivot that is not positive, with A partly factored (NaN from that column on).

Examples

julia> using LinearAlgebra

julia> n = 1024;

julia> M = rand(n, n);

julia> S = M * M' + n * I;

julia> F = cholesky_lower!(copy(S));

julia> istril(F) && F * F' ≈ S
true
source

Usage examples

Solve S * X = B

Factor S = L * Lᵀ with potrf!, then solve with L and Lᵀ using two calls to trsm!.

using NextLA, LinearAlgebra

n = 2048
M = rand(n, n)
S = M * M' + n * I
B = rand(n, 256)

F, info = potrf!('L', copy(S))                    # S = L * L'
X = copy(B)
trsm!('L', 'L', 'N', 'N', 1.0, F, X)              # L * Y = B
trsm!('L', 'L', 'T', 'N', 1.0, F, X)              # L' * X = Y
S * X ≈ B

# output

true

Upper factor

With uplo = 'U', potrf_recursive! writes U with S = Uᵀ * U and leaves the strict lower triangle as it was.

using NextLA, LinearAlgebra

n = 2048
M = rand(n, n)
S = M * M' + n * I

F = potrf_recursive!(copy(S), 512; uplo = 'U')    # block_size 512
U = UpperTriangular(F)
U' * U ≈ S && tril(F, -1) == tril(S, -1)

# output

true

Matrices that are not positive definite

potrf! reports the order of the first leading minor that is not positive definite through info; potrf_recursive! throws PosDefException with the same order.

using NextLA, LinearAlgebra

n = 1024
M = rand(n, n)
S = M * M' + n * I
S[700, 700] = -1.0                                # leading minor of order 700 is indefinite

_, info = potrf!('L', copy(S))
err = try
    potrf_recursive!(copy(S), 256)
catch e
    e
end
info == 700 && err == PosDefException(700)

# output

true

On the GPU

On a CuArray, potrf! calls cuSOLVER, potrf_recursive! stays on the device, and cholesky_blocked! runs its own kernels (not run when the documentation is built).

using NextLA, LinearAlgebra, CUDA

n = 4096
M = CUDA.rand(Float64, n, n)
S = M * M' + n * I
B = CUDA.rand(Float64, n, 256)

F, info = potrf!('L', copy(S))
F2 = potrf_recursive!(copy(S), 1024)
F3 = cholesky_blocked!(copy(S))

X = trsm!('L', 'L', 'N', 'N', 1.0, F, copy(B))
trsm!('L', 'L', 'T', 'N', 1.0, F, X)
info == 0 && tril(F2) ≈ tril(F) && tril(F3) ≈ tril(F) && S * X ≈ B    # true