Parallel vs sequential for loop different results
See original GitHub issueHello I’m using Python 3.10.2 and numba 0.55.1. I am trying to do the following operation:
@njit(parallel=True)
def sum_parallel(data, idxs):
data_len = len(data)
grid = np.zeros((128, 128), dtype=np.float64)
for k in prange(data_len):
i = idxs[1][k]
j = idxs[0][k]
grid[i][j] += data[k]
return grid
def sum_sequential(data, idxs):
data_len = len(data)
grid = np.zeros((128, 128), dtype=np.float64)
for k in prange(data_len):
i = idxs[1][k]
j = idxs[0][k]
grid[i][j] += data[k]
return grid
data = np.random.random((8000))
idxs = np.random.randint(size=(2, 8000), low=0, high=128)
grid_parallel = sum_parallel(data,idxs)
grid_sequential = sum_sequential(data, idxs)
std_difference = np.std(grid_parallel-grid_sequential)
print(std_difference)
0.014976131305802937
I guess there is a race condition here, although I am not sure if this is a bug or if it is something that I have not taken into consideration when writing my parallel function, is there a way to provide a critical section?
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
c++ - Sequential and parallel versions give different results
Show activity on this post. They both give different results. And parallel version is much slower than the sequential version. What may cause ......
Read more >Parallel vs Sequential Stream in Java - GeeksforGeeks
Only a single iteration at a time just like the for-loop. Operates multiple iterations simultaneously in different available cores. Each ...
Read more >Potential Pitfalls in Data and Task Parallelism - Microsoft Learn
In certain cases a parallel loop might run slower than its sequential equivalent. The basic rule of thumb is that parallel loops that...
Read more >Loop-level parallelism - Wikipedia
Loop -level parallelism is a form of parallelism in software programming that is concerned with extracting parallel tasks from loops.
Read more >Parallel For Loop in C# with Examples - Dot Net Tutorials
As you can see in the above output, the standard C# for loop iterates sequentially using a single thread as a result, the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi @miguelcarcamov, thanks for the report. I can reproduce the error when parallel is enabled. Maybe @stuartarchibald knows a workaround to this issue.
Related discussion: https://numba.discourse.group/t/how-do-i-use-thread-locks-in-nopython-mode/1258 https://numba.discourse.group/t/how-to-use-locks-in-nopython-mode/221
Closing this question as it seems to be resolved. Numba now has a discourse forum https://numba.discourse.group/ which is great for questions like this, please do consider posting there in future 😃 Thanks!