public
Last active

how to define an elementwise function in pycuda

  • Download Gist
demo.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/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

Can you provide some further details ?? I am on Ubuntu 12.04 with CUDA 5.5 on a GEFORCE GT 630M card, any particular version of pycuda will suit me ?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.