13

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)?

Community
  • 1
  • 1
Auron
  • 13,626
  • 15
  • 47
  • 54
  • 2
    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 Dec 07 '11 at 13:57
  • 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 Dec 07 '11 at 14:01
  • 2
    check out: http://stackoverflow.com/questions/5953955/threads-concurrent-write – jkysam Dec 07 '11 at 14:05
  • @jkysam Nice, I guess my question can be closed now as nearly exact duplicate. Thank you very much to you and talonmies. – Auron Dec 07 '11 at 14:09
  • 1
    See also: http://stackoverflow.com/questions/19946286/unable-to-find-simple-sum-of-1-to-100-numbers-in-cuda/19946435#19946435 – Ade Miller Nov 13 '13 at 18:51

1 Answers1

15

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.

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • That is what I thought and what I always experienced. I will set this answer as recommended, unless someone wants to challenge this. – Auron Mar 31 '14 at 12:27
  • @Robert Crovella, for the different value cases, you mentioned that one of the values will end up in that location, but what about partial writes? – Nyaruko Mar 10 '19 at 18:11