Assignment using memoryviews

86 views
Skip to first unread message

Joy merwin monteiro

unread,
Oct 9, 2016, 8:15:30 AM10/9/16
to cython-users
Hello,

I'm facing a strange issue, likely due to my lack of understanding of the memoryview syntax.

My code layout is like this:

1) I have a fortran function that accepts 1-d arrays. To pass references, I use memoryviews.

2) My input is a 3-d array, and I have to pass arr[x,y,:] in each call to the fortran function.


The way I tried to do this was

cdef np.double_t[::1] arr_view

#shape of array = (X,Y,Z)
def func(np.ndarray[np.double_t, dim=3] arr):

    global arr_view
    arr_view = np.zeros((Z), dtype=np.double, order='F')
   
    for x in range(X):
       for y in range(Y):
            arr_view[:] = arr[x,y,:]
            fortran_func(<double *>&arr_view[0])

However, this code crashes at
            arr_view[:] = arr[x,y,:]

saying "TypeError: only length-1 arrays can be converted to Python scalars"

however, if I modify this code to read

def func(np.ndarray[np.double_t, dim=3] arr):

    global arr_view
    cdef np.double_t[::1] ptr

    arr_view = np.zeros((Z), dtype=np.double, order='F')
   
    for x in range(X):
       for y in range(Y):
            ptr = arr[x,y,:]
            arr_view[:] = ptr[:]
            fortran_func(<double *>&arr_view[0])

This compiles and runs successfully.

Is there something obviously wrong with the first version that I don't understand?

TIA,
Joy

Robert Bradshaw

unread,
Oct 16, 2016, 2:41:31 AM10/16/16
to cython...@googlegroups.com
Here np.ndarray[np.double_t, ndim=3] is using the "old" buffer syntax, and the other arrays are using the "new" memoryview syntax. This compounded with the fact that

    memory_view_object[:] = non_memory_view_expression

assumes the right-hand-side is a scalar. I'm not really sure how to best resolve the latter...

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages