假设我在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);
}
但是,我希望python看到的MPI_Comm
类型是mpi4py.MPI.Comm
。这可能吗?如果是,怎么做?你知道吗
上述(原始)实现会导致以下行为:
comm = Communicator(MPI.COMM_WORLD)
错误:
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. Communicator()
2. Communicator(arg0: int, arg1: int)
3. Communicator(arg0: int)
以及
comm = Communicator()
print(comm.GetComm())
打印-2080374784
。考虑到MPI_Comm
是什么,这种行为是有意义的,但显然不是我需要的功能。你知道吗
我把包装纸改成
相关问题 更多 >
编程相关推荐