atomicAdd with short


I want to to something like

void kernel(short * array) {


    atomicAdd(array+addr, short_val);



However, atomicAdd is meant for 32-bit-values. Is there a workaround or otherwise is it safe to just cast to int?

Kind Regards

In the special case where [font=“Courier New”]*(array+addr)[/font] always falls into the low half of the word, and short_val is 1, you can use [font=“Courier New”]atomicInc(array+addr, 65536);[/font].

Otherwise you can construct your own atomic function from [font=“Courier New”]atomicCAS()[/font], just like floating point atomic add is done on devices that don’t support it. Remember however that you need to handle separately the case where you add to the low or high half-word to avoid misaligned accesses.

However, if you are not severely constrained on memory, it will probably be faster to convert array into an array of ints.

I actually have memory constraints since I am using shared memory. I adapted the atomicCAS example from the manual and it is working fine so far. :thanks:

Also, if you know the 16 bit addition never overflows, you can just replace it with atomic addition of either 1 or 65536.