PyOpenCL

>> Wednesday, February 16, 2011

I've started using PyOpenCL, which lets you code OpenCL host applications in Python. It's unbelievable how simple it is to work with. For example, let's say you want to create a kernel from a function called foo, set three arguments (a, b, and c), and execute the kernel with 20x16 work-items divided into work-groups of 5x4 work-items each. In C, the code would look like this:

foo_kernel = clCreateKernel(program, "foo", NULL);
clSetKernelArg(foo_kernel, 0, sizeof(a), &a);
clSetKernelArg(foo_kernel, 1, sizeof(b), &b);
clSetKernelArg(foo_kernel, 2, sizeof(c), &c);
size_t global_size[2] = {20, 16};
size_t local_size[2] = {5, 4};
clEnqueueNDRangeKernel(queue, foo_kernel, 2, NULL, global_size, local_size, 0, NULL, NULL);
Here's how it looks with PyOpenCL:
program.foo(queue, (20, 16), (5, 4), a, b, c)
One line of Python code accomplishes the same result as seven lines of C code, and it's so much more intuitive. It almost feels like cheating. If my intended application didn't require C++, I would definitely code with PyOpenCL.

0 comments:

Post a Comment

  © Blogger template Werd by Ourblogtemplates.com 2009

Back to TOP