24#include "openPMD/config.hpp"
28#include "openPMD/binding/python/Common.hpp"
41 PyObject_HEAD MPI_Comm ob_mpi;
51 is_not_an_mpi_communicator
53 using error_type = error_type;
55 std::string error_msg;
58 operator std::string()
const
64inline std::variant<MPI_Comm, py_object_to_mpi_comm_error>
65pythonObjectAsMpiComm(pybind11::object &comm)
67 namespace py = pybind11;
77 using e_t = e::error_type;
79 if (comm.ptr() == Py_None)
80 return e{
"MPI communicator cannot be None.", e_t::invalid_data};
81 if (comm.ptr() ==
nullptr)
82 return e{
"MPI communicator is a nullptr.", e_t::invalid_data};
89 py::str
const comm_pystr = py::repr(comm);
90 std::string
const comm_str = comm_pystr.cast<std::string>();
91 if (comm_str.substr(0, 12) != std::string(
"<mpi4py.MPI."))
93 "comm is not an mpi4py communicator: " + comm_str,
94 e_t::is_not_an_mpi_communicator};
96 if (!py::isinstance<py::class_<openPMD_PyMPIIntracommObject> >(
100 "comm has unexpected type layout in " + comm_str +
101 " (Mismatched MPI at compile vs. runtime? "
102 "Breaking mpi4py release?)",
110 MPI_Comm *mpiCommPtr =
111 &((openPMD_PyMPIIntracommObject *)(comm.ptr()))->ob_mpi;
113 if (PyErr_Occurred())
114 return e{
"MPI communicator access error.", e_t::invalid_data};
115 if (mpiCommPtr ==
nullptr)
118 "MPI communicator cast failed. "
119 "(Mismatched MPI at compile vs. runtime?)",
122 return {*mpiCommPtr};
mpi4py communicator wrapper
Definition Mpi.hpp:40