Dense matrices

The dense part of NextLA, loaded with using NextLA, works on ordinary matrices that hold one element type: Matrix on the CPU, or a GPU array such as CuArray. It provides BLAS-style routines (multiply, triangular solve and multiply, rank-k update) and LAPACK-style factorizations (Cholesky, LU, QR), each written once in Julia and run on every backend. This page explains which routine to choose and how they work; the API reference documents each one.

Choosing a routine

TaskRoutinesUse it when
Multiply, C = αAB + βCgemm!one product, any backend
gemmEx!inputs and output in different types, such as Float16 inputs with Float32 accumulation
gemm_batched!many small products of the same shape
Triangular solve or multiplytrsm!, trmm!the BLAS calls, any backend
unified_rectrxm!the recursive version of both, one call for solve ('S') or multiply ('M')
trsm_batched!many small triangular solves
Rank-k update, C = αAAᵀ + βCsyrk!one update, any backend
recsyrk!the recursive version, also for Float16
syrk_batched!many small updates
Cholesky, A = LLᵀpotrf_recursive!large matrices, CPU or GPU
potrf!the LAPACK-style call; the recursion uses it for its leaves
cholesky_blocked!blocked Cholesky on a GPU
potrf_batched!many small matrices
LU, PA = LUgetrf2!general matrices, partial (row) pivoting
getc2!complete pivoting, rows and columns
lu_recursive!, lu_recursive_nopiv!large matrices that need no pivoting
tile_lu_factor!on a GPU, pivoting within n × n tiles (n ≤ 32)
QR, A = QRNextLA.geqrt!, NextLA.unmqr!a QR of one tile, and applying Q
NextLA.tsqrt!, NextLA.ttqrt! and their *mqr! partnerstile QR, one tile at a time (see below)

In-place routines (names ending in !) overwrite their inputs and follow the BLAS/LAPACK argument conventions: the character flags side ('L' or 'R'), uplo ('L' or 'U'), trans ('N', 'T' or 'C') and diag ('N' or 'U'). The flags are accepted in either case ('l' is the same as 'L'). Which backend runs which routine, and with which element types, is on Backends and GPUs.

How the recursive routines work

potrf_recursive!, lu_recursive!, unified_rectrxm! and recsyrk! all use the same idea (recgemm! too, on a mixed-precision matrix; on a plain matrix it is one GEMM call). The matrix is split into a 2 × 2 block partition,

┌─────┬─────┐
│ A11 │ A12 │
├─────┼─────┤
│ A21 │ A22 │
└─────┴─────┘

the routine calls itself on the diagonal blocks A11 and A22, and the off-diagonal block is handled by large matrix products (GEMM). For Cholesky, for example: factor A11 = L11 L11ᵀ, solve for L21 with a triangular solve, update A22 -= L21 L21ᵀ, then factor A22.

  • Where it splits. At n ÷ 2 when n is a power of two, otherwise at the largest power of two below n, so most blocks have power-of-two sizes.
  • Where it stops. When a block is small enough it goes to a direct method, the leaf: potrf! for blocks of at most 4096 rows in potrf_recursive!, an unpivoted LU of at most 2048 rows in lu_recursive!, and a direct kernel of at most 256 rows (solve) or 16 rows (multiply) in unified_rectrxm! (on CUDA, cuBLAS at both leaves, with the multiply leaf at 256 rows). The leaf size of the two factorizations is their second argument.
  • Why. Almost all the work ends up in large GEMMs, which run near peak speed on CPUs and GPUs, and the same code runs on every backend.

The leaf size changes the speed, not the result:

using NextLA          # for potrf_recursive!
using LinearAlgebra   # for I

n = 2048
A = rand(n, n)
S = A * A' + n * I                     # symmetric positive definite

F1 = potrf_recursive!(copy(S))         # default leaf: one potrf! call
F2 = potrf_recursive!(copy(S), 256)    # leaves of at most 256 rows
F1 ≈ F2

# output

true

Pivoting

RoutinePivotingMatrices it suits
getrf2!, lu!(NextLAMatrix(A))partial: rowsany square or rectangular matrix
getc2!complete: rows and columnssmall matrices where stability matters most
tile_lu_factor!partial, within each n × n tile (n ≤ 32)GPU only; diagonally dominant matrices, or ones that need only in-tile pivoting
lu_base!partial, over the whole matrixany size on the CPU; n ≤ 32 on a GPU
lu_recursive!, lu_recursive_nopiv!nonematrices that need no pivoting, such as diagonally dominant ones

Without pivoting, a zero pivot throws ZeroPivotException, and a small one is not checked, so use the unpivoted routines only when the matrix is known to be safe.

Tile QR

The QR routines follow the tile algorithms of PLASMA: the matrix is cut into square tiles, and each step works on one or two tiles.

  1. NextLA.geqrt! computes the QR factorization of a diagonal tile, storing R in its upper triangle and the Householder reflectors below it (compact WY form, with the factor T).
  2. NextLA.tsqrt! (triangle on square) or NextLA.ttqrt! (triangle on triangle) eliminates a tile below the diagonal against the R above it.
  3. NextLA.unmqr!, NextLA.tsmqr! and NextLA.ttmqr! apply the same transformations to the tiles to the right, or to a right-hand side.

These routines are not exported. A least-squares solve with geqrt! and unmqr!, and examples of the tile kernels, are on the QR API page.

Batched routines

gemm_batched!, trsm_batched!, syrk_batched! and potrf_batched! apply one operation to many matrices of the same size at once. The batch is given as a 3-D array, with the matrices along the third dimension, or as a vector of matrices (potrf_batched! takes a vector only on CUDA). On the CPU the batch is a loop; on a GPU it is one vendor call where the vendor has one. On CUDA, a vector batch whose matrices differ in size or stride is done one matrix at a time. Which layouts each GPU accepts is on Backends and GPUs.

using NextLA          # for gemm_batched!

A = rand(256, 256, 32)                 # 32 matrices of 256 × 256
B = rand(256, 256, 32)
C = zeros(256, 256, 32)

# C[:, :, k] = A[:, :, k] * B[:, :, k] for every k
gemm_batched!('N', 'N', 1.0, A, B, 0.0, C)
all(C[:, :, k] ≈ A[:, :, k] * B[:, :, k] for k in 1:32)

# output

true