当使用prange而不是range时,更新prange循环中的列表会产生错误的结果。你知道吗
from numba import jit, prange
import numpy as np
@jit(parallel=True)
def prange_test(A):
s = [0,0,0,0]
b = 0.
for i in prange(A.shape[0]):
s[i%4] += A[i]
b += A[i]
return s,b
def range_test(A):
s = [0,0,0,0]
b = 0.
for i in range(A.shape[0]):
s[i%4] += A[i]
b += A[i]
return s,b
A = np.random.random(100000)
print(prange_test(A))
print(range_test(A))
b的和是一样的,但s的部分和是错误的:
(array([7013.98962611, 6550.90312863, 7232.49698366, 7246.53627734]), 49955.32870429267)
([12444.683249345742, 12432.449908902432, 12596.461028432543, 12481.734517611982], 49955.32870429247)
尽管文档中有点不清楚,但是当您从
prange
并行循环的不同迭代中写入相同的数据元素时,您不能安全地累积到类似数组的对象中。我今年早些时候提交的这个github issue询问了这个具体问题。你知道吗事实上,这已经再次提出提醒我,我想提交一份公关的麻木文件,以澄清这一点。你知道吗
相关问题 更多 >
编程相关推荐