gpt4 book ai didi

python - 如何用 numba 加速这个 python 函数?

转载 作者:太空宇宙 更新时间:2023-11-03 16:51:06 25 4
gpt4 key购买 nike

我正在尝试加速这个 python 函数:

def twoFreq_orig(z, source_z, num, den, matrix, e):
Z1, Z2 = np.meshgrid(source_z, np.conj(z))
Z1 **= num
Z2 **= den - 1
M = (e ** ((num + den - 2) / 2.0)) * Z1 * Z2
return np.sum(matrix * M, 1)

其中zsource_znp.ndarray(1d,dtype=np.complex128),numdennp.ndarray(2d,dtype=np.float64),矩阵 是一个 np.ndarray(2d,dtype=np.complex128),e 是一个 np.float64 >.

我对 Numba 没有太多经验,但是在阅读了一些教程之后,我想出了这个实现:

@nb.jit(nb.f8[:](nb.c16[:], nb.c16[:], nb.f8[:, :], nb.f8[:, :], nb.c16[:, :], nb.f8))
def twoFreq(z, source_z, num, den, matrix, e):
N1, N2 = len(z), len(source_z)
out = np.zeros(N1)
for r in xrange(N1):
tmp = 0
for c in xrange(N2):
n, d = num[r, c], den[r, c] - 1
z1 = source_z[c] ** n
z2 = z[r] ** d
tmp += matrix[r, c] * e ** ((n + d - 1) / 2.0) * z1 * z2
out[r] = tmp
return out

不幸的是,Numba 实现不但没有加速,反而比原来慢了好几倍。我不知道如何正确使用 Numba。有哪位 Numba 专家可以帮助我吗?

最佳答案

实际上,如果不对数组的属性有更多的了解,我认为您无法做太多事情来加速 numba 函数(是否有一些数学技巧可以更快地完成一些计算)。

但我注意到一个错误:例如,您没有在 numba 版本中结合数组,并且我编辑了一些行以使其更加精简(其中一些可能只是口味)。我已经在适当的地方添加了评论:

@nb.njit
def twoFreq(z, source_z, num, den, matrix, e):
#Replace z with conjugate of z (otherwise the result is wrong!)
z = np.conj(z)
# Size instead of len() don't know if it actually makes a difference but it's cleaner
N1, N2 = z.size, source_z.size
# Must be zeros_like otherwise you create a float array where you want a complex one
out = np.zeros_like(z)
# I'm using python 3 so you need to replace this by xrange later
for r in range(N1):
for c in range(N2):
n, d = num[r, c], den[r, c] - 1
z1 = source_z[c] ** n
z2 = z[r] ** d
# Multiply with 0.5 instead of dividing by 2
# Work on the out array directly instead of a tmp variable
out[r] += matrix[r, c] * e ** ((n + d - 1) * 0.5) * z1 * z2
return out

def twoFreq_orig(z, source_z, num, den, matrix, e):
Z1, Z2 = np.meshgrid(source_z, np.conj(z))
Z1 **= num
Z2 **= den - 1
M = (e ** ((num + den - 2) / 2.0)) * Z1 * Z2
return np.sum(matrix * M, 1)


numb = 1000
z = np.random.uniform(0,1,numb) + 1j*np.random.uniform(0,1,numb)
source_z = np.random.uniform(0,10,numb) + 1j*np.random.uniform(0,1,numb)
num = np.random.uniform(0,1,(numb,numb))
den = np.random.uniform(0,1,(numb,numb))
matrix = np.random.uniform(0,1,(numb,numb)) + 1j*np.random.uniform(0,1,(numb, numb))
e = 5.5

# This failed for your initial version:
np.testing.assert_array_almost_equal(twoFreq(z, source_z, num, den, matrix, e),
twoFreq_orig(z, source_z, num, den, matrix, e))

我的计算机上的运行时间是:

%timeit twoFreq(z, source_z, num, den, matrix, e)

1 loop, best of 3: 246 ms per loop

%timeit twoFreq_orig(z, source_z, num, den, matrix, e)

1 loop, best of 3: 344 ms per loop

它比您的 numpy 解决方案快大约 30%。但我认为通过巧妙地使用广播,numpy 解决方案可以变得更快一些。但尽管如此,我获得的大部分加速都是来自省(introspection)略签名:请注意,您可能使用 C 连续数组,但您给出了任意顺序(因此 numba 可能会慢一点,具体取决于计算机体系结构)。可能通过定义 c16[::-1] 你会得到相同的速度,但通常只是让 numba 推断类型,它可能会尽可能快。异常(exception):您希望每个变量有不同的精度输入(例如您希望 zcomplex128complex64)

当您的 numpy 解决方案内存不足时,您将获得惊人的加速(因为您的 numpy 解决方案已矢量化,因此需要更多 RAM!)使用 numb = 5000 numba 版本的速度大约快了 3 倍比 numpy 的要好。

<小时/>

编辑:

通过巧妙的广播,我的意思是

np.conj(z[:,None]**(den-1)) * source_z[None, :]**(num)

等于

z1, z2 = np.meshgrid(source_z, np.conj(z))
z1**(num) * z2**(den-1)

但在第一个变体中,您只能对 numb 元素进行幂运算,而您有一个 (numb, numb) 形状的数组,因此您可以执行更多“幂”操作不必要的操作(尽管我猜对于小数组,结果可能大部分被缓存并且不是很昂贵)

没有 mgrid 的 numpy 版本(产生相同的结果)如下所示:

def twoFreq_orig2(z, source_z, num, den, matrix, e):
z1z2 = source_z[None,:]**(num) * np.conj(z)[:, None]**(den-1)
M = (e ** ((num + den - 2) / 2.0)) * z1z2
return np.sum(matrix * M, 1)

关于python - 如何用 numba 加速这个 python 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com