Issue #
Disable default OpenMP scoping to force having to declare the scoping of all variables.
Relevance #
When the scope for a variable is not specified in an OpenMP parallel directive, a default scope is assigned to it. Even when set explicitly, using a default scope is considered a bad practice since it can lead to wrong data scopes inadvertently being applied to variables. Thus, it is recommended to explicitly set the scope for each variable.
Actions #
Add default(none)
to disable default OpenMP scoping.
Code example #
In the following code, a variable t
is used in each iteration of the loop to hold a value that is then assigned to the array result
. Since no data scoping is declared for those variables the default will be used. This makes the variable t shared
which is incorrect since it introduces a race condition.
void foo() {
int t;
int result[10];
#pragma omp parallel for
for (int i = 0; i < 10; i++) {
t = i + 1;
result[i] = t;
}
}
The following code disables the default scoping which will make the compiler raise an error due to unspecified scopes.
void foo() {
int t;
int result[10];
#pragma omp parallel for default(none)
for (int i = 0; i < 10; i++) {
t = i + 1;
result[i] = t;
}
}
To fix the code the scope of each variable must be specified. The variable t
must be made private to prevent the race condition.
void foo() {
int t;
int result[10];
#pragma omp parallel for default(none) shared(result) private(t)
for (int i = 0; i < 10; i++) {
t = i + 1;
result[i] = t;
}
}
Related resources #
- PWR005 examples at GitHub
- PWR004: Declare variable OpenMP scoping
- OpenMP 4.5 Complete Specifications, November 2015 [last checked May 2019]
- clang-tidy – openmp-use-default-none, [last checked July 2019]