Skip to content

WIP Emit a helpful error message when trying to run in forking mode #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pytest_mpi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,28 @@ def pytest_generate_tests(metafunc):

@pytest.hookimpl()
def pytest_collection_modifyitems(config, items):
from mpi4py import MPI

global _plugin_in_use

_plugin_in_use = any(item.get_closest_marker("parallel") for item in items)

if not _plugin_in_use:
return

if MPI.COMM_WORLD.size == 1:
has_multi_rank_tests = False
for item in items:
if item.get_closest_marker("parallel") and _extract_nprocs_for_single_test(item) > 1:
has_multi_rank_tests = True
break
if has_multi_rank_tests and not _mpi_supports_multiple_inits():
raise pytest.UsageError(
"MPI library does not support multiple calls to MPI_Init so parallel "
"tests must be run with mpiexec on the 'outside'. For example:\n\t"
"mpiexec -n 2 pytest test_something.py -m parallel[2]"
)

for item in items:
if item.get_closest_marker("parallel"):
# Add extra markers to each test to allow for querying specific levels of
Expand Down Expand Up @@ -191,6 +206,17 @@ def _xdist_active(session):
return False


def _mpi_supports_multiple_inits():
from mpi4py import MPI

assert MPI.Is_initialized()
try:
MPI.Init()
return True
except MPI.Exception:
return False


def _set_parallel_callback(item):
"""Replace the callback for a test item with one that calls ``mpiexec``.

Expand Down
Loading