Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast conversion of C/C++ vector to Numpy array

I'm using SWIG to glue together some C++ code to Python (2.6), and part of that glue includes a piece of code that converts large fields of data (millions of values) from the C++ side to a Numpy array. The best method I can come up with implements an iterator for the class and then provides a Python method:

def __array__(self, dtype=float):
    return np.fromiter(self, dtype, self.size())

The problem is that each iterator next call is very costly, since it has to go through about three or four SWIG wrappers. It takes far too long. I can guarantee that the C++ data are stored contiguously (since they live in a std::vector), and it just feels like Numpy should be able to take a pointer to the beginning of that data alongside the number of values it contains, and read it directly.

Is there a way to pass a pointer to internal_data_[0] and the value internal_data_.size() to numpy so that it can directly access or copy the data without all the Python overhead?

like image 214
Seth Johnson Avatar asked Mar 24 '11 19:03

Seth Johnson


2 Answers

You will want to define __array_interface__() instead. This will let you pass back the pointer and the shape information directly.

like image 137
Robert Kern Avatar answered Oct 11 '22 10:10

Robert Kern


Maybe it would be possible to use f2py instead of swig. Despite its name, it is capable of interfacing python with C as well as Fortran. See http://www.scipy.org/Cookbook/f2py_and_NumPy

The advantage is that it handles the conversion to numpy arrays automatically.

Two caveats: if you don't already know Fortran, you may find f2py a bit strange; and I don't know how well it works with C++.

like image 27
deprecated Avatar answered Oct 11 '22 10:10

deprecated