Hi. My name is Alex Wiltschko.

This is my internet log.



14 June, 2011

Scripting the GPU with PyOpenCL

I've taken an interest recently in computer vision, and some of the applications I'd like to try my hand at involve some intense computations ( à la what these folks have been up to ). This means, roughly, an excuse to try out GPU-accelerated computing. 

Searching around, getting computations done on the GPU isn't an inherently easy task, but fortunately, there've been efforts made to simplify the process. Namely, the Python language has been used to create a kind of an abstraction, that makes a lot of the setup and tedium almost completely transparent.

I wrote up a little code to test the speed of scaling and adding two arrays of numbers together, and the results are pretty darn cool.

Temp

Let me break down what the curves are here. Green is CPU. Red is OpenCL GPU. Crosses (x) denote runs where the data was just left wherever it was stored after the computation, and circles ( o ) are runs where the data was taken out of its resident location at the end of every computation. This penalizes the GPU, because it has to transfer its data from the card, all the way back to memory, whereas a CPU-hosted computation has a much shorter route to RAM, and indeed likely stores the results of its computation in RAM directly. 

What's all this mean? CPUs are fast for short little computations, but GPUs really open up and hit full stride once you start working with interesting chunks of data. Now, this is a log scale, so at the far end, there, any operation which doesn't require constant data retrieval is about 100X faster on the GPU

Keep in mind that this is a pretty trivial application of GPUs, about as simple as it gets. The purpose of this little exercise was just to get my toes wet with these techniques. However, I don't think it's going to be impossibly difficult to apply GPUs to analysis problems in the lab.

Oh, and before I forget, if you want to run this code for yourself, here you go: 

The code requires Python (of course), NumPy, matplotlib, and PyOpenCL. I recommend using the Enthought Python Distribution (EPD) to get everything except PyOpenCL. You can grab PyOpenCL using easy_install. Once you've got the EPD installed, open up your command line and type "easy_install pyopencl". Easy as 3.141 fivvvee. Annndd lastly, I did this all on a Mac, running 10.6.7. Find me on the twitters (@awiltsch) if you want to talk about it.

Search it. Browse it. Subscribe it. Get caught up in it.


Get the RSS feed! Go ahead.