在该函数上使用numba会引发此错误.可能是什么问题



这是我试图加速的函数。我使用的是spyder最新版本,它使用的是python 2.7,Numba版本是0.38.0-

@nb.njit(fastmath = True, parallel = True, error_model = "numpy", nogil = True)
def fun(a, b, c, d, ef):
# start = time.time()
m_d = np.array([-40, -40, -40])
f = np.zeros((128, 128, 128), np.complex64)

for i in range(0, len(d)):
x = nb.int64(math.floor((ef[i][0] - m_d[0]) / 1.2))
y = nb.int64(math.floor((ef[i][1] - m_d[1]) / 1.2))
z = nb.int64(math.floor((ef[i][2] - m_d[2]) / 1.2))
f[x][y][z] = complex(d[i])

e  = 0
g = np.zeros((128, 128, 128), np.complex64)
X = Y = Z = 128
for i in range(len(a)):
x = a[i]
y = b[i]
z = c[i]
for x2 in range(x - 1, x + 5):
for y2 in range(y - 1, y + 5):
for z2 in range(z - 1, z + 5):
if (-1 < x < X and
-1 < y < Y and
-1 < z < Z and
(x != x2 or y != y2 or z != z2) and
(0 <= x2 < X) and
(0 <= y2 < Y)and
(0 <= z2 < Z)):
q = f[x2][y2][z2]
di = np.sqrt((x - x2) ** 2 + (y - y2) ** 2 + (z - z2) ** 2) * 1.2
if di <= 6 and di >= 2:
e = 4
elif di > 6 and di < 8:
e = 38 * di - 224
elif di >= 8:
e = 80
else:
continue
value = q / (e * di)
g[x][y][z] = g[x][y][z] + value

# print "fun : ", time.time() - start
return g

错误为-

task = get()
TypeError: ('__init__() takes exactly 3 arguments (2 given)', <class 'numba.errors.LoweringError'>, ('Failed at nopython (nopython mode backend)nreflected list(array(float32, 1d, C)): unsupported nested memory-managed objectnnFile "test_numba_errorful.py", line 702:ndef fun(a, b, c, d, ef):n    <source elided>n    # m_d = np.array([-40, -40, -40])n    f = np.zeros((128, 128, 128), np.complex64)n    ^n[1] During: lowering "ef = arg(4, name=)"

在修复了一些多处理开销之后,我现在得到了这个错误-

File "/usr/local/lib/python2.7/dist-packages/numba/dispatcher.py", line 360, in _compile_for_args
raise e
LoweringError: reflected list(array(float32, 1d, C)): unsupported nested memory-managed object
File "test_numba_errorful.py", line 702:
def fun(a, b, c, d, ef):
<source elided>
# m_d = np.array([-40, -40, -40])
f = np.zeros((128, 128, 128), np.complex64)
^

错误的原因是什么?我该如何纠正?

几件事:

  1. nopython模式转换函数中不支持time函数。请参阅此处支持的python功能列表:

http://numba.pydata.org/numba-doc/latest/reference/pysupported.html

  1. 您只能使用print作为函数进行打印。在使用python 2时,您将需要from __future__ import print_function

更改以上两项使代码能够使用Numba 0.39对输入进行猜测(我只尝试了标准的numpy数组(。不过,对于您正在使用的版本,从错误中可以看出,您可能正在使用列表列表或numpy数组列表之类的内容,这在早期版本中是不受支持的。

另一个常见的建议是,在处理多维数组时,从性能角度来看,访问x[i,j]总是比访问x[i][j]更好。

最新更新