Vectors API Reference
Distributed vector operations in SafePETSc.
Type
SafePETSc.Vec — TypeVec{T}A distributed PETSc vector with element type T, managed by SafePETSc's reference counting system.
Vec{T} is actually a type alias for DRef{_Vec{T}}, meaning vectors are automatically tracked across MPI ranks and destroyed collectively when all ranks release their references.
Construction
Use Vec_uniform or Vec_sum to create distributed vectors:
# Create from uniform data (same on all ranks)
v = Vec_uniform([1.0, 2.0, 3.0, 4.0])
# Create from sparse contributions (summed across ranks)
using SparseArrays
v = Vec_sum(sparsevec([1, 3], [1.0, 3.0], 4))Operations
Vectors support standard arithmetic operations via broadcasting:
y = x .+ 1.0 # Element-wise addition
y .= 2.0 .* x # In-place scaling
z = x .+ y # Vector additionMatrix-vector multiplication:
y = A * x # Matrix-vector product
LinearAlgebra.mul!(y, A, x) # In-place versionSee also: Vec_uniform, Vec_sum, Mat, zeros_like
Constructors
SafePETSc.Vec_uniform — FunctionVec_uniform(v::Vector{T}; row_partition=default_row_partition(length(v), MPI.Comm_size(MPI.COMM_WORLD)), prefix="") -> DRef{Vec{T}}Create a distributed PETSc vector from a Julia vector, asserting uniform distribution across ranks (on MPI.COMM_WORLD).
v::Vector{T}must be identical on all ranks (mpi_uniform).row_partitionis a Vector{Int} of length nranks+1 where partition[i] is the start row (1-indexed) for rank i-1.prefixis an optional string prefix for VecSetOptionsPrefix() to set vector-specific command-line options.- Returns a DRef that will destroy the PETSc Vec collectively when all ranks release their reference.
SafePETSc.Vec_sum — FunctionVec_sum(v::SparseVector{T}; row_partition=default_row_partition(length(v), MPI.Comm_size(MPI.COMM_WORLD)), prefix="", own_rank_only=false) -> DRef{Vec{T}}Create a distributed PETSc vector by summing sparse vectors across ranks (on MPI.COMM_WORLD).
v::SparseVector{T}can differ across ranks; nonzero entries are summed across all ranks.row_partitionis a Vector{Int} of length nranks+1 where partition[i] is the start row (1-indexed) for rank i-1.prefixis an optional string prefix for VecSetOptionsPrefix() to set vector-specific command-line options.own_rank_only::Bool(default=false): if true, asserts that all nonzero indices fall within this rank's row partition.- Returns a DRef that will destroy the PETSc Vec collectively when all ranks release their reference.
Uses VecSetValues with ADD_VALUES mode to sum contributions from all ranks.
Helper Constructors
SafePETSc.zeros_like — Functionzeros_like(x::Vec{T}; T2::Type{S}=T, prefix::String=x.obj.prefix) -> Vec{S}Create a new distributed vector with the same size and partition as x, filled with zeros.
Arguments
x: Template vector to match size and partitionT2: Element type of the result (defaults to same type asx)prefix: PETSc options prefix (defaults to same prefix asx)
See also: ones_like, fill_like, Vec_uniform
SafePETSc.ones_like — Functionones_like(x::Vec{T}; T2::Type{S}=T, prefix::String=x.obj.prefix) -> Vec{S}Create a new distributed vector with the same size and partition as x, filled with ones.
Arguments
x: Template vector to match size and partitionT2: Element type of the result (defaults to same type asx)prefix: PETSc options prefix (defaults to same prefix asx)
See also: zeros_like, fill_like, Vec_uniform
SafePETSc.fill_like — Functionfill_like(x::Vec{T}, val; T2::Type{S}=typeof(val), prefix::String=x.obj.prefix) -> Vec{S}Create a new distributed vector with the same size and partition as x, filled with val.
Arguments
x: Template vector to match size and partitionval: Value to fill the vector withT2: Element type of the result (defaults to type ofval)prefix: PETSc options prefix (defaults to same prefix asx)
Example
y = fill_like(x, 3.14) # Create a vector like x, filled with 3.14See also: zeros_like, ones_like, Vec_uniform
Partitioning
SafePETSc.default_row_partition — Functiondefault_row_partition(n::Int, nranks::Int) -> Vector{Int}Create a default row partition that divides n rows equally among nranks.
Returns a Vector{Int} of length nranks+1 where partition[i] is the start row (1-indexed) for rank i-1.
Operations
Arithmetic
Vectors support standard Julia arithmetic operations via broadcasting:
y = x .+ 1.0 # Element-wise addition
y = 2.0 .* x # Scaling
z = x .+ y # Vector addition
y .= x .+ 1.0 # In-place operationStandard operators are also overloaded:
z = x + y # Addition
z = x - y # Subtraction
z = -x # NegationLinear Algebra
y = A * x # Matrix-vector multiplication
LinearAlgebra.mul!(y, A, x) # In-place multiplication
w = v' * A # Adjoint-vector times matrix
LinearAlgebra.mul!(w, v', A) # In-placeProperties
T = eltype(v) # Element type
n = length(v) # Vector length
n = size(v, 1) # Size in dimension 1