Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import pycuda.autoinit
  2. import pycuda.driver as drv
  3. from pycuda import gpuarray
  4. from pycuda import cumath
  5. from pycuda.elementwise import ElementwiseKernel
  6. import numpy as np
  7.  
  8. start = drv.Event()
  9. end = drv.Event()
  10.  
  11. N = 10**6
  12.  
  13. a = 2*np.ones(N,dtype=np.float64)
  14.  
  15. start.record()
  16. np.exp(a)
  17. end.record()
  18. end.synchronize()
  19. secs = start.time_till(end)*1e-3
  20. print "Numpy",secs
  21.  
  22. a_gpu = gpuarray.to_gpu(a)
  23. b_gpu = gpuarray.zeros_like(a_gpu)
  24.  
  25. kernel = ElementwiseKernel(
  26. "double *a,double *b",
  27. "b[i] = exp(a[i]);",
  28. "kernel")
  29.  
  30. start.record() # start timing
  31. kernel(a_gpu,b_gpu)
  32. end.record() # end timing
  33. end.synchronize()
  34. secs = start.time_till(end)*1e-3
  35. print "Kernel",secs
  36.  
  37. start.record()
  38. cumath.exp(a_gpu)
  39. end.record()
  40. end.synchronize()
  41. secs = start.time_till(end)*1e-3
  42. print "Cumath", secs
  43.  
  44. Numpy 0.0218774719238
  45. Kernel 0.143336898804
  46. Cumath 0.14694732666
  47.  
  48. Numpy 0.0205841598511
  49. Kernel 0.138252410889
  50. Cumath 0.00203606390953
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement