Basic Arithmetic Operation in PyCuda

A basic functioning operation is carried out

If you like, you may put your contact info here.

License of this example:

Sugeerth Murugesan,UC Davis (sugeerth@gmail.com)

Date:

September 2nd,2013

PyCUDA version:

   1 #!python
   2 import pycuda.driver as cuda
   3 import pycuda.autoinit
   4 from pycuda.compiler import SourceModule
   5 import pycuda.gpuarray as gpuarray
   6 import numpy as np
   7 
   8 # Converting the list into numpy array for faster access and putting it into the GPU for processing... 
   9 start = cuda.Event()
  10 end = cuda.Event()
  11 
  12 N = 222341
  13 
  14 values = np.random.randn(N)
  15 number_of_blocks=N/1024
  16 
  17 # Calculating the (value-max)/max-min computation and storing it in a numpy array. Pre-calculating the maximum and minimum values.
  18 
  19 # Space for the Kernel computation..
  20 
  21 func_mod = SourceModule("""
  22 // Needed to avoid name mangling so that PyCUDA can
  23 // find the kernel function:
  24 extern "C" {
  25 __global__ void func(float *a, int N, float minval, int denom)
  26 {
  27 int idx = threadIdx.x+threadIdx.y*32+blockIdx.x*blockDim.x;
  28 if (idx < N)
  29     a[idx] = (a[idx]-minval)/denom;
  30 }
  31 }
  32 """, no_extern_c=1)
  33 
  34 func = func_mod.get_function('func')
  35 x = np.asarray(values, np.float32)
  36 x_gpu = gpuarray.to_gpu(x)
  37 h_minval = np.int32(0)
  38 h_denom = np.int32(255)
  39 
  40 start.record()
  41 # a function to the GPU to calculate the computation in the GPU.
  42 func(x_gpu.gpudata, np.uint32(N), np.uint32(h_minval), np.uint32(h_denom), block=(1024, 1, 1), grid=(number_of_blocks+1,1,1))
  43 end.record() 
  44 end.synchronize()
  45 secs = start.time_till(end)*1e-3
  46 
  47 print "SourceModule time"
  48 print "%fs" % (secs)
  49 print 'x:       ', x[N-1]
  50 print 'Func(x): ', x_gpu.get()[N-1],'Actual: ',(values[N-1]-0)/(h_denom)
  51 x_colors=x_gpu.get()

PyCuda/Examples/ArithmeticExample (last edited 2014-10-22 00:48:04 by ::ffff:128)