1 import pycuda.gpuarray as gpuarray
   2 import pycuda.autoinit
   3 import numpy
   4 from pycuda.curandom import rand as curand
   5 
   6 a_gpu = curand((50,))
   7 b_gpu = curand((50,))
   8 
   9 from pycuda.elementwise import ElementwiseKernel
  10 lin_comb = ElementwiseKernel(
  11         "float a, float *x, float b, float *y, float *z",
  12         "z[i] = a*x[i] + b*y[i]",
  13         "linear_combination")
  14 
  15 c_gpu = gpuarray.empty_like(a_gpu)
  16 lin_comb(5, a_gpu, 6, b_gpu, c_gpu)
  17 
  18 import numpy.linalg as la
  19 assert la.norm((c_gpu - (5*a_gpu+6*b_gpu)).get()) < 1e-5

PyCuda/Examples/DemoElementwise (last edited 2010-01-31 16:30:34 by ip72-221-120-106)