Created
June 16, 2012 00:35
-
-
Save likr/2939374 to your computer and use it in GitHub Desktop.
pyopencl benchmark
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
import pyopencl as cl | |
from pyopencl import array as clarray | |
from pyopencl import clmath | |
from pyopencl import clrandom | |
import numpy | |
from time import time | |
def timeit(f, args): | |
n = 100 | |
t = 0 | |
for _ in range(n): | |
start = time() | |
f(*args) | |
stop = time() | |
t += stop - start | |
print t / n | |
def runnumpy(n): | |
a = numpy.random.rand(n).astype(numpy.float32) | |
b = numpy.random.rand(n).astype(numpy.float32) | |
timeit(lambda a, b: a + b, (a, b)) | |
timeit(lambda a: a + 1.0, (a,)) | |
timeit(lambda a, b: a * b, (a, b)) | |
timeit(lambda a: a * 2.0, (a,)) | |
timeit(lambda a: numpy.sin(a), (a,)) | |
timeit(lambda a: numpy.sum(a), (a,)) | |
timeit(lambda a, b: numpy.dot(a, b), (a, b)) | |
timeit(lambda : numpy.random.rand(n), ()) | |
def runcl(device, n): | |
ctx = cl.Context([device]) | |
queue = cl.CommandQueue(ctx) | |
a = clrandom.rand(queue, n, numpy.float32) | |
b = clrandom.rand(queue, n, numpy.float32) | |
cl.enqueue_barrier(queue) | |
def add(a, b): | |
a + b | |
cl.enqueue_barrier(queue) | |
timeit(add, (a, b)) | |
def add_s(a): | |
a + 1.0 | |
cl.enqueue_barrier(queue) | |
timeit(add_s, (a,)) | |
def mul(a, b): | |
a * b | |
cl.enqueue_barrier(queue) | |
timeit(mul, (a, b)) | |
def mul_s(a): | |
a * 2.0 | |
cl.enqueue_barrier(queue) | |
timeit(mul_s, (a,)) | |
def sin(a): | |
clmath.sin(a) | |
cl.enqueue_barrier(queue) | |
timeit(sin, (a,)) | |
def sum(a): | |
clarray.sum(a) | |
cl.enqueue_barrier(queue) | |
timeit(sum, (a,)) | |
def dot(a, b): | |
clarray.dot(a, b) | |
cl.enqueue_barrier(queue) | |
timeit(dot, (a,b)) | |
def rand(): | |
clrandom.rand(queue, n, numpy.float32) | |
cl.enqueue_barrier(queue) | |
timeit(rand, ()) | |
ns = [1000, 10000, 100000, 1000000] | |
for platform in cl.get_platforms(): | |
for device in platform.get_devices(): | |
print device.name | |
for n in ns: | |
print n | |
runcl(device, n) | |
print 'numpy' | |
for n in ns: | |
print n | |
runnumpy(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment