User Guide

  • 2021.2
  • 05/21/2021
  • Public Content
  • Download as PDF
Contents

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
omp_lock_t
type in C/C++ or the
type=omp_lock_kind
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
type=omp_lock_kind
. The parallel Fortran code follows after adding
use omp_lib
and the
integer
declaration for
count
:
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
After you rewrite your code to use OpenMP* parallel framework, you can analyze its performance with
Intel® Advisor
perspectives. Use the
Vectorization and Code Insights
perspective to analyze how well you OpenMP code is vectorized or use the
Offload Modeling
perspective to model its performance on a GPU.

Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.