question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

incoherent behaviour with prange

See original GitHub issue

First, thanks a lot for this very useful job !

Here is the problem: I slightly change the example of prange given in the documentation and get a very strange behaviour. Here is my code

from numba import njit, prange
@njit(parallel=True)
def prange_test(A, cond):
    s = 0
    for i in prange(A.shape[0]):
        if cond[i]:
            s += 1
    return s

Here are my inputs

A = np.random.randint(0,10,100)
cond = np.random.randint(0,2,len(A)).astype('bool')

In this case scenario changing njit option from True to False do not affect the result of prange_test(A, cond) (which is what I expect)

However, setting s=1 as a starting condition in prange_test() definition make prange_test(A, cond) results to be different depending of the parallel value ! If parallel==False, the result is ok; If parallel==True, the results is larger of the quantity: 1 x number_threads +1. Setting s=2 as a starting condition in prange_test() definition makes the result to be larger of quantity 2 x number_of_threads +2.

This strange behavior disappears if there is no check of the cond value in prange_test(A, cond) (it returns the length of A+initial s value) whatever the initial value of s It seems inconsistent as it gives wrong results and threads dependent. I do not understand why checking ``cond’ creates inconsistencies. Thanks for the help !

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DrTodd13commented, Aug 26, 2021

@lanougue For a quick fix, try the following. I know it shouldn’t matter if you do s = 1 at the beginning or s+= 1 at the end but given the bug it does matter. We can do something here for += and *= but for more esoteric operators we may not know the identity value and so have to use the initial value as the identity value. The bug is that in this case it doesn’t recognize the operator as addition and so doesn’t know the identity is 0 and so using the previous value of the variable. The complex reason is that there’s a hidden array of reduction values (each worker gets its own) and that array is currently initialized with the value of the reduction variable when entering the loop but that needs to be the identity value.

from numba import njit, prange
@njit(parallel=True)
def prange_test(A, cond):
    s = 0
    for i in prange(A.shape[0]):
        if cond[i]:
            s += 1
    s += 1
    return s
1reaction
lanouguecommented, Aug 26, 2021

The if condition seems to maybe be confusing the analysis for the reduction - If I change prange_init_1 to:

@njit(parallel=True)
def prange_init_1(A, cond):
    s = 1
    for i in prange(A.shape[0]):
        val = 0
        if cond[i]:
            val += 1
        s += val
    return s

then the correct result is obtained. @lanougue does this pattern provide a workaround for your code for the time being?

Thanks for the workaround, I will give a look. Thanks for the reactivity !

Whoever wants to look at this numba bug is welcome. My python level is clearly not good enough for that ! 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Depression and mania: clinical features and brain mechanisms
Speech is rapid and may become incoherent, reflecting flight of ideas. Appetite and libido are increased, and social behaviour may be uninhibited.
Read more >
Attachment and the sources of behavioral pathology
Attachment and the sources of behavioral pathology - Volume 15 Issue 3. ... and singular (coherent) vs. multiple (incoherent) models of attachment: Findings ......
Read more >
The Brain and Behavior
ily involved in behavior such as the nucleus accumbens ... The activation of the slice is shown in orange in panel A in...
Read more >
Theory of universal incoherent metallic transport - ResearchGate
In an incoherent metal, transport is controlled by the collective diffusion of energy and charge rather than by quasiparticle or momentum ...
Read more >
Mode delocalization in disordered photonic Chern insulator
tonics to probe the localization behavior of 2D Chern insulators. ... waveguides (orange arrows); there are no coupling waveguides.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found