Copying to non contiguous memory view to C contiguous

490 views
Skip to first unread message

Daniele Nicolodi

unread,
Nov 17, 2013, 8:06:27 PM11/17/13
to cython...@googlegroups.com
Hello,

how can I copy a non contiguous memory view to a C contiguous one?

The documentation
http://docs.cython.org/src/userguide/memoryviews.html#c-and-fortran-contiguous-copies
says that this should be possible to do something like:

cdef double[::1] a = np.ones(10)
cdef double[::1] b = a[::2].copy()

however it raises an error at compile time:

Memoryview 'double[:]' not conformable to memoryview 'double[::1]'.

There is not way out other than using an explicit for-loop?

Thank you.

Cheers,
Daniele

Robert Bradshaw

unread,
Nov 20, 2013, 2:27:25 AM11/20/13
to cython...@googlegroups.com
What guarantees does copy() provide?

- Robert

Daniele Nicolodi

unread,
Nov 20, 2013, 6:17:57 AM11/20/13
to cython...@googlegroups.com
I think I don't understand the question. The documentation says that
copy() can be used to copy an array to C or Fortran contiguous. In
practice this does not seem to be true, or I'm missing something.

Cheers,
Daniele

Daniele Nicolodi

unread,
Nov 21, 2013, 8:14:50 AM11/21/13
to cython...@googlegroups.com
I really think we have a bug here. Looks like the copy performed by the
copy() method is indeed C contiguous, but it cannot be assigned to a
memoryview object declared C contiguous:

def test():
cdef double[::1] a = np.ones(10)
cdef double[:] b = a[::2]
print b.is_c_contig()
cdef double[:] c = b.copy()
print c.is_c_contig()

correctly prints False, True.

Cheers,
Daniele

Robert Bradshaw

unread,
Nov 23, 2013, 12:25:38 AM11/23/13
to cython...@googlegroups.com
Cython doesn't know that the copy function returns a C continuous
function. Even if NumPy provides this, there could very well be
libraries whose copy() methods don't given this promise (though we do
somehow infer that it returns a double[:]).

- Robert

Daniele Nicolodi

unread,
Nov 24, 2013, 5:13:54 PM11/24/13
to cython...@googlegroups.com
I believe the copy() method is implemented by the typed memoryview
object and not by the object that holds the array the memoryview is
viewing: see
https://github.com/cython/cython/blob/master/Cython/Utility/MemoryView.pyx#L592

If copy() is not intended to be used to obtain C or Fortran contiguous
arrays, the documentation should be amended:
http://docs.cython.org/src/userguide/memoryviews.html#c-and-fortran-contiguous-copies

Cheers,
Daniele

Oliver Haas

unread,
Dec 21, 2013, 7:32:38 PM12/21/13
to cython...@googlegroups.com
I encountered this just today as well. Is there any direct way to tell cython that copy() in fact returns a contiguous array?

Maik Riechert

unread,
Jan 9, 2014, 1:36:14 PM1/9/14
to cython...@googlegroups.com
Daniele Nicolodi wrote:
I believe the copy() method is implemented by the typed memoryview
object and not by the object that holds the array the memoryview is
viewing: see
https://github.com/cython/cython/blob/master/Cython/Utility/MemoryView.pyx#L592

If copy() is not intended to be used to obtain C or Fortran contiguous
arrays, the documentation should be amended:
http://docs.cython.org/src/userguide/memoryviews.html#c-and-fortran-contiguous-copies

The same applies to:
http://docs.cython.org/src/userguide/memoryviews.html#cython-arrays
It says that the memory view "can later be assigned to a C or Fortran contiguous slice (or a strided slice)".
Reply all
Reply to author
Forward
0 new messages