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 z at Read1 and Read2? All the following three combinations are possible,
- Read1:1 Read2:1
- Read1:1 Read2:2
- Read1:2 Read2:2
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,
#pragma omp parallel
{
z = 1;
#pragma omp for firstprivate(z) lastprivate(z) nowait
for (i=0; i<n; i++) {
... = z; // Read1
z = 2; // Write1
}
}
What could be the value of z at Read1? Would it be 2? OpenMP 3.0 Draft has clarified this situation. It says
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 z at Read1 cannot be 2.