Module 10: Open Multi-Processing
Lecture 20: The “omp sections” Directive
Synchronization Construct: “critical” Directive
Format
#pragma omp critical [ name ]
“critical” directive ensures mutual exclusion: Only one thread at a time can enter a critical region
The optional name enables multiple different CRITICAL regions to exist:
Different CRITICAL regions with the same name are treated as the same region.
All CRITICAL sections which are unnamed, are treated as the same section.
If sum is a shared variable, this loop can not run in parallel
for (i = 0; i < N; i++){
.....
sum += a[i];
.....
}
If sum is a shared variable, this loop can not run in parallel
for (i = 0; i < N; i++){
.....
sum += a[i];
.....
}
Use of critical section to parallelize the loop
Example
for (i = 0; i < N; i++){
.....
#pragma omp critical
sum += a[i];
.....
}