使用 pybind11 共享 MPI 通信器
Sharing an MPI communicator using 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_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
是什么,这种行为是有意义的,但显然不是我需要的功能。
我通过将包装器更改为
#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 共享 MPI 通信器
- MPI 集合通信中的指针分配
- 不安全的 MPI 非阻塞通信示例?
- 管理器类和对象类之间的通信
- MPI 应用程序无法连接到 SMPD 管理器 (空):55846 错误 1722
- 时间效率的设计模型,用于从所有MPI流程发送和接收:MPI全部2个通信
- MPI与许多处理器通信时的错误
- MPI 点对点通信到集体通信
- C# 中与 CLI 包装器通信C++未处理的异常
- MPI发送和接收的通信成本
- 已安装 MPI,但仍出现链接器错误LNK2019
- MPI:获取给定通讯器中所有处理器的等级
- 适用于Linux的免费并行(MPI)调试器
- 如何设置MQ以允许三个服务使用MQ资源管理器相互通信
- lexer和解析器之间的通信
- 将带有迭代器的C++程序转换为 Boost MPI 并行程序
- MPI 全对多通信问题
- 使用通信器进行远程内存访问
- excelc++通信器API: _ChartPtr访问冲突
- MPI 中的非阻塞通信和 MPI 等待问题.并非所有信息都正确传递