【发布时间】:2018-08-22 03:44:55
【问题描述】:
我创建了一个 c++ 函数来创建具有正确内存对齐的 numpy 数组。
不知何故,python 和 c++ 为“相同”数组打印了两个不同的地址。
这是否意味着pybind11仍然复制数组?
如果是这样,如何避免抄袭?
pybind11 来自:https://github.com/pybind/pybind11/archive/stable.zip
c++代码:
#include <Eigen/Eigen>
#include <iostream>
#include <pybind11/eigen.h>
template <typename T>
inline Array<T, Dynamic, Dynamic>* eigen_empty(Index rows, Index cols) {
auto a = new Array<T, Dynamic, Dynamic>(rows, cols);
std::cout << "Address: " << (long long) a << "\n";
return a;
}
namespace py = pybind11;
PYBIND11_MODULE(_cxx, m) {
m.def("eigen_empty_d", &eigen_empty<double>,
py::return_value_policy::take_ownership);
} // python would "take_ownership" of the array
python 测试:
>>> x=eigen_empty_d(10,1);x.__array_interface__['data'][0];x.flags.owndata
Address: 32962272 # address printed by c++
29726560 # address from python
False # numpy does not own the data
>>> x=eigen_empty_d(10,1);x.__array_interface__['data'][0];x.flags.owndata
Address: 32962240
29511904
False
>>> x=eigen_empty_d(10,1);x.__array_interface__['data'][0];x.flags.owndata
Address: 29516656
29726560
False
>>> x=eigen_empty_d(10,1);x.__array_interface__['data'][0];x.flags.owndata
Address: 33209440
29726560
False
>>> x=eigen_empty_d(10,1);x.__array_interface__['data'][0];x.flags.owndata
Address: 29429712
29511904
False
【问题讨论】:
-
地址不同,因为它们指向不同类型的不同对象。在 tge Python 端,你有数据(实际的数字数组),在 C++ 端,你有 Eigen Array 对象。
-
但有时c++的数字比python大。有时它更小。这不仅仅是一个持续的转变。
-
哦。我得到它。应该从特征数组打印
(*a).data()。 -
现在它们匹配了。谢谢。