
This recommendation is part of the open catalog of best practice rules for performance that is automatically detected and reported by Codee.
Issue
Read-only variables should be shared instead of private to reduce memory consumption and unnecessary data copying.
Relevance
Since a read-only variable is never written to, it can be safely shared without any risk of race conditions. Sharing variables is more efficient than privatizing them from a memory perspective so it should be favored whenever possible.
Actions
Set the scope of the read-only variable to shared.
Code example
In the following code, arrays A
and B
are never written to. However, they are privatized and thus each thread will hold a copy of each array, effectively using more memory and taking more time to create private copies.
#define SIZE 5
void foo() {
int A[SIZE] = {1, 2, 3, 4, 5};
int B[SIZE] = {5, 4, 3, 2, 1};
int sum[SIZE];
#pragma omp parallel for shared(sum) firstprivate(A, B)
for (int i = 0; i < SIZE; i++) {
sum[i] = A[i] + B[i];
}
}
To save memory, change their scope to shared. This may also prevent memory issues when using arrays, as codes may easily run out of memory for a high number of threads.
#define SIZE 5
void foo() {
int A[SIZE] = {1, 2, 3, 4, 5};
int B[SIZE] = {5, 4, 3, 2, 1};
int sum[SIZE];
#pragma omp parallel for shared(sum, A, B)
for (int i = 0; i < SIZE; i++) {
sum[i] = A[i] + B[i];
}
}
Related resources
- PWR006 examples at GitHub
- OpenMP 4.5 Complete Specifications, November 2015 [last checked May 2019]
References

Building performance into the code from day one with Codee