PetscWrap.jl
PetscWrap.jl is a parallel Julia wrapper for the (awesome) PETSc library. It can be considered as a fork from the GridapPetsc.jl and Petsc.jl projects : these two projects have extensively inspired this project, and some code has even been directly copied.
The main differences with the two aformentionned projects are:
- parallel support : you can solve linear systems on multiple core with
mpirun -n 4 julia foo.jl
; - no dependance to other Julia packages except
MPI.jl
; - possibility to switch from one PETSc "arch" to another;
- less PETSc API functions wrappers for now.
Note that the primary objective of this project is to enable the wrapper of the SLEPc library through the SlepcWrap.jl project.
How to install it
You must have installed the PETSc library on your computer and set the two following environment variables : PETSC_DIR
and PETSC_ARCH
.
At run time, PetscWrap.jl looks for the libpetsc.so
using these environment variables and "load" the library.
To install the package, use the Julia package manager:
pkg> add PetscWrap
Contribute
Any contribution(s) and/or remark(s) are welcome! If you need a function that is not wrapped yet but you don't think you are capable of contributing, post an issue with a minimum working example.
PETSc compat.
This version of PetscWrap.jl has been tested with petsc-3.13. Complex numbers are supported.
How to use it
PETSc methods wrappers share the same name as their C equivalent : for instance MatCreate
or MatSetValue
. Furthermore, an optional "higher level" API, referred to as "fancy", is exposed : for instance create_matrix
or A[i,j] = v
). Note that this second way of manipulating PETSc will evolve according the package's author needs while the first one will try to follow PETSc official API.
You will find examples of use by building the documentation: julia PetscWrap.jl/docs/make.jl
. Here is one of the examples:
A first demo
This example serves as a test since this project doesn't have a "testing" procedure yet. In this example,
the equation u'(x) = 2
with u(0) = 0
is solved on the domain [0,1]
using a backward finite
difference scheme.
In this example, PETSc classic method names are used. For more fancy names, check the fancy version.
Note that the way we achieve things in the document can be highly improved and the purpose of this example is only demonstrate some method calls to give an overview.
To run this example, execute : mpirun -n your_favorite_positive_integer julia example1.jl
Import package
using PetscWrap
Initialize PETSc. Command line arguments passed to Julia are parsed by PETSc. Alternatively, you can
also provide "command line arguments by defining them in a string, for instance
PetscInitialize("-ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always")
or by providing each argument in
separate strings : PetscInitialize(["-ksp_monitor_short", "-ksp_gmres_cgs_refinement_type", "refine_always")
PetscInitialize()
Number of mesh points and mesh step
n = 11
Δx = 1. / (n - 1)
Create a matrix and a vector
A = MatCreate()
b = VecCreate()
Set the size of the different objects, leaving PETSC to decide how to distribute. Note that we should set the number of preallocated non-zeros to increase performance.
MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n)
VecSetSizes(b, PETSC_DECIDE, n)
We can then use command-line options to set our matrix/vectors.
MatSetFromOptions(A)
VecSetFromOptions(b)
Finish the set up
MatSetUp(A)
VecSetUp(b)
Let's build the right hand side vector. We first get the range of rows of b
handled by the local processor.
As in PETSc, the rstart, rend = *GetOwnershipRange
methods indicate the first row handled by the local processor
(starting at 0), and the last row (which is rend-1
). This may be disturbing for non-regular PETSc users. Checkout
the fancy version of this example for a more Julia-like convention.
b_start, b_end = VecGetOwnershipRange(b)
Now let's build the right hand side vector. Their are various ways to do this, this is just one.
n_loc = VecGetLocalSize(b) # Note that n_loc = b_end - b_start...
VecSetValues(b, collect(b_start:b_end-1), 2 * ones(n_loc))
And here is the differentiation matrix. Rembember that PETSc.MatSetValues simply ignores negatives rows indices.
A_start, A_end = MatGetOwnershipRange(A)
for i in A_start:A_end-1
MatSetValues(A, [i], [i-1, i], [-1. 1.] / Δx, INSERT_VALUES) # MatSetValues(A, I, J, V, INSERT_VALUES)
end
Set boundary condition (only the proc handling index 0
is acting)
(b_start == 0) && VecSetValue(b, 0, 0.)
Assemble matrices
MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)
VecAssemblyBegin(b)
MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)
VecAssemblyEnd(b)
At this point, you can inspect A
or b
using a viewer (stdout by default), simply call
MatView(A)
VecView(b)
Set up the linear solver
ksp = KSPCreate()
KSPSetOperators(ksp, A, A)
KSPSetFromOptions(ksp)
KSPSetUp(ksp)
Solve the system. We first allocate the solution using the VecDuplicate
method.
x = VecDuplicate(b)
KSPSolve(ksp, b, x)
Print the solution
VecView(x)
Access the solution (this part is under development), getting a Julia array; and then restore it
array, ref = VecGetArray(x) # do something with array
@show array
VecRestoreArray(x, ref)
Free memory
MatDestroy(A)
VecDestroy(b)
VecDestroy(x)
Finalize Petsc
PetscFinalize()