Lecture 12 Synchronization Constructs in OpenMP
Lecture 12 Synchronization Constructs in OpenMP
• Summary
• Additional Resources
Synchronization Clauses
• #pragma omp barrier
• #pragma omp single
• #pragma omp master
• #pragma omp critical
• #pragma omp atomic
• #pragma omp ordered
• #pragma omp flush[(list)]
• #pragma omp threadprivate(variable list)
Barrier Directive
• On encountering this directive, all threads in a team wait until
others have caught up, and then release
#pragma omp barrier
Single Directive
• A single directive specifies a structured block that is executed by a
single (arbitrary) thread in parallel region
• Implicit barrier
#pragma omp single [clause list]
structured block
Master Directive
• The master directive is a specialization of the single directive in
which only the master thread executes the structured block
• No implicit barrier
#pragma omp master
structured block
Critical Section
• A Critical Section is a code segment that has a shared variable and
need to be executed as an atomic action
It means that in a group of cooperating processes/threads, at a given point
of time, only one process must be executing its critical section
11.667
+ 3.765
11.667
15.432 + 3.563
15.230
• OMP_NUM_THREADS
• OMP_DYNAMIC
• OMP_SCHEDULE
• OMP_NESTED
Environment Variables in OpenMP (Cont.)
OMP_NUM_THREADS
• Specifies the default number of threads created upon entering a
parallel region
• The number of threads can be changed during run-time using:
omp_set_num_threads (int threads) routine [OR]
num_threads clause ---> num_threads(int threads)
• Race Condition
A race condition is created when one process may “race ahead” of another
and overwrite the change made by the first process to the shared variable
• Atomic Directive
The atomic directive specifies that the single memory location update
should be performed as an atomic operation
Summary (Cont.)
• Environment Variables in OpenMP
OMP_NUM_THREADS
OMP_DYNAMIC
OMP_Dynamic [dynamic.c]
OMP_SCHEDULE
OMP_NESTED
OMP_NESTED [nested.c]
Additional Resources
• Introduction to Parallel Computing by Ananth Grama and
Anshul Gupta
https://www.youtube.com/watch?v=nE-xN4Bf8XI&list=PLLX-
Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG&index=1
Questions?