Common Mistakes in Using OpenMP 5: Assuming Non-existing Synchronization Before Entering Worksharing Construct
Thursday Sep 20, 2007
There is no synchronization between the threads in a team when they enter a worksharing construct. Many people assume there is a barrier before the threads enter a worksharing construct, especially when there is a FIRSTPRIVATE used in the worksharing construct. This is a common mistake.
For example, in the following code, assume two threads - thread 1 and thread 2 are in the team, and Read1 is executed by thread 1 and Read2 is executed by thread 2.
#pragma omp parallel
{
if (omp_get_thread_num()==0)
z = 1;
else
z = 2;
#pragma omp sections firstprivate(z)
{
#pragma omp section
{
... = z; // Read1
}
#pragma omp section
{
... = z; // Read2
}
}
}
What are the values of If there were a synchronization before the worksharing construct, then the above (Read1:1, Read2:2) is not possible.
Now, look at the following example which has both FIRSTPRIVATE and LASTPRIVATE,
What could be the value of If a list item appears in both firstprivate and lastprivate clauses, the update
required for lastprivate occurs after all initializations for firstprivate.
So, the value of
#pragma omp parallel
{
z = 1;
#pragma omp for firstprivate(z) lastprivate(z) nowait
for (i=0; i<n; i++) {
... = z; // Read1
z = 2; // Write1
}
}










