Parallel Programming 2
Parallel Programming 2
Parallel Programming 2
Standard for shared memory programming for
scientific applications.
Has specific support for scientific application needs.
Acceptance among vendors and application writers
(including for GPU).
See http://www.openmp.org for more info.
OpenMP API Overview
API is a set of compiler directives inserted in the
source program (in addition to some library
functions).
Ideally, compiler directives do not affect
sequential code.
– pragma’s in C / C++ .
(special) comments in Fortran code.
OpenMP API Example
Sequential code:
statement1;
statement2;
statement3;
Assume we want to execute statement 2 in
parallel, and statement 1 and 3 sequentially.
OpenMP API Example (2 of 2)
By giving a parallel directive, the user asserts that
the program will remain correct if the statement is
executed in parallel.
• OpenMP compiler does not check correctness.
Some tools exist for helping with that.
Totalview - good parallel debugger
API Semantics
Sequential
Program
Parallel
Program
OpenMP Example Usage (2 of 2)
If you give sequential switch,
comments and pragmas are ignored.
If you give parallel switch,
comments and/or pragmas are read, and
cause translation into parallel program.
Ideally, one source for both sequential and
parallel program (big maintenance plus).
OpenMP Directives
• Parallelization directives:
parallel section
parallel for
• Data environment directives:
shared, private, threadprivate, reduction, etc.
• Synchronization directives:
barrier, critical
General Rules about Directives
They always apply to the next statement,
which must be a structured block.
Examples
– #pragma omp …
statement
– #pragma omp …
{ statement1; statement2; statement3; }
OpenMP Parallel Region
A number of threads are spawned at entry.
Each thread executes the same code.
Each thread waits at the end.
Very similar to a number of create/join’s
with the same function in Pthreads.
Getting Threads to do Different Things
Through explicit thread identification (as in Pthreads).
Through work-sharing directives.
Thread Identification
int omp_get_thread_num()
int omp_get_num_threads()
Gets the thread id.
Gets the total number of threads.
Example
Always occur within a parallel region directive.
Two principal ones are
– parallel for
– parallel section
OpenMP Parallel For