6.4 OpenMP
OpenMP is a concurrency platform for multithreaded, shared-memory parallel processing architecture for C, C++, and Fortran. By using OpenMP, the programmer is able to incrementally parallelize the program with little programming effort. The programmer manually inserts compiler directives to assist the compiler into generating threads for the parallel processor platform. The user does not need to create the threads nor worry about the tasks assigned to each thread. In that sense, OpenMP is a higher-level programming model compared with pthreads in the POSIX library.
At the current state of the art, there is something to be gained using manual parallelization. Automatic parallelizing compilers cannot compete with a hand-coded parallel program. OpenMP uses three types of constructs to control the parallelization of a program [69]:
1. Compiler directives
2. Runtime library routines
3. Environment variables
To compile an OpenMP program, one would issue the command
gcc -openmp file.c -o file.
Listing 6.4 The following pseudocode is a sketch of how OpenMP parallelizes a serial code [69]:
1: #include <omp.h>
2: main () {
3: int var1, var2, var3;
4: Serial code executed by master thread
5:
6: #pragma omp parallel private(var1, var2) shared(var3)
7: {
8: Parallel section executed by all threads
9:
10: }
11: Resume serial code
12: }
Line 1 is an include file that defines ...
Get Algorithms and Parallel Computing now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.