I have a C++ class which I intend to call from python's mpi4py interface such that each node spawns the class. On the C++ side, I'm using the Open MPI library (installed via homebrew) and pybind11.
The C++ class is as follows:
#include <pybind11/pybind11.h>
#include <iostream>
#include <chrono>
#include <thread>
#include <vector>
#include <mpi.h>
// #define PyMPI_HAVE_MPI_Message 1
// #include <mpi4py/mpi4py.h>
namespace py = pybind11;
class SomeComputation
{
float multiplier;
std::vector<int> test;
MPI_Comm comm_;
public:
void Init()
{
int rank;
MPI_Comm_rank(comm_, &rank);
test.clear();
test.resize(10, rank);
}
void set_comm(MPI_Comm comm){
this->comm_ = comm;
}
SomeComputation(float multiplier_) : multiplier(multiplier_){}
~SomeComputation() { std::cout << "Destructor Called!\n"; }
float compute(float input)
{
std::this_thread::sleep_for(std::chrono::milliseconds((int)input * 10));
for (int i = 0; i != 10; ++i)
{
std::cout << test[i] << " ";
}
std::cout << std::endl;
return multiplier * input;
}
};
PYBIND11_MODULE(module_name, handle)
{
py::class_<SomeComputation>(handle, "Cpp_computation")
.def(py::init<float>()) // args of constructers are template args
.def("set_comm", &SomeComputation::set_comm)
.def("compute", &SomeComputation::compute)
.def("cpp_init", &SomeComputation::Init);
}
and here's the python interface spawning the same C++:
from build.module_name import *
import time
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
m = Cpp_computation(44.0) # send communicator to cpp
m.cpp_init()
i = 0
while i < 5:
print(m.compute(i))
time.sleep(1)
i+=1
I've already tried "Sharing an MPI communicator using pybind11" but I'm stuck at a long unhelpful error (full message):
[...]
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/pybind11.h:1398:22: required from 'pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = void (SomeComputation::*)(ompi_communicator_t*); Extra = {}; type_ = SomeComputation; options = {}]'
/Users/purusharth/Documents/hiwi/pympicontroller/main.cpp:79:7: required from here
/opt/homebrew/Cellar/gcc/11.2.0_3/include/c++/11/type_traits:1372:38: error: invalid use of incomplete type 'struct ompi_communicator_t'
1372 | : public integral_constant<bool, __is_base_of(_Base, _Derived)>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Users/purusharth/Documents/hiwi/pympicontroller/main.cpp:6:
/opt/homebrew/Cellar/open-mpi/4.1.2/include/mpi.h:419:16: note: forward declaration of 'struct ompi_communicator_t'
419 | typedef struct ompi_communicator_t *MPI_Comm;
| ^~~~~~~~~~~~~~~~~~~
[...]
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/pybind11.h:1398:22: required from 'pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = void (SomeComputation::*)(ompi_communicator_t*); Extra = {}; type_ = SomeComputation; options = {}]'
/Users/purusharth/Documents/hiwi/pympicontroller/main.cpp:79:7: required from here
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/detail/descr.h:40:19: error: invalid use of incomplete type 'struct ompi_communicator_t'
40 | return {{&typeid(Ts)..., nullptr}};
| ^~~~~~~~~~
In file included from /Users/purusharth/Documents/hiwi/pympicontroller/main.cpp:6:
/opt/homebrew/Cellar/open-mpi/4.1.2/include/mpi.h:419:16: note: forward declaration of 'struct ompi_communicator_t'
419 | typedef struct ompi_communicator_t *MPI_Comm;
| ^~~~~~~~~~~~~~~~~~~
[...]
from /Users/purusharth/Documents/hiwi/pympicontroller/main.cpp:1:
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/detail/descr.h:40:42: error: could not convert '{{<expression error>, nullptr}}' from '<brace-enclosed initializer list>' to 'std::array<const std::type_info*, 3>'
40 | return {{&typeid(Ts)..., nullptr}};
| ^
| |
| <brace-enclosed initializer list>
[...]
In file included from /Users/purusharth/Documents/hiwi/pympicontroller/main.cpp:1:
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/pybind11.h: In instantiation of 'void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = pybind11::cpp_function::cpp_function<void, SomeComputation, ompi_communicator_t*, pybind11::name, pybind11::is_method, pybind11::sibling>(void (SomeComputation::*)(ompi_communicator_t*), const pybind11::name&, const pybind11::is_method&, const pybind11::sibling&)::<lambda(SomeComputation*, ompi_communicator_t*)>; Return = void; Args = {SomeComputation*, ompi_communicator_t*}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]':
[..]
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/pybind11.h:1398:22: required from 'pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = void (SomeComputation::*)(ompi_communicator_t*); Extra = {}; type_ = SomeComputation; options = {}]'
/Users/purusharth/Documents/hiwi/pympicontroller/main.cpp:79:7: required from here
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/pybind11.h:266:73: in 'constexpr' expansion of 'pybind11::detail::descr<18, SomeComputation, ompi_communicator_t>::types()'
/Users/purusharth/Documents/hiwi/pympicontroller/pybind11/include/pybind11/pybind11.h:266:39: error: 'constexpr' call flows off the end of the function
266 | PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types();
| ^~~~~
The error points to .def("set_comm", &SomeComputation::set_comm)
What is the cause of these errors, and how should they be resolved?
UPDATE: Added answer below by using custom type caster as explained in this answer. But is it the only way to go about it?