我最近一直在用Numba做实验,但我还是不能理解:
在带有NumPy数组的普通Python函数中,可以执行以下操作:
# Subtracts two NumPy arrays and returns an array as the result
def sub(a, b):
res = a - b
return res
但是,当您使用Numba的@guvectorize
修饰符时:
结果甚至不正确。更糟糕的是,它还抱怨“对[parameters]使用[math operator]无效”
我很困惑。即使我试过这个:
# Subtracts two NumPy arrays and returns an array as the result
@guvectorize(['void(float32[:], float32[:], float32[:])'],'(n),(n)->(n)')
def subTt(a, b, res):
res = np.subtract(a,b)
结果仍然不正确。考虑到this is supposed to be a supported Math operation,我不明白它为什么不起作用。在
我知道标准方法是这样的:
# Subtracts two NumPy arrays and returns an array as the result
@guvectorize(['void(float32[:], float32[:], float32[:])'],'(n),(n)->(n)')
def subTtt(a, b, res):
for i in range(a.shape[0]):
res[i] = a[i] - b[i]
这确实如预期的那样有效。在
但我的方式有什么问题?在
p/S这只是一个简单的例子来解释我的问题,实际上我并不打算使用@guvectorize
来减去数组:p
P/P/S我怀疑这与数组如何复制到gpu内存有关,但我不确定。。。
P/P/P/S This看起来很相关,但是这里的函数只在一个线程上运行,对吗。。。在
正确的写法是:
您所尝试的失败的原因是python语法的限制,而不是numba特有的。在
name = expr
将name的值重新绑定到expr,它永远不会改变name的原始值,就像在c++引用中一样。在name[] = expr
调用(本质上),name.__setitem__
它可以用来修改名称,就像numpy数组一样,空片段{相关问题 更多 >
编程相关推荐