使用Numba优化后仍性能低下的Mie散射计算函数mie_S1_S2加速方案咨询
优化Numba加速的Mie散射计算性能瓶颈
我在做Mie散射相关的Python计算时碰到了性能瓶颈,用pycallgraph分析后发现,耗时大头集中在miepython.miepython.mie_S1_S2模块(粉色高亮部分),每次调用大概要0.47秒。
这个函数已经用Numba的@njit装饰器做了即时编译优化,但实际运行速度还是没达到预期。函数里的循环总迭代次数大概是25000次(len(mu)=50,len(a)-1=500),下面是这个模块的源代码:
import numpy as np from numba import njit, int32, float64, complex128 __all__ = ('ez_mie', 'ez_intensities', 'generate_mie_costheta', 'i_par', 'i_per', 'i_unpolarized', 'mie', 'mie_S1_S2', 'mie_cdf', 'mie_mu_with_uniform_cdf', ) @njit((complex128, float64, float64[:]), cache=True) def _mie_S1_S2(m, x, mu): """ Calculate the scattering amplitude functions for spheres. The amplitude functions have been normalized so that when integrated over all 4*pi solid angles, the integral will be qext*pi*x**2. The units are weird, sr**(-0.5) Args: m: the complex index of refraction of the sphere x: the size parameter of the sphere mu: array of angles, cos(theta), to calculate scattering amplitudes Returns: S1, S2: the scattering amplitudes at each angle mu [sr**(-0.5)] """ nstop = int(x + 4.05 * x**0.33333 + 2.0) + 1 a = np.zeros(nstop - 1, dtype=np.complex128) b = np.zeros(nstop - 1, dtype=np.complex128) _mie_An_Bn(m, x, a, b) nangles = len(mu) S1 = np.zeros(nangles, dtype=np.complex128) S2 = np.zeros(nangles, dtype=np.complex128) nstop = len(a) for k in range(nangles): pi_nm2 = 0 pi_nm1 = 1 for n in range(1, nstop): tau_nm1 = n * mu[k] * pi_nm1 - (n + 1) * pi_nm2 S1[k] += (2 * n + 1) * (pi_nm1 * a[n - 1] + tau_nm1 * b[n - 1]) / (n + 1) / n S2[k] += (2 * n + 1) * (tau_nm1 * a[n - 1] + pi_nm1 * b[n - 1]) / (n + 1) / n temp = pi_nm1 pi_nm1 = ((2 * n + 1) * mu[k] * pi_nm1 - (n + 1) * pi_nm2) / n pi_nm2 = temp # calculate norm = sqrt(pi * Qext * x**2) n = np.arange(1, nstop + 1) norm = np.sqrt(2 * np.pi * np.sum((2 * n + 1) * (a.real + b.real))) S1 /= norm S2 /= norm return [S1, S2]
另外,这个代码里调用的_mie_An_Bn函数也已经用Numba做了优化,源代码如下:
@njit((complex128, float64, complex128[:], complex128[:]), cache=True) def _mie_An_Bn(m, x, a, b): """ Compute arrays of Mie coefficients A and B for a sphere. This estimates the size of the arrays based on Wiscombe's formula. The length of the arrays is chosen so that the error when the series are summed is around 1e-6. Args: m: the complex index of refraction of the sphere x: the size parameter of the sphere Returns: An, Bn: arrays of Mie coefficents """ psi_nm1 = np.sin(x) # nm1 = n-1 = 0 psi_n = psi_nm1 / x - np.cos(x) # n = 1 xi_nm1 = complex(psi_nm1, np.cos(x)) xi_n = complex(psi_n, np.cos(x) / x + np.sin(x)) nstop = len(a) if m.real > 0.0: D = _D_calc(m, x, nstop + 1) for n in range(1, nstop): temp = D[n] / m + n / x a[n - 1] = (temp * psi_n - psi_nm1) / (temp * xi_n - xi_nm1) temp = D[n] * m + n / x b[n - 1] = (temp * psi_n - psi_nm1) / (temp * xi_n - xi_nm1) xi = (2 * n + 1) * xi_n / x - xi_nm1 xi_nm1 = xi_n xi_n = xi psi_nm1 = psi_n psi_n = xi_n.real else: for n in range(1, nstop): a[n - 1] = (n * psi_n / x - psi_nm1) / (n * xi_n / x - xi_nm1) b[n - 1] = psi_n / xi_n xi = (2 * n + 1) * xi_n / x - xi_nm1 xi_nm1 = xi_n xi_n = xi psi_nm1 = psi_n psi_n = xi_n.real
我用的示例输入参数是:
m = 1.336-2.462e-09j x = 8526.95 mu = np.array([-1., -0.7500396, 0.46037385, 0.5988121, 0.67384093, 0.72468684, 0.76421644, 0.79175856, 0.81723714, 0.83962897, 0.85924182, 0.87641596, 0.89383665, 0.90708978, 0.91931481, 0.93067567, 0.94073113, 0.94961222, 0.95689496, 0.96467123, 0.97138347, 0.97791831, 0.98339434, 0.98870543, 0.99414948, 0.9975728, 0.9989995, 0.9989995, 0.9989995, 0.9989995, 0.9989995,0.99899951, 0.99899951, 0.99899951, 0.99899951, 0.99899951, 0.99899951, 0.99899951, 0.99899951, 0.99899951, 0.99899952, 0.99899952, 0.99899952, 0.99899952, 0.99899952, 0.99899952, 0.99899952, 0.99899952, 0.99899952, 1. ])
想请教大家几个问题:
- 有没有什么方法可以进一步加速这个计算?
- 是不是存在某些因素阻碍了Numba发挥最优的优化效果?
- 当前这个0.47秒的单次调用速度是不是已经达到了合理的水平?
内容的提问来源于stack exchange,提问作者Ogiad
相关产品推荐
相关产品推荐

