Docsity

OpenMP: A Parallel Programming API for Shared Memory Systems - Prof. Jingke Li, Study notes of Computer Science

Toggle sidebar
bg1
OpenMP
Jingke Li
Portland State University
Jingke Li (Portland State University) CS 415/515 OpenMP 1 / 25
OpenMP
An API for a set of compiler directives, library routines, and environment
variables that can be used to specify shared-memory parallelism.
•Jointly defined by a group of major computer hardware and software
vendors. It is exp ected to become an ANSI standard.
•Supports Fortran (77, 90, and 95), C, and C++.
•Has been implemented on most Unix platforms and on some Windows
platforms. (Some of the advanced features, such as nested parallelism
and dynamic threads, are not implemented in all implementations.)
Jingke Li (Portland State University) CS 415/515 OpenMP 2 / 25
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download OpenMP: A Parallel Programming API for Shared Memory Systems - Prof. Jingke Li and more Study notes Computer Science in PDF only on Docsity!

OpenMP

Jingke Li

Portland State University

Jingke Li (Portland State University) CS 415/515 OpenMP 1 / 25

OpenMP

An API for a set of compiler directives, library routines, and environment variables that can be used to specify shared-memory parallelism.

  • Jointly defined by a group of major computer hardware and software vendors. It is expected to become an ANSI standard.
  • Supports Fortran (77, 90, and 95), C, and C++.
  • Has been implemented on most Unix platforms and on some Windows platforms. (Some of the advanced features, such as nested parallelism and dynamic threads, are not implemented in all implementations.)

OpenMP

Characteristics:

  • Non-intrusive — OpenMP directives do not change a program’s sequential semantics.
  • Explicit Parallelism — OpenMP offers the programmer full control over parallelization.

Release History:

  • Oct. 1997: Fortran version 1.
  • Late 1998: C/C++ version 1.
  • June 2000: Fortran version 2.
  • April 2002: C/C++ version 2.
  • May 2005: Combined version 2.
  • May 2008: Combined version 3.

More info can be found at the official website: www.openmp.org

Jingke Li (Portland State University) CS 415/515 OpenMP 3 / 25

Compilers and Platforms

  • Fujitsu Fortran/C/C++ compilers
  • HP Fortran/C/C++ compilers for HP-UX PA-RISC/Itanium systems
  • IBM XL Fortran/C compilers for IBM AIX Systems
  • Intel Fortran/C++ compilers for Intel IA32 and IA64 systems
  • PGI PGF77/PGF90 compilers for Intel Linux/Windows systems
  • SGI MIPSpro 7.4 compilers for SGI IRIX Systems
  • Sun Studio compilers
  • gcc 4 C/C++ compilers

Parallel Regions (cont.)

  • One can set the number of threads in the parallel directive.
  • One can also differentiate multiple copies of the parallel region code by using thread ids.

program hello integer tid !$omp parallel num_threads(4) private(tid) tid = omp_get_thread_num() print *, "hello world from thread = ", tid !$omp end parallel end

Jingke Li (Portland State University) CS 415/515 OpenMP 7 / 25

Variants of Parallel Regions

  • Distribute a loop among the threads using the do directive: program example integer i integer, parameter:: n= real a(n), b(n) !$omp parallel default(private) shared(a,b) !$omp do do i=2,n b(i) = (a(i) - a(i-1))/2. end do !$omp end do !$omp end parallel end

Variants of Parallel Regions (cont.)

  • Or, explicitly specify code for each individual thread to execute by using the sections directive: program sections_example integer n !$omp parallel !$omp sections private(n) !$omp section n = omp_get_thread_num() print *, "section 1 executed by thread ", n !$omp section n = omp_get_thread_num() print *, "section 2 executed by thread ", n !$omp section n = omp_get_thread_num() print *, "section 3 executed by thread ", n !$omp end sections !$omp end parallel end program sections_example

Jingke Li (Portland State University) CS 415/515 OpenMP 9 / 25

Compiler Directives

!$omp directive-name [clauses]

Directive Names:

parallel — defining parallel regions do — executing do loops in parallel

parallel do — combined form of parallel and do directives

sections — executing different code blocks with different threads single — restricting a block to a single thread of execution master — restricting a block to execution by the master thread critical — restricting a block to execution by one thread at a time barrier — synchronizing all threads flush — forcing a consistent view of memory for a thread atomic — avoiding conflicts when writing to memory ordered — specifying a block for serial execution

OpenMP Memory Model

OpenMP provides a relaxed memory consistency model, similar to weak ordering. It specifically allows the reordering of accesses within a thread to different variables unless they are separated by a flush (see below) that includes the variables.

  • (^) All OpenMP threads have access to the (shared) memory, but each thread is allowed to have its own temporary view of the memory. (Each thread also has access to a threadprivate memory, which is not accessible by other threads.)
  • A value written to a variable can remain in the thread’s temporary view until it is forced to memory at a later time. Likewise, a read from a variable may retrieve the value from the thread’s temporary view, unless it is forced to read from memory.
  • The OpenMP flush operation enforces consistency between the temporary view and memory.

Jingke Li (Portland State University) CS 415/515 OpenMP 13 / 25

The Flush Operation

The flush operation is the only way in an OpenMP program, to guarantee that a value will move between two threads. In order to move a value from one thread to a second thread, OpenMP requires these four actions in exactly the following order:

  1. the first thread writes the value to the shared variable
  2. the first thread flushes the variable
  3. the second thread flushes the variable
  4. the second thread reads the variable P1:

A = 1; ... flush(A);

P2:

flush(A); B = A;

The above code does not work as written, since we need to guarantee that the flush(A) op in P2 happens after the same op in P1.

The Flush Operation (cont.)

A flush of all visible variables is implied

  • (^) in a barrier region
  • (^) at entry and exit from parallel, critical and ordered regions
  • (^) at entry and exit from combined parallelwork-sharing regions
  • (^) during lock API routines (only in version 2.5)

Pre-Version 2.5 Headaches:

opm_set_lock(lck); local_idx = global_idx; global_idx++; opm_unset_lock(lck);

opm_set_lock(lck); #pragma omp flush(global_idx) local_idx = global_idx; global_idx++; #pragma omp flush(global_idx) opm_unset_lock(lck); Wrong! Still wrong!

Jingke Li (Portland State University) CS 415/515 OpenMP 15 / 25

The Flush Operation (cont.)

The correct version:

opm_set_lock(lck); #pragma omp flush(global_idx, lck) local_idx = global_idx; global_idx++; #pragma omp flush(global_idx, lck) opm_unset_lock(lck);

The lock variable lck must be included in the flush set, to both prevent reordering with respect to the lock calls, and to keep the global value of global idx up to date.

Library Routines (cont.)

  • (^) Set execution environment functions: nested parallelism, dynamic adjustment of threads. omp_set_dynamic(truefalse) omp_get_dynamic() omp_set_nested(true|false) omp_get_nested()

Jingke Li (Portland State University) CS 415/515 OpenMP 19 / 25

Runtime Environment

OpenMP provides four environment variables for controlling the execution of parallel code.

All environment variable names are uppercase. The values assigned to them are not case sensitive.

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"

Runtime Environment (cont.)

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.

OMP NESTED — Enables or disables nested parallelism. Valid values are TRUE or FALSE.

Jingke Li (Portland State University) CS 415/515 OpenMP 21 / 25

OpenMP 3.

  • (^) Added tasks — allow parallelization of irregular problems:
    • Recursive algorithms
    • while loops
  • Extended loop directives to handle (perfectly) nested loops with the collapse clause.
  • Augmented env variables and runtime library routines.
    • (^) e.g. OMP THREAD LIMIT and get omp thread limit().

Loop Collapsing Example

OpenMP 2.5:

!$omp parallel do do i = 1, n !$omp parallel do do j = 1, m do k = 1, L work(i,j,k) enddo enddo enddo

OpenMP 3.0:

!$omp parallel do collapse(2) do i = 1, n do j = 1, m do k = 1, L work(i,j,k) enddo enddo enddo