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
x = cl.clrandom.rand(queue, n, np.float64)
Want to compute the sum of the squares of all entries in x
.
First, using numpy
, as result1
(watch out: .get()
)
result1 = np.sum(x.get()**2)
Then, using PyOpenCL:
from pyopencl.reduction import ReductionKernel
Syntax:
ReductionKernel(context, dtype, netural, reduce_expr, map_expr, arguments)
rknl = ReductionKernel(ctx, np.float64,
neutral="0",
reduce_expr="a+b", map_expr="x[i]*x[i]",
arguments="double *x")
result2 = rknl(x)
type(result2)
result2.shape
Now check the result:
print(result2.get()-result1)