loli
loli

Reputation: 405

Acessing data buffer of typed memoryview in Cython

I have a function expecting array pointers in Cython, e.g. with the signature

cdef void foo(DTYPE_t* x)

and a function which receives a typed memoryview from which I would like to call the first function, e.g.:

def bar(DTYPE_t[:,::1] X not None):
    foo(X[0])

Which naturally does not even compile. I've been trying for some hours now to figure out a way to access the data pointer underlying the memory view i.e. something like X.data.

Is there a way to achieve this? I sadly can not adept foo to accept memoryviews.

Upvotes: 3

Views: 1461

Answers (2)

loli
loli

Reputation: 405

The solution is that simple, it's quite embarrassing

&X[i,j]

i.e. the call will become

foo(&X[i,0])

Which, by the way, also works with the old style numpy arrays, which are initialized like

object[int, ndim=2, mode='strided'] X

PS: If you would like to pass C-array, X[i][j] would be required, which equally works for typed memoryviews.

Upvotes: 2

Sturla Molden
Sturla Molden

Reputation: 1144

You want this:

foo(&X[0,0])

Upvotes: 4

Related Questions