OpenMP in a nutshell
OpenMP is a library for parallel programming in the SMP (symmetric multi-processors, or shared-memory processors) model. When programming with OpenMP, all threads share memory and data. OpenMP supports C, C++ and Fortran. The OpenMP functions are included in a header file called omp.h .
OpenMP program structure: An OpenMP program has sections that are sequential and sections that are parallel. In general an OpenMP program starts with a sequential section in which it sets up the environment, initializes the variables, and so on.
When run, an OpenMP program will use one thread (in the sequential sections), and several threads (in the parallel sections).
There is one thread that runs from the beginning to the end, and it's called the master thread. The parallel sections of the program will cause additional threads to fork. These are called the slave threads.
A section of code that is to be executed in parallel is marked by a special directive (omp pragma). When the execution reaches a parallel section (marked by omp pragma), this directive will cause slave threads to form. Each thread executes the parallel section of the code independently. When a thread finishes, it joins the master. When all threads finish, the master continues with code following the parallel section.
Each thread has an ID attached to it that can be obtained using a runtime library function (called omp_get_thread_num()). The ID of the master thread is 0.
Why OpenMP? More efficient, and lower-level parallel code is possible, however OpenMP hides the low-level details and allows the programmer to describe the parallel code with high-level constructs, which is as simple as it can get.
OpenMP has directives that allow the programmer to:
specify the parallel region
specify whether the variables in the parallel section are private or shared
specify how/if the threads are synchronized
specify how to parallelize loops
specify how the works is divided between threads (scheduling)
Compiling and running OpenMP code
The OpenMP functions are included in a header file called omp.h . The public linux machines dover and foxcroft have gcc/g++ installed with OpenMP support. All you need to do is use the -fopenmp flag on the command line:
gcc -fopenmp hellosmp.c -o hellosmp
It’s also pretty easy to get OpenMP to work on a Mac. A quick search with google reveals that the native apple compiler clang is installed without openmp support. When you installed gcc it probably got installed without openmp support. To test, go to the terminal and try to compile something:
gcc -fopenmp hellosmp.c -o hellosmp
If you get an error message saying that “omp.h†is unknown, that mans your compiler does not have openmp support.
hellosmp.c:12:10: fatal error: 'omp.h' file not found
#include
^
1 error generated.
make: *** [hellosmp.o] Error 1
references:
https://tildesites.bowdoin.edu/~ltoma/teaching/cs3225-GIS/fall17/Lectures/openmp.html#:~:text=OpenMP%20is%20a%20library%20for,a%20header%20file%20called%20omp.
No comments:
Post a Comment