This defect is part of the open catalog of best practice rules for performance that is automatically detected and reported by Codee.
Issue
A variable is being incorrectly privatized in the OpenMP datascoping clauses.
Relevance
In some parallel loops , variables need to be private to each thread but the value computed in the last loop iterations needs to be used afterwards. For such cases, the OpenMP private scoping is incorrect and lastprivate must be used instead.
Actions
Change the data scope of the variable to ‘lastprivate’.
Code example
In the following example, liveOut has an incorrect private clause:
double example(int m, double *A, double *B, double *C) {
double liveOut;
// liveOut is private but used after the loop, should be lastprivate
#pragma omp parallel for private(liveOut)
for (int i = 0; i < m; i++) {
liveOut = A[i] * B[i];
C[i] = C[i] + liveOut;
}
liveOut += 5;
return liveOut;
}
The clause needs to be updated to lastprivate so that the value computed in the last loop iteration survives the loop:
double example(int m, double *A, double *B, double *C) {
double liveOut;
// liveOut is private but used after the loop, should be lastprivate
#pragma omp parallel for lastprivate(liveOut)
for (int i = 0; i < m; i++) {
liveOut = A[i] * B[i];
C[i] = C[i] + liveOut;
}
liveOut += 5;
return liveOut;
}
Related resources
References
- Data-Sharing Attribute Clauses – OPENMP API Specification: Version 5.0 November 2018 [last checked August 2021]

Building performance into the code from day one with Codee