Sharing an MPI communicator using pybind11(使用pybind11共享MPI通信器)
本文介绍了使用pybind11共享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);
}
但是,我希望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通信器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:使用pybind11共享MPI通信器
基础教程推荐
猜你喜欢
- c++ STL设置差异 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
