Method for pybind11 and numpy to Interact
- 2021-07-09 08:45:01
- OfStack
You can interact with numpy using an buffer protocol-compliant object.
What does this buffer_protocol need? To have the following interface:
struct buffer_info {
void *ptr;
ssize_t itemsize;
std::string format;
ssize_t ndim;
std::vector<ssize_t> shape;
std::vector<ssize_t> strides;
};
In fact, a pointer to the array + information about each dimension is enough. Then we can use pointer + offset to access the number at any position in the number.
Here is an example of running:
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
py::buffer_info buf1 = input1.request(), buf2 = input2.request();
if (buf1.ndim != 1 || buf2.ndim != 1)
throw std::runtime_error("Number of dimensions must be one");
if (buf1.size != buf2.size)
throw std::runtime_error("Input shapes must match");
/* No pointer is passed, so NumPy will allocate the buffer */
auto result = py::array_t<double>(buf1.size);
py::buffer_info buf3 = result.request();
double *ptr1 = (double *) buf1.ptr,
*ptr2 = (double *) buf2.ptr,
*ptr3 = (double *) buf3.ptr;
for (size_t idx = 0; idx < buf1.shape[0]; idx++)
ptr3[idx] = ptr1[idx] + ptr2[idx];
return result;
}
PYBIND11_MODULE(test, m) {
m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
}
buf in array_t is a compatible interface.
Dimension information of pointers and corresponding numbers can be obtained in buf.
For convenience, we can even use Eigen as our numpy compatible interface:
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <Eigen/LU>
// N.B. this would equally work with Eigen-types that are not predefined. For example replacing
// all occurrences of "Eigen::MatrixXd" with "MatD", with the following definition:
//
// typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatD;
Eigen::MatrixXd inv(const Eigen::MatrixXd &xs)
{
return xs.inverse();
}
double det(const Eigen::MatrixXd &xs)
{
return xs.determinant();
}
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
m.doc() = "pybind11 example plugin";
m.def("inv", &inv);
m.def("det", &det);
}
More references:
https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html
https://github.com/tdegeus/pybind11_examples
Summarize