Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Allow custom communicators #12

@matz-e

Description

@matz-e

This is technically not as trivial as expected, since it requires something along the lines of

#include <mpi4py/mpi4py.h>
 
namespace pybind11 { namespace detail {
  template <> struct type_caster<mpi4py_comm> {
    public:
      PYBIND11_TYPE_CASTER(mpi4py_comm, _("mpi4py_comm"));

      // Python -> C++
      bool load(handle src, bool) {
        PyObject *py_src = src.ptr();

        // Check that we have been passed an mpi4py communicator
        if (PyObject_TypeCheck(py_src, &PyMPIComm_Type)) {
          // Convert to regular MPI communicator
          value.value = *PyMPIComm_Get(py_src);
        } else {
          return false;
        }

        return !PyErr_Occurred();
      }

      // C++ -> Python
      static handle cast(mpi4py_comm src,
                         return_value_policy /* policy */,
                         handle /* parent */)
      {
        // Create an mpi4py handle
        return PyMPIComm_New(src.value);
      }
  };
}} // namespace pybind11::detail

See: https://stackoverflow.com/a/62449190

The issue is that it requires that we build our C++ against the C++ backend of mpi4py. Which opens us up to incompatibilities, e.g., because pip install mpi4py installs one version but CMake picks up an entirely different version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions