Are long integer assignments atomic? Atomicity of assignment operator

Hi All,

It seems I cannot find an answer to this trivial question. If I have a “long integer” global variable and assign to it some new value from a thread, would that operation be performed atomically or would other threads (if they happen to read the same variable at the same time) be able to read some intermediate value which is neither the old value nor the newly assigned value? And what would happen if it’s a “long long integer”?

Many thanks! :smile:

I think you need

unsigned long long int atomicExch(unsigned long long int* address, unsigned long long int val);

please check B.11.1.3 of CUDA programming guide of 4.0RC2

Thanks for the suggestion :smile:

However, I’m not sure if an atomic exchange operation is really necessary. That is, the writing thread is not interested in the old value that is being overwritten. What matters is that the assignment to the global variable is performed atomically:

long* global_var = // Pointer to the global variable.

*global_var = my_long_value; // This is what I need to be atomic.

If another thread at the same time reads global_var, I wouldn’t mind if it finds the old value or the new value, what matters is that it does not find some undefined garbage.

I hope I explained my query well this time, sorry for any confusions O:)

At the PTX level, there are write instructions for 32, 64, and 128 bit words. They probably correspond directly to machine instructions, which would be a necessary condition for the write to be done as a single unit. However, I don’t believe I’ve seen the behavior of multiple writes to the same location exactly specified to the level required to answer your question.