User Guide

  • 2020
  • 06/18/2020
  • Public Content

OpenMP Locks

Consider the following annotated C/C++ serial code:
int count; void Tick() { ANNOTATE_LOCK_ACQUIRE(0); count++; ANNOTATE_LOCK_RELEASE(0);
To implement a lock, use the OpenMP types, variables, and functions to provide more flexible and powerful use of locks. For example, for simple locks, use the
type in C/C++ or the
in Fortran.
Locks can be wrapped inside C++ classes, as shown in the following parallel C/C++ code:
#include <omp.h> int count; omp_lock_t countMutex; struct CountMutexInit { CountMutexInit() { omp_init_nest_lock (&countMutex); } ~CountMutexInit() { omp_destroy_nest_lock(&countMutex); } } countMutexInit; // The declaration of the above object causes countMutex to // be initialized on program startup, and to be destroyed when // the program completes, via the constructor and destructor. struct CountMutexHold { CountMutexHold() { omp_set_nest_lock (&countMutex); } ~CountMutexHold() { omp_unset_nest_lock (&countMutex); } }; void Tick() { // unlocks on scope exit CountMutexHold releaseAtEndOfScope; count++; } ...
Consider the following annotated Fortran serial code:
program BBB integer(kind=4) :: count = 0 . . . contains subroutine Tick call annotate_lock_acquire(0) count = count + 1 call annotate_lock_release(0) end subroutine Tick . . . end program BBB
For simple locks with Fortran code, use the
. The parallel Fortran code follows after adding
use omp_lib
and the
declaration for
program BBB use omp_lib integer(kind=4) :: count = 0 integer (kind=omp_lock_kind) countMutex call omp_nest_lock_init(countMutex) . . . contains subroutine Tick call omp_set_nest_lock(countMutex) count = count + 1 call omp_unset_nest_lock(countMutex) end subroutine Tick . . . end program BBB

Product and Performance Information


Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804