Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 0.64 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. import pycuda.gpuarray as gpuarray
  3. import pycuda.autoinit
  4.  
  5. from pycuda.elementwise import ElementwiseKernel
  6.  
  7. # define elementwise `add()` function
  8. add = ElementwiseKernel(
  9.         "float *a, float *b, float *c",
  10.         "c[i] = a[i] + b[i]",
  11.         "add")
  12.  
  13. # create a couple of random matrices with a given shape
  14. from pycuda.curandom import rand as curand
  15. shape = 128, 1024
  16. a_gpu = curand(shape)
  17. b_gpu = curand(shape)
  18.  
  19. # compute sum on a gpu
  20. c_gpu = gpuarray.empty_like(a_gpu)
  21. add(a_gpu, b_gpu, c_gpu)
  22.  
  23. # check the result
  24. import numpy.linalg as la
  25. print (c_gpu - (a_gpu + b_gpu))
  26. assert la.norm((c_gpu - (a_gpu + b_gpu)).get()) < 1e-5