cancel
Showing results for
Did you mean:

# Archives Discussions

Challenger

## OpenCL optimization: removing conditional assignments

###### Here is a quote from the AMD OpenCL optimization guide:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

## Remove Conditional Assignments

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;

becomes

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.

1 Solution
Staff

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.

9 Replies
Exemplar

It means that if there is read/write to global or local memory.

Challenger

I see, thanks!

Challenger

I am still confused, can you please give me a simple example of a case where

an if statement cannot be elminated ?

Exemplar

//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;

Challenger

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.

Challenger

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.

Challenger

Also, why can't the compiler deal with this? i.e. eliminating branches.

Staff

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.

Challenger

OK, thanks!  I think this should be clarified in the document, as current text is confusing.