import pyopencl as cl
import pyopencl.array
import pyopencl.clrandom
import numpy as np
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
n = 10**7
a = cl.clrandom.rand(queue, n, np.float32)
b = cl.clrandom.rand(queue, n, np.float32)
We would like to evaluate this linear combination:
c1 = 5*a + 6*b
A problem with this is that every single operator (all three of them--and easily more for complicated expressions) corresponds to a kernel call, which can lead to high overhead. Let's try and avoid that by stuffing the entire operation into one kernel, in turn saving lots of memory traffic:
from pyopencl.elementwise import ElementwiseKernel
lin_comb = ElementwiseKernel(ctx,
"float a, float *x, float b, float *y, float *c",
"c[i] = a*x[i] + b*y[i]")
c2 = cl.array.empty_like(a)
lin_comb(5, a, 6, b, c2)
import numpy.linalg as la
print(la.norm(c1.get() - c2.get()))
Did this optimization pay off?
from time import time
queue.finish()
start_time = time()
for i in range(10):
c1 = 5*a + 6*b
queue.finish()
print("elapsed: {0} s".format(time()-start_time))
from time import time
queue.finish()
start_time = time()
for i in range(10):
lin_comb(5, a, 6, b, c2)
queue.finish()
print("elapsed: {0} s".format(time()-start_time))