Module 10: Open Multi-Processing
Lecture 20: The “omp sections” Directive
Synchronization Constructs: “atomic” Clause
Format: atomic
#pragma omp atomic
statement expression
The atomic directive specifies that a specific memory location must be updated atomically, rather than letting multiple threads attempt to write to it
Barrier
Why barriers?
Suppose we run each of these two loops in parallel
for( i = 0; i < N; i++)
a[i] = b[i] + c[i];
for(i = 0; i < N; i++)
d[i] = a[i] + b[i];
This may give us wrong result
WHY ?
Barrier
We should have updated all a[]’s before using them
for( i = 0; i < N; i++)
a[i] = b[i] + c[i];
All threads should wait here for other threads to complete so we need a barrier here
for(i = 0; i < N; i++)
d[i] = a[i] + b[i];
All threads wait at the barrier point and only continue when all threads have reached the barrier point
If there is the guarantee that the mapping of iterations onto threads is identical for both loops, there will not be a data race in this case