亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無需復制即可將 C++ 的 Eigen::Matrix 數組返回到 Python

無需復制即可將 C++ 的 Eigen::Matrix 數組返回到 Python

呼如林 2023-04-11 15:27:41
我有一些生成和操作矩陣數組的 C++ 代碼Eigen。最后我想在 python 中使用這些矩陣,并認為這可能是pybind11.基本上我想要在 python 中返回的是兩個嵌套列表/numpy 數組 mat_a(I, 4, 4)和mat_b(J, K, 4, 4). 因為我必須在 C++ 中做很多線性代數的東西,所以我想使用 Eigen,我使用的數據結構是 std::array<std::array<Eigen::Matrix4f, 2>, 3>>> mat_b  // for J=3, K=2. 現在的問題是如何有效地將它傳遞給python?此外,我想對多個輸入x = [x_0, x_1, ..., x_N] 執行這些計算,結果mat_a(N, I, 4, 4)超出mat_b(N, J, K, 4, 4)預期。每個計算都是獨立的,但我認為用 C++x_i重寫這個循環可能會更快。x_i另一方面,如果我們在 C++ 中只有固定大小的數組,任務會變得更容易,這個循環也可以轉移到 python。這是我的問題的一些虛擬代碼(I=5,J=3,K=2):// example.cpp#include <pybind11/pybind11.h>#include <pybind11/eigen.h>#include <pybind11/stl.h>#include <pybind11/functional.h>#include <pybind11/stl_bind.h>#include <array>#include <vector>#include <Eigen/Dense>Eigen::Matrix4f get_dummy(){    Eigen::Matrix4f mat_a;    mat_a << 1, 2, 3, 4,             5, 6, 7, 8,             9, 8, 7, 6,             5, 4, 3, 2;    return mat_a;}std::pair< std::vector<std::array<Eigen::Matrix4f, 5> >,           std::vector<std::array<std::array<Eigen::Matrix4f, 2>, 3> > >  get_matrices(std::vector<float> & x){    std::vector<std::array<Eigen::Matrix4f, 5> > mat_a(x.size());    std::vector< std::array< std::array< Eigen::Matrix4f, 2>, 3> > mat_b(x.size());    //    for (u_int i=0; i< x.size(); i++)    //        do_stuff(x[i], mat_a[i], mat_b[i]);    mat_a[0][0] = get_dummy();    return std::make_pair(mat_a, mat_b);    }PYBIND11_MODULE(example, m) {    m.def("get_dummy", &get_dummy, pybind11::return_value_policy::reference_internal);    m.def("get_matrices", &get_matrices, pybind11::return_value_policy::reference_internal);}我通過以下方式編譯代碼:c++ -O3 -Wall -shared -std=c++14 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
查看完整描述

2 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

您最好的選擇可能是在 python 端創建數據,以便對其進行重新計數和垃圾收集。


test.py


import example

import numpy as np


array = np.zeros((3, 2, 4, 4), 'f4')


example.do_math(array, 3, 2)

print(array[0, 0])

例子.cpp


#define PY_SSIZE_T_CLEAN

#include <Python.h>


#include <Eigen/Dense>


Eigen::Matrix4f get_dummy() {

    Eigen::Matrix4f mat_a;

    mat_a << 1, 2, 3, 4,

             5, 6, 7, 8,

             9, 8, 7, 6,

             5, 4, 3, 2;

    return mat_a;

}


PyObject * example_meth_do_math(PyObject * self, PyObject * args, PyObject * kwargs) {

    static char * keywords[] = {"array", "rows", "cols", NULL};


    PyObject * array;

    int rows, cols;


    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oii", keywords, &array, &rows, &cols)) {

        return NULL;

    }


    Py_buffer view = {};

    if (PyObject_GetBuffer(array, &view, PyBUF_SIMPLE)) {

        return NULL;

    }


    Eigen::Matrix4f * ptr = (Eigen::Matrix4f *)view.buf;


    for (int i = 0; i < rows; ++i) {

        for (int j = 0; j < cols; ++j) {

            ptr[i * cols + j] = get_dummy();

        }

    }


    PyBuffer_Release(&view);

    Py_RETURN_NONE;

}


PyMethodDef module_methods[] = {

    {"do_math", (PyCFunction)example_meth_do_math, METH_VARARGS | METH_KEYWORDS, NULL},

    {},

};


PyModuleDef module_def = {PyModuleDef_HEAD_INIT, "example", NULL, -1, module_methods};


extern "C" PyObject * PyInit_example() {

    PyObject * module = PyModule_Create(&module_def);

    return module;

}

setup.py


from setuptools import Extension, setup


ext = Extension(

    name='example',

    sources=['./example.cpp'],

    extra_compile_args=['-fpermissive'],

    include_dirs=['.'], # add the path of Eigen

    library_dirs=[],

    libraries=[],

)


setup(

    name='example',

    version='0.1.0',

    ext_modules=[ext],

)

從這里添加第二個參數并使用兩個數組進行計算應該是微不足道的。


您可以使用python setup.py develop.


如果你想分發它,你可以創建一個 wheel 文件python setup.py bdist_wheel。


我曾經numpy創建數據,這確保了數據的底層內存是 C 連續的。


這個例子很簡單,它使用一個 Matrix4f 指針來迭代一個 3x2 矩陣數組。隨意將 轉換ptr為Eigen::Array<Eigen::Matrix4f>, 3, 2>。您不能將其強制轉換為 an std::vector,因為 an 的內部數據std::vector包含指針。


請注意,std::vector<std::array<...>>內存中沒有單個連續數組。改用Eigen::Array。


編輯:


這是一個使用Eigen Array Map:


PyObject * example_meth_do_math(PyObject * self, PyObject * args, PyObject * kwargs) {

    static char * keywords[] = {"array", NULL};


    PyObject * array;


    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", keywords, &array)) {

        return NULL;

    }


    Py_buffer view = {};

    if (PyObject_GetBuffer(array, &view, PyBUF_SIMPLE)) {

        return NULL;

    }


    Eigen::Map<Eigen::Array<Eigen::Matrix4f, 2, 3>> array_map((Eigen::Matrix4f *)view.buf, 2, 3);


    for (int i = 0; i < 2; ++i) {

        for (int j = 0; j < 3; ++j) {

            array_map(i, j) = get_dummy();

        }

    }


    PyBuffer_Release(&view);

    Py_RETURN_NONE;

}


查看完整回答
反對 回復 2023-04-11
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

線性代數不會那么流暢(在那里很難擊敗 Eigen),但會類似于您在 numpy 中所做的(np.dot(A,B)例如。

如果您想堅持使用 Eigen,請注意使用 STL 有一些技術細節。由于您std::array不再能夠包含固定數量的矩陣,因此當您移動到std::vector您會遇到對齊問題(誠然,我不完全理解)。很快就會為您提供 xtensor 的有效實現。


查看完整回答
反對 回復 2023-04-11
  • 2 回答
  • 0 關注
  • 313 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號