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:
- Unnecessary copying of the array between
c++
andnumpy (python)
. - Correct dereferencing of the variables.
- Easy straight-forward approach.
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;
}
In CodeReview, there is a fantastic answer: Link...
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
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
Here's a link on how Python can be used to interface between many different languages along with examples.
http://docs.scipy.org/doc/numpy/user/c-info.python-as-glue.html
This is a quick and easy example of how to pass numpy arrays to c++ using Cython:
http://www.birving.com/blog/2014/05/13/passing-numpy-arrays-between-python-and/
As an additional way, without touching directly to the Python C API, it is possible to use pybind11 ( header-only library) :
CPP :
In
py_test.py
:Output :
See the official documentation.
ps: I'm using Eigen for the C++ Matrix.
Try out xtensor and the xtensor-python python bindings.
xtensor is a C++ library meant for numerical analysis with multi-dimensional array expressions.
xtensor provides
Example of usage
Initialize a 2-D array and compute the sum of one of its rows and a 1-D array.
Outputs
Creating a Numpy-style universal function in C++.
Python code:
Outputs