Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 14th, 2010 | Syntax: Python | Size: 0.63 KB | Hits: 60 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/env python
  2.  
  3. import pycuda.autoinit
  4. import pycuda.driver as cuda
  5. import numpy
  6.  
  7. a = numpy.array([ [1., 2., 1., 1.],
  8.                   [1., 1., 1., 1.],
  9.                   [1., 1., 1., 1.],
  10.                   [1., 1., 6., 1.] ]).astype(numpy.float32)
  11.  
  12. a_gpu = cuda.mem_alloc(a.nbytes)
  13. cuda.memcpy_htod(a_gpu, a)
  14.  
  15. mod = cuda.SourceModule("""
  16.  __global__ void doublify(float *a)
  17.  {
  18.    int idx = threadIdx.x + threadIdx.y * 4;
  19.    a[idx] *= 2;
  20.  }
  21.  """)
  22.  
  23. func = mod.get_function("doublify")
  24. func(a_gpu, block=(4,4,1))
  25.  
  26. a_doubled = numpy.empty_like(a)
  27. cuda.memcpy_dtoh(a_doubled, a_gpu)
  28.  
  29. print "Input:"
  30. print a
  31.  
  32. print "Output:"
  33. print a_doubled