Utilities
This page documents utility functions for initialization, options handling, system information, and code auditing.
Initialization
The initialization functions manage PETSc's lifecycle, including MPI initialization and cleanup.
PETSc.check_petsc_wrappers_version — Function
check_petsc_wrappers_version(petsclib=nothing)Load the generated petsc_wrappers_version.jl (if present) and compare the declared wrapper version PETSC_WRAPPERS_VERSION with the installed PETSc version obtained from LibPETSc.PetscGetVersionNumber for petsclib.
Arguments
petsclib: optionalPetscLibTypeor path string. Ifnothing, the first availablePETSc.petsclibs[1]is used.
Returns a named tuple: (:wrappers_version, :installed_version, :match). match is true when versions are equal, false when they differ, and nothing if either side could not be determined.
PETSc.finalize — Method
finalize(petsclib)
Finalize the petsclib, if no petsclib is given then all PETSc.petsclibs will be finalized.
External Links
- PETSc Manual:
Sys/PetscFinalize
PETSc.finalized — Method
PETSc.initialize — Method
initialize([petsclib]; log_view = false, options = String[])Initialize the petsclib. If no petsclib is given, all PETSc.petsclibs will be initialized.
Additionally:
This will initialize MPI if it has not already been initialized.
It will disable the PETSc signal handler (via
Sys/PetscPopSignalHandlerAdd an
atexithook to callPETSc.finalize.
Arguments
log_view::Bool = false: Enable PETSc's-log_viewperformance logging. When enabled, PETSc will output performance statistics at finalization.options::Vector{String} = String[]: Additional PETSc command-line options. These are passed via thePETSC_OPTIONSenvironment variable.
Examples
# Basic initialization
PETSc.initialize(petsclib)
# Enable performance logging to stdout
PETSc.initialize(petsclib; log_view = true)
# Write log to a file
PETSc.initialize(petsclib; log_view = true, options = [":logfile.txt"])
# Enable memory logging
PETSc.initialize(petsclib; log_view = true, options = [":logfile.txt", "-log_view_memory"])
# Pass custom PETSc options without logging
PETSc.initialize(petsclib; options = ["-malloc_debug", "-on_error_abort"])External Links
- PETSc Manual:
Sys/PetscInitializeNoArguments
PETSc.initialized — Method
initialized(petsclib)
Check if petsclib is initialized
External Links
- PETSc Manual:
Sys/PetscInitialized
PETSc.inttype — Method
inttype(petsclib::PetscLibType)return the int type for the associated petsclib
PETSc.scalartype — Method
scalartype(petsclib::PetscLibType)return the scalar type for the associated petsclib
PETSc.set_petsclib — Method
set_petsclib(library_path::String; PetscScalar=Float64, PetscInt=Int64)Create a custom PETSc library instance from a user-specified shared library path.
This function allows you to use a custom-compiled PETSc library instead of the pre-built libraries provided by PETSc_jll. The custom library must be:
- Compiled as a shared/dynamic library (not static)
- Built with the correct scalar type (real vs complex) and integer size
- Compatible with the same MPI installation that MPI.jl is using
Arguments
library_path::String: Path to the custom PETSc shared library (e.g., "/path/to/libpetsc.so")PetscScalar::Type: Scalar type used by the library. Options:Float64,Float32,Complex{Float64}, orComplex{Float32}. Default:Float64PetscInt::Type: Integer type used by the library. Options:Int32orInt64. Default:Int64
Returns
- A
PetscLibTypeinstance that can be used withinitialize(),finalize(), and all other PETSc.jl functions
Examples
using PETSc
# For a custom double-precision real PETSc library with 64-bit indices
petsclib = PETSc.set_petsclib("/path/to/custom/libpetsc.so";
PetscScalar=Float64, PetscInt=Int64)
# For a single-precision complex library with 32-bit indices
petsclib = PETSc.set_petsclib("/opt/petsc/lib/libpetsc.so";
PetscScalar=Complex{Float32}, PetscInt=Int32)
# Initialize and use the custom library
PETSc.initialize(petsclib)
# ... your code ...
PETSc.finalize(petsclib)See Also
initialize: Initialize a PETSc libraryfinalize: Finalize a PETSc library
Options
PETSc uses an options database to configure solvers and other objects at runtime. These functions provide access to the options system.
PETSc.Options — Method
Options(petsclib; kwargs...)Create a PETSc options database for the given petsclib.
Keyword arguments are converted to PETSc options:
- Options with value
nothingortrueare set without a value (flags) - Options with value
falseare not set - Other values are converted to strings
Examples
julia> using PETSc
julia> petsclib = PETSc.petsclibs[1];
julia> PETSc.initialize(petsclib)
julia> opt = PETSc.Options(
petsclib,
ksp_monitor = nothing,
ksp_view = true,
pc_type = "mg",
pc_mg_levels = 1,
false_opt = false,
)
#PETSc Option Table entries:
-ksp_monitor
-ksp_view
-pc_mg_levels 1
-pc_type mg
#End of PETSc Option Table entries
julia> opt["ksp_monitor"]
""
julia> opt["pc_type"]
"mg"
julia> opt["pc_type"] = "ilu"
"ilu"
julia> opt["pc_type"]
"ilu"
julia> opt["false_opt"]
ERROR: KeyError: key "bad_key" not found
julia> opt["bad_key"]
ERROR: KeyError: key "bad_key" not foundExternal Links
- PETSc Manual:
Sys/PetscOptionsCreate
PETSc.parse_options — Method
parse_options(args::Vector{String})Parse the args vector into a NamedTuple that can be used as the options for the PETSc solvers.
julia --project file.jl -ksp_monitor -pc_type mg -ksp_view -da_refine=1PETSc.typedget — Method
typedget(opt::NamedTuple, key::Symbol, default::T)Parse opt similar to Base.get but ensures that the returned value is the same type as the default value. When T <: NTuple keys that result in a single value will be filled into an NTuple of the same length as T; in the case of strings it is parsed using Base.split with comma delimiter
Examples
julia> opt = (tup = (1, 2, 3), string_tup = "1,2,3", string_int = "4", int = 4)
(tup = (1, 2, 3), string_tup = "1,2,3", string_int = "4", int = 4)
julia> typedget(opt, :int, 7)
4
julia> typedget(opt, :bad_key, 7)
7
julia> typedget(opt, :tup, (1, 1, 1))
(1, 2, 3)
julia> typedget(opt, :string_tup, (1, 1, 1))
tokens = SubString{String}["1", "2", "3"]
(1, 2, 3)
julia> typedget(opt, :string_int, (1, 1, 1))
tokens = SubString{String}["4"]
(4, 4, 4)
julia> typedget(opt, :int, (1, 1, 1))
(4, 4, 4)
julia> typedget(opt, :int, (1., 1., 1.))
(4.0, 4.0, 4.0)System Utilities
General system-level utilities for working with PETSc objects.
PETSc.getcomm — Method
comm = function getcomm(
obj::Union{
PetscVec{PetscLib},
PetscMat{PetscLib},
PetscKSP{PetscLib},
#PetscSNES{PetscLib},
#PetscDM{PetscLib},
},
) where {PetscLib}Gets the MPI communicator for any of the objects above
Code Auditing
The audit utilities help identify potential memory leaks by tracking PETSc object creation and destruction.
PETSc.audit_petsc_file — Method
audit_petsc_file(path::AbstractString)Scan a PETSc.jl source file and report PETSc object creations and destroys.
This utility reads path, looks for common PETSc object creation patterns (KSP(...), SNES(...), DMDA(...), DMStag(...), as well as VecCreate..., VecDuplicate..., Vec...WithArray(...) and corresponding Mat... creators via either PETSc. or LibPETSc.), plus common DM/Vec/Mat allocators like DMGlobalVec, DMLocalVec, DMGetCoordinateDM, DMStagCreateCompatibleDMStag, and DMCreateMatrix; and for explicit destroy(x) calls (optionally prefixed with PETSc.).
It prints three sections:
- CREATION statements: line numbers and variables assigned to created objects
- DESTROY calls: line numbers and variables destroyed
- POSSIBLY UNDESTROYED objects: variables that appear in creations but have no matching destroy call
- FINALIZE calls: presence of
PETSc.finalize(petsclib); suggests adding it if missing
- FINALIZE calls: presence of
Notes:
- Heuristic only: matches common constructors and
LibPETSccreation routines; it does not follow control flow or scopes. - Creations without assignment cannot be cross-checked against destroys.
- Objects produced via helpers (e.g.,
similar(...)) are not detected unless they use known creation patterns.
Returns nothing.