A.6 Using the lastprivate Clause
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

A.6 Using the lastprivate Clause 

Correct execution sometimes depends on the value that the last iteration of a loop assigns to a variable. Such programs must list all such variables as arguments to a lastprivate clause (Section 2.7.2.3 on page 27) so that the values of the variables are the same as when the loop is executed sequentially.

#pragma omp parallel
{
   #pragma omp for lastprivate(i)
      for (i=0; i<n-1; i++)
         a[i] = b[i] + b[i+1];
}
a[i]=b[i];

In the preceding example, the value of i at the end of the parallel region will equal n–1, as in the sequential case.

Show:
© 2016 Microsoft