3
import numpy.linalg as la
5
a = numpy.random.rand(50000).astype(numpy.float32)
6
b = numpy.random.rand(50000).astype(numpy.float32)
8
ctx = cl.create_some_context()
9
queue = cl.CommandQueue(ctx)
12
a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a)
13
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
14
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes)
16
prg = cl.Program(ctx, """
17
__kernel void sum(__global const float *a,
18
__global const float *b, __global float *c)
20
int gid = get_global_id(0);
21
c[gid] = a[gid] + b[gid];
25
prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf)
27
a_plus_b = numpy.empty_like(a)
28
cl.enqueue_read_buffer(queue, dest_buf, a_plus_b).wait()
30
print la.norm(a_plus_b - (a+b)), la.norm(a_plus_b)