发送C ++数组Python和背面(扩展C ++与numpy的) [英] Sending a C++ array to Python and back (Extending C++ with Numpy)

查看:792
本文介绍了发送C ++数组Python和背面(扩展C ++与numpy的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发送一个 C ++ 数组一个python函数 numpy的阵列,并取回另一 numpy的阵列。与 numpy的文档和一些其他线程咨询和扭捏code后,终于在code是工作,但我想知道,如果这code写最佳考虑:

I am going to send a c++ array to a python function as numpy array and get back another numpy array. After consulting with numpy documentation and some other threads and tweaking the code, finally the code is working but I would like to know if this code is written optimally considering the:


  • 的数组不必要的复制 C ++ numpy的(蟒蛇)

  • 的变量解引用正确

  • 易于直接的方法。

C ++ code:

C++ code:

// python_embed.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "Python.h"
#include "numpy/arrayobject.h"
#include<iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    import_array()

    // Build the 2D array
    PyObject *pArgs, *pReturn, *pModule, *pFunc;
    PyArrayObject *np_ret, *np_arg;
    const int SIZE{ 10 };
    npy_intp dims[2]{SIZE, SIZE};
    const int ND{ 2 };
    long double(*c_arr)[SIZE]{ new long double[SIZE][SIZE] };
    long double* c_out;
    for (int i{}; i < SIZE; i++)
        for (int j{}; j < SIZE; j++)
            c_arr[i][j] = i * SIZE + j;

    np_arg = reinterpret_cast<PyArrayObject*>(PyArray_SimpleNewFromData(ND, dims, NPY_LONGDOUBLE, 
        reinterpret_cast<void*>(c_arr)));

    // Calling array_tutorial from mymodule
    PyObject *pName = PyUnicode_FromString("mymodule");
    pModule = PyImport_Import(pName);
    Py_DECREF(pName);
    if (!pModule){
        cout << "mymodule can not be imported" << endl;
        Py_DECREF(np_arg);
        delete[] c_arr;
        return 1;
    }
    pFunc = PyObject_GetAttrString(pModule, "array_tutorial");
    if (!pFunc || !PyCallable_Check(pFunc)){
        Py_DECREF(pModule);
        Py_XDECREF(pFunc);
        Py_DECREF(np_arg);
        delete[] c_arr;
        cout << "array_tutorial is null or not callable" << endl;
        return 1;
    }
    pArgs = PyTuple_New(1);
    PyTuple_SetItem(pArgs, 0, reinterpret_cast<PyObject*>(np_arg));
    pReturn = PyObject_CallObject(pFunc, pArgs);
    np_ret = reinterpret_cast<PyArrayObject*>(pReturn);
    if (PyArray_NDIM(np_ret) != ND - 1){ // row[0] is returned
        cout << "Function returned with wrong dimension" << endl;
        Py_DECREF(pFunc);
        Py_DECREF(pModule);
        Py_DECREF(np_arg);
        Py_DECREF(np_ret);
        delete[] c_arr;
        return 1;
    }
    int len{ PyArray_SHAPE(np_ret)[0] };
    c_out = reinterpret_cast<long double*>(PyArray_DATA(np_ret));
    cout << "Printing output array" << endl;
    for (int i{}; i < len; i++)
        cout << c_out[i] << ' ';
    cout << endl;

    // Finalizing
    Py_DECREF(pFunc);
    Py_DECREF(pModule);
    Py_DECREF(np_arg);
    Py_DECREF(np_ret);
    delete[] c_arr;
    Py_Finalize();
    return 0;
}

在codeReview,有一个梦幻般的回答:<一href=\"http://$c$creview.stackexchange.com/questions/92266/sending-a-c-array-to-python-numpy-and-back/92353#92353\">Link...

In CodeReview, there is a fantastic answer: Link...

推荐答案

从我的经验,这似乎是pretty效率。
为了获得更高的效率了它试试这个:
http://ubuntuforums.org/showthread.php?t=1266059

From my experience that seems to be pretty efficient. To get even more efficiency out of it try this : http://ubuntuforums.org/showthread.php?t=1266059

使用编织你可以内联C / C ++ code在Python,这样可能是有用的。

Using weave you can inline C/C++ code in Python so that could be useful.

http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.weave.inline.html

下面是关于Python如何被使用,举例许多不同的语言之间的接口的连接。

Here's a link on how Python can be used to interface between many different languages along with examples.

HTTP://docs.scipy。组织/ DOC / numpy的/用户/ C-info.python-AS-glue.html

这是如何numpy的数组传递给C ++使用用Cython一个快速和简单的例子:

This is a quick and easy example of how to pass numpy arrays to c++ using Cython:

HTTP://blog.birving。 COM / 2014/05 /结业numpy的阵列之间 - 的python-and.html

这篇关于发送C ++数组Python和背面(扩展C ++与numpy的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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