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.

Parallel vs sequential for loop different results

See original GitHub issue

Hello 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:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
guilhermeleobascommented, Apr 15, 2022

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

0reactions
stuartarchibaldcommented, May 20, 2022

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!

Read more comments on GitHub >

github_iconTop 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 >

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