import pyopencl as cl
import pyopencl.array
import pyopencl.clrandom
import numpy as np
import numpy.linalg as la
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
n = 10**7
x = cl.clrandom.rand(queue, n, np.float64)
Want to compute the prefix sum of the squares of all entries in x
.
First, using numpy
, as result1
:
result1 = np.cumsum(x.get()**2)
Then, using PyOpenCL:
from pyopencl.scan import GenericScanKernel
Syntax:
GSK(context, dtype, arguments, input_expr, scan_expr using a
and b
, neutral, output_statement with item
)
sknl = GenericScanKernel(ctx, np.float64,
arguments="double *y, double *x",
input_expr="x[i]*x[i]",
scan_expr="a+b", neutral="0",
output_statement="y[i] = item")
result2 = cl.array.empty_like(x)
sknl(result2, x)
print(la.norm(result2.get() - result1))
More features: