Hoyt Koepke wrote:
> My question -- which probably has as much to do with cython internals
> as anything else -- is about the best way to pass ndarray buffer
> objects between functions.  For instance, I'd like to do this:
> 
> cimport numpy as cnp
> 
> cdef void foo(cnp.ndarray[float] X):
>     ...

Just a question: Is there a reason you like to keep numpy in a "cnp" 
prefix (I'm assuming you keep plain numpy in "np"). I'm asking because 
if there is a reason I'm interested in eliminating it.

> 
> but currently I believe I either have to do
> 
> cdef void foo(object Xarg):
>     cnp.ndarray[float] X = Xarg
>     ....
> 
> or
> 
> def void foo(cnp.ndarray[float] X):
>     ...
> 
> both of which seem to be far from the most efficient way to do things.

Yes, I agree that this can be bothersome. It crept in as an unintended 
side-effect of fixing something else close to the release (and we didn't 
want to shake up things too much). Fixing it is non-trivial and it does 
come a bit down on priority lists, but you can consider it a defect. 
This is now:

http://trac.cython.org/cython_trac/ticket/61

As for efficiency, example 1 (not allowed) and 2 are the same except for 
refcounting (which doesn't matter at all in a function using a buffer), 
so use 2.

So it is all about notational convenience. Between functions, all that 
is passed is always a pure Python object and the buffer is always 
acquired within the local scope of the function.

I guess it could be benefitial to change the calling convention so that 
the caller resonsible for acquiring the buffer could perhaps be more 
efficient but it is also a fair amount of work, so I think it is 
unlikely that it will happen soon. (But I can see that for small utility 
funcs used on a small subset of an array it could play a role, so doing 
something like that is not out of the question).

-- 
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to