Skip to content

Commit d7da286

Browse files
committed
tests: Add command line tests with random prefixes
1 parent ce92e4d commit d7da286

4 files changed

Lines changed: 84 additions & 26 deletions

File tree

devito/petsc/iet/routines.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ def _make_options_callback(self):
149149

150150
for k, v in params.items():
151151
option = f'-{prefix}{k}'
152-
if option in sys.argv:
152+
# if option in sys.argv:
153+
# TODO: Pre-build the KSPGetArgs operator and run it here
154+
# to drop the global _petsc_clargs
155+
# tmp = petsc_get_args_op.apply()
156+
import devito.petsc.initialize
157+
if option in devito.petsc.initialize._petsc_clargs:
153158
# Ensures that the command line args take priority
154159
continue
155160
option_name = String(option)

devito/petsc/initialize.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
_petsc_initialized = False
1313

1414

15+
global _petsc_clargs
16+
1517
def PetscInitialize(clargs=sys.argv):
1618
global _petsc_initialized
19+
global _petsc_clargs
20+
1721
if not _petsc_initialized:
22+
if clargs is not sys.argv:
23+
clargs = [sys.argv[0], *clargs]
24+
# TODO: drop this
25+
_petsc_clargs = clargs
1826
dummy = Symbol(name='d')
1927
# TODO: Potentially just use cgen + the compiler machinery in Devito
2028
# to generate these "dummy_ops" instead of using the Operator class.
@@ -37,7 +45,6 @@ def PetscInitialize(clargs=sys.argv):
3745
argv_pointer = (POINTER(c_char)*len(clargs))(
3846
*map(lambda s: cast(s, POINTER(c_char)), argv_bytes)
3947
)
40-
4148
op_init.apply(argc=len(clargs), argv=argv_pointer)
4249

4350
atexit.register(op_finalize.apply)

devito/petsc/logging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ class PetscReturnVariable:
203203
name='KSPGetTolerances',
204204
variable_type=(PetscScalar, PetscScalar, PetscScalar, PetscInt),
205205
input_params='ksp',
206-
output_param=('rtol', 'abstol', 'dtol', 'maxits'),
206+
# TODO: check if maxits is max_its in command line
207+
output_param=('rtol', 'atol', 'dtol', 'maxits'),
207208
),
208209
'kspgetconvergedreason': PetscReturnVariable(
209210
name='KSPGetConvergedReason',

tests/test_petsc.py

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import os
55
import re
6+
import sys
67
from collections import OrderedDict
78

89
from conftest import skipif
@@ -23,15 +24,37 @@
2324
from devito.petsc.logging import PetscSummary
2425
from devito.petsc.solver_parameters import linear_solve_defaults
2526

27+
@pytest.fixture(scope='session')
28+
def command_line():
29+
# one prefix per test
30+
prefix = ('d17weqroegn', 'riabfodkj')
31+
32+
petsc_option = (
33+
('ksp_rtol',),
34+
('ksp_rtol','ksp_atol')
35+
)
36+
value = (
37+
('1e-8',),
38+
('1e-11','1e-15'),
39+
)
40+
argv = []
41+
42+
expected = {}
43+
for p, opt, val in zip(prefix, petsc_option, value, strict=True):
44+
for o, v in zip(opt, val, strict=True):
45+
argv.extend([f'-{p}_{o}', v])
46+
expected[p] = zip(opt,val)
47+
return argv, expected
48+
2649

2750
@pytest.fixture(scope='session', autouse=True)
28-
def petsc_initialization():
51+
def petsc_initialization(command_line):
52+
argv, _ = command_line
2953
# TODO: Temporary workaround until PETSc is automatically
3054
# initialized
3155
configuration['compiler'] = 'custom'
3256
os.environ['CC'] = 'mpicc'
33-
# PetscInitialize(argv)
34-
PetscInitialize()
57+
PetscInitialize(argv)
3558

3659

3760
@skipif('petsc')
@@ -1710,26 +1733,48 @@ def test_multiple_operators(self, log_level):
17101733
# TODO: Add test to check that the command line args override anything set
17111734
# in the solver_parameters dictionary
17121735

1713-
# @skipif('petsc')
1714-
# def test_command_line_priority(self):
1715-
# """
1716-
# Test solver parameters specifed via the command line
1717-
# take precedence over those set in the solver_parameters
1718-
# dictionary.
1719-
# """
1720-
# prefix = 'd17weqroegn'
1721-
1722-
# solver1 = PETScSolve(
1723-
# self.eq1, target=self.e, solver_parameters={
1724-
# 'ksp_rtol': '1e-9',
1725-
# 'snes_view': None}
1726-
# )
1727-
# with switchconfig(language='petsc'):
1728-
# op = Operator(solver1)
1729-
# op.apply()
1730-
1731-
# # Check that the command line option specifying the ksp_rtol took
1732-
# # priorty over the solver
1736+
@skipif('petsc')
1737+
def test_command_line_priority_1(self, command_line):
1738+
"""
1739+
Test solver parameters specifed via the command line
1740+
take precedence over those set in the solver_parameters
1741+
dictionary.
1742+
"""
1743+
prefix = 'd17weqroegn'
1744+
_, expected = command_line
1745+
1746+
solver1 = PETScSolve(
1747+
self.eq1, target=self.e,
1748+
options_prefix=prefix
1749+
)
1750+
with switchconfig(language='petsc', log_level='DEBUG'):
1751+
op = Operator(solver1)
1752+
summary = op.apply()
1753+
1754+
petsc_summary = summary.petsc
1755+
entry = petsc_summary.get_entry('section0', prefix)
1756+
for opt, val in expected[prefix]:
1757+
assert str(entry.KSPGetTolerances[opt.removeprefix('ksp_')]) == val
1758+
1759+
@skipif('petsc')
1760+
def test_command_line_priority_2(self, command_line):
1761+
"""
1762+
"""
1763+
prefix = 'riabfodkj'
1764+
_, expected = command_line
1765+
1766+
solver1 = PETScSolve(
1767+
self.eq1, target=self.e,
1768+
options_prefix=prefix
1769+
)
1770+
with switchconfig(language='petsc', log_level='DEBUG'):
1771+
op = Operator(solver1)
1772+
summary = op.apply()
1773+
1774+
petsc_summary = summary.petsc
1775+
entry = petsc_summary.get_entry('section0', prefix)
1776+
for opt, val in expected[prefix]:
1777+
assert str(entry.KSPGetTolerances[opt.removeprefix('ksp_')]) == val
17331778

17341779

17351780
class TestHashing:

0 commit comments

Comments
 (0)