我正在尝试使用target='cuda'运行以下兼容Numba-nopython的函数
@numba.jit(nopython = True)
def hermite_polynomials(X, N):
r'''
Evaluate the orthonormal Hermite polynomials on
:math:`(\mathbb{R},\frac{1}{\sqrt{2\pi}}\exp(-x^2/2)dx)` in :math:`X\subset\mathbb{R}`
:param X: Locations of desired evaluations
:type X: One dimensional np.array
:param N: Number of polynomials
:rtype: numpy.array of shape :code:`X.shape[0] x N`
'''
out = np.zeros((X.shape[0], N))
deg = N - 1
factorial = np.ones((1,N))
for i in range(1,N):
factorial[0,i:]*=i
orthonormalizer = 1 / np.sqrt(factorial)
if deg < 1:
out = np.ones((X.shape[0], 1))
else:
out[:, 0] = np.ones((X.shape[0],))
out[:, 1] = X
for n in range(1, deg):
out[:, n + 1] = X * out[:, n] - n * out[:, n - 1]
return out * orthonormalizer然而,我没有找到任何示例代码,它们既对我来说足够容易理解(只有Python和MATLAB的经验,没有计算机科学家的经验),又很难到真正有帮助的程度(我只找到了a+b类型的示例)。
到目前为止,我已经实现了下面的函数,需要向它传递一个1数组(我自己不能定义一个数组,cuda.local.array((N,1),dtype=float64)会导致一个ConstantInferenceError)。我承认我必须逐项进行乘法运算,因此需要额外的for循环,但这并不起作用,因为我得到了一个Invalid usage of * with parameters (array(float64, 1d, C), float64)错误。
@numba.jit(target = 'cuda')
def hermite_polynomials2(X, N,out):
r'''
Evaluate the orthonormal Hermite polynomials on
:math:`(\mathbb{R},\frac{1}{\sqrt{2\pi}}\exp(-x^2/2)dx)` in :math:`X\subset\mathbb{R}`
:param X: Locations of desired evaluations
:type X: One dimensional np.array
:param N: Number of polynomials
:rtype: numpy.array of shape :code:`X.shape[0] x N`
'''
deg = N-1
L = X.shape[0]
if deg == 0:
return
else:
out[:, 1] = X
for n in range(1, deg):
for j in range(L):
out[j, n + 1] = X * out[j, n] - n * out[j, n - 1]
factorial = 1
for i in range(1,N):
factorial *= i
for j in range(L):
out[j,i] /= np.sqrt(factorial)
return 我怎么做乘法呢?
发布于 2018-04-04 12:19:50
你可能想要这样的东西:
for j in range(L):
out[j, n + 1] = X[j] * out[j, n] - n * out[j, n - 1]但请注意,编写此内核的整个练习大多是徒劳的。引用相关的documentation
为了获得最佳性能,用户应该编写代码,使每个线程一次只处理一个元素。
你写的内核将是完全串行的。它将比CPU版本慢。您需要以完全不同的方式编写代码,才能使其在GPU上具有任何价值。
https://stackoverflow.com/questions/49635151
复制相似问题