Issue
A variable is not being correctly handled in the OpenMP multithreading datascoping clauses.
Relevance
Specifying an invalid scope for a variable will most likely introduce a race condition, making the result of the code unpredictable. For instance, when a variable is written from parallel threads and the specified scoping is shared instead of private.
Actions
Set the proper scope for the variable.
Code example
The following code inadvertently shares the inner loop index variable j for all threads, which creates a race condition. This happens because a scoping has not been specified and it will be shared by default.
void foo() {
int result[10][10];
int i, j;
#pragma omp parallel for shared(result)
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
result[i][j] = 0;
}
}
}
To fix this, the j variable must be declared private. The following code also specifies a private scope for i although in this case it is redundant since OpenMP automatically handles the parallelized loop index variable.
void foo() {
int result[10][10];
int i, j;
#pragma omp parallel for shared(result) private(i, j)
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
result[i][j] = 0;
}
}
}
You can find this and more examples at GitHub.
Related resources
- OpenMP 4.5 Complete Specifications, November 2015 [last checked May 2019]
References

Building performance into the code from day one with Codee