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?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With