mpi.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import mpi4py.futures
  2. from mpi4py import MPI
  3. from .parallel import ProcessPoolExecutor
  4. class MPIPoolExecutor(ProcessPoolExecutor):
  5. """
  6. The `MPIPoolExecutor` class uses a pool of MPI processes to execute calls
  7. asynchronously.
  8. `MPIPoolExecutor` is recommended to use on cluster nodes which support
  9. MPI-2 protocol.
  10. Notes
  11. -----
  12. `-m mpi4py.futures` command line option is needed to execute Python scripts
  13. with MPI:
  14. .. code-block:: sh
  15. mpiexec -n numprocs python -m mpi4py.futures pyfile [arg] ...
  16. For more information of how to launch MPI processes in Python refer to
  17. https://mpi4py.readthedocs.io/en/stable/mpi4py.futures.html#command-line
  18. """
  19. def _create_executor(self):
  20. return mpi4py.futures.MPIPoolExecutor(self.max_workers)
  21. def _extra_repr(self):
  22. extra_old = super(MPIPoolExecutor, self)._extra_repr()
  23. info = dict(MPI.INFO_ENV)
  24. return "{old}, {mpi}".format(old=extra_old, mpi=info)
  25. class MPICommExecutor(MPIPoolExecutor):
  26. """
  27. Legacy MPI-1 implementation for cluster nodes which do not support MPI-2
  28. protocol.
  29. Parameters
  30. ----------
  31. comm : MPI.Intracomm or None
  32. MPI (intra)communicator. If None, set to `MPI.COMM_WORLD`.
  33. Default: None
  34. root : int
  35. Designated master process.
  36. Default: 0
  37. Notes
  38. -----
  39. `-m mpi4py.futures` command line option is needed to execute Python scripts
  40. with MPI:
  41. .. code-block:: sh
  42. mpiexec -n numprocs python -m mpi4py.futures pyfile [arg] ...
  43. For more information of how to launch MPI processes in Python refer to
  44. https://mpi4py.readthedocs.io/en/stable/mpi4py.futures.html#command-line
  45. """
  46. def __init__(self, comm=None, root=0):
  47. super(MPICommExecutor, self).__init__(max_workers=None)
  48. if comm is None:
  49. comm = MPI.COMM_WORLD
  50. self.comm = comm
  51. self.root = root
  52. def _create_executor(self):
  53. return mpi4py.futures.MPICommExecutor(comm=self.comm, root=self.root)