问题
I ran this code I read on a CUDA Python intro page:-
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()
And I am getting the following error on terminal:-
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
It was supposed to run the code using the gpu but I am getting that error. I've installed anaconda, updated conda, installed accelerate using conda, installed cudatoolkit, installed numba using conda. I tried compiling it using both python2 and python3
What do I do?
回答1:
Got it. As pointed out by WarrenWeckesser and Robert Crovella, NumbaPro has been deprecated and all the features were moved to numba. So instead of numbapro you are supposed to write numba
from numba import vectorize
Also the target needs to be set to 'cuda' instead of 'gpu'
@vectorize(["float32(float32, float32)"], target='cuda')
def VectorAdd(a, b):
return a + b
回答2:
I tried to run it in both (CPU and GPU) after modifying, the CPU is faster than GPU
first one in 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()
the time:
vectoradd_time 0.046457 second
the second one in 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()
the time:
VectorAdd took 0.240731 seconds
this result depend on the specifications of your CPU.
来源:https://stackoverflow.com/questions/48326314/no-module-named-numbapro