Hello, my group is working on a race detection tool and our tool reported a potential data race in this repository. We wanted to check with you to see if the race is real.
The reported race is in the openmp parallel region at stencil.c:91.
#pragma omp parallel default(shared) private(i, j, k, bp)
{
for (in = 0; in < sorted_index[num_refine+1]; in++) {
bp = &blocks[sorted_list[in].n];
for (i = 1; i <= x_block_size; i++)
for (j = 1; j <= y_block_size; j++)
for (k = 1; k <= z_block_size; k++)
work[i][j][k] = (bp->array[var][i-1][j ][k ] +
bp->array[var][i ][j-1][k ] +
bp->array[var][i ][j ][k-1] +
bp->array[var][i ][j ][k ] +
bp->array[var][i ][j ][k+1] +
bp->array[var][i ][j+1][k ] +
bp->array[var][i+1][j ][k ])/7.0;
for (i = 1; i <= x_block_size; i++)
for (j = 1; j <= y_block_size; j++)
for (k = 1; k <= z_block_size; k++)
bp->array[var][i][j][k] = work[i][j][k];
}
}
The induction variable in the outer for loop, in
, looks to be shared among all threads. This causes a write/write race when in
is incremented which could cause undefined behavior with regards to the value of in
.
Also, it may be possible for bp
to point to the same location on multiple threads. If multiple threads set in
to 0 for the initial loop iteration, the threads will read the same value from the sorted_list
and use the same offset into blocks
for bp
at line 94
// bp points to the same location if in has the same value on different threads
bp = &blocks[sorted_list[in].n];
This potentially leads to lots of races. The write to bp->array[var][i][j][k]
at line 108 races with itself, and all the reads on bp->array
on lines 98-104 when multiple threads use the same value in bp
.
I am not familiar with this code so I am not sure that I understand what is happening correctly.
Does this look like a real race to you?