Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallelism in Cython does not work

I have a following Cython code:

from cython import parallel
from libc.stdio cimport printf

def test_func():
    cdef int thread_id = -1
    with nogil, parallel.parallel(num_threads=10):
        thread_id = parallel.threadid()
        printf("Thread ID: %d\n", thread_id)

However, it always starts only one thread, i.e. output is always only

Thread ID: 0

What am I doing wrong to get multithreading?

like image 733
Roman Avatar asked Dec 30 '25 01:12

Roman


1 Answers

Cython uses OpenMP for it's multithreading capabilities.

To enable OpenMP the compiler will need an extra flag to be passed to it while compiling and linking otherwise the parallel part of your code will be ignored.

The flags for some popular compilers are as follows:

  • GCC = -fopenmp
  • MSVC = /openmp
  • icc = -openmp

Assuming that you have saved your function in the file test.pyx the following setup.py should work if you are using GCC.

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

extensions = [Extension(
                "test",
                sources=["test.pyx"],
                extra_compile_args=["-fopenmp"],
                extra_link_args=["-fopenmp"]
            )]

setup(
    ext_modules = cythonize(extensions)
)

Once compiled like that the code should spawn 10 threads when run:

In [1]: import test

In [2]: test.test_func()
Thread ID: 9
Thread ID: 1
Thread ID: 6
Thread ID: 7
Thread ID: 3
Thread ID: 8
Thread ID: 5
Thread ID: 4
Thread ID: 0
Thread ID: 2

If you want some more information than this page in the cython docs has a good basic guide of how to use parallelism with cython.

like image 192
Simon Gibbons Avatar answered Dec 31 '25 13:12

Simon Gibbons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!