Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform cython files compilation in parallel?

I would like to perform cython files compilation in parallel.

So, I take a look at Cython.Build source file, and find the following signature for cythonize function:

def cythonize(module_list, exclude=None, nthreads=0, aliases=None,
              quiet=False, force=False, language=None,
              exclude_failures=False, **options):

And the following comment about cythonize nthreads option:

"For parallel compilation, set the 'nthreads' option to the number of
concurrent builds."

So I tried to use this option in my setup.py file, like that:

from setuptools import setup
from Cython.Build import cythonize
from Cython.Distutils.extension import Extension

EXTENSIONS = [Extension(...)
              ...
              Extension(...)]

setup(name='...',
      ...
      ext_modules=cythonize(EXTENSIONS, nthreads=8),
      ...)

But my .pyx files are still compiled sequentially using 1 thread.

I do not understand what I am doing wrong here and how to use nthreads option to perform cythonize compilation in parallel ?

like image 248
Axel Borja Avatar asked Jun 24 '16 12:06

Axel Borja


1 Answers

Cython builds are a two step process:

  1. source.py to source.c
  2. source.c to source.o

The nthreads argument to cythonize() controls the concurrency of the first process, but not the second.

For the second process build_ext takes a -j argument to control the concurrency of builds, so you can speed up your builds like this:

python setup.py build_ext -j 4

Or if you are building a wheel, you can use:

python setup.py build_ext -j 4 bdist_wheel

like image 91
cdr Avatar answered Sep 17 '22 04:09

cdr