Vector of numpy arrays ?

48 views
Skip to first unread message

Jérôme Kieffer

unread,
Feb 9, 2012, 3:48:33 PM2/9/12
to cython...@googlegroups.com

Dear Cython community,

I am wrapping C++ code and would need to store numpy.ndarrays intermediately so that I can process them in parallel:
There is a (variable) number of such arrays that is coming from the python call and I would like to call a C++ function on them (possibly in parallel using prange). The C++ function takes a <float*> as input.

def doAll(*listArg):
n=len(listArg)
vector[float*] listArrayFloat = vector[float*]( <size_t> n) #here is the problem
vector[Result] listOut = vector[Result]( <size_t> n)
for i,obj in ennumerate(listArg):
listArrayFloat[i] = <float*> obj.data
for i in prange(n):
listOut[i] = cppFunct(listArrayFloat[i])

I was both unsuccessful in vector[float*] and vector[ numpy.ndarray[]] so there is probably something I am missing. Maybe you have an idea on how to make things working. Thanks

With my best regards,
--
Jérôme Kieffer <jerome....@terre-adelie.org>

PS: this works:

def doOne(ndarray inp not None):
cdef Result out
out = cppFunct(<float*> inp.data)

Jérôme Kieffer

unread,
Feb 10, 2012, 1:27:35 PM2/10/12
to cython...@googlegroups.com

I managed to do something like:

cdef vector[float*] lstInput
for obj in listArg:
if isinstance(obj,numpy.ndarray):
lstInput.push_back(<float*> obj.data)

which seems to work.

This raises another question:
Do you know why vector.push_back() or vector.size() have no "nogil" mention in the pxd file?

Thanks.
--
Jérôme Kieffer <goo...@terre-adelie.org>

Robert Bradshaw

unread,
Feb 11, 2012, 12:06:05 AM2/11/12
to cython...@googlegroups.com
2012/2/10 Jérôme Kieffer <goo...@terre-adelie.org>:

>
> I managed to do something like:
>
> cdef vector[float*] lstInput
> for obj in listArg:
>    if isinstance(obj,numpy.ndarray):
>        lstInput.push_back(<float*> obj.data)
>
> which seems to work.

Yep, that would do it, assuming your data is contiguous.

> This raises another question:
> Do you know why vector.push_back() or vector.size() have no "nogil" mention in the pxd file?

Because you don't need the gil to call them. Yeah, it's a wart.

- Robert

Reply all
Reply to author
Forward
0 new messages