3

I am trying to make use of prange to run my loop in parallel. I am using nested prange. But during debugging I get not desired results.

This is my code:

%%cython
# distutils: language = c++
# distutils: extra_compile_args = -fopenmp
# distutils: extra_link_args = -fopenmp
cimport cython.parallel as parallel
from libcpp.vector cimport vector

cdef int current_row, current_col
cdef int current_thread_id

cdef vector[int] container_1
cdef vector[int] container_2

for current_row in parallel.prange(1000, num_threads=10, nogil=True):
    for current_col in parallel.prange(1000, num_threads=10):
        current_thread_id = parallel.threadid()
        if current_thread_id == 4:
            container_1.push_back(current_row)
            container_2.push_back(current_col)

print container_1[0], ' ', container_2[0]

print container_1.back(), ' ', container_2.back()

This is what I get on the output:

400   0
499   999

As I see the inner loop is always executed fully for each thread. Is it possible to make the nested pranges to work? And if yes, how can I track the outer thread id and inner thread id?

warmspringwinds
  • 1,147
  • 2
  • 14
  • 31
  • 1
    Why the OpenMP tag? This seems to have no OpenMP relevance at all... – Jim Cownie Jun 30 '15 at 12:35
  • 1
    @JimCownie Cython prange uses OpenMP as its mechanism so the tag is somewhat relevant (although I agree, not completely relevant). – DavidW Jun 30 '15 at 20:53
  • 1
    @warmspringwinds - Have a look at the answers to this question (amoung others) http://stackoverflow.com/questions/10540760/openmp-nested-parallel-for-loops-vs-inner-parallel-for. I don't think you can use the `collapse` feature from Cython. Why do you need to parallelize both loops? – DavidW Jun 30 '15 at 20:59
  • @warmspringwinds Has this been solved? – schneiderfelipe Apr 26 '20 at 03:42

0 Answers0