Skip to content

Commit bb1d1fc

Browse files
authored
Merge pull request #215 from JuliaParallel/bk/wrapping_tests
Wrapping the (nearly) full PETSc library
2 parents 4a8fbbb + d31c437 commit bb1d1fc

200 files changed

Lines changed: 254498 additions & 6982 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
os:
2828
- macOS-15-intel # with intel processors
2929
- ubuntu-latest
30+
# - windows-latest # Disabled: Windows PETSc_jll built without MPI, requires WSL for testing
3031
arch:
3132
- x64
3233
include:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ deps/petsc-*.tar.gz
77
*.jl.mem
88
docs/build/
99
docs/site/
10-
Manifest.toml
10+
Manifest.toml
11+
wrapping/.CondaPkg

Project.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
name = "PETSc"
22
uuid = "ace2c81b-2b5f-4b1e-a30d-d662738edfe0"
3-
authors = ["Simon Byrne <simonbyrne@gmail.com>"]
4-
version = "0.3.1"
3+
authors = ["Boris Kaus <kaus@uni-mainz.de>", "Viral B. Shah <virals@gmail.com>", "Erik Schnetter <eschnetter@perimeterinstitute.ca>", "Jeremy E. Kozdon <jeremy@kozdon.net>", "Simon Byrne <simonbyrne@gmail.com>"]
4+
version = "0.4.0"
55

66
[deps]
7+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
78
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
89
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"
1011
MPIPreferences = "3da0fdf6-3ccc-4f1b-acd9-58baa6c99267"
12+
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1113
PETSc_jll = "8fa3689e-f0b9-5420-9873-adf6ccf46f2d"
1214
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1315
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
16+
SparseDiffTools = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
17+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
18+
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
1419

1520
[compat]
1621
MPI = "0.20"
@@ -20,12 +25,14 @@ SparseArrays = "1.10"
2025
julia = "1.10"
2126

2227
[extras]
28+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
2329
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
2430
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
2531
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
32+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2633
SparseDiffTools = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
2734
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2835
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
2936

3037
[targets]
31-
test = ["ForwardDiff", "UnicodePlots", "Test", "Plots", "SparseDiffTools", "Printf"]
38+
test = ["ForwardDiff", "UnicodePlots", "Test", "Plots", "SparseDiffTools", "Printf", "Random", "CairoMakie"]

README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,52 @@
33
[![Build Status](https://github.com/JuliaParallel/PETSc.jl/workflows/CI/badge.svg)](https://github.com/JuliaParallel/PETSc.jl/actions/workflows/ci.yml)
44
[![doc dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://juliaparallel.github.io/PETSc.jl/dev/)
55

6-
This package provides a low level interface for [PETSc](https://www.mcs.anl.gov/petsc/) and allows combining julia features (such as automatic differentiation) with the PETSc infrastructure and nonlinear solvers.
6+
`PETSc.jl` provides an interface to the Portable, Extensible Toolkit for Scientific Computation ([PETSc](https://petsc.org)) library, allowing the combination of Julia features (such as automatic differentiation) with the PETSc's infrastructure, including linear, nonlinear, and optimization solvers, timesteppers, domain management (DM), and more, in a distributed-memory (MPI) environment.
77

8-
## Installation
8+
This package comprises two main components:
9+
10+
1. An automatically generated, low-level interface for large parts of the PETSc API (see `PETSc.LibPETSc`).
11+
2. A curated, high-level, more Julianic interface for selected functionality.
12+
13+
The low-level interface covers nearly the entire PETSc API, but may be awkward to work with and likely requires previous experience with PETSc to use effectively. The high level interface is designed to be more familiar and convenient for Julia users, and allows, for example, to set matrix entries with `A[1,2] = 3.0`, rather than having to call `LibPETSc.MatSetValue`. It, however, exposes only a small portion of the functionality of the underlying library.
914

15+
## Installation
1016
This package can be added with the julia command:
1117
```julia
12-
]add PETSc
18+
julia>]add PETSc
1319
```
1420
The installation can be tested with
1521
```julia
16-
]test PETSc
22+
julia>]test PETSc
1723
```
1824

1925
## PETSc binaries
2026

21-
By default, the package uses a pre-built binary of (see [PETSc_jll](https://github.com/JuliaBinaryWrappers/PETSc_jll.jl)) along with a default installation of `MPI.jl`, so you don't have to install it on your machine.
27+
By default, the package uses a pre-built binary of PETSc (see [PETSc_jll](https://github.com/JuliaBinaryWrappers/PETSc_jll.jl)) along with a default installation of `MPI.jl`, so you don't have to install it on your machine.
2228

23-
If you want to use the package with custom builds of the PETSc library, this can be done by specifying the environment variable `JULIA_PETSC_LIBRARY`. This is a colon separated list of paths to custom builds of PETSc; the reason for using multiple builds is to enable single, double, and complex numbers in the same julia session. These should be built against the same version of MPI as used with `MPI.jl`
29+
If you want to use the package with custom builds of the PETSc library, this can be done by using the function `set_petsclib` which requires you to point to the correct dynamic library (which should be compatible with the MPI version used by `MPI.jl`)
2430

25-
After setting the variable you should
2631
```julia
27-
]build PETSc
28-
```
29-
and the library will be persistently set until the next time the build command is issued.
32+
using PETSc
3033

31-
To see the currently set library use
34+
# Create custom library instance
35+
petsclib = set_petsclib("/path/to/custom/libpetsc.so";
36+
PetscScalar=Float64, PetscInt=Int64)
37+
# Use it like any precompiled library
38+
PETSc.initialize(petsclib, log_view=true)
39+
# ... your code ...
40+
PETSc.finalize(petsclib)
41+
```
42+
To get an overview of available precompiled libraries:
3243
```julia
33-
using PETSc
34-
PETSc.libs
44+
julia>using PETSc
45+
julia>[PETSc.petsclibs...]
3546
```
47+
3648
## Windows users
3749
The package currently does not work on windows, mainly because `MicrosoftMPI_jll` does not function when used along with the precompiled version used in `PETSc_jll`. Windows users are therefore advised to install the [Windows Subsystem for Linux](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) (WSL) and run PETSc through there.
3850

3951
## Getting started
40-
The documentation is currently rather minimalistic; yet, if you want to see what is possible have a look at the [examples](./examples/) directory or at the tests in the [test](./test) directory. We do keep the tests up to date, so that is a good starting point.
52+
The documentation is currently rather minimalistic; yet, if you want to see what is possible have a look at the [examples](./examples/) directory or at the tests in the [test](./test) directory. We do keep the tests up to date, so that is a good starting point.
53+
54+
Note, that we do not have tests in place for the whole library at this stage. The best supported parts are `DMDA`,`DMStag`, `KSP`,`SNES`,`Vec` and `Mat` interfaces, while other parts such as `DMPlex` do not have a high-level interface or tests yet. Users will thus have to rely on the low-level interface.

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"
4+
PETSc = "ace2c81b-2b5f-4b1e-a30d-d662738edfe0"

docs/make.jl

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,52 @@ using Documenter, PETSc
33
makedocs(;
44
modules=[PETSc],
55
sitename="PETSc.jl",
6+
checkdocs=:exports, # Only check exported functions, skip LibPETSc internals
7+
warnonly=true, # Warn but don't error for any documentation issues
68
format=Documenter.HTML(;
79
prettyurls=get(ENV, "CI", "false") == "true",
10+
size_threshold_warn = nothing, # Disable size warnings for large low-level API pages
11+
size_threshold = nothing, # Disable size errors for large low-level API pages
812
),
913
pages=[
1014
"Home" => "index.md",
1115
"Getting Started" => "man/getting_started.md",
12-
"PETSc" => Any[
16+
"High-level interface" => Any[
17+
"Vec" => "man/vec.md",
1318
"Mat" => "man/mat.md",
19+
"DM" => "man/dm.md",
20+
"DMDA" => "man/dmda.md",
1421
"DMStag" => "man/dmstag.md",
22+
"KSP" => "man/ksp.md",
23+
"SNES" => "man/snes.md",
1524
],
16-
"List of functions" => "man/listfunctions.md"
25+
"Low-level interface (LibPETSc)" => Any[
26+
"Introduction" => "man/lowlevel_intro.md",
27+
"Vec" => "man/vec_lowlevel.md",
28+
"Mat" => "man/mat_lowlevel.md",
29+
"DM" => Any[
30+
"DM" => "man/dm_lowlevel.md",
31+
"DMDA" => "man/dmda_lowlevel.md",
32+
"DMPlex" => "man/dmplex_lowlevel.md",
33+
"DMStag" => "man/dmstag_lowlevel.md",
34+
"DMSwarm" => "man/dmswarm_lowlevel.md",
35+
"DMForest" => "man/dmforest_lowlevel.md",
36+
"DMNetwork" => "man/dmnetwork_lowlevel.md",
37+
"DMShell and others" => "man/dmshell_lowlevel.md",
38+
],
39+
"KSP" => "man/ksp_lowlevel.md",
40+
"SNES" => "man/snes_lowlevel.md",
41+
"TS (Time Stepping)" => "man/ts_lowlevel.md",
42+
"Tao (Optimization)" => "man/tao_lowlevel.md",
43+
"IS (Index Sets)" => "man/is_lowlevel.md",
44+
"PetscViewer (I/O)" => "man/petscviewer_lowlevel.md",
45+
"PetscSection (DOF Layout)" => "man/petscsection_lowlevel.md",
46+
"PetscSF (Communication)" => "man/petscsf_lowlevel.md",
47+
"AO (Application Ordering)" => "man/ao_lowlevel.md",
48+
],
49+
"Utilities" => "man/utilities.md",
50+
"FAQ" => "man/FAQ.md",
51+
"Contributing" => "man/contributing.md",
1752
],
1853
)
1954

docs/src/index.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
# PETSc.jl
22

3-
[PETSc.jl](https://github.com/JuliaParallel/PETSc.jl) is a Julia wrapper for the Portable, Extensible Toolkit for Scientific Computation [PETSc](https://petsc.org/release/documentation/manual/) package, which allows solving ordinary and partial differential equations in parallel on laptops or massively parallel high-performance systems.
3+
[PETSc.jl](https://github.com/JuliaParallel/PETSc.jl) is a Julia wrapper for the Portable, Extensible Toolkit for Scientific Computation [PETSc](https://petsc.org/) package, which allows solving ordinary and partial differential equations in parallel on laptops or massively parallel high-performance systems.
44

5-
The use of Julia greatly simplifies the code that developers have to write, while allowing to employ Julia features such as automatic differentiation. The Julia wrapper also comes with a pre-built library, which greatly simplifies the process of getting your first code working in parallel, on different operating systems. In many cases, the Julia code is significantly shorter than its C counterpart.
5+
The use of Julia greatly simplifies the code that developers have to write, while allowing them to employ Julia features such as automatic differentiation. The Julia wrapper also comes with a pre-built library, which greatly simplifies the process of getting your first code working in parallel on different operating systems. In many cases, the Julia code is significantly shorter than its C counterpart.
66

7-
This wrapper mimics the PETSc-functionality as closely as possible, but remains work in progress (meaning that not everything has been translated yet). See the official [user guide](https://petsc.org/release/overview/) if you want to learn more about PETSc in general. For Julia-specific examples, have a look at our [examples](https://github.com/JuliaParallel/PETSc.jl/tree/main/examples) or [tests](https://github.com/JuliaParallel/PETSc.jl/tree/main/test).
7+
This wrapper mimics the PETSc functionality as closely as possible, but remains work in progress. We have (semi-automatically) translated most of the functionality of PETSc, along with help docstrings. Yet, the higher-level interface is only available for part of the library. Likewise, the tests currently only cover part of the library, so we can only guarantee that this part works.
8+
9+
See the official [user guide](https://petsc.org/release/overview/) if you want to learn more about PETSc in general. For Julia-specific examples, have a look at our [examples](https://github.com/JuliaParallel/PETSc.jl/tree/main/examples) or [tests](https://github.com/JuliaParallel/PETSc.jl/tree/main/test).
10+
11+
## The High-Level Interface
12+
13+
The high-level interface is designed to be familiar and convenient for Julia users, but exposes only a small portion of the functionality of the underlying PETSc library.
14+
15+
For example, with this interface, PETSc's [KSP](https://petsc.org/release/docs/manual/ksp) linear solvers (including Krylov methods) can be used in a way similar to solvers from other Julia packages. See the example in [Getting started](@ref) and the API in [KSP](@ref).
16+
17+
## The Low-Level Interface
18+
19+
The low-level interface covers most of the PETSc API, but may be awkward to work with and likely requires previous experience with PETSc to use effectively. It is (mostly) automatically generated by borrowing the Python code that creates Fortran wrappers for PETSc.
20+
21+
The high-level interface described in [KSP](@ref) creates a [KSP](https://petsc.org/release/docs/manual/ksp) linear solver object via the low-level interface to [`KSPSolve()`](https://petsc.org/release/docs/manualpages/KSP/KSPCreate.html), with the use of constructs which require knowledge of PETSc's nature as a [C library](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/). Expert users are of course free to directly use the low-level interface, as in this simple example which directly calls [`PetscGetVersionNumber()`](https://petsc.org/release/docs/manualpages/PetscGetVersionNumber.html).
22+
23+
```julia
24+
using MPI
25+
MPI.Initialized() || MPI.Init()
26+
using PETSc
27+
28+
# Select the appropriate library
29+
petsclib = PETSc.petsclibs[1]
30+
31+
# Initialize PETSc (with logging turned off; the default)
32+
PETSc.initialize(petsclib, log_view=false)
33+
34+
# Call the low-level interface (which always starts with petsclib)
35+
version = LibPETSc.PetscGetVersionNumber(petsclib)
36+
37+
# Print result
38+
println("PETSc $(version[1]).$(version[2]).$(version[3])")
39+
40+
# Finalize
41+
PETSc.finalize(petsclib)
42+
```

docs/src/man/FAQ.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Frequently Asked Questions
2+
3+
4+
## 1. Can I use my own PETSc library?
5+
Yes, see the function `set_petsclib`. You do need to compile this library as a dynamic library, and `MPI.jl` must be configured to be compatible with the MPI library used to compile PETSc. If you run this on a HPC system, and you don't know exactly which options were used, you can compile one of the PETSc examples and run it with the `-log_view` command line option. At the end of the simulation, it will give you the configuration options used on that machine.
6+
7+
Please note that the version of PETSc should be compatible with the version used for the wrapper.
8+
9+
## 2. Help, my code crashes?
10+
That is very possible. If you provide a *short* minimum working example (MWE), feel free to open an issue on the github repo, so we can check it out.
11+
12+
## 3. What about the garbage collector in Julia?
13+
Using the GC in combination with MPI code is a tricky business. The users are therefore responsible to free PETSc objects in the code, as shown in the various examples. We have some help for that:
14+
1. The julia function `PETSc.audit_petsc_file("path/to/your/file.jl")` which scans your julia file and tries to guess whether objects are destroyed.
15+
2. You can initialize the PETSc library with `log_view=true`. At the end of the code, it will give an
16+
17+
## 4. Is it compatible with GPUs?
18+
The precompiled `PETSc_jll` binaries are currently not compatible with GPUs. You can, however, use your own PETSc compilation and things should be fine.

0 commit comments

Comments
 (0)