BUG: assignment to typed memoryview
Hello, I believe there is a bug in how assignment to typed memoryviews is handled in the compiler. I think the following code should be valid code: cdef double[::1] a = np.arange(10, dtype=np.double) cdef double[::1] b = np.empty(a.size // 2) b[:] = a[::2] However, the Cython compiler complains with: Memoryview 'double[:]' not conformable to memoryview 'double[::1]'. A similar thing happens with memoryview copies: cdef double[::1] a = np.arange(10, dtype=np.double) cdef double[::1] b b = a[::2].copy() In both cases I would expect Cython to copy the non contiguous elements of the source array to the destination array (or to a newly created array in the second case). The copy() method is defined here https://github.com/cython/cython/blob/master/Cython/Utility/MemoryView.pyx#L... and (while I haven't spent much time analyzing it in detail) it seems to do the right thing. Indeed, if I short-circuit the src_conforms_to_dst() function https://github.com/cython/cython/blob/master/Cython/Compiler/MemoryView.py#L... the code compiles and runs correctly in both cases. I don't know much about how the Cython compiler works, therefore I don't know where to dig for the source of this problem. Can someone point me in the right direction? Thanks. Cheers, Daniele
On 26 Nov 2013, at 16:18, Daniele Nicolodi <daniele@grinta.net> wrote:
Hello,
I believe there is a bug in how assignment to typed memoryviews is handled in the compiler. I think the following code should be valid code:
cdef double[::1] a = np.arange(10, dtype=np.double) cdef double[::1] b = np.empty(a.size // 2) b[:] = a[::2]
Then you are wrong. This should not be valid code. You have declared b to be contiguous, a[::2] is a discontiguous slice. Thus a[::2] cannot be assigned to b. Sturla
On 04/12/2013 12:55, Sturla Molden wrote:
On 26 Nov 2013, at 16:18, Daniele Nicolodi <daniele@grinta.net> wrote:
Hello,
I believe there is a bug in how assignment to typed memoryviews is handled in the compiler. I think the following code should be valid code:
cdef double[::1] a = np.arange(10, dtype=np.double) cdef double[::1] b = np.empty(a.size // 2) b[:] = a[::2]
Then you are wrong. This should not be valid code. You have declared b to be contiguous, a[::2] is a discontiguous slice. Thus a[::2] cannot be assigned to b.
Hello Sturla, please note that I'm copying the array elements, not assigning to the memoryview object: there is an important difference between `b[:] = a[::2]` and `b = a[::2]`. Removing the IMHO wrong check, the generated C code correctly copies the array elements from a to b. Indeed the following code is perfectly fine: cdef double[::1] a = np.arange(10, dtype=np.double) cdef double[::1] b = np.empty(a.size // 2) cdef double[:] t c = a[::2] b[:] = c Thank for looking at this. Cheers, Daniele
participants (2)
-
Daniele Nicolodi -
Sturla Molden