7

pybind 新手 - 阅读文档,但我不知道如何将其应用于 2D 数组。

我有两个存储 3d 坐标的数组shape = (10,3)

a = np.zeros(shape=(10,3))
b = np.ones(shape=(10,3)) * 3
c = a + b

现在,使用 pybind,我如何在 C++ 中对 numpy 数组执行此操作?

在某些文档中,我阅读以使用[]运算符访问元素,在其他文档中使用(). 如何分配 3D 矢量?我将如何获取指向数组元素的指针以使用 strides 进行赋值 - 或者它是否有运算符?

4

2 回答 2

17

PyBind 很棒,向作者/维护者大声喊叫!你有一个几乎可以工作的例子here

适应您的问题,它会给出类似(El Dude评论后编辑的答案):

#include <iostream>
#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();
  py::buffer_info buf2 = input2.request();

  if (buf1.size != buf2.size) {
    throw std::runtime_error("Input shapes must match");
  }

  /*  allocate the buffer */
  py::array_t<double> 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;
  int X = buf1.shape[0];
  int Y = buf1.shape[1];

  for (size_t idx = 0; idx < X; idx++) {
    for (size_t idy = 0; idy < Y; idy++) {
      ptr3[idx*Y + idy] = ptr1[idx*Y+ idy] + ptr2[idx*Y+ idy];
    }
  }
 
  // reshape array to match input shape
  result.resize({X,Y});

  return result;
}


PYBIND11_MODULE(example, m) {
        m.doc() = "Add two vectors using pybind11"; // optional module docstring

        m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
}

我使用 python2.7 和 gcc v5.4 在 linux 上构建(我必须使用与文档中提供的命令略有不同的命令,因为找不到 Python.h,因此我添加了指向 python 2.7 的链接)

c++ -O3 -Wall -shared -std=c++11 -fPIC -I/usr/include/python2.7 -lpython2.7 `python -m pybind11 --includes` example.cpp -o example`python-config --extension-suffix

你会从 python 调用它

import numpy as np
import example # [bad] name I chose for my compiled module

a = np.zeros((10,3))
b = np.ones((10,3)) * 3 
c = example.add_arrays(a, b)

print c

希望能帮助到你。


编辑 - 我创建了一个github 存储库,其中包含一些基于 PyBind11 的完整示例,应该可以在所有平台上编译。

于 2018-04-06T13:16:03.947 回答
1

诀窍是使用缓冲区类。它被很好地隐藏/复杂地隐藏在文档和示例中,但被提及(@Christian 的帖子)。

缓冲区包含指向数据的指针以及步幅和其他数组参数。本质上是通过request方法访问的 numpy 标头。从那里开始很容易使用,但发现它有点痛苦,因为该示例使用漂亮的C11 auto类型来解释这种用法。

于 2018-04-06T17:53:20.087 回答