Skip to content

Cython specifics

Thomas Bonald edited this page Mar 20, 2024 · 8 revisions

Cython usage

The interest of Cython is to generate C/C++ code from Python-like syntax (in .pyx files). It is not necessary for the user to have Cython installed. It is only necessary for developers.

Generating C code with Cython

When adding or editing .pyx files, running Cython and compiling the resulting C++ files is required.

To do this, type the following in a terminal from the root of the package:

python setup.py build_ext --inplace

This requires a compiler installed on your machine (GCC/G++ on Linux/OSX and Visual Studio Tools on Windows).

Parallelization

Cython-based parallelisation relies on OpenMP. Most Linux installations ship with compatible compilers. MacOS requires additional installations (see relevant links here and here). Windows seems a bit trickier as no free OpenMP-compatible compiler is available at the moment.

Standard libraries from C/C++

Some elements of the standard libraries can be found in Cython, see this and this.

Tips

  • Keep the Cython code as short as possible. If you have some of your code in algo.py and some in algo_core.pyx, try to put as many things as possible in the first file. This will make code review more easy and reduce the probability of bugs or mistakes. In this end, your Cython code usually encapsulates only a for loop.
  • Remove calls to external dependencies such as numpy. A numpy array as input is interpreted as a MemoryView by Cython, no need to make the conversion in the .pyx file. If your final output is also a numpy array, simply call np.asarray(output) in the .py file.
  • If your Cython output is a libcpp.vector object, it will be interpreted as a list in the .py file.
  • Cython generates one .html file per .pyx file upon compilation. These files give an intuitive view of the size of the corresponding C/C++ code: the yellow segments correspond to Python instructions that have been translated in many C/C++ ones. The darker the yellow, the more C/C++ instruction there are. Ideally, your aim would be to have few and light yellow segments.
  • When writing Cython code, run the first few times without any decorators (like @cython.boundscheck(False) and @cython.wraparound(False)) to make sure your code works. Add the decorators at a near final stage (so as to prevent SegmentationFault errors) to increase the speed-up.