When to use OpenMP directives?

Reading time: 15 minutes

If some section of your code can be parallelized and you have more than one processor, you should definitely speed up the execution of your program using OpenMP directives.

In short, before using OpenMP, you need to take a look at the following:

  • profile your code
  • determine the sections where the most time is spent and see if it can be parallelized
  • use Amdahl's Law to determine potential speed up
  • put directives in the code and measure run times on serial and parallel runs

Amdahl's law

Amdahl's law states that speedup is a function of the fraction of the code that can be parallelized and the number of processors and non-parallel sections do not gain performance

Speedup(N) = 1/((1-p)+p/N)

where p = portion of the code that can be made parallel
N = number of processors

Example:

p N Speedup
.5 2 1.3
.5 4 1.6
.5 infinite 2
.9 2 1.81
.9 4 3.07
.9 infinite 10

Profile your code

By profiling, you can find out where in the code the most time is spent and then, you can focus on that section to make the code execute faster. The steps to profile your code are:

  • Compile with "-g -gp" (no optimization) options
  • Run the program in a serial mode and produce file gmon.out
  • Use gprof to display the profile data and the lines where the program spends the most time.

Once you have found out where the most time is spent, you can use OpenMP directories and observe your code run faster