Getting started
This page shows how to install NextLA and use it. The examples in Using NextLA cover the two parts of the library (dense matrices and mixed precision), then show NextLA on NVIDIA, AMD, Intel and Apple GPUs.
Installation
using Pkg
Pkg.add("NextLA") # latest registered release
# development version (main)
Pkg.add(url = "https://github.com/NextLinearAlgebra/NextLA.jl")NextLA needs Julia 1.10 or later and is tested on 1.10, the latest release and nightly. For GPU support, also add the backend package you want: CUDA, AMDGPU, oneAPI or Metal. To check an installation, run the test suite as described in Running the tests.
Using NextLA
In-place routines (names ending in !) overwrite their inputs and follow the BLAS/LAPACK argument conventions, including the character flags side, uplo, trans and diag.
Dense matrices: solve a symmetric positive definite system
syrk! builds S = A Aᵀ + n I, potrf_recursive! factors it as S = L Lᵀ, two calls to trsm! solve S X = B, and gemm! computes the residual.
using NextLA # for syrk!, potrf_recursive!, trsm!, gemm!
using LinearAlgebra # for I, norm
n = 2048
A = rand(n, n)
B = rand(n, 256)
S = Matrix{Float64}(n * I, n, n) # start from n I
syrk!('L', 'N', 1.0, A, 1.0, S) # lower triangle: S = A Aᵀ + n I
potrf_recursive!(S) # Cholesky: L in the lower triangle
X = copy(B)
trsm!('L', 'L', 'N', 'N', 1.0, S, X) # solve L Y = B
trsm!('L', 'L', 'T', 'N', 1.0, S, X) # solve Lᵀ X = Y
R = copy(B) # residual R = B - (A Aᵀ + n I) X
gemm!(R, A * A' + n * I, X; alpha = -1.0, beta = 1.0)
norm(R) / norm(B) < 1e-10
# output
trueThe same dense part of NextLA covers other kinds of systems:
- General square matrix:
getrf2!computes an LU factorization with row pivoting,laswpapplies the same row swaps toB, and two calls totrsm!solveA X = B. See LU. - Least squares:
NextLA.geqrt!computes a QR factorization ofA,NextLA.unmqr!appliesQᵀto the right-hand side, and a triangular solve withRgives the solution. See QR.
The Dense matrices manual page explains this in detail: which routine to choose, how the recursive routines work, and pivoting.
Mixed precision: the same solve with fewer bits
adaptive_precisions picks a precision for each level of the block tree, SymmMixedPrec stores S that way, potrf_recursive! factors it in place, and unified_rectrxm! solves with the mixed-precision factor.
using NextLA # for potrf_recursive!, unified_rectrxm!
using NextLA.MixedPrecision # for adaptive_precisions, mixed-precision types
using LinearAlgebra # for I, norm
n = 2048
A = rand(n, n)
S = A * A' + n * I # symmetric positive definite
B = rand(n, 256)
# pick a precision for each block level, store S that way, and factor it
p = adaptive_precisions(S, DataType[Float16, Float32, Float64], 256, 1e-6)
M = SymmMixedPrec(S, 'L'; precisions = p)
potrf_recursive!(M) # S = L Lᵀ, L stored in M
L = TriMixedPrec(M) # the factor, without copying
X = copy(B)
unified_rectrxm!('L', 'L', 'N', 1.0, 'S', L, X) # solve L Y = B
unified_rectrxm!('L', 'L', 'T', 1.0, 'S', L, X) # solve Lᵀ X = Y
norm(S * X - B) / norm(B) < 1e-5 # residual of the solve
# output
trueThe Mixed precision manual page explains this in detail: how the matrix is split into blocks, how to choose their precisions, and the other mixed-precision routines.
On a GPU
Build the matrices on the GPU and call the same routines: NextLA uses the vendor library where it wraps one and its own portable kernels otherwise, and mixed-precision matrices keep their data on the device.
NVIDIA GPUs (CUDA)
using NextLA # for potrf_recursive!, trsm!
using CUDA # for CUDA.rand
using LinearAlgebra # for I
n = 4096
A = CUDA.rand(Float64, n, n)
S = A * A' + n * I # on the GPU
B = CUDA.rand(Float64, n, 256)
F = potrf_recursive!(copy(S)) # Cholesky on the GPU
X = trsm!('L', 'L', 'N', 'N', 1.0, F, copy(B)) # solve L Y = B
trsm!('L', 'L', 'T', 'N', 1.0, F, X) # solve Lᵀ X = Y
S * X ≈ B # output: trueAMD GPUs (AMDGPU)
using NextLA # for lu_recursive!, unified_rectrxm!
using NextLA.MixedPrecision # for FullMixedPrec
using AMDGPU # for ROCArray
using LinearAlgebra # for I, norm
n = 4096
A = ROCArray(rand(n, n) + n * I) # diagonally dominant, on the GPU
B = ROCArray(rand(n, 256))
# A = L U in mixed precision: the off-diagonal blocks are stored in Float32
M = FullMixedPrec(A; precisions = DataType[Float32, Float64])
lu_recursive!(M)
X = copy(B)
unified_rectrxm!('L', 'L', 'N', 'U', 1.0, 'S', M, X) # solve L Y = B
unified_rectrxm!('L', 'U', 'N', 'N', 1.0, 'S', M, X) # solve U X = Y
norm(A * X - B) / norm(B) < 1e-5 # output: trueIntel GPUs (oneAPI)
using NextLA # for trsm!, trmm!
using oneAPI # for oneArray
using LinearAlgebra # for I
n = 4096
L = oneArray(rand(Float32, n, n) + n * I) # only its lower triangle is used
B = oneArray(rand(Float32, n, 256))
X = copy(B)
trsm!('L', 'L', 'N', 'N', 1f0, L, X) # solve L X = B
Y = copy(X)
trmm!('L', 'L', 'N', 'N', 1f0, L, Y) # multiply back: Y = L X
Y ≈ B # output: trueApple GPUs (Metal)
using NextLA # for unified_rectrxm!
using Metal # for MtlArray
using LinearAlgebra # for I
n = 4096
L = MtlArray(rand(Float32, n, n) + n * I) # Metal GPUs work in Float32
B = MtlArray(rand(Float32, n, 256))
X = copy(B)
unified_rectrxm!('L', 'L', 'N', 1f0, 'M', L, X) # multiply: X = L B
unified_rectrxm!('L', 'L', 'N', 1f0, 'S', L, X) # solve L Z = X: back to B
X ≈ B # output: trueGetting help
Questions, bug reports and feature requests go to the GitHub issues.
Where to go next
- Dense matrices: choosing a routine, how the recursive routines work, pivoting, tile QR and batched routines.
- Mixed precision: the full guide to
NextLA.MixedPrecision. - Backends and GPUs: which routine runs on which backend, and with which element types.
- Benchmarks: speed against the vendor libraries.
- API reference: every routine, organised like the source; each page ends with more worked examples.