Skip to content

Commit cdb83ef

Browse files
authored
PETSc clean up (#2740)
* misc/compiler: Rename/separate files, clean up, add lower_petsc_symbols and change PETScSolve to petscsolve
1 parent a19f136 commit cdb83ef

38 files changed

+2882
-2561
lines changed

conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from devito.ir.iet import (FindNodes, FindSymbols, Iteration, ParallelBlock,
1515
retrieve_iteration_tree)
1616
from devito.tools import as_tuple
17-
from devito.petsc.utils import PetscOSError, get_petsc_dir
17+
from devito.petsc.config import PetscOSError, get_petsc_dir
1818

1919
try:
2020
from mpi4py import MPI # noqa

devito/core/cpu.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
check_stability, PetscTarget)
1515
from devito.tools import timed_pass
1616

17+
from devito.petsc.iet.passes import lower_petsc_symbols
18+
1719
__all__ = ['Cpu64NoopCOperator', 'Cpu64NoopOmpOperator', 'Cpu64AdvCOperator',
1820
'Cpu64AdvOmpOperator', 'Cpu64FsgCOperator', 'Cpu64FsgOmpOperator',
1921
'Cpu64CustomOperator', 'Cpu64CustomCXXOperator', 'Cpu64AdvCXXOperator',
@@ -143,6 +145,9 @@ def _specialize_iet(cls, graph, **kwargs):
143145
# Symbol definitions
144146
cls._Target.DataManager(**kwargs).process(graph)
145147

148+
# Lower PETSc symbols
149+
lower_petsc_symbols(graph, **kwargs)
150+
146151
return graph
147152

148153

@@ -222,6 +227,9 @@ def _specialize_iet(cls, graph, **kwargs):
222227
# Symbol definitions
223228
cls._Target.DataManager(**kwargs).process(graph)
224229

230+
# Lower PETSc symbols
231+
lower_petsc_symbols(graph, **kwargs)
232+
225233
# Linearize n-dimensional Indexeds
226234
linearize(graph, **kwargs)
227235

devito/ir/iet/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Section, HaloSpot, ExpressionBundle)
55
from devito.tools import timed_pass
66
from devito.petsc.types import MetaData
7-
from devito.petsc.iet.utils import petsc_iet_mapper
7+
from devito.petsc.iet.nodes import petsc_iet_mapper
88

99
__all__ = ['iet_build']
1010

devito/ir/iet/visitors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ class FindSymbols(LazyVisitor[Any, list[Any], None]):
10681068
Drive the search. Accepted:
10691069
- `symbolics`: Collect all AbstractFunction objects, default
10701070
- `basics`: Collect all Basic objects
1071-
- `abstractsymbols`: Collect all AbstractSymbol objects
1071+
- `symbols`: Collect all AbstractSymbol objects
10721072
- `dimensions`: Collect all Dimensions
10731073
- `indexeds`: Collect all Indexed objects
10741074
- `indexedbases`: Collect all IndexedBase objects

devito/passes/iet/languages/C.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from devito.passes.iet.langbase import LangBB
88
from devito.symbolics import c_complex, c_double_complex
99
from devito.tools import dtype_to_cstr
10-
from devito.petsc.utils import petsc_type_mappings
10+
11+
from devito.petsc.config import petsc_type_mappings
1112

1213
__all__ = ['CBB', 'CDataManager', 'COrchestrator']
1314

@@ -82,3 +83,6 @@ class PetscCPrinter(CPrinter):
8283
_restrict_keyword = ''
8384

8485
type_mappings = {**CPrinter.type_mappings, **petsc_type_mappings}
86+
87+
def _print_Pi(self, expr):
88+
return 'PETSC_PI'

devito/petsc/config.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
import ctypes
3+
from pathlib import Path
4+
5+
from petsctools import get_petscvariables, MissingPetscException
6+
7+
from devito.tools import memoized_func
8+
9+
10+
class PetscOSError(OSError):
11+
pass
12+
13+
14+
@memoized_func
15+
def get_petsc_dir():
16+
petsc_dir = os.environ.get('PETSC_DIR')
17+
if petsc_dir is None:
18+
raise PetscOSError("PETSC_DIR environment variable not set")
19+
else:
20+
petsc_dir = (Path(petsc_dir),)
21+
22+
petsc_arch = os.environ.get('PETSC_ARCH')
23+
if petsc_arch is not None:
24+
petsc_dir += (petsc_dir[0] / petsc_arch,)
25+
26+
petsc_installed = petsc_dir[-1] / 'include' / 'petscconf.h'
27+
if not petsc_installed.is_file():
28+
raise PetscOSError("PETSc is not installed")
29+
30+
return petsc_dir
31+
32+
33+
@memoized_func
34+
def core_metadata():
35+
petsc_dir = get_petsc_dir()
36+
37+
petsc_include = tuple([arch / 'include' for arch in petsc_dir])
38+
petsc_lib = tuple([arch / 'lib' for arch in petsc_dir])
39+
40+
return {
41+
'includes': ('petscsnes.h', 'petscdmda.h'),
42+
'include_dirs': petsc_include,
43+
'libs': ('petsc'),
44+
'lib_dirs': petsc_lib,
45+
'ldflags': tuple([f"-Wl,-rpath,{lib}" for lib in petsc_lib])
46+
}
47+
48+
49+
try:
50+
petsc_variables = get_petscvariables()
51+
except MissingPetscException:
52+
petsc_variables = {}
53+
54+
55+
def get_petsc_type_mappings():
56+
try:
57+
petsc_precision = petsc_variables['PETSC_PRECISION']
58+
except KeyError:
59+
printer_mapper = {}
60+
petsc_type_to_ctype = {}
61+
else:
62+
petsc_scalar = 'PetscScalar'
63+
# TODO: Check to see whether Petsc is compiled with
64+
# 32-bit or 64-bit integers
65+
printer_mapper = {ctypes.c_int: 'PetscInt'}
66+
67+
if petsc_precision == 'single':
68+
printer_mapper[ctypes.c_float] = petsc_scalar
69+
elif petsc_precision == 'double':
70+
printer_mapper[ctypes.c_double] = petsc_scalar
71+
72+
# Used to construct ctypes.Structures that wrap PETSc objects
73+
petsc_type_to_ctype = {v: k for k, v in printer_mapper.items()}
74+
# Add other PETSc types
75+
petsc_type_to_ctype.update({
76+
'KSPType': ctypes.c_char_p,
77+
'KSPConvergedReason': petsc_type_to_ctype['PetscInt'],
78+
'KSPNormType': petsc_type_to_ctype['PetscInt'],
79+
})
80+
return printer_mapper, petsc_type_to_ctype
81+
82+
83+
petsc_type_mappings, petsc_type_to_ctype = get_petsc_type_mappings()
84+
85+
86+
petsc_languages = ['petsc']

0 commit comments

Comments
 (0)