I’m pleased to announce the release of PyOpenCL 0.90, a complete, functional Python wrapper around OpenCL.
Its home page is at http://mathema.tician.de/software/pyopencl.
PyOpenCL has complete documentation and a Wiki.
Download PyOpenCL here from its page in the Python package index.
Since there are a few competing efforts, what sets PyOpenCL apart?
[*] Object cleanup tied to lifetime of objects. This idiom, often called RAII in C++, makes it much easier to write correct, leak- and crash-free code.
[*] Completeness. PyOpenCL puts the full power of OpenCL’s API at your disposal, if you wish. Every obscure get_info() query and all CL calls are accessible.
[*] Automatic Error Checking. All errors are automatically translated into Python exceptions.
[*] Speed. PyOpenCL’s base layer is written in C++, so all the niceties above are virtually free.
[*] Helpful, complete Documentation
[*] Liberal license. PyOpenCL is open-source and free for commercial, academic, and private use under the MIT/X11 license.
If that sounds similar to PyOpenCL’s sister project PyCUDA, that is by no means a coincidence.
Here’s some sample code to give you an impression:
import pyopencl as cl
import numpy
import numpy.linalg as la
a = numpy.random.rand(50000).astype(numpy.float32)
b = numpy.random.rand(50000).astype(numpy.float32)
ctx = cl.create_context_from_type(cl.device_type.ALL)
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_buf = cl.create_host_buffer(
ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, a)
b_buf = cl.create_host_buffer(
ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, b)
dest_buf = cl.create_buffer(ctx, mf.WRITE_ONLY, b.nbytes)
prg = cl.create_program_with_source(ctx, """
__kernel void sum(__global const float *a,
__global const float *b, __global float *c)
{
int gid = get_global_id(0);
c[gid] = a[gid] + b[gid];
}
""").build()
prg.sum(queue, a.shape, a_buf, b_buf, dest_buf)
a_plus_b = numpy.empty_like(a)
cl.enqueue_read_buffer(queue, dest_buf, a_plus_b).wait()
print la.norm(a_plus_b - (a+b))