Skip to content

Commit 8a135d5

Browse files
committed
misc: Add petsc_type_to_ctype
1 parent cd36b2c commit 8a135d5

2 files changed

Lines changed: 11 additions & 13 deletions

File tree

devito/petsc/logging.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from devito.petsc.types import (
88
PetscInt, PetscScalar, KSPType, KSPConvergedReason, KSPNormType
99
)
10-
from devito.petsc.utils import petsc_type_mappings, fixed_petsc_type_mappings
10+
from devito.petsc.utils import petsc_type_to_ctype
1111

1212

1313
class PetscEntry:
@@ -142,7 +142,6 @@ def __init__(self, name, pname, petsc_option_mapper, sobjs, section_mapper,
142142
self.function_list = function_list
143143
self.formatted_prefix = inject_solve.expr.rhs.formatted_prefix
144144

145-
mapper = {v: k for k, v in petsc_type_mappings.items()}
146145
pfields = []
147146

148147
# All petsc options needed to form the PetscInfo struct
@@ -152,11 +151,9 @@ def __init__(self, name, pname, petsc_option_mapper, sobjs, section_mapper,
152151
for petsc_option in self._fields:
153152
# petsc_type is e.g. 'PetscInt', 'PetscScalar', 'KSPType'
154153
petsc_type = str(petsc_option.dtype)
155-
if petsc_type in mapper:
156-
ctype = mapper[petsc_type]
157-
else:
158-
ctype = fixed_petsc_type_mappings[petsc_type]
154+
ctype = petsc_type_to_ctype[petsc_type]
159155
pfields.append((petsc_option.name, ctype))
156+
160157
super().__init__(name, pname, pfields)
161158

162159
@property

devito/petsc/utils.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,18 @@ def get_petsc_type_mappings():
9292
return mapper
9393

9494

95+
# This mapping is used by the printer to always convert
96+
# int, float/double to `PetscInt` and `PetscScalar`
9597
petsc_type_mappings = get_petsc_type_mappings()
9698

9799

98-
# NOTE: These mappings are only used when constructing ctypes.Structures
99-
# that wrap PETSc objects. In the generated C code, the fields will still
100-
# appear as the actual PETSc types.
101-
fixed_petsc_type_mappings = {
100+
# Used to construct ctypes.Structures that wrap PETSc objects
101+
petsc_type_to_ctype = {v: k for k, v in petsc_type_mappings.items()}
102+
petsc_type_to_ctype.update({
102103
'KSPType': ctypes.c_char_p,
103-
'KSPConvergedReason': ctypes.c_int,
104-
'KSPNormType': ctypes.c_int,
105-
}
104+
'KSPConvergedReason': petsc_type_to_ctype['PetscInt'],
105+
'KSPNormType': petsc_type_to_ctype['PetscInt'],
106+
})
106107

107108

108109
petsc_languages = ['petsc']

0 commit comments

Comments
 (0)