///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
A conditional of the form “if-then-else” generates branching. Use the select() function to replace these structures with conditional assignments that do not cause branching. For example:
if(x==1) r=0.5;
if(x==2) r=1.0;
r = select(r, 0.5, x==1);
r = select(r, 1.0, x==2);
Note that if the body of the if statement contains an I/O, the if statement cannot be eliminated.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I am confused about the last statement:
If the body of the if statement contains an I/O, the if statement cannot be eliminated.
Can someone please explain?
Solved! Go to Solution.
Sorry for this late reply. As I have been told, here I/O means reading/writing to disk/IO devices (say, keyboard) and not memory.
It means that if there is read/write to global or local memory.
I see, thanks!
I am still confused, can you please give me a simple example of a case where
an if statement cannot be elminated ?
//this will be optimized to select instruction
if(a[gid]>10)b[gid] = 1;
else b[gid] = 0;
//full branch
if(a[gid]>10)b[gid] = 1;
else c[gid] = 0;
nou: thanks for your sample code. To clarify, I would like to see an if block that cannot be converted to a select because of I/O calls.
Let me hazard a guess:
local uint bits1[10];
local uint bits2[10];
uint temp = 0;
...
if (bits1[0] = bits2[0] == 5) {
temp = 1;
}
In this case, cannot eliminate the if, because of IO.
Also, why can't the compiler deal with this? i.e. eliminating branches.
Sorry for this late reply. As I have been told, here I/O means reading/writing to disk/IO devices (say, keyboard) and not memory.
OK, thanks! I think this should be clarified in the document, as current text is confusing.