我想检查cumath
和ElementwiseKernel
之间的速度差异,因为this example表明Elementwise
的执行速度可能比cumath
快。我正在测试一个不同的操作,我猜测Elementwise
将是更快的方法。
import pycuda.autoinit
import pycuda.driver as drv
from pycuda import gpuarray
from pycuda import cumath
from pycuda.elementwise import ElementwiseKernel
import numpy as np
start = drv.Event()
end = drv.Event()
N = 10**6
a = 2*np.ones(N,dtype=np.float64)
start.record()
np.exp(a)
end.record()
end.synchronize()
secs = start.time_till(end)*1e-3
print "Numpy",secs
a_gpu = gpuarray.to_gpu(a)
b_gpu = gpuarray.zeros_like(a_gpu)
kernel = ElementwiseKernel(
"double *a,double *b",
"b[i] = exp(a[i]);",
"kernel")
start.record() # start timing
kernel(a_gpu,b_gpu)
end.record() # end timing
end.synchronize()
secs = start.time_till(end)*1e-3
print "Kernel",secs
start.record()
cumath.exp(a_gpu)
end.record()
end.synchronize()
secs = start.time_till(end)*1e-3
print "Cumath", secs
我第一次运行它时得到:
Numpy 0.022
Kernel 0.143
Cumath 0.147
第二次运行在同一个Python解释器中:
Numpy 0.021
Kernel 0.138
Cumath 0.002
我知道ElementwiseKernel
和cumath
在第一次运行时都很慢,但我不明白为什么ElementwiseKernel
在第二次运行时没有更快运行