Solvers API Reference

Linear solver functionality in SafePETSc.

Type

SafePETSc.KSPType
KSP{T,Prefix}

A PETSc KSP (Krylov Subspace) linear solver with element type T and prefix type Prefix, managed by SafePETSc's reference counting system.

KSP{T,Prefix} is actually a type alias for DRef{_KSP{T,Prefix}}, meaning solvers are automatically tracked across MPI ranks and destroyed collectively when all ranks release their references.

KSP objects can be reused for multiple linear systems with the same matrix, avoiding the cost of repeated factorization or preconditioner setup.

Construction

See the KSP constructor for creating solver instances.

Usage

KSP 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 = KSP(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 KSP constructor

Initialization

SafePETSc.InitFunction
Init() -> nothing

MPI Collective

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).

Sets up PETSc options for prefix types (MPIDENSE and MPIAIJ).

SafePETSc.InitializedFunction
Initialized() -> Bool

MPI Non-Collective

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)

MPI Collective

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)                       # KSP matrix dimensions