[Cython] Comments on example code?
Robert Bradshaw
robertwb at math.washington.edu
Wed Nov 26 21:03:39 CET 2008
On Nov 26, 2008, at 10:24 AM, Gabriel Gellner wrote:
> On Tue, Nov 25, 2008 at 12:06:06PM -0800, Robert Bradshaw wrote:
>> One way to do it would be to flatten/compact the ndarray early on (so
>> one know the entries are contiguous) and then pass the raw float* and
>> its length (keeping the original array around so you don't have to
>> worry about memory issues.
>>
> Yeah I do this with my own code at times, but I am generally
> dealing with a
> matlab audience, and this seems like dark magic. I don't need
> maximum speed,
> just as fast as is still understandable to basic programmers.
Certainly a worthy goal. One of the main motivations of Cython is to
achieve maximum speed without sacrificing understandability. There
is, of course, a lot that remains to be done in this direction :).
When you're speaking of callbacks, would it be to magic to pass (C)
function pointers around?
>
>> Note that __call__, though not as slow as a normal python function
>> call, still has Python semantics (i.e. all of its arguments and its
>> return value have to pass through Python objects) so just using a
>> cdef method could speed things up considerably. (I wonder how hard it
>> would be to support cpdef __call__?) The call to empty is probably
>> dominating things too--since the 3 seems hard coded in anyways, I
>> would accept (and return) a
>>
> How hard do you think it would be to write something that sets up
> an empty (or
> zeroed) ndarray do avoid the python call? Upon further thinking I
> imagine
> this is the major issue from f2py.
>
> If it is doable for mortals I would try my hand at it. I imagine it
> is just
> getting the .data and some metadata of the struct setup correctly,
> but I don't
> fully understand the magic of the ndarray in cython.
I bet there's probably a C-level function in NumPy that one could
just call, if one can find it. Either that or malloc/calloc a chunk
and use NumPy's C API to create a new ndarray out of that raw data.
> If this is possible would it make sense to add it to the numpy.pyx
> file?
Yes, certainly. This is (part of) what Dag was talking about being
able to put inline functions in .pxd files.
>
>> cdef struct data:
>> float x
>> float y
>> float z
>>
>> instead of a 3-element ndarray.
>>
> I don't have control of the calling program routine, so this won't
> work . . .
Sure.
> Again, thanks for all the responses, I continue to think Cython is
> pythons
> greatest `killer` feature for science.
Thanks.
More information about the Cython-dev
mailing list