If I have several threads trying to write the same value to a single location in memory, is it possible to have a race condition? Can the data somehow get corrupted during the writes? There is no preceding read or test conditions, only the write…
EDIT: To clarify, I’m computing a dot product on a GPU. I’m using several threads to calculate the individual products (one thread per row/column element) and saving them to a temporary location in memory. I need to then sum those intermediate products and save the result.
I was thinking about having all threads individually perform this sum/store operation since branching on a GPU can hurt performance. (You would think it should take the same amount of time for the sum/store whether it’s done by a single thread or all threads, but I’ve tested this and there is a small performance hit.) All threads will get the same sum, but I’m concerned about a race condition when they each try to write their answer to the same location in memory. In the limited testing I’ve done, everything seems fine, but I’m still nervous…
There is no problem having multiple threads writing a single (presumably shared or global) memory location in CUDA, even “simultaneously” i.e. from the same line of code.
If you care about the order of the writes, then this is a problem, as CUDA makes no guarantee of order, for multiple threads executing the same write operation to the same memory location. If this is an issue, you should use atomics or some other method of refactoring your code to sort it out. (It doesn’t sound like this is an issue for you.)
Presumably, as another responder has stated, you care about the result at some point. Therefore it’s necessary to have a barrier of some sort, either explicit (e.g. __synchthreads(), for multiple threads within a block using shared memory for example) or implicit (e.g. end of a kernel, for multiple threads writing to a location in global memory) before you read that location and expect a sensible result. Note these are not the only possible barrier methods that could give you sane results, just two examples. Warp-synchronous behavior or other clever coding techniques could be leveraged to ensure the sanity of a read following a collection of writes.