Solvers API Reference

Linear solver functionality in SafePETSc.

Type

SafePETSc.SolverType
Solver{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 matrix

See also: Mat, Vec, the Solver constructor

Initialization

SafePETSc.InitFunction
Init() -> nothing

Ensure 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.InitializedFunction
Initialized() -> Bool

Return 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_stringFunction
petsc_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 = B

In-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 solver

Right 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 = B

Properties

m, n = size(ksp)                       # Solver matrix dimensions