6

When I try to run the cython code below to generate an empty array, it segfaults.

Is there any way of generating empty numpy arrays in python without calling np.empty()?

cdef np.npy_intp *dims = [3]
cdef np.ndarray[np.int_t, ndim=1] result = np.PyArray_EMPTY(1, dims, 
                                                            np.NPY_INTP, 0)
4
  • What is wrong about np.empty() ? If you're doing it only once at the initialization stage, you don't care if it's marginally slower than using C functions directly. Commented Aug 26, 2013 at 8:38
  • 2
    If you are performing operations on arrays of size <1000, the cost of np.empty() alone is greater than the entire loop. see (stackoverflow.com/questions/18410342/…) where I described the problem. I'm trying to solve it, but now found a new problem: namely the use of the np.PyArray_EMPTY() function Commented Aug 26, 2013 at 9:05
  • 1
    Are np.NPY_INTP and np.int_t the same type in your system? Commented Aug 26, 2013 at 9:44
  • Is the first line really correct cython? It seems dubious to me. Commented Aug 26, 2013 at 12:39

1 Answer 1

3

You might have solved this a long time ago, but for the benefit of anyone who stumbles across this question while trying to figure out why their cython code segfaults, here's a possible answer.

When you get a segfault when using the numpy C API, the first thing to check is that you have called the function import_array(). That might be the problem here.

For example, here's foo.pyx:

cimport numpy as cnp


cnp.import_array()  # This must be called before using the numpy C API.

def bar():
    cdef cnp.npy_intp *dims = [3]
    cdef cnp.ndarray[cnp.int_t, ndim=1] result = \
        cnp.PyArray_EMPTY(1, dims, cnp.NPY_INTP, 0)
    return result

Here's a simple setup.py for building the extension module:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np


setup(cmdclass={'build_ext': build_ext},
      ext_modules=[Extension('foo', ['foo.pyx'])],
      include_dirs=[np.get_include()])

Here's the module in action:

In [1]: import foo

In [2]: foo.bar()
Out[2]: array([4314271744, 4314271744, 4353385752])

In [3]: foo.bar()
Out[3]: array([0, 0, 0])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.