Use of multicore machines

These days, many people have multi-CPU computers. The Intel Core 2 Duo is everywhere, and one also sees quadcore or better.

However, when I see people with long-running programs they invariably use only a single CPU. A waste. These programs could finish in half the time with just a few additional lines of code.

A code fragment

The OpenMP (Open Multi-Processing) API defines a very convenient way to instruct the compiler to generate code for parallel execution.

#include <omp.h>

#pragma omp parallel for
                for (i=0; i<ub; i++) {

Add to your C program an include line and a pragma line, and give the gcc compiler the -fopenmp flag, and lo! your program uses more than one CPU. If you want, you can control the number of CPUs used (even dynamically, say by reading a small file once a second).


There is a lot of overhead in the above construction, so one should use it only if an iteration of the for loop takes appreciable time.

Be careful! One has to use locking, or arrange the algorithm in such a way that the various CPUs do not modify the same data, or even data in the same cache line.

Be careful! Your C library may be unable to handle simultaneous calls.

This is a big topic, and there are many details. Read some introduction.