import numpy as np
cimport numpy as np
cdef class test:
cdef public np.ndarray np2darray
def __init__(self):
self.np2darray = np.zeros((100, 100), dtype=np.float64)
cdef access(self):
# create a function-local 'fast' buffer
cdef np.ndarray[np.float64_t, ndim=2, mode='c'] np2darray
# acquire the buffer; access to np2darray will do what you have above
np2darray = self.np2darray
return np2darray[0,0]
It is, though it's unclear how to do so. In particular, the stride and
pointer information must be stack-local variables for gcc to nicely
optimize the indexing code.
Or assembly :P. (Just kidding, Fortran may be a viable choice here,
but I have my own obvious bias.) Eventually, I do want to be able to
at least make this transparent to the user (perhaps even the stride
information could be cached on the object itself, and NumPy arrays
could be augmented with a "dirty" bit for flagging when they are
reallocate or reshaped).
> Passing subarrays is still slow.
Well, at least it's still O(1) with NumPy.
> For that you will find that Fortran is the
> only language that really works.
That's because the only way to allocate memory in Fortran was to
allocate a huge array at the start then manually partition it out... I
also see Cython eventually supporting this, passing around raw buffer
structs in many cases.
- Robert