Solvers API Reference
Linear solver functionality in SafePETSc.
Type
SafePETSc.Solver — TypeSolver{T}A PETSc KSP (Krylov Subspace) linear solver with element type T, managed by SafePETSc's reference counting system.
Solver{T} is actually a type alias for DRef{_KSP{T}}, meaning solvers are automatically tracked across MPI ranks and destroyed collectively when all ranks release their references.
Solvers can be reused for multiple linear systems with the same matrix, avoiding the cost of repeated factorization or preconditioner setup.
Construction
See the Solver constructor for creating solver instances.
Usage
Solvers can be used implicitly via the backslash operator, or explicitly for reuse:
# Implicit (creates and destroys solver internally)
x = A \ b
# Explicit (reuse solver for multiple solves)
ksp = Solver(A)
x1 = similar(b)
x2 = similar(b)
LinearAlgebra.ldiv!(ksp, x1, b1) # First solve
LinearAlgebra.ldiv!(ksp, x2, b2) # Second solve with same matrixInitialization
SafePETSc.Init — FunctionInit() -> nothingEnsure MPI and PETSc are initialized in the recommended order (MPI first, then PETSc). Safe to call multiple times. Does not register custom finalizers; rely on library defaults for shutdown (MPI.jl finalizes at exit; PETSc may remain initialized).
SafePETSc.Initialized — FunctionInitialized() -> BoolReturn true if both MPI and PETSc are initialized. This is a simple conjunction of MPI.Initialized() and PETSc.initialized for the active PETSc library.
SafePETSc.petsc_options_insert_string — Functionpetsc_options_insert_string(options_string::String)Insert command-line style options into PETSc's global options database.
Example: petsc_options_insert_string("-dense_mat_type mpidense")
This sets options that will be used by PETSc objects created with the corresponding prefix. PETSc must already be initialized by the caller.
Linear Solves
Direct Solve
# Vector RHS
x = A \ b # Solve Ax = b
x = A' \ b # Solve A^T x = b
# Matrix RHS (must be dense)
X = A \ B # Solve AX = B
X = A' \ B # Solve A^T X = BIn-Place Solve
# Vector RHS
LinearAlgebra.ldiv!(x, A, b) # x = A \ b
LinearAlgebra.ldiv!(ksp, x, b) # Using reusable solver
# Matrix RHS (must be dense)
LinearAlgebra.ldiv!(X, A, B) # X = A \ B
LinearAlgebra.ldiv!(ksp, X, B) # Using reusable solverRight Division
# Vector
x_adj = b' / A # Solve x^T A = b^T
# Matrix (must be dense)
X = B / A # Solve XA = B
X = B / A' # Solve XA^T = BProperties
m, n = size(ksp) # Solver matrix dimensions