[Cython] NumPy output works on 32bit not on 64bit
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Sun Sep 14 12:15:54 CEST 2008
Marcin Cieślik wrote:
> Hello,
>
> Does anyone know why this code fails on a 64bit linux machine (it is
> working on 32 just fine):
> The compiler complains:
>
> bit.c:792: warning: passing argument 3 of '*(PyArray_API + 744u)' from
> incompatible pointer type
>
> and I get a segmentation fault
>
> If it is not an issue with Cython could you please direct me
> somewhere, if have almost no C/C++ skill.
See comments below.
> #####
> cimport numpy as np
> ctypedef np.double_t DTYPE_t
This isn't related to the problem, but when you are using the NPY_DOUBLE
etc. constants directly you should instead do
ctypedef np.npy_double DTYPE_t
This is because there isn't a 1:1 correspondance between the "shorthand"
types and the NPY_TYPE constants (see bottom of
Cython/Includes/numpy.pxd for details. BTW, patches for numpy.pxd to add
the Py_Array_SimpleNew... calls are welcome if submitted).
> from numpy cimport NPY_DOUBLE, NPY_UINT
> from stdlib cimport malloc, realloc, free
>
> cdef extern from "arrayobject.h":
> cdef object PyArray_SimpleNewFromData(int nd, int *dims,\
> int typenum, void *data)
dims should be declared with the type npy_intp instead (and this is also
what your compiler complained about).
> cdef void import_array()
> import_array()
>
> print sizeof(DTYPE_t)
> print sizeof(void*)
> print sizeof(DTYPE_t*)
>
> cdef int i, k = 100
Here is the problem. If you make k an npy_intp instead then it will be a
64-bit int. So:
cdef np.npy_intp k = 100
This wouldn't matter in many circumstances but as you are taking the
address to k the call to SimpleNewFromData it is crucial. (So NumPy
thought your array had a random billion elements coming from whatever
bits were after the 32-bit "k" variable on the stack...)
--
Dag Sverre
More information about the Cython-dev
mailing list