mpi4py: Importing the C API fails on Windows

I am trying to build a C++ Python extension (with pybind11) that uses MPI and mpi4py on Windows. I am working in a conda environment and I installed mpi4py as follows:

conda install mpi4py -c conda-forge --yes

The following:

// import the mpi4py API
if (import_mpi4py() < 0) {
  throw std::runtime_error("Could not load mpi4py API.");
}

throws the exception with traceback:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\IEUser\VeloxChemMP\build_dbg_14.27\lib\python3.8\site-packages\veloxchem\__init__.py", line 2, in <module>
    from .veloxchemlib import AtomBasis
ImportError: Could not load mpi4py API.

Running mpiexec -n 4 python -c “from mpi4py import MPI; comm = MPI.COMM_WORLD; print(comm.Get_rank())” works as expected. I am not sure whether this is a bug or some silly mistake I am making.

This issue was migrated from moved from BitBucket #177

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 60 (38 by maintainers)

Commits related to this issue

Most upvoted comments

I’ll merge the bugfix/msmpi branch soon.

@robertodr Thanks! About a new mpi4py release, it will happen eventually, but not immediately. However, we could easily patch the current conda-forge package to include this fix. Would that be convenient to you?

Remember that if you pass -DPyMPI_HAVE_MPI_Message to your compiler flags, the current release mpi4py release still works.

Or you can keep using the following in your code:

#include <mpi.h>

#ifdef MSMPI_VER
#define PyMPI_HAVE_MPI_Message 1
#endif

#include <mpi4py/mpi4py.h>

I will keep using the #ifdef ... #define ... in my code, keeping an eye out for new releases. Thanks!

After the rebase, it builds (https://gitlab.com/robertodr/pybind11-mpi4py/-/jobs/1226563962) and runs tests (https://gitlab.com/robertodr/pybind11-mpi4py/-/jobs/1226563986) without problems. Will there be a new release of mpi4py after the branch is merged?

@leofang thanks for your help!

OK, I misunderstood the whole conversation then 🤦🏻

Ha we replied at the same time. Yeah sorry I got confused too. Glad it worked out.

@robertodr @dalcinl Looks like the bugfix/msmpi branch + the latest msmpi conda package makes everything work! https://gitlab.com/robertodr/pybind11-mpi4py/-/pipelines/294561734

Hi @robertodr I see you removed this snippet

#ifdef MSMPI_VER
#define PyMPI_HAVE_MPI_Message 1
#endif

But we didn’t patch the mpi4py conda package, so IIUC this is still needed. Maybe you should follow Lisandro’s advice on applying the patch from this branch: https://github.com/mpi4py/mpi4py/compare/bugfix/msmpi.

OK, I misunderstood the whole conversation then 🤦🏻 Indeed with the new msmpi package and mpi4py from the bugfix branch the build works and tests pass: https://gitlab.com/robertodr/pybind11-mpi4py/-/jobs/1223946613

The build and test jobs are split in two stages. The test stage sets up the environment and picks up the build artifacts from the previous one. I can run them together, though I doubt it would make a difference.

Thanks for testing @robertodr! OK looks like we need a rebuild. @dalcinl Please check out conda-forge/mpi4py-feedstock#43 when you have time.

Thank you for working on it so thoroughly!

Thanks for testing @robertodr! OK looks like we need a rebuild. @dalcinl Please check out https://github.com/conda-forge/mpi4py-feedstock/pull/43 when you have time.

It still fails: https://gitlab.com/robertodr/pybind11-mpi4py/-/jobs/1221796371 but at least we’re back to:

ImportError: ValueError: mpi4py.MPI.Message size changed, may indicate binary incompatibility. Expected 40 from C header, got 32 from PyObject

@robertodr btw, in case this is still a concern: I rebuilt msmpi on Conda-Forge so that it has the correct MPI version. Maybe you’d like to give it a shot and see if you still see any warnings/errors?

Uhm, now testing fails: https://gitlab.com/robertodr/pybind11-mpi4py/-/jobs/1211049781#L185

First, let me write the correct MSMPI_VER to mpi.h

I believe this should be enough for current mpi4py release to build without any patch.

I believe that the problem comes from using MSMPI from conda-forge. mpi4py is not seeing the right MS MPI version number at build time, because that does not come from mpi.h, but from the Windows registry (when MS MPI is installed from the official Microsoft installer packages).

@leofang @RyanMcCarthy-NOAA The MSMPI package in conda-forge is slightly broken, MSMPI_VER from mpi.h does not reflect the actual version number. Because of that, the mpi4py package is disabling a lot of MPI features (e.g. MPI_Message and matched probes).

@robertodr Could you please try the bugfix/msmpi branch I just pushed (commit 2b8fc6f?

Hopefully in the weekend!

@robertodr Could you please try the bugfix/msmpi branch I just pushed (commit 2b8fc6f)?

Thanks the help! Do you wish this issue to be kept open until you figure out a more permanent fix?

What about the following way? [link]

if (import_mpi4py() < 0) { throw py::error_already_set(); }

Oh, sorry, I should have figured it out… The problem is that your wrapper is swallowing the actual Python exception and traceback, so this is not really useful.

Can you try to build and run the code in mpi4py’s demo/wrap-c directory? If that one fails, we should get the nice traceback we are looking for.