This is an old revision of the document!

Userspace program:


import pyopencl as cl
import numpy as np

# create context
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)

# create some input data, in this case array filled with zeros
a = np.array(np.zeros((20),dtype=np.uint64))

# create opencl buffer
buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=a) 

# load kernel source
SRC = ''.join(f.readlines())

# compile it
prg = cl.Program(ctx, SRC).build()


# launch the kernel
event = prg.brm(queue, a.shape, None, buf)
# copy data back from opencl
cl.enqueue_copy(queue, a, buf)

# print it
print("CL returned:")


// kernel definition
kernel void brm(global ulong *buf) {

    // kernel index
    private size_t me = get_global_id(0);

    // local vars
    private ulong a;

    // read input data
    a = buf[me];

    // excersise some magic
    a = a + me*me;

    // write result back to memory
    buf[me] = a;
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki