cython: run prange sequentially (profiling/debugging)
<p>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.)</p>

<p>One big question to which I haven't found an answer arose when profiling/debugging:</p>

<p><strong>Is there a way to turn off parallelization (i.e. run "prange"s sequentially) at either a global or local level?</strong></p>

<p>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.</p>

<p>Just in case it's relevant: I'm using Python 3 (currently 3.4.5).</p>

<p>There's at least three very easy ways:</p>

<ol>
<li><p><code>prange</code> takes a <code>num_threads</code> argument. Set that equal to 1. This gives you local control.</p></li>
<li><p>If you compile without OpenMP it'll still still run, but not in parallel. Remove <code>extra_compile_args=['-fopenmp']</code> and <code>extra_link_args=['-fopenmp']</code> (or equivalent) from setup.py. (OpenMP is implemented in terms of <code>#pragmas</code> so just gets ignored without compiler support). This gives you an easy global switch.</p></li>
<li><p>OpenMP <a href="https://software.intel.com/en-us/node/528373" rel="nofollow">defines an environmental variable for the maximum number of threads</a> <code>OMP_NUM_THREADS</code>. Set that to 1 in your operating system and then run your cython program. This gives a global switch (without recompiling)</p></li>
</ol>

python python-3.x debugging parallel-processing cython