Static Analysis Problem Type Reference
A variable is written inside a parallel region with a NOWAIT clause and read afterwards without synchronization.
The NOWAIT clause allows thread to continue execution of a parallel region without waiting for the other threads in the team to complete the region. In other words, it suppresses the implied barrier at the end of the parallel region. In this case, there is a variable that is written inside a parallel region and read after the region, without any BARRIER between the end of the parallel region and the read.
This usage creates a race condition between the threads inside the parallel region (which might write the variable) and the threads outside the parallel region (which might read the variable).
A similar problem can arise with TASK regions. In this case, a TASKWAIT directive is needed to force the main thread to wait until all the threads in the task region have finished their work.
|
ID |
Code Location |
Description |
|---|---|---|
|
1 |
Memory read |
The place where the variable was read outside the parallel region |
|
2 |
Memory write |
The place where the variable was written inside the parallel region |
|
3 |
OpenMP declaration |
The place where the parallel region was defined |
#include <stdio.h>
#include <omp.h>
int main(int argc, char **argv)
{
int i;
int sum = 0;
#pragma omp parallel shared(sum)
{
#pragma omp single
{
#pragma omp task
{
for (i = 0; i < 1000; i++) {
a[i] = i;
sum = sum + a[i];
}
}
// Need "#pragma omp taskwait" here
printf("%d\n", sum); // sum is indeterminate here
}
}
return 0;
}