Multiple writes to global memory


Can multiple threads write to the same global memory location without using atomics ASSUMING I do not care which thread’s data is actually written in the end?

In particular I want multiple threads to be able to set something (unsigned int) to true. I do not care who set it and multiple threads might try set it. I simply want to know if someone or no one tried to set it at all. I assume since this is simply a 4-byte store nothing too funny will happen.

Also, is this deterministic? Obviously it is chaotic as to who’s value will get written in the end, but for the same kernel/starting conditions will it always be the same thread to “win”?

Thank you

Yes, at least one would succeed.

No, it’s not deterministic. It depends on the thread scheduler and run-time behavior of your code. If one thread takes longer time to fetch a global variable then last time, you may have different execution sequence.

we’ve used that in our project. We had to re-do the algorith, because this approach is just TOO SLOW

what I would recommend is that threads synchronize somehow and only one thread does actually writting. Now you will have that boolean set from multiple blocks, but do it so that each block writes to each own address. Then just reduce this set in a parallel manner, like in the example “reduction”. Such approach may happen to be faster, otherwise blocks stay in line and that reduces performance significantly.