12 10 2010

Is freaking awesome! So, lets say I have something like this

for(int i=0; i<99999;i++)
        a[i] = b[i] + 9;

all you need to do is

#include <omp.h>
#pragma omp parallel for
for(int i=0; i<99999;i++)
        a[i] = b[i] + 9;

… and thats it. Make sure you have OPEN-MP installed of course. One pragma and the loop is automatically parallelized. In linux, there is an environment variable OMP_NUM_THREADS that controls how many threads the loop runs in. By default though it will use all cores on your machine. REALLY, its just that simple.

Edit: OK, you also need the -fopenmp switch if you are using GCC. But overall, it really IS that simple.




2 responses

13 10 2010
Richard Friedman

You might also have to turn on a compiler switch (command-line option) to make the recognize the pragmas and not treat them as comments.

For example, the Oracle (Sun) Solaris Studio C compiler uses the -xopenmp option.

Typically, these options are not “on” by default.

13 10 2010

Oh yes, definitely. For gcc this was -fopenmp. Thanks… I’ve updated my post

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: