Errors About Atomic Functions in if()
Hi I am a beginner of CUDA, and I am writing a global function using atomic functions in if(), like: if(atomicCAS(&p[x],x,y)==x) { do sth; } else printf("x %d, y %d, px %d\n",x,y,p[x]); and, it sometimes prints on the screen, like: x 1, y 2, px 2 How could it happened!?
Hi

I am a beginner of CUDA, and I am writing a global function using atomic functions in if(), like:

if(atomicCAS(&p[x],x,y)==x)
{
do sth;
}
else
printf("x %d, y %d, px %d\n",x,y,p[x]);

and, it sometimes prints on the screen, like:

x 1, y 2, px 2

How could it happened!?

#1
Posted 12/07/2017 12:45 PM   
I guess you mean that with that output, you would have expected the if statement to take the then path, not the else path. But the read of p[x] in the printf statement is not guaranteed to be the same value of p[x] observed by the atomic, or set by the atomic. It depends on what else is happening in your program. Presumably you are using an atomic to resolve simultaneous access. Such things can usually not be explained by inspecting such a small portion of your code, but actually require understanding of your entire program.
I guess you mean that with that output, you would have expected the if statement to take the then path, not the else path.

But the read of p[x] in the printf statement is not guaranteed to be the same value of p[x] observed by the atomic, or set by the atomic. It depends on what else is happening in your program. Presumably you are using an atomic to resolve simultaneous access. Such things can usually not be explained by inspecting such a small portion of your code, but actually require understanding of your entire program.

#2
Posted 12/07/2017 02:53 PM   
Scroll To Top

Add Reply