有没有一个很好的方法来发送数据从python上下文到C + +没有太多的副本参与 [英] Is there a good way to send data from python context to C++ without too much copy involved

查看:229
本文介绍了有没有一个很好的方法来发送数据从python上下文到C + +没有太多的副本参与的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能会令人困惑。

The title could be confusing. Here I will state my question more clearly.

我想创建一个基于python的网站(很多现有的框架如Flask和cherryPy),还有一个C ++计算发动机为了处理速度。所以我需要为python创建一个接口来调用C ++函数。幸运的是boost.python可以做到这一点。然而,每次我从python发送一个数据,一个矩阵,到C + +,我必须使用python列表,这意味着我必须将矩阵数据转换为列表,并在C + +上下文转换列表为内部矩阵对象。结果,出现了大量的数据复制,这不是一种智能或高效的方法。所以我的问题是,如果,考虑到复杂性,我们不通过boost.python映射C ++矩阵类到一个python类,有没有更好的方法来做类似的工作没有或只有少量的副本?

I want to create a website based on python (a lot of existing framework like Flask and cherryPy) and along with a C++ computation engine for the sake of processing speed. So I need to create an interface for python to call C++ functions. Fortunately the boost.python can do the job. However, every time I send a data from python, say a matrix, to C++, I have to use python list, which means I have to transform the matrix data into list and in C++ context transform the list into an internal matrix object. As a result, a lot of data copy occur, which could not be an intelligent or efficient approach. So my questions is that if, considering the complexity, we don't map C++ matrix class to a python class through boost.python, is there a better way to do the similar work without or only with small number of copy?

推荐答案


然而,每次我从python发送一个数据到C ++,I
必须使用python列表,这意味着我必须将矩阵
数据转换为列表,并在C ++上下文中将列表转换为内部
矩阵对象。

However, every time I send a data from python, say a matrix, to C++, I have to use python list, which means I have to transform the matrix data into list and in C++ context transform the list into an internal matrix object.

不,你不必使用python列表。您可以使用numpy数组,它将数据分配为C连续段,可以将其传递到C ++,而不需要复制,并使用矩阵包装器类将其视为矩阵。

No, you don't have to use the python list. You can use numpy array which allocates data as a C contiguous segment which can be passed down to C++ without copying and viewed as a matrix using a matrix wrapper class.

在python使用numpy分配2d数组:

In python allocate 2d array using numpy:

>>> y=np.empty((2,5), dtype=np.int16)
>>> y
array([[   12, 27750, 26465,  2675,     0],
       [    0,     0,     0,  2601,     0]], dtype=int16)
>>> y.flags['C_CONTIGUOUS']
True
>>> foo(y,2,5)

使用以下暴露给python的函数将矩阵数据传递给C ++: / p>

Pass matrix data to C++ using below function exposed to python:

void foo(python::object obj, size_t size1, size_t size2)
{
    PyObject* pobj = obj.ptr();
    Py_buffer pybuf;
    PyObject_GetBuffer(pobj, &pybuf, PyBUF_SIMPLE);
    void *buf = pybuf.buf;
    int16_t *p = (int16_t*)buf;
    Py_XDECREF(pobj);

    MyMatrixWrapper matrix(p, size1, size2);
    // ....
}

这篇关于有没有一个很好的方法来发送数据从python上下文到C + +没有太多的副本参与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆