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 shared in the OpenMP datascoping clauses.
Relevance
Specifying an invalid scope for a variable may introduce race conditions and produce incorrect results. For instance, when a variable is written from parallel threads and the specified scoping is shared instead of private.
Actions
Change the data scope of the variable from shared to private.
Code example
In the following code no variable is privatized:
void example(int **result, unsigned rows, unsigned cols) {
int i, j;
// j is implicitly shared and it should be private!
#pragma omp parallel for shared(result)
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = 0;
}
}
}
This introduced a data race due to j being shared among threads. It should be privatized:
void example(int **result, unsigned rows, unsigned cols) {
int i, j;
// j is implicitly shared and it should be private!
#pragma omp parallel for shared(result) private(j)
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = 0;
}
}
}
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