mpiexec and python mpi4py gives rank 0 and size 1 for all processes

Viewed 20

I'm on macOS 10.14. Attempting the following in test.py:

from mpi4py import MPI

comm = MPI.COMM_WORLD
i = comm.Get_rank()
p = comm.Get_size()

print('proc {}/{}'.format(i, p))

results in

proc 0/1
proc 0/1
proc 0/1
proc 0/1

The typical cause of this problem, as far as I can tell, is having two different MPI implementations installed. This can happen if the library used in building mpi4py is different than the one providing the mpiexec that I use to run the program.

I thought I did this carefully, though. I built mpi4py from source as per the instructions provided in the docs, via

$ python setup.py build --mpi=openmpi

where openmpi is defined in mpi.cfg as

[openmpi]
mpi_dir              = /usr/local/Cellar/open-mpi/4.0.2
mpicc                = %(mpi_dir)s/bin/mpicc
mpicxx               = %(mpi_dir)s/bin/mpicxx
include_dirs         = %(mpi_dir)s/include
#libraries            = mpi
library_dirs         = %(mpi_dir)s/lib
runtime_library_dirs = %(library_dirs)s

I verify that the install went as intended in python:

$ python
>>> import mpi4py
>>> mpi4py.get_config()
{'mpicc': '/usr/local/Cellar/open-mpi/4.0.2/bin/mpicc', 'mpicxx': '/usr/local/Cellar/open-mpi/4.0.2/bin/mpicxx', 'library_dirs': '/usr/local/Cellar/open-mpi/4.0.2/lib', 'runtime_library_dirs': '/usr/local/Cellar/open-mpi/4.0.2/lib'}

which matches the mpiexec in use:

$ which mpiexec
/usr/local/Cellar/open-mpi/4.0.2/bin/mpiexec

and the situation is identical for mpirun. What am I missing? Is there some "piece" of the package which is for some reason silently expecting mpich, even though this all seems to add up?

1 Answers

I was never able to resolve this particular issue, but instead tried uninstalling my original installation of mpi4py, and then instead installing mpi4py via conda in a fresh miniconda environment, which comes with it's own binaries e.g. mpiexec, mpicc... This is way cleaner than trying to muck around with the dependencies yourself. It now just works, though I need to point to the new mpiexec manually (since my original openmpi installation via homebrew needs to be kept as it remains a dependency for other things). I must do

/Users/me/miniconda3/bin/mpiexec -n 4 python ./test.py

I didn't try this in the first place because my conda installation was having issues unrelated to the present issue. If that's also your roadblock, just reinstall miniconda or whatever Anaconda distro you prefer.

Related