aragilar / pytest-mpi

Pytest plugin for working with MPI
https://pytest-mpi.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
19 stars 7 forks source link

Question: Can you please provide an example with if-statements? Does it work with pytest hypothesis? #15

Closed bcm0 closed 3 years ago

bcm0 commented 3 years ago

Like suggested in the docs I run mpirun -n 2 python -m pytest --with-mpi.

import pytest
from mpi4py import MPI
import hypothesis.strategies as st

@pytest.mark.mpi
@given(senddata=st.integers())
def test_simple(self, senddata):
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()

    if rank == 0:
        comm.send(senddata, dest=1)
    elif rank == 1:
        recvdata = comm.recv(source=0)
        assert senddata == recvdata

The input arguments get mixed and the assert fails. Most likely this is due to the tests being run twice with mpirun -n 2. I would love to see a working example.

aragilar commented 3 years ago

h5py uses pytest-mpi (that's why I wrote it).

I suspect in your case though the problem is hypothesis starts with different random numbers or uses a different seed - you probably need to configure it (or the environment it runs in) to make sure both processes use the same ordering of numbers.

bcm0 commented 3 years ago

I found hypothesis seeds and now my problem is fixed. Thank you very much! mpirun -n 2 python -m pytest --with-mpi --hypothesis-seed 1234