Python 使用pybind11共享MPI通信器

Python 使用pybind11共享MPI通信器,python,c++,mpi,pybind11,Python,C++,Mpi,Pybind11,假设我在MPI通信器周围创建了一个包装器: class Communicator { public: Communicator() : comm(MPI_COMM_WORLD) {} Communicator(int const color, int const key) { MPI_Comm_split(MPI_COMM_WORLD, color, key, &comm); } Communicator(MPI_

假设我在MPI通信器周围创建了一个包装器:

class Communicator {
   public:
      Communicator() : comm(MPI_COMM_WORLD) {}

      Communicator(int const color, int const key) {
        MPI_Comm_split(MPI_COMM_WORLD, color, key, &comm);
      }

      Communicator(MPI_Comm comm) : comm(comm) {}

      MPI_Comm GetComm() const { return comm; }
    private:
      MPI_Comm comm;
}; 
我想使用pybind11围绕这个对象创建一个python包装器,看起来像这样:

void CommunicatorWrapper(pybind11::module &m) {
   py::class_<Communicator, std::shared_ptr<Communicator> > commWrap(m, "Communicator");

   commWrap.def(py::init( []() { return new Communicator(); } ));
   commWrap.def(py::init( [](int const color, int const key) { return new Communicator(color, key); } ));
   commWrap.def(py::init( [](MPI_Comm comm) { return new Communicator(comm); } ));
   commWrap.def("GetComm", &Communicator::GetComm);
}
错误:

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. Communicator()
2. Communicator(arg0: int, arg1: int)
3. Communicator(arg0: int)


打印
-2080374784
。考虑到MPI_Comm是什么,这种行为是有意义的,但显然不是我需要的功能。

我通过将包装器更改为

#include <mpi4py/mpi4py.h>

pybind11::handle CallGetComm(Communicator *comm) {
    const int rc = import_mpi4py();
    return pybind11::handle(PyMPIComm_New(comm->GetComm()));;
}

void CommunicatorWrapper(pybind11::module &m) {
   py::class_<Communicator, std::shared_ptr<Communicator> > commWrap(m, "Communicator");

   commWrap.def(py::init( []() { return new Communicator(); } ));
   commWrap.def(py::init( [](int const color, int const key) { return new Communicator(color, key); } ));
   commWrap.def(py::init( [](pybind11::handle const& comm) {
     const int rc = import_mpi4py();
     assert(rc==0);
     return new Communicator(*PyMPIComm_Get(comm.ptr()));
    } ));
   commWrap.def("GetComm", &CallGetComm);
}
#包括
pybind11::handle CallGetComm(通信器*comm){
const int rc=import_mpi4py();
返回pybind11::handle(PyMPIComm_New(comm->GetComm());;
}
无效通讯器包装器(pybind11::模块&m){
py::类_uCommWrap(m,“通信器”);
def(py::init([](){returnnewcommunicator();}));
def(py::init([](int const color,int const key){返回新的通信器(color,key);});
def(py::init([](pybind11::handle const&comm){
const int rc=import_mpi4py();
断言(rc==0);
返回新通信器(*PyMPIComm_Get(comm.ptr());
} ));
commWrap.def(“GetComm”和&CallGetComm);
}
似乎相关,但ob\u mpi对象似乎并不实际存在。
comm = Communicator()
print(comm.GetComm())
#include <mpi4py/mpi4py.h>

pybind11::handle CallGetComm(Communicator *comm) {
    const int rc = import_mpi4py();
    return pybind11::handle(PyMPIComm_New(comm->GetComm()));;
}

void CommunicatorWrapper(pybind11::module &m) {
   py::class_<Communicator, std::shared_ptr<Communicator> > commWrap(m, "Communicator");

   commWrap.def(py::init( []() { return new Communicator(); } ));
   commWrap.def(py::init( [](int const color, int const key) { return new Communicator(color, key); } ));
   commWrap.def(py::init( [](pybind11::handle const& comm) {
     const int rc = import_mpi4py();
     assert(rc==0);
     return new Communicator(*PyMPIComm_Get(comm.ptr()));
    } ));
   commWrap.def("GetComm", &CallGetComm);
}