LU
| Routine | Pivoting | Notes |
|---|---|---|
LinearAlgebra.lu!(::NextLAMatrix) | partial (default), complete, row-nonzero or none | getrf2! for the default; NextLA's generic loop for the others |
getrf2! | partial | recursive LAPACK getrf2 |
lu_recursive! | none | recursive, square matrices |
lu_recursive_nopiv! | none | recursive, any shape |
lu_base! | partial | single-workgroup kernel for one block |
tile_lu_factor! | partial, within each tile | tiled driver over lu_base! |
The unpivoted routines require a matrix that needs no pivoting, e.g. a diagonally dominant one. A zero pivot throws ZeroPivotException; small pivots are not checked.
The mixed-precision method of lu_recursive! for FullMixedPrec is under Mixed-precision LAPACK.
Pivoted LU (LAPACK style)
NextLA.CompletePivoting — Type
CompletePivoting()Pivoting strategy for LU with row and column interchanges, giving A = P * L * U * Q with P and Q permutation matrices. It has no fields. Pass it to lu!(A::NextLAMatrix, CompletePivoting()) or to getc2!; both return a NextLA.LU, which holds the row pivots in ipiv and the column pivots in jpiv.
LinearAlgebra.lu! — Method
Computes an LU factorization of a general M-by-N matrix A using partial pivoting with row exchanges. The factorization has form A = P * L * U where P is a permutation matrix, L is lower triangula with unit diagonal elements (lower trapezoidal if m > n), and U is upper triangular (upper trapezoidal if m < n)
This is the recursive form of the algorithm.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 1024); b = rand(1024);
julia> F = lu!(NextLAMatrix{Float64}(copy(A)));
julia> A * (F \ b) ≈ b
trueNextLA.geru! — Method
geru!(alpha::T, x::AbstractVector{T}, y::AbstractVector{T}, A::AbstractMatrix{T})Performs operation A = alpha * x * y^T + A, where alpha is a scalar, x is an m element vector, y is an n element vector, and A is an m-by-n matrix
Examples
julia> A = rand(1024, 1024); x = rand(1024); y = rand(1024);
julia> B = copy(A); geru!(2.0, x, y, B);
julia> B ≈ A + 2.0 * x * transpose(y)
trueNextLA.getc2! — Method
getc2!(A::NextLAMatrix{T}, ::CompletePivoting; check::Bool = true, allowsingular::Bool = false) where {T<:BlasFloat} -> NextLA.LUComputes an LU factorization with complete pivoting of the n-by-n matrix A. The factorization has the form A = P * L * U * Q, where P and Q are permutation matrices, L is lower triangular with unit diagonal elements and U is upper triangular. It returns a NextLA.LU holding the factors, the row pivots ipiv and the column pivots jpiv. A pivot smaller than smin throws SingularException, unless check = false or allowsingular = true.
Arguments
- 'A' : matrix, dimension (n,n)
- on entry, the n-by-n matrix A to be factored
- on exit, the factors L and U from the factorization A = P * L * U * Q; the unit diagonal elements of L are not stored.
- If U(k,k) appears to be less than smin, U(k,k) is given the value smin, i.e, given a nonsingular pertubed system
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 1024);
julia> F = getc2!(NextLAMatrix{Float64}(copy(A)), CompletePivoting());
julia> p = LinearAlgebra.LU(F.factors, F.ipiv, 0).p; q = LinearAlgebra.LU(F.factors, F.jpiv, 0).p;
julia> UnitLowerTriangular(F.factors) * UpperTriangular(F.factors) ≈ A[p, q]
trueNextLA.getrf2! — Method
getrf2!(A::AbstractMatrix{T}, ipiv::AbstractVector{Int}, info::Int) -> (A, ipiv, info)Computes an LU factorization of a general M-by-N matrix A using partial pivoting with row exchanges. The factorization has form A = P * L * U where P is a permutation matrix, L is lower triangula with unit diagonal elements (lower trapezoidal if m > n), and U is upper triangular (upper trapezoidal if m < n)
This is the recursive form of the algorithm. T must be a BLAS float: Float32, Float64, ComplexF32 or ComplexF64. The call returns (A, ipiv, info).
Arguments
'A' : matrix, dimension (m,n)
- On entry, the m-by-n matrix to be factored
- On exit, the factors L and U from the factorization A = P * L * U; the unit diagonal elements of L are not stored
'ipiv' : dimension (min(m,n))
- the pivot indicies; for 1 <= i <= min(m,n), row i of the matrix was interchanged with row ipiv[i]
'info' : an
Int; the value passed in is ignored. The status comes back as the third element of the returned tuple:- =0: successful exit
- <0: if info = -i, the i-th argument had an illegal value
0: if info = i, U[i,i] is exactly zero. The factorization has been completed, but the factor U is exactly singular and division by zero will occur if it is used to solve a system of equations
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 1024); B = copy(A); ipiv = zeros(Int, 1024);
julia> _, _, info = getrf2!(B, ipiv, 0);
julia> F = LinearAlgebra.LU(B, ipiv, info); # wrap the LAPACK-style output
julia> info == 0 && F.L * F.U ≈ A[F.p, :]
trueNextLA.laswp — Method
laswp(A::AbstractMatrix, first::Integer, last::Integer, ipiv::AbstractVector{Int}, incx::Integer) -> nothingApplies the row interchanges in ipiv to A, in place, as LAPACK's laswp does: for each i from first to last, row i of A is swapped with row ipiv[i], over all columns.
Arguments
A: the matrix whose rows are swapped, in place.first,last: the first and last entries ofipivto apply.ipiv: the pivot indices, asgetrf2!returns them.incx:1applies the interchanges in order, fromfirsttolast;-1applies them in reverse order, fromlasttofirst;0does nothing. The code always steps throughipivone entry at a time, so use1or-1.
Returns nothing.
Recursive and tiled LU
NextLA.lu_recursive! — Function
lu_recursive!(A::AbstractMatrix, block_size::Integer=LU_BLOCK_SIZE) -> ARecursive unpivoted LU of a square matrix, in place: A = L * U with L unit lower triangular and U upper triangular, packed into A. The recursion splits n at the largest power of two below it (at n ÷ 2 when n is a power of two) and factors blocks of at most block_size rows without pivoting, keeping the bulk of operations in A's precision. lu_recursive_nopiv! takes a matrix of any shape.
Unpivoted, leaves included, so the matrix must need no pivoting – a diagonally dominant one, for instance. A zero pivot throws ZeroPivotException; a small one is not checked.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 1024) + 1024I; # diagonally dominant: needs no pivoting
julia> F = lu_recursive!(copy(A), 256);
julia> UnitLowerTriangular(F) * UpperTriangular(F) ≈ A
trueNextLA.lu_recursive_nopiv! — Function
lu_recursive_nopiv!(A::AbstractMatrix, block_size::Integer=LU_BLOCK_SIZE) -> ARecursive unpivoted LU of a matrix of any shape, in place: A = L * U with L unit lower trapezoidal and U upper trapezoidal, packed into A.
Splits at half the shorter side, factors the left panel over the full height, solves for the U block to its right and updates the trailing block before recurring into it. lu_recursive! is the square-only counterpart; prefer it when A is square, and this when it is not.
Unpivoted, leaves included, so the matrix must need no pivoting – a diagonally dominant one, for instance. A zero pivot throws ZeroPivotException; a small one is not checked.
Examples
julia> using LinearAlgebra
julia> A = rand(2048, 1024); A[1:1024, :] += 2048I; # tall, needs no pivoting
julia> F = lu_recursive_nopiv!(copy(A), 256);
julia> L = tril(F, -1) + Matrix(I, 2048, 1024); U = triu(F[1:1024, :]);
julia> L * U ≈ A
trueNextLA.lu_base! — Method
lu_base!(A) -> (A, p, info)LU with partial pivoting in one work-group, in place: L (unit diagonal) and U are packed into A, and p is the row permutation as a vector, A0[p, :] == L * U. As LAPACK's getrf, info is 0 or the first k with a zero U[k, k], and nothing is thrown. On a GPU n must be at most 32.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 1024);
julia> F, p, info = lu_base!(copy(A));
julia> info == 0 && UnitLowerTriangular(F) * UpperTriangular(F) ≈ A[p, :]
trueNextLA.tile_lu_factor! — Method
tile_lu_factor!(A, n) -> (A, p, info)Tiled LU with partial pivoting within each n x n tile, in place, on a GPU: L (unit diagonal) and U are packed into A, and p is the row permutation as a vector, A0[p, :] == L * U. A tile's row swaps reach the rest of its tile row as a gather of those rows. As LAPACK's getrf, info is 0 or the first k with a zero U[k, k], and nothing is thrown. The tile n must divide both dimensions and be at most 32.
It never pivots across tiles, so it is not stable on general matrices: about 1 random matrix in 1000 hits a tiny or zero pivot, giving a wrong result or info > 0 with no warning. Use it for diagonally dominant matrices, or ones that need only in-tile pivoting.
Examples
GPU only (it throws ArgumentError on the CPU backend), so this example is not run as a doctest:
julia> using NextLA, CUDA, LinearAlgebra
julia> A = rand(1024, 1024) + 1024I; # diagonally dominant
julia> F, p, info = tile_lu_factor!(CuArray(A), 32);
julia> F = Array(F);
julia> info == 0 && UnitLowerTriangular(F) * UpperTriangular(F) ≈ A[p, :]
trueUsage examples
Each example stands on its own and ends with a check that prints true.
Pivoted LU of a NextLAMatrix, then a solve with several right-hand sides through the returned LinearAlgebra.LU object:
using NextLA, LinearAlgebra
A = rand(2048, 2048)
B = rand(2048, 16)
F = lu!(NextLAMatrix{Float64}(copy(A))) # partial pivoting, computed by getrf2!
X = F \ B
A * X ≈ B
# output
truegetrf2! called directly returns LAPACK's pivot vector; laswp applies those row interchanges to the right-hand side before the two triangular solves:
using NextLA, LinearAlgebra
n = 2048
A = rand(n, n)
b = rand(n, 1)
F = copy(A)
ipiv = zeros(Int, n)
_, _, info = getrf2!(F, ipiv, 0)
x = copy(b)
laswp(x, 1, n, ipiv, 1) # x := P * b
trsm!('L', 'L', 'N', 'U', 1.0, F, x) # solve L y = P b (unit diagonal)
trsm!('L', 'U', 'N', 'N', 1.0, F, x) # solve U x = y
info == 0 && A * x ≈ b
# output
trueComplete pivoting with getc2!, which permutes both rows and columns, A[p, q] = L * U:
using NextLA, LinearAlgebra
A = rand(1024, 1024)
F = getc2!(NextLAMatrix{Float64}(copy(A)), CompletePivoting())
p = LinearAlgebra.LU(F.factors, F.ipiv, 0).p # row permutation from ipiv
q = LinearAlgebra.LU(F.factors, F.jpiv, 0).p # column permutation from jpiv
UnitLowerTriangular(F.factors) * UpperTriangular(F.factors) ≈ A[p, q]
# output
trueUnpivoted recursive LU of a diagonally dominant matrix, with leaves of at most 512 rows, used to solve a system:
using NextLA, LinearAlgebra
n = 4096
A = rand(n, n) + n * I # diagonally dominant: no pivoting needed
F = lu_recursive!(copy(A), 512)
b = rand(n, 1)
x = copy(b)
trsm!('L', 'L', 'N', 'U', 1.0, F, x) # solve L y = b (unit diagonal)
trsm!('L', 'U', 'N', 'N', 1.0, F, x) # solve U x = y
A * x ≈ b
# output
truelu_recursive_nopiv! factors a tall panel, here 4096 by 1024; L is unit lower trapezoidal and U square:
using NextLA, LinearAlgebra
m, n = 4096, 1024
A = rand(m, n)
A[1:n, :] += m * I # needs no pivoting
F = lu_recursive_nopiv!(copy(A), 256)
L = tril(F, -1) + Matrix(I, m, n)
U = triu(F[1:n, :])
L * U ≈ A
# output
trueOn a GPU the same calls take device arrays. tile_lu_factor! runs on a GPU only, with tiles of at most 32. This block is not run when the documentation is built:
using NextLA, CUDA, LinearAlgebra
n = 4096
A = rand(n, n) + n * I
dF = lu_recursive!(CuArray(A), 1024) # the leaves go to cuSOLVER, unpivoted
F = Array(dF)
UnitLowerTriangular(F) * UpperTriangular(F) ≈ A
B = rand(2048, 2048)
dG, p, info = tile_lu_factor!(CuArray(B), 32) # pivoting within each 32×32 tile
G = Array(dG)
info == 0 && UnitLowerTriangular(G) * UpperTriangular(G) ≈ B[p, :]