cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

mihzaha
Journeyman III

improbable if statement cost

I have something like this:

 

while(some long loop)

{

if(something very improbable (0.001%) )

{

continue;

}

}

 

what exactly happens there? Will the if ever converge, or will I have a single thread running alone after entering that if, making everything 2x slower in the workgroup until the end of the kernel?

 

and another off topic question:

is this correct for replacing an if? : b=(a+b!=0)*b/(a+b);

I want to result to be 0 when both are 0, otherwise b/a+b

(if the sum of a and b is 0,  a+b!=0 returns 0, 0*anything is 0, otherwise, the comparison returns 1?)

 

[edit]

and what about a simple if:

if(something improbable ) do something small

else do something else

is that costly?

 

Thank you again for the great help

0 Likes
2 Replies

mihzaha,
If threads within a wavefront diverge on flow control, all branches of the flow control are executed by every thread in the wavefront that a single thread traverses. The threads that are not supposed to execute the branch have their output suppressed, but the computation still occurs.
0 Likes

Thank you, so the if statement costs as much as executing both branches (if the threads diverge) + cost of evaluating the expressions inside the condition of the if, and no other significant additional cost.

 

Mihai Zaharescu

0 Likes