Pycuda性能基准:ElementwiseKernel vs cumath?

时间:2014-08-21 21:57:10

标签: python performance pycuda

我想检查cumathElementwiseKernel之间的速度差异,因为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

我知道ElementwiseKernelcumath在第一次运行时都很慢,但我不明白为什么ElementwiseKernel在第二次运行时没有更快运行

0 个答案:

没有答案