添加答案以将其从未答复的队列中删除。
该示例中的代码已损坏。您的 numba 或 CUDA 安装没有任何问题。您的问题中的代码(或您从中复制代码的博客)不可能发出博客文章声称的结果。
有很多方法可以对其进行修改以使其发挥作用。一种是这样的:
from numba import vectorize, jit, cuda
import numpy as np
# to measure exec time
from timeit import default_timer as timer
# normal function to run on cpu
def func(a):
for i in range(10000000):
a[i]+= 1
# function optimized to run on gpu
@vectorize(['float64(float64)'], target ="cuda")
def func2(x):
return x+1
if __name__=="__main__":
n = 10000000
a = np.ones(n, dtype = np.float64)
start = timer()
func(a)
print("without GPU:", timer()-start)
start = timer()
func2(a)
print("with GPU:", timer()-start)
Here func2
成为为设备编译的 ufunc。然后它将在 GPU 上的整个输入数组上运行。这样做的作用是:
$ python bogoexample.py
without GPU: 4.314514834433794
with GPU: 0.21419800259172916
所以它更快,但请记住 GPU 时间包括编译 GPU ufunc 所需的时间
另一种选择是实际编写 GPU 内核。像这样:
from numba import vectorize, jit, cuda
import numpy as np
# to measure exec time
from timeit import default_timer as timer
# normal function to run on cpu
def func(a):
for i in range(10000000):
a[i]+= 1
# function optimized to run on gpu
@vectorize(['float64(float64)'], target ="cuda")
def func2(x):
return x+1
# kernel to run on gpu
@cuda.jit
def func3(a, N):
tid = cuda.grid(1)
if tid < N:
a[tid] += 1
if __name__=="__main__":
n = 10000000
a = np.ones(n, dtype = np.float64)
for i in range(0,5):
start = timer()
func(a)
print(i, " without GPU:", timer()-start)
for i in range(0,5):
start = timer()
func2(a)
print(i, " with GPU ufunc:", timer()-start)
threadsperblock = 1024
blockspergrid = (a.size + (threadsperblock - 1)) // threadsperblock
for i in range(0,5):
start = timer()
func3[blockspergrid, threadsperblock](a, n)
print(i, " with GPU kernel:", timer()-start)
运行如下:
$ python bogoexample.py
0 without GPU: 4.885275377891958
1 without GPU: 4.748716968111694
2 without GPU: 4.902181145735085
3 without GPU: 4.889955999329686
4 without GPU: 4.881594380363822
0 with GPU ufunc: 0.16726416163146496
1 with GPU ufunc: 0.03758022002875805
2 with GPU ufunc: 0.03580896370112896
3 with GPU ufunc: 0.03530424740165472
4 with GPU ufunc: 0.03579768259078264
0 with GPU kernel: 0.1421878095716238
1 with GPU kernel: 0.04386183246970177
2 with GPU kernel: 0.029975440353155136
3 with GPU kernel: 0.029602501541376114
4 with GPU kernel: 0.029780613258481026
在这里您可以看到内核的运行速度比 ufunc 稍快,并且缓存(这是 JIT 编译函数的缓存,而不是调用的记忆)显着加快了 GPU 上的调用速度。