This post assumes you have read the introduction and prerequisite: Visual Studio Race Detection for C++ AMP. I will also assume that you have enabled all the race detection options of the Exceptions dialog, as described in that prerequisite blog post. In this post, I am going to share an example that demonstrates detection of write after read hazards between threads belonging to the same tile.
To show an example of a write after read hazard, we’re going to use the Matrix Multiplication sample. This time let’s comment out the second tidx.barrier.wait() statement, on line 141, to see what happens in its absence:
126: for (int i = 0; i < N; i += tile_size)
128: tile_static _type localB[tile_size][tile_size];
129: tile_static _type localA[tile_size][tile_size];
131: localA[localIdx][localIdx] = av_a(globalIdx, i + localIdx);
132: localB[localIdx][localIdx] = av_b(i + localIdx, globalIdx);
136: for (unsigned k = 0; k < tile_size; k++)
138: temp_c += localA[localIdx][k] * localB[k][localIdx];
141: // tidx.barrier.wait();
Running this program with all race detection options turned on, we encounter the write-after-read hazard warning dialog notification triggered by thread [0,0][0,0]:
This particular race hazard can be a bit difficult to see as the previous access is reported to be at a later line (line 138) than the current instruction (line 131):
As you might have guessed, the conflicting instruction is executed at an earlier iteration of the loop. The problem here is thread [0,0][0,0] (current thread) is retrieving the next set of data and overwriting the previous data where thread [0,0][0,3] (conflicting thread) might not yet be done with processing the previous set of data. The tile_barrier that was commented out ensures that all threads are done with an iteration of the loop before moving on to the next iteration.
If your code is in a loop, remember that it might switch between writing & reading memory locations between iterations of the loop. You can unroll the loop to make it easier to spot such patterns. In our case above it switches from reading (line 138) to writing (line 131).