
Untitled
By: a guest on
Apr 15th, 2012 | syntax:
None | size: 0.64 KB | hits: 10 | expires: Never
#!/usr/bin/env python
import pycuda.gpuarray as gpuarray
import pycuda.autoinit
from pycuda.elementwise import ElementwiseKernel
# define elementwise `add()` function
add = ElementwiseKernel(
"float *a, float *b, float *c",
"c[i] = a[i] + b[i]",
"add")
# create a couple of random matrices with a given shape
from pycuda.curandom import rand as curand
shape = 128, 1024
a_gpu = curand(shape)
b_gpu = curand(shape)
# compute sum on a gpu
c_gpu = gpuarray.empty_like(a_gpu)
add(a_gpu, b_gpu, c_gpu)
# check the result
import numpy.linalg as la
print (c_gpu - (a_gpu + b_gpu))
assert la.norm((c_gpu - (a_gpu + b_gpu)).get()) < 1e-5