-
Notifications
You must be signed in to change notification settings - Fork 221
/
conftest.py
173 lines (150 loc) · 5.47 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
from subprocess import check_call
import pytest
from sympy import cos, Symbol # noqa
from devito import (Grid, TimeDimension, SteppingDimension, SpaceDimension, # noqa
Constant, Function, TimeFunction, Eq, configuration, SparseFunction, # noqa
SparseTimeFunction) # noqa
from devito.compiler import sniff_mpi_distro
from devito.tools import as_tuple
try:
from mpi4py import MPI # noqa
except ImportError:
MPI = None
def skipif(items, whole_module=False):
assert isinstance(whole_module, bool)
items = as_tuple(items)
# Sanity check
accepted = set(configuration._accepted['backend'])
accepted.update({'no%s' % i for i in configuration._accepted['backend']})
accepted.update({'nompi'})
unknown = sorted(set(items) - accepted)
if unknown:
raise ValueError("Illegal skipif argument(s) `%s`" % unknown)
skipit = False
for i in items:
# Skip if no MPI
if i == 'nompi':
if MPI is None:
skipit = "mpi4py/MPI not installed"
break
continue
# Skip if an unsupported backend
if i == configuration['backend']:
skipit = "`%s` backend unsupported" % i
break
try:
_, noi = i.split('no')
if noi != configuration['backend']:
skipit = "`%s` backend unsupported" % i
break
except ValueError:
pass
if skipit is False:
return pytest.mark.skipif(False, reason='')
else:
if whole_module:
return pytest.skip(skipit, allow_module_level=True)
else:
return pytest.mark.skip(skipit)
def EVAL(exprs, *args):
"""
Convert strings into SymPy objects.
Required to work around this 'won't fix' Python3 issue: ::
http://stackoverflow.com/questions/29336616/eval-scope-in-python-2-vs-3
"""
# Cannot use list comprehension because of the issue linked in the docstring
scope = {}
for i in args:
try:
scope[i.base.function.name] = i
for j in i.base.function.indices:
scope[j.name] = j
except AttributeError:
scope[i.label.name] = i
for j in i.function.indices:
scope[j.name] = j
processed = []
for i in as_tuple(exprs):
processed.append(eval(i, globals(), scope))
return processed[0] if isinstance(exprs, str) else processed
def parallel(item):
"""
Run a test in parallel. Readapted from:
``https://github.com/firedrakeproject/firedrake/blob/master/tests/conftest.py``
"""
mpi_exec = 'mpiexec'
mpi_distro = sniff_mpi_distro(mpi_exec)
marker = item.get_closest_marker("parallel")
mode = as_tuple(marker.kwargs.get("mode", 2))
for m in mode:
# Parse the `mode`
if isinstance(m, int):
nprocs = m
scheme = 'basic'
restrain = False
else:
if len(m) == 2:
nprocs, scheme = m
restrain = False
elif len(m) == 3:
nprocs, scheme, restrain = m
else:
raise ValueError("Can't run test: unexpected mode `%s`" % m)
if restrain and os.environ.get('MPI_RESTRAIN', False):
# A computationally expensive test that would take too long to
# run on the current machine
continue
# Only spew tracebacks on rank 0.
# Run xfailing tests to ensure that errors are reported to calling process
if item.cls is not None:
testname = "%s::%s::%s" % (item.fspath, item.cls.__name__, item.name)
else:
testname = "%s::%s" % (item.fspath, item.name)
args = ["-n", "1", "python", "-m", "pytest", "--runxfail", "-s",
"-q", testname]
if nprocs > 1:
args.extend([":", "-n", "%d" % (nprocs - 1), "python", "-m", "pytest",
"--runxfail", "--tb=no", "-q", testname])
# OpenMPI requires an explicit flag for oversubscription. We need it as some
# of the MPI tests will spawn lots of processes
if mpi_distro == 'OpenMPI':
call = [mpi_exec, '--oversubscribe'] + args
else:
call = [mpi_exec] + args
# Tell the MPI ranks that they are running a parallel test
os.environ['DEVITO_MPI'] = scheme
try:
check_call(call)
finally:
os.environ['DEVITO_MPI'] = '0'
def pytest_configure(config):
"""Register an additional marker."""
config.addinivalue_line(
"markers",
"parallel(mode): mark test to run in parallel"
)
def pytest_runtest_setup(item):
partest = os.environ.get('DEVITO_MPI', 0)
try:
partest = int(partest)
except ValueError:
pass
if item.get_closest_marker("parallel") and not partest:
# Blow away function arg in "master" process, to ensure
# this test isn't run on only one process
dummy_test = lambda *args, **kwargs: True
if item.cls is not None:
attr = item.originalname or item.name
setattr(item.cls, attr, dummy_test)
else:
item.obj = dummy_test
def pytest_runtest_call(item):
partest = os.environ.get('DEVITO_MPI', 0)
try:
partest = int(partest)
except ValueError:
pass
if item.get_closest_marker("parallel") and not partest:
# Spawn parallel processes to run test
parallel(item)