Docsity

OpenMP Parallel Computing, Lecture notes of Parallel Computing and Programming

Partial preview of the text

Download OpenMP Parallel Computing and more Lecture notes Parallel Computing and Programming in PDF only on Docsity!

Module 5 – OpenMP

Parallel Computing

2021 Parallel Computing | UNITEN^1

Objectives

  • To explain the concept of OpenMP in parallel computing
  • To apply the OpenMP API in the program
  • To explain the usage of OpenMP directives 2021 Parallel Computing | UNITEN^2

OpenMP Is Not

  • An automatic parallel programming model
    • parallelism is explicit
    • programmer has full control (and responsibility) over parallelization
  • Meant for distributed-memory parallel systems (by itself)
    • designed for shared address spaced machines
  • Necessarily implemented identically by all vendors
  • Guaranteed to make the most efficient use of shared memory
    • no data locality control

OpenMP Targets Ease of Use

  • OpenMP does not require that single-threaded code be changed for threading - enables incremental parallelization of a serial program
  • OpenMP only adds compiler directives
    • pragmas (C/C++); significant comments in Fortran
    • if a compiler does not recognize a directive, it ignores it
    • simple & limited set of directives for shared memory programs
    • significant parallelism possible using just 3 or 4 directives
    • both coarse-grain and fine-grain parallelism
  • If OpenMP is disabled when compiling a program, the program will execute sequentially

OpenMP Directive Format

  • OpenMP directive forms
    • C and C++ use compiler directives
    • prefix: #pragma …
  • A directive consists of a directive name followed by clauses
  • C: #pragma omp parallel num_threads(4)…

A Simple Example Using parallel

C/C++

#pragma omp parallel [clause ...] newline if (scalar_expression) private (list) shared (list) default (shared | none) firstprivate (list) reduction (operator: list) copyin (list) num_threads (integer-expression) } structured_block } *newline - Required. Precedes the structured block which is enclosed by this directive. ** Long directive lines can be "continued" on succeeding lines by escaping the newline character with a backslash ("") at the end of a directive line.

OpenMP parallel Region Directive #pragma omp parallel [clause list]

  • Typical clauses in [clause list]
  • Conditional parallelization
    • if (scalar expression)
      • – determines whether the parallel construct creates threads
  • Degree of concurrency
    • num_threads (integer expression): max # threads to create
  • Data Scoping
    • private (variable list)
      • specifies variables local to each thread
    • firstprivate (variable list)
      • similar to the private
      • private variables are initialized to variable value before the parallel directive
    • shared (variable list)
      • specifies that variables are shared across all the threads
    • default (data scoping specifier)
      • default data scoping specifier may be shared or none
    • copyin(variable-list)
    • reduction(operator: variable-list)
  • https://computing.llnl.gov/tutorials/openMP/

Possible Output

  • Hello World from thread =
  • Hello World from thread =
  • Hello World from thread =
  • Number of threads =
  • Hello World from thread =
  • Hello World from thread =
  • Hello World from thread =
  • Hello World from thread =
  • Hello World from thread =
    • OMP_NUM_THREADS Assume :

Number of Threads

  • To determine the number of threads that are requested, the following rules will be considered in order. The first rule whose condition is met will be applied:

1. Evaluation of the IF clause

2. If the num_threads clause is present, then the value of the integer expression is

the number of threads requested.

3. If the omp_set_num_threads library function has been called, then the value of

the argument in the most recently executed call is the number of threads

requested.

4. If the environment variable OMP_NUM_THREADS is defined, then the value of this

environment variable is the number of threads requested.

5. If none of the methods above were used, then the number of threads requested is

implementation-defined

Interpreting an OpenMP Parallel Directive Meaning

  • if (is_parallel==1) num_threads( 8 ) —If the value of the variable is_parallel is one, create 8 threads
  • shared(b) —each thread shares a single copy of variable b
  • private(a) firstprivate(c) —each thread gets private copies of variables a and c —each private copy of c is initialized with the value of c in the “initial thread,” which is the one that encounters the parallel directive
  • default(none) —default state of a variable is specified as none (rather than shared) —signals error if not all variables are specified as shared or private

#pragma omp parallel if (is_parallel==1) num_threads( 8 ) \

shared(b) private(a) firstprivate(c) default(none)

{ /* structured block */

Specifying Worksharing

  • Within the scope of a parallel directive, worksharing directives allow concurrency between iterations or tasks
  • A work-sharing construct distributes the execution of the associated statement among the members of the team that encounter it.
  • The work-sharing directives do not launch new threads, and there is no implied barrier on entry to a work-sharing construct.
  • OpenMP (for C/C++) provides four directives
    • for: concurrent loop iterations
    • sections: concurrent tasks
    • single: one arbitrary thread executes the code

DO / for - shares iterations of a loop across the team. Represents a type of "data parallelism". SECTIONS - breaks work into separate, discrete sections. Each section is executed by a thread. Can be used to implement a type of "functional parallelism". SINGLE - serializes a section of code

Worksharing for Directive for directive partitions parallel iterations across threads

*- Usage: #pragma omp for [clause list] / for loop */

  • Possible clauses in [clause list]**

— private, firstprivate, lastprivate

— reduction

— schedule, nowait, and ordered

- Implicit barrier at end of for loop