C: Simple multi-processor optimization


Programming threads explicitly is extremely verbose. But how else can you utilize all processors on your machine? You could use a compiler that is so good it can optimize your code and do everything.

However, with OpenMP it is as easy as adding 3 lines:

Look for a expensive loop in your code

+ #include <omp.h>

...

+#pragma omp parallel for

                for (i = 0; i < n; i++) {

                        for (j = 0; j < n2; j++) {

                                do_something(m, i, j);

                        }

                }

...

add -fopenmp to your compile-flags.

3 lines. By default, the number of processors used is the number available, and the work is split up between the threads automatically, but you can set it at runtime (!) using the environment variable OMP_NUM_THREADS.

OpenMP is of course much more powerful, but this little change might already improve your optimization. It scales very well if you have enough work for each thread.

Last word: The concepts and way of programming when sharing load between computers (clusters, grids) is totally different from multi-processor programming. You will have to choose at some point. OpenMP is not meant for clusters. Have a look at programming with BOINC.

  1. No comments yet.
(will not be published)

  1. No trackbacks yet.