On 24 March 2013 06:25, Alexander Kalinin <
alec.k...@gmail.com> wrote:
> Hello cython users,
>
> I am new to cython and I am trying to use cython to build OpenMP parallel
> numpy code. I try to write first cython openmp code:
>
> from cython.parallel import prange, parallel
>
> cimport openmp
>
>
> def calcOpenMP():
>
> openmp.omp_set_dynamic(1)
>
> num_threads = 0
>
> with nogil, parallel():
>
> num_threads = openmp.omp_get_num_threads()
>
> print num_threads
>
>
> Buy I get error:
> hello.pyx:14:21: local variable 'num_threads' referenced before assignment
> building 'hello' extension
>
> I don't understand why variable num_threads is unassigned?
>
>
> Sincerely,
> Alexander
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
cython-users...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>
The semantics are a little weird, basically Cython deletes the all
variables that are private variables in the parallel block, because it
wouldn't know which value to assume for the variable. This doesn't
really apply here, since the value would be the same in all threads.
However, you can work around this by using prange with one iteration:
for i in prange(1):
num_threads = omp_get_num_threads()
There is also omp_get_max_threads(), which gives an upper bound on the
number of threads a parallel region will assume.