Mixed-precision LAPACK
The recursive Cholesky and LU methods of NextLA.MixedPrecision, in src/mxp/lapack. Load them with using NextLA.MixedPrecision.
NextLA.potrf_recursive! — Method
potrf_recursive!(A::SymmMixedPrec, block_size::Integer=POTRF_BLOCK_SIZE) -> AFactors the symmetric positive definite A in place: A = L * transpose(L) with L stored in the lower triangle when A.uplo == 'L', or A = transpose(U) * U with U in the upper triangle when it is 'U'. It walks the block tree of A: each leaf is factored by the dense potrf_recursive! with block_size and A.uplo, each off-diagonal block is solved against the factored diagonal block before it, and the trailing block is updated with recsyrk!. A matrix that is not positive definite throws PosDefException from a leaf; its info is the order of the first leading minor that is not positive definite, counted in the whole matrix, and A is then partly factored, as with the dense potrf_recursive!.
An off-diagonal block is solved against the T_Base diagonal block on its stored values, since the solve is linear and its scale carries through, in a T_Base copy rounded back once when it is narrower; a result past floatmax of the block's type is stored with the block's scale raised. A leaf stored with a scale factor throws ArgumentError, since its factor does not scale with it. reconstruct_matrix(TriMixedPrec(A)) recovers the dense factor with zeros in the other triangle; reconstruct_matrix(A) would mirror it into both.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024); S = A * A' + 1024I;
julia> M = SymmMixedPrec(S, 'L'; precisions = DataType[Float32, Float64]);
julia> potrf_recursive!(M);
julia> L = reconstruct_matrix(TriMixedPrec(M));
julia> isapprox(L, cholesky(S).L; rtol = 1e-5)
trueNextLA.lu_recursive! — Method
lu_recursive!(A::FullMixedPrec, block_size::Integer=LU_BLOCK_SIZE;
parallel=size(A, 1) > RECLU_PARALLEL_THRESHOLD) -> AFactors A = L * U in place without pivoting, walking the block tree of A: each leaf is factored by the dense lu_recursive! with block_size, each off-diagonal block is solved against the factored diagonal block before it, and the trailing block is updated with recgemm!. L has a unit diagonal and is stored below it, U on and above it, as LAPACK's getrf stores them without pivots, so A must not need pivoting – a diagonally dominant matrix, for example. A zero pivot throws ZeroPivotException from a leaf, with info the column of that pivot counted in the whole matrix; A is then partly factored.
An off-diagonal block is solved against the T_Base diagonal block on its stored values, since the solve is linear and its scale carries through, in a T_Base copy rounded back once when it is narrower; a result past floatmax of the block's type is stored with the block's scale raised. A leaf stored with a scale factor throws ArgumentError, since one scale cannot hold both L and U.
When the top-level off-diagonal blocks are stored narrower than Float32, the Float32 GEMMs inside the factorization (leaves, solves and trailing updates) may run on TF32 tensor cores on CUDA: their inputs are rounded to a 10-bit fraction, as Float16 storage already rounds, and the sums stay Float32.
With parallel, the two off-diagonal solves of a level, which are independent, run as concurrent tasks (@async) at the top level and, below it, at each level with more than RECLU_PARALLEL_THRESHOLD rows; the trailing recgemm! uses its own threshold. parallel = false runs them one after the other.
transpose(A) throws ArgumentError: its factors are not the factors of A stored another way. Build the container from the transposed matrix, or factor A and solve with trans = 'T'.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024) + 1024I; # diagonally dominant: no pivoting needed
julia> M = FullMixedPrec(A; precisions = DataType[Float32, Float64]);
julia> lu_recursive!(M);
julia> F = reconstruct_matrix(M); # L below the diagonal, U on and above it
julia> isapprox(UnitLowerTriangular(F) * UpperTriangular(F), A; rtol = 1e-5)
trueUsage examples
Each example stands on its own and ends with a check that prints true: the relative error of the mixed-precision result against the same computation in Float64, with a tolerance that matches the coarsest precision used.
Cholesky with precisions from adaptive_precisions, then a solve
potrf_recursive! factors a SymmMixedPrec in place, and TriMixedPrec(M) hands the factor to unified_rectrxm! for the two triangular solves.
using NextLA, NextLA.MixedPrecision, LinearAlgebra
n = 2048
A = rand(n, n)
S = A * A' + n * I # symmetric positive definite
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
B = rand(n, 256)
X = copy(B)
unified_rectrxm!('L', 'L', 'N', 1.0, 'S', L, X) # L * Y = B
unified_rectrxm!('L', 'L', 'T', 1.0, 'S', L, X) # L' * X = Y
relerr(X, Y) = norm(X - Y) / norm(Y)
Lf = reconstruct_matrix(L)
relerr(Lf * Lf', S) < 1e-5 && relerr(S * X, B) < 1e-5
# output
trueLU without pivoting, then a solve
lu_recursive! stores L (unit diagonal) and U in the same FullMixedPrec, and unified_rectrxm! reads either triangle of it.
using NextLA, NextLA.MixedPrecision, LinearAlgebra
n = 2048
A = rand(n, n) + n * I # diagonally dominant: no pivoting needed
M = FullMixedPrec(A; precisions = DataType[Float32, Float64])
lu_recursive!(M) # A = L * U, both stored in M
B = rand(n, 256)
X = copy(B)
unified_rectrxm!('L', 'L', 'N', 'U', 1.0, 'S', M, X) # L * Y = B, unit diagonal
unified_rectrxm!('L', 'U', 'N', 'N', 1.0, 'S', M, X) # U * X = Y
norm(A * X - B) / norm(B) < 1e-5
# output
trueOn the GPU
FullMixedPrec built from a CuArray stores its blocks as CuArrays, and lu_recursive! and reconstruct_matrix work on it unchanged (this block is not run when the documentation is built).
using CUDA, NextLA, NextLA.MixedPrecision, LinearAlgebra
n = 4096
A = CUDA.rand(Float64, n, n) + n * I # diagonally dominant, on the GPU
M = FullMixedPrec(A; precisions = DataType[Float16, Float32, Float64])
lu_recursive!(M)
F = Array(reconstruct_matrix(M)) # reconstruct_matrix returns a CuMatrix
isapprox(UnitLowerTriangular(F) * UpperTriangular(F), Array(A); rtol = 1e-3) # true