13
votes

This question is closely related to this one, but mine belongs to the CUDA world.

I have several threads in my kernel that could write the very same value in the same global memory location. This has been working fine, but I'm afraid that it could be potentially bogus, and that so far I was just being lucky.

Is there any possibility of memory corruption or unexpected behavior in my workflow (due to data races, cache syncing, etc)?

1
The short answer is yes. Either redesign your algorithm or use atomic memory access operators if you absolutely must have multiple threads writing to the same memory.talonmies
I should have asked this question before having my algorithms nearly ready to go :( It's ok, time for some rethinking and refactoring! It would be nice to know the reason, though.Auron
@jkysam Nice, I guess my question can be closed now as nearly exact duplicate. Thank you very much to you and talonmies.Auron

1 Answers

13
votes

I have several threads in my kernel that could write the very same value in the same global memory location.

Contrary to some of the comments, this is safe. By safe, I mean that the value written will show up in that global memory location. There is no possibility that a write will not occur, and there is no possibility of some other spurious data corruption. Tom's answer here is applicable to this.

If different values are being written, then one of the values will end up in that location, but which value is undefined.