If anyone was interested I found that the easiest solution involved
patching up the strides after calling PyArray_SimpleNewFromData(). I
still haven't gotten any sort of memory interaction so any objects
created by Blitz are deleted by Blitz, while Python objects are deleted
by Python. (Irrespective of any pointers held by the other side.)
I find for my purposes that this is sufficient since I just want to be
able to peer into the workings of a c++ program.
-Philip.
Philip Sterne wrote:
Hi all,
I hope this is the correct place to post my question. I'd like python
to interface with c++ code that makes heavy use of Blitz++ arrays. After
a day's hacking I appear to have a simple working solution which I am
attaching. (It uses Boost.Python and CMake.)
However I believe this solution won't work if the blitz array is not
laid out contiguously in memory. I also haven't really thought about
reference counting issues (although the example seems to work). I
imagine that those sorts of issues will lead me to call the more
complicated:
PyArray_New(...)
or:
PyArray_NewFromDescr(...)
instead of the PyArray_SimpleNewFromData(...) that I currently use.
However I couldn't figure out some of the extra arguments from the API
documentation.
Can someone point out all the things that will break when this code
actually gets used in the real world (and maybe even how to avoid them)?
Thanks for your time!
-Philip.
------------------------------------------------------------------------
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
#include <boost/python.hpp>
#include <numpy/arrayobject.h>
#include <blitz/array.h>
using namespace boost::python;
using namespace blitz;
template<class T, int N>
Array<T,N> py_to_blitz(object arr)
{
PyArrayObject* arr_obj = (PyArrayObject*) arr.ptr();
TinyVector<int,N> shape(0);
TinyVector<int,N> strides(0);
for (int i = 0; i < N; i++)
{
shape[i] = arr_obj->dimensions[i];
strides[i] = arr_obj->strides[i]/sizeof(T);
}
return Array<T,N>((T*) arr_obj->data,shape,strides,neverDeleteData);
}
template<class T, int TYPENUM, int N>
PyObject* blitz_to_py(Array<T,N> arr)
{
import_array1((PyObject*)NULL);
npy_intp dims[N];
for (int n=0;n<N; n++)
dims[n] = arr.extent(n);
PyObject* ans = PyArray_SimpleNewFromData(N, dims, TYPENUM, arr.data());
//Now fix the strides:
npy_intp* str = PyArray_STRIDES(ans);
for (int n=0;n<N;n++)
str[n] = arr.stride(n)*sizeof(T);
return ans;
}
void print(Array<bool,1>& b)
{
for (int n=0; n<b.size(); n++)
std::cout << b(n)<<'\t';
std::cout << endl;
}
BOOST_PYTHON_MODULE(libsimple)
{
class_<Array<bool ,1> >("Bool1");
class_<Array<bool ,2> >("Bool2");
class_<Array<bool ,3> >("Bool3");
class_<Array<double,1> >("Double1");
class_<Array<double,2> >("Double2");
class_<Array<double,3> >("Double3");
def("tBool1",&py_to_blitz<bool,1>);
def("tBool2",&py_to_blitz<bool,2>);
def("tBool3",&py_to_blitz<bool,3>);
def("fBool1",&blitz_to_py<bool, PyArray_BOOL, 1>);
def("fBool2",&blitz_to_py<bool, PyArray_BOOL, 2>);
def("fBool3",&blitz_to_py<bool, PyArray_BOOL, 3>);
def("tDouble1",&py_to_blitz<double,1>);
def("tDouble2",&py_to_blitz<double,2>);
def("tDouble3",&py_to_blitz<double,3>);
def("fDouble1",&blitz_to_py<double, PyArray_DOUBLE, 1>);
def("fDouble2",&blitz_to_py<double, PyArray_DOUBLE, 2>);
def("fDouble3",&blitz_to_py<double, PyArray_DOUBLE, 3>);
def("cout",&print);
}
from pylab import *
from libsimple import *
py = rand(10)<0.5
print py
c = toBoolVec(py)
cout(c)
py2 = fromBoolVec(c)
del(py)
print py2
del(py2)
cout(c)
del c
cmake_minimum_required(VERSION 2.6)
PROJECT(simple)
find_package(PythonInterp)
find_package(PythonLibs)
find_package(Boost COMPONENTS python)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES (${Boost_INCLUDE_DIR})
ADD_DEFINITIONS(-Wall -O2)
add_library (simple SHARED simple.cpp)
SET_TARGET_PROPERTIES(simple
PROPERTIES
SOVERSION 0.0.1
VERSION 0.0.1
)
TARGET_LINK_LIBRARIES (simple ${PYTHON_LIBRARIES})
TARGET_LINK_LIBRARIES (simple ${Boost_LIBRARIES})
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion