Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doing " += 1" in arrays does not add one in prange loop #5942

Closed
2 tasks done
yaoyuki opened this issue Jun 30, 2020 · 2 comments · Fixed by #6046
Closed
2 tasks done

doing " += 1" in arrays does not add one in prange loop #5942

yaoyuki opened this issue Jun 30, 2020 · 2 comments · Fixed by #6046

Comments

@yaoyuki
Copy link

yaoyuki commented Jun 30, 2020

Reporting a bug

import numpy as np
import numba as nb

d = 10
k = 2

@nb.njit(parallel = True)
def test_parallel(d, k):

    gg      = np.zeros((d, k), dtype = nb.int32) 
    gg_next = np.zeros((d, k), dtype = nb.int32)   
        
          
    for i_gg in nb.prange(d): #this part can be parallelized.

        # set value for gg[i_gg]
        for n in range(k):
            gg[i_gg, n] = i_gg

        
        gg_next[i_gg, :]  = gg[i_gg, :] # initialize gg_next[i_gg]
        gg_next[i_gg, 0] += 1

    # print errors (without prange to print results nicely)
    for i_gg in range(d):
        print('i_gg = ', i_gg , ', gg = ', gg[i_gg], ', gg_next = ', gg_next[i_gg])

then I got

test_parallel(d, k)
i_gg =  0 , gg =  [0 0] , gg_next =  [1 0]
i_gg =  1 , gg =  [1 1] , gg_next =  [1 1]
i_gg =  2 , gg =  [2 2] , gg_next =  [1 2]
i_gg =  3 , gg =  [3 3] , gg_next =  [1 3]
i_gg =  4 , gg =  [4 4] , gg_next =  [1 4]
i_gg =  5 , gg =  [5 5] , gg_next =  [1 5]
i_gg =  6 , gg =  [6 6] , gg_next =  [1 6]
i_gg =  7 , gg =  [7 7] , gg_next =  [1 7]
i_gg =  8 , gg =  [8 8] , gg_next =  [1 8]
i_gg =  9 , gg =  [9 9] , gg_next =  [1 9]

If we slightly modify code in this way, or simply drop 'parallel = True'

@nb.njit(parallel = True)
def test_parallel_nocolon(d, k):

    gg      = np.zeros((d, k), dtype = nb.int32)
    gg_next = np.zeros((d, k), dtype = nb.int32)   
        
          
    for i_gg in nb.prange(d): #this part can be parallelized.

        # set value for gg[i_gg]
        for n in range(k):
            gg[i_gg, n] = i_gg

        
        gg_next[i_gg]  = gg[i_gg] # <--- only diff
        gg_next[i_gg, 0] += 1

    # print errors (without prange to print results nicely)
    for i_gg in range(d):
        print('i_gg = ', i_gg , ', gg = ', gg[i_gg], ', gg_next = ', gg_next[i_gg])
test_parallel_nocolon(d, k)

then I get

i_gg =  0 , gg =  [0 0] , gg_next =  [1 0]
i_gg =  1 , gg =  [1 1] , gg_next =  [2 1]
i_gg =  2 , gg =  [2 2] , gg_next =  [3 2]
i_gg =  3 , gg =  [3 3] , gg_next =  [4 3]
i_gg =  4 , gg =  [4 4] , gg_next =  [5 4]
i_gg =  5 , gg =  [5 5] , gg_next =  [6 5]
i_gg =  6 , gg =  [6 6] , gg_next =  [7 6]
i_gg =  7 , gg =  [7 7] , gg_next =  [8 7]
i_gg =  8 , gg =  [8 8] , gg_next =  [9 8]
i_gg =  9 , gg =  [9 9] , gg_next =  [10  9]
@esc
Copy link
Member

esc commented Jul 1, 2020

@yaoyuki thanks for reporting this. I can reproduce this and it looks like a bug in the parfors component of Numba. My guess would be that something is going wrong during array analysis.

@esc
Copy link
Member

esc commented Jul 1, 2020

cc @DrTodd13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants