Tutorials | Exercises | Abstracts | LC Workshops | Comments | Search | Privacy & Legal Notice


OpenMP

Blaise Barney, Lawrence Livermore National Laboratory UCRL-MI-133316

Table of Contents

  1. Abstract
  2. Introduction
    1. What is OpenMP?
    2. History
    3. Goals of OpenMP
  3. OpenMP Programming Model
  4. OpenMP Directives
    1. Fortran Directive Format
    2. C/C++ Directive Format
    3. Directive Scoping
    4. PARALLEL Construct
    5. Work-Sharing Constructs
      1. DO / for Directive
      2. SECTIONS Directive
      3. WORKSHARE Directive
      4. SINGLE Directive
    6. Combined Parallel Work-Sharing Constructs
      1. PARALLEL DO / parallel for Directive
      2. PARALLEL SECTIONS Directive
      3. PARALLEL WORKSHARE Directive
    7. TASK Construct
    8. Synchronization Constructs
      1. MASTER Directive
      2. CRITICAL Directive
      3. BARRIER Directive
      4. TASKWAIT Directive
      5. ATOMIC Directive
      6. FLUSH Directive
      7. ORDERED Directive
    9. THREADPRIVATE Directive
    10. Data Scope Attribute Clauses
      1. PRIVATE Clause
      2. SHARED Clause
      3. DEFAULT Clause
      4. FIRSTPRIVATE Clause
      5. LASTPRIVATE Clause
      6. COPYIN Clause
      7. COPYPRIVATE Clause
      8. REDUCTION Clause
    11. Clauses / Directives Summary
    12. Directive Binding and Nesting Rules
  5. Run-Time Library Routines
    1. OMP_SET_NUM_THREADS
    2. OMP_GET_NUM_THREADS
    3. OMP_GET_MAX_THREADS
    4. OMP_GET_THREAD_NUM
    5. OMP_GET_THREAD_LIMIT
    6. OMP_GET_NUM_PROCS
    7. OMP_IN_PARALLEL
    8. OMP_SET_DYNAMIC
    9. OMP_GET_DYNAMIC
    10. OMP_SET_NESTED
    11. OMP_GET_NESTED
    12. OMP_SET_SCHEDULE
    13. OMP_GET_SCHEDULE
    14. OMP_SET_MAX_ACTIVE_LEVELS
    15. OMP_GET_MAX_ACTIVE_LEVELS
    16. OMP_GET_LEVEL
    17. OMP_GET_ANCESTOR_THREAD_NUM
    18. OMP_GET_TEAM_SIZE
    19. OMP_GET_ACTIVE_LEVEL
    20. OMP_INIT_LOCK
    21. OMP_DESTROY_LOCK
    22. OMP_SET_LOCK
    23. OMP_UNSET_LOCK
    24. OMP_TEST_LOCK
    25. OMP_INIT_NEST_LOCK
    26. OMP_DESTROY_NEST_LOCK
    27. OMP_SET_NEST_LOCK
    28. OMP_UNSET_NEST_LOCK
    29. OMP_TEST_NEST_LOCK
    30. OMP_GET_WTIME
    31. OMP_GET_WTICK
  6. Environment Variables
  7. Memory and Performance Issues
  8. LLNL Specific Information and Recommendations
  9. References and More Information
  10. Exercise


Abstract


OpenMP is an Application Program Interface (API), jointly defined by a group of major computer hardware and software vendors. OpenMP provides a portable, scalable model for developers of shared memory parallel applications. The API supports C/C++ and Fortran on multiple architectures, including UNIX & Windows NT. This tutorial covers most of the major features of OpenMP, including its various constructs and directives for specifying parallel regions, work sharing, synchronization and data environment. Runtime library functions and environment variables are also covered. This tutorial includes both C and Fortran example codes and a lab exercise.

Level/Prerequisites: Geared to those who are new to parallel programming with OpenMP. Basic understanding of parallel programming in C or Fortran assumed. For those who are unfamiliar with Parallel Programming in general, the material covered in EC3500: Introduction to Parallel Computing would be helpful.



Introduction

What is OpenMP?

OpenMP Is: OpenMP Logo

OpenMP Is Not:

References:



Introduction

History

Ancient History: Which Led To�

Release History

OpenMP Release Timeline

Introduction

Goals of OpenMP

Standardization: Lean and Mean:

Ease of Use:

Portability:



OpenMP Programming Model


Shared Memory, Thread Based Parallelism:

Explicit Parallelism:

Fork - Join Model:

Compiler Directive Based:

Nested Parallelism Support:

Dynamic Threads:

I/O:

Memory Model: FLUSH Often?



Example OpenMP Code Structure



OpenMP Directives

Fortran Directives Format

Format: (case insensitive)

Example:

Fixed Form Source:

Free Form Source:

General Rules:

OpenMP Directives

C / C++ Directives Format

Format:

Example:

General Rules:



OpenMP Directives

Directive Scoping

Do we do this now...or do it later? Oh well, let's get it over with early...

Static (Lexical) Extent:

Orphaned Directive:

Dynamic Extent:

Example:

Why Is This Important?



OpenMP Directives

PARALLEL Region Construct

Purpose:

Format:

Notes:

How Many Threads?

Dynamic Threads:

Nested Parallel Regions:

Clauses:

Restrictions:


Example: Parallel Region



OpenMP Directives

Work-Sharing Constructs

Types of Work-Sharing Constructs:

Restrictions:



OpenMP Directives

Work-Sharing Constructs
DO / for Directive

Purpose:

Format:

Clauses:

Restrictions:


Example: DO / for Directive



OpenMP Directives

Work-Sharing Constructs
SECTIONS Directive

Purpose:

Format:

Clauses:

Questions:

Restrictions:


Example: SECTIONS Directive



OpenMP Directives

Work-Sharing Constructs
WORKSHARE Directive

Purpose:

Format:

Restrictions:


Example: WORKSHARE Directive



OpenMP Directives

Work-Sharing Constructs
SINGLE Directive

Purpose:

Format:

Clauses:

Restrictions:



OpenMP Directives

Combined Parallel Work-Sharing Constructs



OpenMP Directives

TASK Construct

Purpose:

Format:

Clauses and Restrictions:



OpenMP Directives

Synchronization Constructs



OpenMP Directives

Synchronization Constructs
MASTER Directive

Purpose:

Format:

Restrictions:



OpenMP Directives

Synchronization Constructs
CRITICAL Directive

Purpose:

Format:

Notes:

Restrictions:


Example: CRITICAL Construct



OpenMP Directives

Synchronization Constructs
BARRIER Directive

Purpose:

Format:

Restrictions:



OpenMP Directives

Synchronization Constructs
TASKWAIT Directive

Purpose:

Format:

Restrictions:



OpenMP Directives

Synchronization Constructs
ATOMIC Directive

Purpose:

Format:

Restrictions:



OpenMP Directives

Synchronization Constructs
FLUSH Directive

Purpose:

Format:

Notes:



OpenMP Directives

Synchronization Constructs
ORDERED Directive

Purpose:

Format:

Restrictions:



OpenMP Directives

THREADPRIVATE Directive

Purpose:

Format:

Notes:

Restrictions:



OpenMP Directives

Data Scope Attribute Clauses


PRIVATE Clause

Purpose:

Format:

Notes:

Questions:


SHARED Clause

Purpose:

Format:

Notes:


DEFAULT Clause

Purpose:

Format:

Notes:

Restrictions:


FIRSTPRIVATE Clause

Purpose:

Format:

Notes:


LASTPRIVATE Clause

Purpose:

Format:

Notes:


COPYIN Clause

Purpose:

Format:

Notes:


COPYPRIVATE Clause

Purpose:

Format:


REDUCTION Clause

Purpose:

Format:

Example: REDUCTION - Vector Dot Product:

Restrictions:



OpenMP Directives

Clauses / Directives Summary



OpenMP Directives

Directive Binding and Nesting Rules

Note This section is provided mainly as a quick reference on rules which govern OpenMP directives and binding. Users should consult their implementation documentation and the OpenMP standard for other rules and restrictions.

Directive Binding:

Directive Nesting:



Run-Time Library Routines


Overview:


OMP_SET_NUM_THREADS

Purpose:

Format:

Notes & Restrictions:


OMP_GET_NUM_THREADS

Purpose:

Format:

Notes & Restrictions:


OMP_GET_MAX_THREADS

Purpose:

Notes & Restrictions:


OMP_GET_THREAD_NUM

Purpose:

Format:

Notes & Restrictions:

Examples:


OMP_GET_THREAD_LIMIT

Purpose:

Format:

Notes:


OMP_GET_NUM_PROCS

Purpose:

Format:


OMP_IN_PARALLEL

Purpose:

Format:

Notes & Restrictions:


OMP_SET_DYNAMIC

Purpose:

Format:

Notes & Restrictions:


OMP_GET_DYNAMIC

Purpose:

Format:

Notes & Restrictions:


OMP_SET_NESTED

Purpose:

Format:

Notes & Restrictions:


OMP_GET_NESTED

Purpose:

Format:

Notes & Restrictions:


OMP_SET_SCHEDULE
OMP_SET_SCHEDULE
OMP_GET_SCHEDULE
OMP_SET_MAX_ACTIVE_LEVELS
OMP_GET_MAX_ACTIVE_LEVELS
OMP_GET_LEVEL
OMP_GET_ANCESTOR_THREAD_NUM
OMP_GET_TEAM_SIZE
OMP_GET_ACTIVE_LEVEL
OMP_INIT_NEST_LOCK
OMP_DESTROY_NEST_LOCK
OMP_SET_NEST_LOCK
OMP_UNSET_NEST_LOCK
OMP_TEST_NEST_LOCK





OMP_INIT_LOCK

Purpose:

Format:

Notes & Restrictions:


OMP_DESTROY_LOCK

Purpose:

Format:

Notes & Restrictions:


OMP_SET_LOCK

Purpose:

Format:

Notes & Restrictions:


OMP_UNSET_LOCK

Purpose:

Format:

Notes & Restrictions:


OMP_TEST_LOCK

Purpose:

Format:

Notes & Restrictions:


OMP_GET_WTIME

Purpose:

Format:

Notes & Restrictions:


OMP_GET_WTICK

Purpose:

Format:

Notes & Restrictions:



Environment Variables

OMP_SCHEDULE

Applies only to DO, PARALLEL DO (Fortran) and for, parallel for (C/C++) directives which have their schedule clause set to RUNTIME. The value of this variable determines how iterations of the loop are scheduled on processors. For example:

setenv OMP_SCHEDULE "guided, 4"
setenv OMP_SCHEDULE "dynamic"

OMP_NUM_THREADS

Sets the maximum number of threads to use during execution. For example:

setenv OMP_NUM_THREADS 8

OMP_DYNAMIC

Enables or disables dynamic adjustment of the number of threads available for execution of parallel regions. Valid values are TRUE or FALSE. For example:

setenv OMP_DYNAMIC TRUE

OMP_NESTED

Enables or disables nested parallelism. Valid values are TRUE or FALSE. For example:

setenv OMP_NESTED TRUE

Implementation notes:

  • Your implementation may or may not support nested parallelism and/or dynamic threads. If nested parallelism is supported, it is often only nominal, in that a nested parallel region may only have one thread.

  • Consult your implementation's documentation for details - or experiment and find out for yourself if you can't find it in the documentation.

OMP_STACKSIZE

New with OpenMP 3.0. Controls the size of the stack for created (non-Master) threads. Examples:

setenv OMP_STACKSIZE 2000500B
setenv OMP_STACKSIZE "3000 k "
setenv OMP_STACKSIZE 10M
setenv OMP_STACKSIZE " 10 M "
setenv OMP_STACKSIZE "20 m "
setenv OMP_STACKSIZE " 1G"
setenv OMP_STACKSIZE 20000

OMP_WAIT_POLICY

New with OpenMP 3.0. Provides a hint to an OpenMP implementation about the desired behavior of waiting threads. A compliant OpenMP implementation may or may not abide by the setting of the environment variable. Valid values are ACTIVE and PASSIVE. ACTIVE specifies that waiting threads should mostly be active, i.e., consume processor cycles, while waiting. PASSIVE specifies that waiting threads should mostly be passive, i.e., not consume processor cycles, while waiting. The details of the ACTIVE and PASSIVE behaviors are implementation defined. Examples:

setenv OMP_WAIT_POLICY ACTIVE
setenv OMP_WAIT_POLICY active
setenv OMP_WAIT_POLICY PASSIVE
setenv OMP_WAIT_POLICY passive

OMP_MAX_ACTIVE_LEVELS

New with OpenMP 3.0. Controls the maximum number of nested active parallel regions. The value of this environment variable must be a non-negative integer. The behavior of the program is implementation defined if the requested value of OMP_MAX_ACTIVE_LEVELS is greater than the maximum number of nested active parallel levels an implementation can support, or if the value is not a non-negative integer. Example:

setenv OMP_MAX_ACTIVE_LEVELS 2

OMP_THREAD_LIMIT

New with OpenMP 3.0. Sets the number of OpenMP threads to use for the whole OpenMP program. The value of this environment variable must be a positive integer. The behavior of the program is implementation defined if the requested value of OMP_THREAD_LIMIT is greater than the number of threads an implementation can support, or if the value is not a positive integer. Example:

setenv OMP_THREAD_LIMIT 8



Memory and Performance Issues


Thread Stack Size:



LLNL Specific Information and Recommendations


LC OpenMP Implementations:

Compiling:

Documentation:


This completes the tutorial.

Evaluation Form       Please complete the online evaluation form - unless you are doing the exercise, in which case please complete it at the end of the exercise.

Where would you like to go now?



 
References and More Information


https://computing.llnl.gov/tutorials/openMP/
Last Modified: 03/01/2010 22:40:15 blaiseb@llnl.gov
UCRL-MI-133316