3

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?

Simon Gibbons
  • 6,969
  • 1
  • 21
  • 34
Roman
  • 2,225
  • 5
  • 26
  • 55

1 Answers1

7

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.

Simon Gibbons
  • 6,969
  • 1
  • 21
  • 34