Sulabh Tiwari Sulabh Tiwari - 12 days ago 5
C Question

Identify C function in C code generated using Cython

I have been trying to understand how cython really works. Very first, I have written test.pyx file containing,

import numpy as np
a=2;b=3;
np.sum(a,b)
np.subtract(a,b)


I wrap this code in setup.py file,

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize("test.pyx"))


Later I run test.pyx from command line as,

python setup.py build_ext --inplace


This command generates test.c code. The generated C code is really big and difficult to understand. Now, I just like to know where can I find implementation of np.sum(a,b) . If i am successful to get the Python to C translated sum function can I use it as an independent function in other C codes. Will this function has some dependencies that I have to copy along with it.

Looking forward for the good answers.

Thank you guys for your suggestion. After having a look at html file what i see looks like,

+06: import numpy as np
__pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 6, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) __PYX_ERR(0, 6, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;


This means it is calling __pyx_n_s_numpy module. How can a C compiler runs this module. I think implementation of this module is in .py format.

Answer

You can run cython -a test.pyx, and it will open your default web browser with a generated HTML that has each Cython code line annotated with C code that was generated for it.

The primary purpose of this is help you optimize the code by eliminating unnecessary Python calls, so it highlights lines that involve much interaction with Python runtime.

As a Cython newbie myself, I cannot overstate its helpfulness.

Note that as per the "primary purpose" above, it doesn't include boilerplate code outside the part that directly corresponds to source lines. Use the link to the raw C on the generated page to see that.

  • E.g. __pyx_n_s_numpy is a PyObject holding the "numpy" string. As you can see by searching it, Cython creates these upon initialization for all string constants that need to be passed to Python runtime. __Pyx_Import() is also an autogenerated function that wraps the builtin __import__.

Fortunately (=thanks to Cython's author), the autogenerated names are very descriptive, so you rarely need to actually look them up: in most cases, you can guess what the entity is/does. And all Cython-autogenerated entities are helpfully prefixed to distinguish them from e.g. ones from Python C API.