OpenCL in openFrameworks example – 1 milion particles @ 100-200fps (2009)

Recently I’ve been playing a lot with OpenCL, the new API / framework designed to handle cross-platform parallel computing (i.e. a simple way of running code simultaneously on all cores of your CPU, GPU or other processors). Implementations have been cropping up this year in NVidia drivers or ATI drivers, but most famously it’s included with Mac OSX 10.6 Snow Leopard.

To cut a long story short I’ve been working on a simple-to-use C++ wrapper for some of the most common functions, imaginatively called ofxOpenCL and here is a little demo of 1 million particles running at 100-200fps.

NOTE: The Vimeo compression destroys most of the particles, so I suggest downloading the quicktime directly from the vimeo page

This is 1,000,000 particles being interacted on by mouse, updated on GPU (with springy behaviours ) via an OpenCL kernel, data written straight to a VBO and rendered – without ever coming back to host (i.e. main memory + cpu etc.)

Frame-rate is around 100-200fps running on a macbook pro with GF 9600GT. That’s 100-200fps on a laptop! (albeit a pretty decent one), but I’m dying to try this on a GF 285 GTX – which has 7.5x the number of cores, 2.5x the fillrate and 3.5x the memory bandwidth – for only £250!!

The kernel for this is surprisingly simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
__kernel void updateParticleWithoutCollision(__global Particle* pIn, __global float2* pOut, const float2 mousePos, const float2 dimensions){
int id = get_global_id(0);
__global Particle *p = &pIn[id];

float2 diff = mousePos - pOut[id];
float invDistSQ = 1.0f / dot(diff, diff);
diff *= 300.0f * invDistSQ;

p->vel += (dimensions*0.5 - pOut[id]) * CENTER_FORCE2 - diff* p->mass;
pOut[id] += p->vel;
p->vel *= DAMP2;

float speed2 = dot(p->vel, p->vel);
if(speed2<MIN_SPEED2) pOut[id] = mousePos + diff * (1 + p->mass);
}

This example is based on Rui’s opencl example at http://vimeo.com/7298380.

Discussion on the matter at http://www.openframeworks.cc/forum/viewtopic.php?f=10&t=2728&p=15107#p15…

source code for ofxOpenCL and the above example at
http://code.google.com/p/ofxmsaof/downloads/list
(the SVN is likely to be more recent).

Recently I’ve been playing a lot with OpenCL, the new API / framework designed to handle cross-platform parallel computing (i.e. a […]

Related keywords

open source, opencl, openframeworks