Magnetic pendulum, the OpenCL way. 2009-11-23
For the last couple of days I did some effort on porting my Python implementation of a magnetic pendulum (German post here, the slides include lots of pictures) to OpenCL. It’s really cool to leverage GPU power for your own benefit.
This is an implementation of a magnetic pendulum in OpenCL. A spherical pendulum is influenced by magnets. For every point of origin the program finds the magnet the pendulum’s body will be nearest when it comes to rest (friction is taken into account). It will create an image, where every pixel (points in the --plane) is mapped to a color representing each magnet.
The problem is ideally suited for parallel computing as every initial condition can be solved independently from the others. I did a Python version some time ago that utilized Scipy. Although it was quite optimized with C code, it performed very bad. To create a map of 1600×1600 pixels, it took about 2700 minutes! Leveraging the power hidden in GPUs, I wanted to know what speed-up would be reached.
The code works quite good so far. I began coding a CPU version in C that is a lot faster than the original Python version, but even my decent GPU outperforms that by a factor of ten. The map from above can now be done in 284 seconds. That is seconds, not minutes!