Skip to content

Commit

Permalink
Parallel test configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallwork23 committed Nov 26, 2021
1 parent 6bb0da6 commit 9970f2c
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from subprocess import check_call


def pytest_runtest_teardown(item, nextitem):
Expand All @@ -12,3 +13,84 @@ def pytest_runtest_teardown(item, nextitem):
Kernel._cache.clear()
TSFCKernel._cache.clear()
JITModule._cache.clear()


def parallel(item):
"""
Run a test in parallel.
NOTE: Copied from firedrake/src/firedrake/tests/conftest.py
:arg item: The test item to run.
"""
from mpi4py import MPI
if MPI.COMM_WORLD.size > 1:
raise RuntimeError("Parallel test can't be run within parallel environment")
marker = item.get_closest_marker("parallel")
if marker is None:
raise RuntimeError("Parallel test doesn't have parallel marker")
nprocs = marker.kwargs.get("nprocs", 3)
if nprocs < 2:
raise RuntimeError("Need at least two processes to run parallel test")

# Only spew tracebacks on rank 0.
# Run xfailing tests to ensure that errors are reported to calling process
call = [
"mpiexec", "-n", "1", "python", "-m", "pytest", "--runxfail", "-s", "-q",
"%s::%s" % (item.fspath, item.name)
]
call.extend([
":", "-n", "%d" % (nprocs - 1), "python", "-m", "pytest", "--runxfail", "--tb=no", "-q",
"%s::%s" % (item.fspath, item.name)
])
check_call(call)


def pytest_configure(config):
"""
Register an additional marker.
NOTE: Copied from firedrake/src/firedrake/tests/conftest.py
"""
config.addinivalue_line(
"markers",
"parallel(nprocs): mark test to run in parallel on nprocs processors")


def pytest_runtest_setup(item):
"""
Special setup for parallel tests.
NOTE: Copied from firedrake/src/firedrake/tests/conftest.py
"""
if item.get_closest_marker("parallel"):
from mpi4py import MPI
if MPI.COMM_WORLD.size > 1:
# Turn on source hash checking
from firedrake import parameters
from functools import partial

def _reset(check):
parameters["pyop2_options"]["check_src_hashes"] = check

# Reset to current value when test is cleaned up
item.addfinalizer(partial(_reset,
parameters["pyop2_options"]["check_src_hashes"]))

parameters["pyop2_options"]["check_src_hashes"] = True
else:
# Blow away function arg in "master" process, to ensure
# this test isn't run on only one process.
item.obj = lambda *args, **kwargs: True


def pytest_runtest_call(item):
"""
Special call for parallel tests.
NOTE: Copied from firedrake/src/firedrake/tests/conftest.py
"""
from mpi4py import MPI
if item.get_closest_marker("parallel") and MPI.COMM_WORLD.size == 1:
# Spawn parallel processes to run test
parallel(item)

0 comments on commit 9970f2c

Please sign in to comment.