cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

boxerab
Challenger

OpenCL optimization: removing conditional assignments

Here is a quote from the AMD OpenCL optimization guide:

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

Optimizing Kernels for Southern Island GPUs

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.

Can someone please explain?

0 Likes
1 Solution
dipak
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.

View solution in original post

0 Likes
9 Replies
nou
Exemplar

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

0 Likes

I see, thanks!

0 Likes

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

an if statement cannot be elminated ?

0 Likes

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

0 Likes

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.

0 Likes

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.

0 Likes
boxerab
Challenger

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

0 Likes
dipak
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.

0 Likes

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

0 Likes