Skip to content

Instantly share code, notes, and snippets.

@zed
Created April 13, 2011 02:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zed/916835 to your computer and use it in GitHub Desktop.
Save zed/916835 to your computer and use it in GitHub Desktop.
how to define an elementwise function in pycuda
#!/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
@Arkapravo
Copy link

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment