ABSTRACT
In this chapter, we will introduce OpenMP and highlight its strengths and weaknesses. The OpenMP statements commonly used to add parallelism to typical scientific
CONTENTS
13.1 What Is OpenMP? ............................................................................................................ 173 13.2 OpenMP and Multithreading .........................................................................................174 13.3 Compiling Applications with OpenMP (GCC, Intel, PGI), Fortran, and C ............. 175 13.4 OpenMP Basic Environment Variables ........................................................................ 176 13.5 OpenMP Basics ................................................................................................................ 177
13.5.1 Syntax, Directives, and Clauses (Examples in C and Fortran) ...................... 177 13.6 Parallel Regions and Worksharing ................................................................................ 179
13.6.1 OpenMP Do/For .................................................................................................. 179 13.6.2 Scheduling Constructs ........................................................................................ 182 13.6.3 Sections and Tasks ............................................................................................... 183 13.6.4 Single and Master ................................................................................................ 183
13.7 Data Scoping ..................................................................................................................... 184 13.7.1 Synchronization and Mutual Exclusion ........................................................... 186
13.8 More Language Constructs ............................................................................................ 188 13.8.1 Runtime Library Application Programming Interface .................................. 188 13.8.2 Environment Variables ........................................................................................ 189
13.9 Practical Issues ................................................................................................................. 190 13.9.1 Race Conditions ................................................................................................... 190 13.9.2 Reducing Errors ................................................................................................... 191
13.10 Concluding Remarks ....................................................................................................... 192 References ..................................................................................................................................... 192 Further Reading .......................................................................................................................... 193
applications will also be introduced. By the end of this chapter, the reader is expected to be able to parallelize C/C++ or Fortran code and solve typical parallelization errors such as race conditions.