Wrong results when using jit(parallel=True)
See original GitHub issueThis bug report is very similar to issue #7984 . Using Python 3.8.7, numba-0.55.1 with llvmlite-0.38.0, Under Win-10:
from numpy import ndarray
import numpy as np
import numba
from numba import jit
def test_par(img: ndarray) -> ndarray:
rad = 5
w2 = rad + rad
min_value = np.amin(img)
# print("")
img = img - min_value
s = img.shape
a_p = np.zeros((s[0] + w2, s[1] + w2), dtype=np.int32)
a_p[rad:rad+s[0], rad:rad+s[1]] = img
b = np.zeros(s, dtype=np.float32)
for i in range(rad):
for j in range(rad):
a_1 = a_p[i:i+s[0], j:j+s[1]]
b = b + a_1
return b
test_np = test_par
test_nb = numba.jit(nopython=True, fastmath=True, parallel=False)(test_par)
test_nb_p = numba.jit(nopython=True, fastmath=True, parallel=True)(test_par)
if __name__ == '__main__':
image = np.empty((256, 256), dtype=np.uint8)
for k in range(256):
for m in range(256):
image[k, m] = k
res0 = test_np(image)
res1 = test_nb(image)
res2 = test_nb_p(image)
diff1 = np.max(np.abs(res1-res0))
print(diff1)
diff2 = np.max(np.abs(res2-res0))
print(diff2)
The results are: 0.0 4250.0
There seems to be a race condition that causes different results for parallel=true.
Strangely enough, when uncommenting the print("") command, the results are OK.
Looks like the print command stops somehow the race condition (like openning the box of Schrödinger’s cat:-)).
Tracking back the problem, on numba-0.52.0 & llvmlite-0.35.0 this problem does not exist, but on numba-0.53.0 & llvmlite-0.36.0 it appears.
I hope that you can reproduce this bug.
Thanks,
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Usage of parallel option in numba.jit decoratior makes ...
The problem with parallel=True is that it's a black-box. Numba doesn't even guarantee that it will actually parallelize anything.
Read more >Automatic parallelization with @jit - Numba
parallel =True ) is support for explicit parallel loops. One can use Numba's ; prange instead of ; range to specify that a...
Read more >Automatic parallelization with @jit - Numba documentation
Setting the parallel option for jit() enables a Numba transformation pass that attempts to automatically parallelize and perform other optimizations on (part ...
Read more >How do I parallelize this code? - Support - Numba Discussion
If I run with parallel=False and prange the result is correct (that is ... from numba import jit import numpy as np @jit(nopython=True, ......
Read more >numba.pdf - PRACE Events
Cannot be used in conjunction with parallel=True. 6. Page 7. The Jit compiler options/toggles o parallel=True - enables the automatic parallelization of a ......
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

Thanks for the report @AvinoamK. Thanks also for debugging this further @apmasell and @czgdp1807. That inserting a
printin that specific location “fixes” it suggests it’s probably an issue to do with fusion of parfors nodes as theprintstatement would defeat that.Here’s a MWR:
if the
'fusion'option in theparalleloptions dictionary is set toFalsethen the code works as expected. Fusion probably shouldn’t occur in this case as the fused code would be computing theaminof an array that’s being mutated. Would guess the cross iteration dependency checks need making aware of this sort of use case, but would need to look at it more.I investigated a little bit more after your comment @stuartarchibald . I think that the
has_cross_iter_depis giving a False positive that there is no cross iteration dependency. So, I applied the following diff which adds a conservative check to figure out cross iteration dependency. Basically after the diff, if in a loop body, two successive statements have a common variable thenhas_cross_iter_depwill assume that there is a cross iteration dependency. However I think this is very restrictive. May be we can relax it to, “only when LHS of the previous statement is the RHS of the current statement” then assume a cross iteration dependency. One more example wheremainfails but with the applied diff things workout.https://github.com/numba/numba/blob/87ab859d95aa6322a72d3d31027f39a6ddf4af10/numba/parfors/parfor.py#L4113
Diff
Definition of
foowhich fail withmainbranch but work with the above diffPlease feel free to correct me if I mis-interpreted something. I am learning about Numba with the help of these investigations and your comments. Thanks. 😃