Reputation: 1262
I have a meanwhile pretty large code base written mostly in Cython. Meanwhile, I've started parallelizing it by replacing "range"s by "prange"s. (So far more or less at random, as I still have to develop a gut feeling as to where I really profit from this and where not so much.)
One big question to which I haven't found an answer arose when profiling/debugging:
Is there a way to turn off parallelization (i.e. run "prange"s sequentially) at either a global or local level?
A global switch would be most convenient, but even if there's a local solution that should at least allow me to implement a global switch myself.
Just in case it's relevant: I'm using Python 3 (currently 3.4.5).
Upvotes: 0
Views: 80
Reputation: 30913
There's at least three very easy ways:
prange
takes a num_threads
argument. Set that equal to 1. This gives you local control.
If you compile without OpenMP it'll still still run, but not in parallel. Remove extra_compile_args=['-fopenmp']
and extra_link_args=['-fopenmp']
(or equivalent) from setup.py. (OpenMP is implemented in terms of #pragmas
so just gets ignored without compiler support). This gives you an easy global switch.
OpenMP defines an environmental variable for the maximum number of threads OMP_NUM_THREADS
. Set that to 1 in your operating system and then run your cython program. This gives a global switch (without recompiling)
Upvotes: 2