Skip to content

Commit c2b7fb7

Browse files
committed
misc: Add another test
1 parent f53c608 commit c2b7fb7

8 files changed

Lines changed: 69 additions & 24 deletions

File tree

devito/ir/iet/visitors.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ def _gen_struct_decl(self, obj, masked=()):
292292
for i, (n, ct) in zip(fields, ctype._fields_):
293293
try:
294294
entries.append(self._gen_value(i, 0, masked=('const',)))
295-
296295
except AttributeError:
297296
cstr = self.ccode(ct)
298297
if ct is c_restrict_void_p:

devito/petsc/iet/logging.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ class PetscLogger:
1313
Class for PETSc loggers that collect solver related statistics.
1414
"""
1515
def __init__(self, level, get_info=[], **kwargs):
16-
self.function_list = get_info
1716

17+
self.function_list = get_info
1818
self.sobjs = kwargs.get('solver_objs')
1919
self.sreg = kwargs.get('sregistry')
20-
2120
self.section_mapper = kwargs.get('section_mapper', {})
2221
self.inject_solve = kwargs.get('inject_solve', None)
2322

devito/petsc/iet/passes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def lower_petsc(iet, **kwargs):
6969
# Map PETScSolve to its Section (for logging)
7070
section_mapper = MapNodes(Section, PetscMetaData, 'groupby').visit(iet)
7171

72-
# Prefixes within the same Operator should not be duplicated
72+
# Prefixes within the same `Operator` should not be duplicated
7373
prefixes = [d.expr.rhs.user_prefix for d in data if d.expr.rhs.user_prefix]
7474
duplicates = {p for p in prefixes if prefixes.count(p) > 1}
7575

devito/petsc/initialize.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
global _petsc_clargs
1616

1717

18-
dummy = Symbol(name='d')
19-
20-
2118
def PetscInitialize(clargs=sys.argv):
2219
global _petsc_initialized
2320
global _petsc_clargs
2421

2522
if not _petsc_initialized:
23+
dummy = Symbol(name='d')
24+
2625
if clargs is not sys.argv:
2726
clargs = [sys.argv[0], *clargs]
2827

devito/petsc/logging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def __repr__(self):
2929

3030
class PetscSummary(dict):
3131
"""
32-
# TODO: Actually print to screen when DEBUG of PERF is enabled
3332
A summary of PETSc statistics collected for all solver runs
3433
associated with a single operator during execution.
3534
"""
@@ -69,6 +68,8 @@ def petsc_entry(self, petscinfo):
6968
Create a named tuple entry for the given PetscInfo object,
7069
containing the values for each PETSc function call.
7170
"""
71+
# Collect the function names associated with this PetscInfo
72+
# instance (i.e., for a single PETScSolve).
7273
funcs = [
7374
petsc_return_variable_dict[f].name for f in petscinfo.function_list
7475
]

devito/petsc/solve.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ def PETScSolve(target_exprs, target=None, solver_parameters=None,
6767
provided, a default prefix is generated by Devito.
6868
6969
get_info : list[str], optional
70-
A list of PETSc API functions to collect statistics from the solver.
71-
For example, `['kspgetiterationnumber', 'kspgettolerances']`.
72-
Capitalisation does not matter; e.g. `'KSPGetIterationNumber'` and
73-
`'kspgetiterationnumber'` are treated the same.
70+
A list of PETSc API functions (case insensitive) to collect statistics
71+
from the solver.
7472
7573
List of available functions:
7674
- ['kspgetiterationnumber', 'kspgettolerances', 'kspgetconvergedreason',
@@ -96,6 +94,7 @@ def __init__(self, solver_parameters=None, target_exprs=None, options_prefix=Non
9694
self.solver_parameters = linear_solver_parameters(solver_parameters)
9795
self.time_mapper = None
9896
self.target_exprs = target_exprs
97+
# The original options prefix provided by the user
9998
self.user_prefix = options_prefix
10099
self.formatted_prefix = format_options_prefix(options_prefix)
101100
self.get_info = [f.lower() for f in get_info]

examples/petsc/init_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import os
2-
from devito.petsc.initialize import PetscInitialize, PetscGetArgs
2+
from devito.petsc.initialize import PetscInitialize
33
from devito import configuration
44
configuration['compiler'] = 'custom'
55
os.environ['CC'] = 'mpicc'
66

77
PetscInitialize()
88
print("helloworld")
9-
10-
PetscGetArgs()

tests/test_petsc.py

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,25 @@
2525

2626
@pytest.fixture(scope='session')
2727
def command_line():
28-
# One random prefix to use per test that "tests" the command line args
29-
prefix = ('d17weqroeg', 'riabfodkj5', 'fir8o3lsak', 'zwejklqn25')
28+
29+
# Random prefixes to validate command line argument parsing
30+
prefix = (
31+
'd17weqroeg', 'riabfodkj5', 'fir8o3lsak',
32+
'zwejklqn25', 'qtr2vfvwiu')
3033

3134
petsc_option = (
3235
('ksp_rtol',),
3336
('ksp_rtol', 'ksp_atol'),
3437
('ksp_rtol', 'ksp_atol', 'ksp_divtol', 'ksp_max_it'),
35-
('ksp_type',)
36-
38+
('ksp_type',),
39+
('ksp_divtol', 'ksp_type')
3740
)
3841
value = (
3942
(1e-8,),
4043
(1e-11, 1e-15),
4144
(1e-3, 1e-10, 50000, 2000),
42-
('cg',)
45+
('cg',),
46+
(22000, 'richardson'),
4347
)
4448
argv = []
4549
expected = {}
@@ -1663,9 +1667,6 @@ def test_tolerances(self, log_level):
16631667

16641668
@skipif('petsc')
16651669
def test_clearing_options(self):
1666-
# TODO: Extend this test to check that options are only cleared
1667-
# if they have NOT ben set via the command line
1668-
16691670
# Explicitly set the solver parameters
16701671
solver1 = PETScSolve(
16711672
self.eq1, target=self.e, solver_parameters={'ksp_rtol': '1e-10'}
@@ -1737,7 +1738,7 @@ def test_multiple_operators(self, log_level):
17371738
def test_command_line_priority_tols_1(self, command_line, log_level):
17381739
"""
17391740
Test solver tolerances specifed via the command line
1740-
take precedence over those set by the defaults.
1741+
take precedence over those specified in the defaults.
17411742
"""
17421743
prefix = 'd17weqroeg'
17431744
_, expected = command_line
@@ -1838,6 +1839,55 @@ def test_command_line_priority_ksp_type(self, command_line, log_level):
18381839
assert entry.KSPGetType == val
18391840
assert not entry.KSPGetType == params['ksp_type']
18401841

1842+
@skipif('petsc')
1843+
def test_not_set_or_clear_command_line_opts(self, command_line):
1844+
"""
1845+
Verify that if an option is set via the command line,
1846+
the corresponding entry in `linear_solve_defaults` or in the user
1847+
specified `solver_parameters` is not set or cleared in the
1848+
generated code. (The command line option will have already been set in
1849+
the global PetscOptions database during PetscInitialize().)
1850+
"""
1851+
prefix = 'qtr2vfvwiu'
1852+
1853+
solver = PETScSolve(
1854+
self.eq1, target=self.e,
1855+
# Specify a solver parameter that is not set via the
1856+
# command line (see the `command_line` fixture for this prefix).
1857+
solver_parameters={'ksp_rtol': '1e-10'},
1858+
options_prefix=prefix
1859+
)
1860+
with switchconfig(language='petsc'):
1861+
op = Operator(solver)
1862+
1863+
set_options_callback = str(op._func_table['SetPetscOptions0'].root)
1864+
clear_options_callback = str(op._func_table['ClearPetscOptions0'].root)
1865+
1866+
# Check that the `ksp_rtol` option IS set and cleared explicitly
1867+
# since it is NOT set via the command line.
1868+
assert f'PetscOptionsSetValue(NULL,"-{prefix}_ksp_rtol","1e-10")' \
1869+
in set_options_callback
1870+
assert f'PetscOptionsClearValue(NULL,"-{prefix}_ksp_rtol")' \
1871+
in clear_options_callback
1872+
1873+
# Check that the `ksp_divtol` and `ksp_type` options are NOT set
1874+
# or cleared explicitly since they ARE set via the command line.
1875+
assert f'PetscOptionsSetValue(NULL,"-{prefix}_div_tol",' \
1876+
not in set_options_callback
1877+
assert f'PetscOptionsSetValue(NULL,"-{prefix}_ksp_type",' \
1878+
not in set_options_callback
1879+
assert f'PetscOptionsClearValue(NULL,"-{prefix}_div_tol"));' \
1880+
not in clear_options_callback
1881+
assert f'PetscOptionsClearValue(NULL,"-{prefix}_ksp_type"));' \
1882+
not in clear_options_callback
1883+
1884+
# Check that options specifed by the `linear_solver_defaults`
1885+
# are still set and cleared
1886+
assert f'PetscOptionsSetValue(NULL,"-{prefix}_ksp_atol",' \
1887+
in set_options_callback
1888+
assert f'PetscOptionsClearValue(NULL,"-{prefix}_ksp_atol"));' \
1889+
in clear_options_callback
1890+
18411891

18421892
class TestHashing:
18431893

0 commit comments

Comments
 (0)