Generated by Cython 0.24.1
Yellow lines hint at Python interaction.
Click on a line that starts with a "+
" to see the C code that Cython generated for it.
Raw output: numpyext.c
+01: """
__pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 1, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_2) < 0) __PYX_ERR(0, 1, __pyx_L1_error) __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
02: This tutorial compares:
03:
04: 1. Python code with numpy
05: 2. Cython code with typed memoryviews
06:
07: Supported Array Types
08: ---------------------
09: Cython supports the following numerical array types:
10:
11: 1. numpy arrays: cdef np.ndarray[np.double_t, ndim=2] some_array
12: - requires cimport numpy as np.
13: - cython data types have "_t" suffix.
14: 2. Python/Cython memoryviews: cdef double[::, ::1] some_array
15: - ::1 ensures the array is c-contiguous in memory.
16: 3. C Array: cdef double **some_array
17: - Assumes data is c-contiguous (nparray.flags.c_contiguous == True)
18:
19: How to Compile
20: --------------
21: Unix:
22: $python setup.py build_ext --inplace
23: (Generates a .so file)
24:
25: Windows
26: $python setup.py build_ext --inplace -c msvc2010
27: (Generates a .pyd file)
28:
29: How to Run
30: ----------
31: $python
32: from nimimo.tutorial.cythonext.numpyext import numpy
33: numpyext.python_sum(30)
34: numpyext.cython_sum(30)
35:
36: Annotations
37: -----------
38: Annotations highlight the interactions with the python interpreter (yellow lines)
39: cython numpyext.pyx -a
40: """
41: import cython
+42: import numpy as np
__pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 42, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) __PYX_ERR(0, 42, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
43: cimport numpy as np
44:
+45: from nimimo.utilities.profiler import time_it
__pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 45, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __Pyx_INCREF(__pyx_n_s_time_it); __Pyx_GIVEREF(__pyx_n_s_time_it); PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_time_it); __pyx_t_2 = __Pyx_Import(__pyx_n_s_nimimo_utilities_profiler, __pyx_t_1, -1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 45, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; __pyx_t_1 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_time_it); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 45, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); if (PyDict_SetItem(__pyx_d, __pyx_n_s_time_it, __pyx_t_1) < 0) __PYX_ERR(0, 45, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
46:
+47: @time_it
__pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_time_it); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 47, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1);
+48: def numpy_sum(a):
/* Python wrapper */ static PyObject *__pyx_pw_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_1numpy_sum(PyObject *__pyx_self, PyObject *__pyx_v_a); /*proto*/ static PyMethodDef __pyx_mdef_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_1numpy_sum = {"numpy_sum", (PyCFunction)__pyx_pw_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_1numpy_sum, METH_O, 0}; static PyObject *__pyx_pw_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_1numpy_sum(PyObject *__pyx_self, PyObject *__pyx_v_a) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("numpy_sum (wrapper)", 0); __pyx_r = __pyx_pf_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_numpy_sum(__pyx_self, ((PyObject *)__pyx_v_a)); /* function exit code */ __Pyx_RefNannyFinishContext(); return __pyx_r; } static PyObject *__pyx_pf_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_numpy_sum(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a) { PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("numpy_sum", 0); /* … */ /* function exit code */ __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); __Pyx_XDECREF(__pyx_t_2); __Pyx_XDECREF(__pyx_t_3); __Pyx_XDECREF(__pyx_t_4); __Pyx_AddTraceback("nimimo.tutorial.cythonext.numpyext.numpyext.numpy_sum", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* … */ __pyx_tuple__20 = PyTuple_Pack(1, __pyx_n_s_a); if (unlikely(!__pyx_tuple__20)) __PYX_ERR(0, 48, __pyx_L1_error) __Pyx_GOTREF(__pyx_tuple__20); __Pyx_GIVEREF(__pyx_tuple__20); /* … */ __pyx_t_3 = PyCFunction_NewEx(&__pyx_mdef_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_1numpy_sum, NULL, __pyx_n_s_nimimo_tutorial_cythonext_numpye); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 48, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_3); __pyx_t_4 = NULL; if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_1))) { __pyx_t_4 = PyMethod_GET_SELF(__pyx_t_1); if (likely(__pyx_t_4)) { PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_1); __Pyx_INCREF(__pyx_t_4); __Pyx_INCREF(function); __Pyx_DECREF_SET(__pyx_t_1, function); } } if (!__pyx_t_4) { __pyx_t_2 = __Pyx_PyObject_CallOneArg(__pyx_t_1, __pyx_t_3); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 47, __pyx_L1_error) __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; __Pyx_GOTREF(__pyx_t_2); } else { __pyx_t_5 = PyTuple_New(1+1); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 47, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_5); __Pyx_GIVEREF(__pyx_t_4); PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4); __pyx_t_4 = NULL; __Pyx_GIVEREF(__pyx_t_3); PyTuple_SET_ITEM(__pyx_t_5, 0+1, __pyx_t_3); __pyx_t_3 = 0; __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 47, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; } __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; if (PyDict_SetItem(__pyx_d, __pyx_n_s_numpy_sum, __pyx_t_2) < 0) __PYX_ERR(0, 48, __pyx_L1_error) __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; __pyx_codeobj__21 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__20, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_dylanbespalko_repos_nimim, __pyx_n_s_numpy_sum, 48, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__21)) __PYX_ERR(0, 48, __pyx_L1_error)
+49: return np.sum(a)
__Pyx_XDECREF(__pyx_r); __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 49, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_sum); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 49, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_3); __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; __pyx_t_2 = NULL; if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) { __pyx_t_2 = PyMethod_GET_SELF(__pyx_t_3); if (likely(__pyx_t_2)) { PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3); __Pyx_INCREF(__pyx_t_2); __Pyx_INCREF(function); __Pyx_DECREF_SET(__pyx_t_3, function); } } if (!__pyx_t_2) { __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_v_a); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 49, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); } else { __pyx_t_4 = PyTuple_New(1+1); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 49, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); __Pyx_GIVEREF(__pyx_t_2); PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2); __pyx_t_2 = NULL; __Pyx_INCREF(__pyx_v_a); __Pyx_GIVEREF(__pyx_v_a); PyTuple_SET_ITEM(__pyx_t_4, 0+1, __pyx_v_a); __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 49, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; } __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; __pyx_r = __pyx_t_1; __pyx_t_1 = 0; goto __pyx_L0;
50:
+51: @time_it
__pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_time_it); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 51, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1);
52: @cython.boundscheck(False) # Never index outside of the bounds of the array.
53: @cython.wraparound(False) # Eliminate support of negative indexing.
+54: def cython_sum(double[::1] a): # contiguous 1D array of doubles
/* Python wrapper */ static PyObject *__pyx_pw_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_3cython_sum(PyObject *__pyx_self, PyObject *__pyx_arg_a); /*proto*/ static PyMethodDef __pyx_mdef_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_3cython_sum = {"cython_sum", (PyCFunction)__pyx_pw_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_3cython_sum, METH_O, 0}; static PyObject *__pyx_pw_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_3cython_sum(PyObject *__pyx_self, PyObject *__pyx_arg_a) { __Pyx_memviewslice __pyx_v_a = { 0, 0, { 0 }, { 0 }, { 0 } }; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("cython_sum (wrapper)", 0); assert(__pyx_arg_a); { __pyx_v_a = __Pyx_PyObject_to_MemoryviewSlice_dc_double(__pyx_arg_a); if (unlikely(!__pyx_v_a.memview)) __PYX_ERR(0, 54, __pyx_L3_error) } goto __pyx_L4_argument_unpacking_done; __pyx_L3_error:; __Pyx_AddTraceback("nimimo.tutorial.cythonext.numpyext.numpyext.cython_sum", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; __pyx_r = __pyx_pf_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_2cython_sum(__pyx_self, __pyx_v_a); /* function exit code */ __Pyx_RefNannyFinishContext(); return __pyx_r; } static PyObject *__pyx_pf_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_2cython_sum(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_a) { double __pyx_v_s; int __pyx_v_i; int __pyx_v_n; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("cython_sum", 0); /* … */ /* function exit code */ __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_4); __Pyx_AddTraceback("nimimo.tutorial.cythonext.numpyext.numpyext.cython_sum", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __PYX_XDEC_MEMVIEW(&__pyx_v_a, 1); __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* … */ __pyx_tuple__22 = PyTuple_Pack(5, __pyx_n_s_a, __pyx_n_s_a, __pyx_n_s_s, __pyx_n_s_i, __pyx_n_s_n); if (unlikely(!__pyx_tuple__22)) __PYX_ERR(0, 54, __pyx_L1_error) __Pyx_GOTREF(__pyx_tuple__22); __Pyx_GIVEREF(__pyx_tuple__22); /* … */ __pyx_t_5 = PyCFunction_NewEx(&__pyx_mdef_6nimimo_8tutorial_9cythonext_8numpyext_8numpyext_3cython_sum, NULL, __pyx_n_s_nimimo_tutorial_cythonext_numpye); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 54, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_5); __pyx_t_3 = NULL; if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_1))) { __pyx_t_3 = PyMethod_GET_SELF(__pyx_t_1); if (likely(__pyx_t_3)) { PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_1); __Pyx_INCREF(__pyx_t_3); __Pyx_INCREF(function); __Pyx_DECREF_SET(__pyx_t_1, function); } } if (!__pyx_t_3) { __pyx_t_2 = __Pyx_PyObject_CallOneArg(__pyx_t_1, __pyx_t_5); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 51, __pyx_L1_error) __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; __Pyx_GOTREF(__pyx_t_2); } else { __pyx_t_4 = PyTuple_New(1+1); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 51, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); __Pyx_GIVEREF(__pyx_t_3); PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3); __pyx_t_3 = NULL; __Pyx_GIVEREF(__pyx_t_5); PyTuple_SET_ITEM(__pyx_t_4, 0+1, __pyx_t_5); __pyx_t_5 = 0; __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_4, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 51, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; } __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; if (PyDict_SetItem(__pyx_d, __pyx_n_s_cython_sum, __pyx_t_2) < 0) __PYX_ERR(0, 54, __pyx_L1_error) __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; __pyx_codeobj__23 = (PyObject*)__Pyx_PyCode_New(1, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__22, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_dylanbespalko_repos_nimim, __pyx_n_s_cython_sum, 54, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__23)) __PYX_ERR(0, 54, __pyx_L1_error)
+55: cdef double s = 0.0
__pyx_v_s = 0.0;
+56: cdef int i, n = a.shape[0]
__pyx_v_n = (__pyx_v_a.shape[0]);
+57: for i in range(n):
__pyx_t_1 = __pyx_v_n; for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) { __pyx_v_i = __pyx_t_2;
+58: s += a[i]
__pyx_t_3 = __pyx_v_i; __pyx_v_s = (__pyx_v_s + (*((double *) ( /* dim=0 */ ((char *) (((double *) __pyx_v_a.data) + __pyx_t_3)) )))); }
+59: return s
__Pyx_XDECREF(__pyx_r); __pyx_t_4 = PyFloat_FromDouble(__pyx_v_s); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 59, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); __pyx_r = __pyx_t_4; __pyx_t_4 = 0; goto __pyx_L0;