Is there a way to create a customized atomic operation in cuda?
I am asking because i got a problem that i need to check this kernel code:
if (img[index] != clone[index])
atomicExch(cmp, index);
the main problem is that img, clone and cmp are device memories, HOWEVER, cmp is a SINGLE unity memory, so it can be seen as a shared memory, so, all threads have acesss to it, with the atomicExch i guarantee that once someone triggers it, no one will stop it, so far so good, the main problem is the if that can trigger race conditions… i would like to make the both the if and the atomicExch to be executed in a atomic way… anyone have any idea of how to do that? or if it is at least possible?
If you are trying to do what I think you are then how about making a flag in shared memory
if (img[index] != clone[index])
set flag
synchthreads
if ( flag is set )
…
NB it is OK for several or even all threads in the block to set the flag as the hardware will queue them
e.g. if you set the flag to tid then its value will be one of the tid’s but which one is undefined.
(NB tid is not a good value to use because one thread has tid of zero.)
I wrote a long reply here about avoiding such complex atomic operations, and how to use an inefficient lock token as a last-resort answer to rare atomic issues anyway.
But I replaced that now because as I was writing up the code, I noticed that your example doesn’t have a problem that atomics or locks would solve anyway.
Since you don’t use the value stored in cmp, the atomicExch is effectively just an assignment.
Your test condition does not test cmp itself, so there’s no need to lock or exchange anything.
So just write “if (img[index] != clone[index]) *cmp=index;”
Now the final value of *cmp is subject to a race condition… if multiple threads have set it, then ONE will have succeeded at the end, but it is undefined which one was that winner.
If you really need a consistent winner, then just use atomicMin(cmp, index) or atomicMax(cmp, index), and at the end of processing, cmp will hold the smallest or largest index respectively.