atomic operation within a if block

Hey everyone !

I wonder about something really strange.

Here it’s my code :

if(condition)

{

  int nbr = atomicAdd(adrr, 1);

  /*

   other part of the function, not interesting here

  */

}

Yes, i know, not complicated :dry: but have a look to the next part.

What you have to know is, for a test, “condition” is always FALSE and when i run my algorithm, the duration is about 4seconds.

Then i tried this:

if(false)

{

  int nbr = atomicAdd(adrr, 1);

  /*

   other part of the function, not interesting here

  */

}

And here i run my code in 200ms.

I know that the problem comes from the atomicAdd because i commented every single line of the block, and without it i don’t have any problem.

I check it out and i’m sure that “condition” is false and that the function doesn’t go through the “if” block.

Does someone know where does the problem come from ? :argh:

Why, even if i don’t call the atomicAdd in the first case, i have this long duration :omg: ?

Thank you very much, i’m a little bit lost right know…

What happens to the execution time if you remove the atomicAdd from both variants? An if-statement with a condition that is always false at run time is not equivalent to an if-statement with a condition that is known false at compile time, independent of the HW architecture. While they are functionally equivalent, there are performance implications. An if-statement with a condition that is known to be false at compile time can be removed by the compiler as “dead code”. This then may allow additional optimizations (for example, it may significantly lengthen a basic block my merging straight-line code preceeding and following the if-statement). If there is a “fat point” with regard to register pressure inside the if-statement, removal of the if-statement will lower register pressure, thus potentially increasing occupancy and performance.

Hello ! Thanks for your quick answer.
I tried to remove the atomicAdd and i can run my program in 200ms in both cases.
Do you think that confirms what you said?

Your observation doesn’t jibe with my hypothesis. My next hypothesis would be that the condition does not actually always evaluate to false at runtime, as you assume it does. Beyond that I have no further ideas at the moment.

I looked at the value which is stored at “addr” and at the end of my kernel execution it’s still 0, the initial value, so i’m pretty sure that the atomicAdd is never called.
I’ll see tomorrow with my internship manager, maybe he’ll have an idea.

Thank you very much for trying to help!