pybind11 数据交互

pybind提供了较方便的数据交互方式,有很多c++类型可以直接在Python中使用,但是为了使程序更加高效,往往需要手动转换

?1

void print_vector(const std::vector<int> &v) {
    for (auto item : v)
        std::cout << item << "\n";
}

>>> print_vector([1, 2, 3])
#这里python可以直接调用c++中的函数,但是传入参数后,参数会默认copy

为了避免复制,可以手动将python中的类型传入到C++中。

void print_list(py::list my_list) {
    for (auto item : my_list)
        std::cout << item << " ";
}

?2

与上面类似,Eigen::MatrixXdnumpy.ndarray也可以通过copy的形式直接在c++或python中使用。
为了避免传值copy,也可以使用传引用的方式,此时c++中要使用Eigen::Ref<const MatrixType> 并且,应该满足1.数据类型相同double与float64;2.Storage orders要相同。 注意如果MatrixType前面没有const此时要求numpy array是writeable才可传引用。为了避免复制,可以bind参数时设置py::arg().noconvert()

c++传到python中时也可以通过设置return value policy等保证传引用

 class MyClass {
            Eigen::MatrixXd big_mat = Eigen::MatrixXd::Zero(10000, 10000);
        public:
            Eigen::MatrixXd &getMatrix() { return big_mat; }
            const Eigen::MatrixXd &viewMatrix() { return big_mat; }
        };
        
        // Later, in binding code:
        py::class_<MyClass>(m, "MyClass")
            .def(py::init<>())
            .def("copy_matrix", &MyClass::getMatrix) // Makes a copy!
            .def("get_matrix", &MyClass::getMatrix, py::return_value_policy::reference_internal)
            .def("view_matrix", &MyClass::viewMatrix, py::return_value_policy::reference_internal)
            ;
a = MyClass()
m = a.get_matrix()   # flags.writeable = True,  flags.owndata = False
v = a.view_matrix()  # flags.writeable = False, flags.owndata = False
c = a.copy_matrix()  # flags.writeable = True,  flags.owndata = True
# m[5,6] and v[5,6] refer to the same element, c[5,6] does not.

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值