Skip to content

Commit 32611a7

Browse files
authored
Use petsctools package (#4194)
* Add version_spec check to setup.py * Move OptionsManager to petsctools
1 parent 1156c4a commit 32611a7

File tree

15 files changed

+119
-343
lines changed

15 files changed

+119
-343
lines changed

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
(r'py:.*', r'ufl\..*'),
136136
(r'py:.*', r'PETSc\..*'),
137137
(r'py:.*', r'progress\..*'),
138+
(r'py:.*', r'petsctools\..*'),
138139
# Ignore undocumented PyOP2
139140
('py:class', 'pyop2.caching.Cached'),
140141
# Ignore mission docs from Firedrake internal "private" code

docs/source/optimising.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Caveats
136136

137137
.. code-block:: python
138138
139-
from firedrake.petsc import OptionsManager
139+
from petsctools import OptionsManager
140140
141141
# If the -log_view flag is passed you don't need to call
142142
# PETSc.Log.begin because it is done automatically.

firedrake/__init__.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
1-
import sys
2-
from firedrake.configuration import setup_cache_dirs
1+
# The range of PETSc versions supported by Firedrake. Note that unlike in
2+
# firedrake-configure and pyproject.toml where we want to be strict about
3+
# the specific version, here we are more permissive. This is to catch the
4+
# case where users don't update their PETSc for a really long time or
5+
# accidentally install a too-new release that isn't yet supported.
6+
PETSC_SUPPORTED_VERSIONS = ">=3.23.0"
7+
8+
9+
def init_petsc():
10+
import os
11+
import sys
12+
import petsctools
13+
14+
# We conditionally pass '-options_left no' as in some circumstances (e.g.
15+
# when running pytest) PETSc complains that command line options are not
16+
# PETSc options.
17+
if os.getenv("FIREDRAKE_DISABLE_OPTIONS_LEFT") == "1":
18+
petsctools.init(sys.argv + ["-options_left", "no"], version_spec=PETSC_SUPPORTED_VERSIONS)
19+
else:
20+
petsctools.init(sys.argv, version_spec=PETSC_SUPPORTED_VERSIONS)
21+
22+
23+
# Ensure petsc is initialised right away
24+
init_petsc()
325

426
# Set up the cache directories before importing PyOP2.
27+
from firedrake.configuration import setup_cache_dirs
28+
529
setup_cache_dirs()
630

7-
# Ensure petsc is initialised by us before anything else gets in there.
8-
# We conditionally pass '-options_left no' as in some circumstances (e.g.
9-
# when running pytest) PETSc complains that command line options are not
10-
# PETSc options.
11-
import os
12-
import petsc4py
13-
if os.getenv("FIREDRAKE_DISABLE_OPTIONS_LEFT") == "1":
14-
petsc4py.init(sys.argv + ["-options_left", "no"])
15-
else:
16-
petsc4py.init(sys.argv)
17-
del os, petsc4py
1831

1932
# Initialise PETSc events for both import and entire duration of program
33+
import petsctools
2034
from firedrake import petsc
21-
_is_logging = "log_view" in petsc.OptionsManager.commandline_options
35+
_is_logging = "log_view" in petsctools.OptionsManager.commandline_options
2236
if _is_logging:
2337
_main_event = petsc.PETSc.Log.Event("firedrake")
2438
_main_event.begin()
@@ -29,6 +43,7 @@
2943
import atexit
3044
atexit.register(lambda: _main_event.end())
3145
del atexit
46+
del petsctools
3247
del petsc
3348

3449
from ufl import *
@@ -85,6 +100,7 @@
85100
# from firedrake._deprecation import output
86101
# sys.modules["firedrake.output"] = output
87102
from firedrake.output import *
103+
import sys
88104
sys.modules["firedrake.plot"] = plot
89105
from firedrake.plot import *
90106

@@ -104,7 +120,7 @@ def set_blas_num_threads():
104120
105121
"""
106122
from ctypes import cdll
107-
from firedrake.petsc import get_blas_library
123+
from petsctools import get_blas_library
108124

109125
try:
110126
blas_lib_path = get_blas_library()

firedrake/checkpointing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import finat.ufl
55
from pyop2 import op2
66
from pyop2.mpi import COMM_WORLD, internal_comm, MPI
7+
from petsctools import OptionsManager
78
from firedrake.cython import hdf5interface as h5i
89
from firedrake.cython import dmcommon
9-
from firedrake.petsc import PETSc, OptionsManager
10+
from firedrake.petsc import PETSc
1011
from firedrake.mesh import MeshTopology, ExtrudedMeshTopology, DEFAULT_MESH_NAME, make_mesh_from_coordinates, DistributedMeshOverlapType
1112
from firedrake.functionspace import FunctionSpace
1213
from firedrake import functionspaceimpl as impl

firedrake/eigensolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""Specify and solve finite element eigenproblems."""
2+
from petsctools import OptionsManager, flatten_parameters
23
from firedrake.assemble import assemble
34
from firedrake.bcs import extract_subdomain_ids, restricted_function_space
45
from firedrake.function import Function
56
from firedrake.ufl_expr import TrialFunction, TestFunction
67
from firedrake import utils
7-
from firedrake.petsc import OptionsManager, flatten_parameters
88
from firedrake.exceptions import ConvergenceError
99
from ufl import replace, inner, dx
1010
try:

firedrake/mesh.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
MPI, COMM_WORLD, internal_comm, is_pyop2_comm, temp_internal_comm
2323
)
2424
from pyop2.utils import as_tuple
25+
from petsctools import OptionsManager, get_external_packages
2526

2627
import firedrake.cython.dmcommon as dmcommon
2728
from firedrake.cython.dmcommon import DistributedMeshOverlapType
@@ -32,9 +33,7 @@
3233
from firedrake.utils import as_cstr, IntType, RealType
3334
from firedrake.logging import info_red
3435
from firedrake.parameters import parameters
35-
from firedrake.petsc import (
36-
PETSc, OptionsManager, get_external_packages, DEFAULT_PARTITIONER
37-
)
36+
from firedrake.petsc import PETSc, DEFAULT_PARTITIONER
3837
from firedrake.adjoint_utils import MeshGeometryMixin
3938
from pyadjoint import stop_annotating
4039
import gem

0 commit comments

Comments
 (0)