1 import pycuda.driver as cuda
   2 import pycuda.gpuarray as gpuarray
   3 import pycuda.autoinit
   4 import numpy
   5 from pycuda.curandom import rand as curand
   6 
   7 a = (numpy.random.randn(400)
   8         +1j*numpy.random.randn(400)).astype(numpy.complex64)
   9 b = (numpy.random.randn(400)
  10         +1j*numpy.random.randn(400)).astype(numpy.complex64)
  11 
  12 a_gpu = gpuarray.to_gpu(a)
  13 b_gpu = gpuarray.to_gpu(b)
  14 
  15 from pycuda.elementwise import ElementwiseKernel
  16 complex_mul = ElementwiseKernel(
  17         "pycuda::complex<float> *x, pycuda::complex<float> *y, pycuda::complex<float> *z",
  18         "z[i] = x[i] * y[i]",
  19         "complex_mul",
  20         preamble="#include <pycuda-complex.hpp>",)
  21 
  22 c_gpu = gpuarray.empty_like(a_gpu)
  23 complex_mul(a_gpu, b_gpu, c_gpu)
  24 
  25 import numpy.linalg as la
  26 error = la.norm(c_gpu.get() - (a*b))
  27 print error
  28 assert error < 1e-5

PyCuda/Examples/DemoComplex (last edited 2012-01-13 22:34:15 by AndreasKloeckner)