Created
April 13, 2011 02:04
-
-
Save zed/916835 to your computer and use it in GitHub Desktop.
how to define an elementwise function in pycuda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?