我运行了我在CUDA Python简介页面上阅读的代码: -
import numpy as np
from timeit import default_timer as timer
from numbapro import vectorize
@vectorize(["float32(float32, float32)"], target='gpu')
def VectorAdd(a, b):
return a + b
def main():
N = 32000000
A = np.ones(N, dtype=np.float32)
B = np.ones(N, dtype=np.float32)
C = np.zeros(N, dtype=np.float32)
start = timer()
C = VectorAdd(A, B)
vectoradd_timer = timer() - start
print("C[:5] = " + str(C[:5]))
print("C[-5:] = " + str(C[-5:]))
print("VectorAdd took %f seconds" % vectoradd_timer)
if __name__ == '__main__':
main()
我在终端上收到以下错误: -
dtn34@dtn34-ubuntu:~/Python$ python asd.py
Traceback (most recent call last):
File "asd.py", line 3, in <module>
from numbapro import vectorize
ImportError: No module named numbapro
它应该使用gpu运行代码,但我收到了这个错误。我已经安装了anaconda,更新了conda,使用conda安装加速,安装了cudatoolkit,使用conda安装了numba。 我尝试使用python2和python3
进行编译我该怎么办?
答案 0 :(得分:7)
知道了。正如WarrenWeckesser和Robert Crovella所指出的那样,NumbaPro已被弃用,所有功能都转移到了numba。 因此,你应该写numba
而不是numbaprofrom numba import vectorize
此外,目标需要设置为&#39; cuda&#39;而不是&#39; gpu&#39;
@vectorize(["float32(float32, float32)"], target='cuda')
def VectorAdd(a, b):
return a + b
答案 1 :(得分:0)
修改后,我试图同时在CPU和GPU中运行它,CPU的速度比GPU快
CPU中的第一个:
import numpy as np
from timeit import default_timer as timer
# from numba import vectorize
# @vectorize(["float32(float32, float32)"], target='cuda')
def VectorAdd(a ,b):
return a + b
def main():
N = 32000000
A = np.ones(N, dtype=np.float32)
B = np.ones(N, dtype=np.float32)
C = np.ones(N, dtype=np.float32)
srart = timer()
C = VectorAdd(A,B)
vectoradd_time = timer() - srart
print ("C[:5] = " + str(C[:5]))
print ("C[:-5] = " + str(C[:-5]))
print ('vectoradd_time %f second' % vectoradd_time)
if __name__== '__main__':
main()
时间:
vectoradd_time 0.046457 second
GPU中的第二个:
import numpy as np
from timeit import default_timer as timer
from numba import vectorize
@vectorize(["float32(float32, float32)"], target='cuda')
def VectorAdd(a, b):
return a + b
def main():
N = 32000000
A = np.ones(N, dtype=np.float32)
B = np.ones(N, dtype=np.float32)
C = np.zeros(N, dtype=np.float32)
start = timer()
C = VectorAdd(A, B)
vectoradd_timer = timer() - start
print("C[:5] = " + str(C[:5]))
print("C[-5:] = " + str(C[-5:]))
print("VectorAdd took %f seconds" % vectoradd_timer)
if __name__ == '__main__':
main()
时间:
VectorAdd took 0.240731 seconds
此结果取决于您的CPU规格。