View Full Version : PyOpenCL: OpenCL Python Bindings

08-25-2009, 09:54 AM
I'm pleased to announce the release of PyOpenCL 0.90 (http://mathema.tician.de/software/pyopencl), a complete, functional Python (http://python.org) wrapper around OpenCL.

Its home page is at http://mathema.tician.de/software/pyopencl.

PyOpenCL has complete documentation (http://documen.tician.de) and a Wiki (http://wiki.tiker.net/PyOpenCL) available.

Download PyOpenCL here (http://pypi.python.org/pyopencl) from its page in the Python package index.

Since there are a few competing (http://python-opencl.next-touch.com/) efforts (http://sourceforge.net/projects/pyopencl/), 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.[/*:m:20t6iq6e]
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.[/*:m:20t6iq6e]
Automatic Error Checking. All errors are automatically translated into Python exceptions.[/*:m:20t6iq6e]
Speed. PyOpenCL’s base layer is written in C++, so all the niceties above are virtually free.[/*:m:20t6iq6e]
Helpful, complete Documentation (http://documen.tician.de/pyopencl)[/*:m:20t6iq6e]
Liberal license. PyOpenCL is open-source and free for commercial, academic, and private use under the MIT/X11 license.[/*:m:20t6iq6e]

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];

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))

08-26-2009, 10:27 AM
Whoops--the above post has the wrong download link.

Correct download link (http://pypi.python.org/pypi/pyopencl)

11-03-2011, 06:46 AM
Thanks for the detailed reply!! That helped me to fix my problem. You are my life saver!!!