I wanted to check the difference in speed between cumath
and ElementwiseKernel
, since this example shows that Elementwise
can perform faster than cumath
. I am testing a different operation, where I would guess that Elementwise
would be the faster method.
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
The first time I run it I get:
Numpy 0.022
Kernel 0.143
Cumath 0.147
The second run in the same Python interpreter:
Numpy 0.021
Kernel 0.138
Cumath 0.002
I understand that ElementwiseKernel
and cumath
are both slow on the first run, but I don't understand why ElementwiseKernel
doesn't get any faster on the second run.
Numpy 0.017, Kernel 0.091, Cumath 0.093
. BumpingN=10**8
this becomesNumpy 1.50, Kernel 0.148, Cumath 0.163
. I don't think this is an issue any more.