Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


changing code on its roots


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 lomateron   Members   -  Reputation: 363

Like
1Likes
Like

Posted 13 June 2013 - 05:39 PM

I have this code

 

float Code(float a,float b, int c)

{

    float r=0;

 

      if( c==0)

      {

           r=a*b;

      }

     else if( c==1)

     {

           r=a/b;

     }

     else if( c==2)

     {

          r=2*a;

     }

 

     return r;

}

 

 

 

Lets say i don't have 3 different "if" but a thousand different "if", and "c" as it is in the code just selects one "if" 

 

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code

 

Or is there anything related to this that solves the problem in a different way?


Edited by lomateron, 13 June 2013 - 08:30 PM.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 9066

Like
2Likes
Like

Posted 13 June 2013 - 05:49 PM

A switch statement? It'll create a jump table to efficiently map code to execute to the value of c.

switch(c)
{
  case 0: r = a * b; break;
  case 1: r = a / b; break;
  case 2: r = 2 * a; break;
  // ...
}

Or you can directly return if you have nothing to after the conditional.

 

EDIT: damn, missed the HLSL tag, I don't think it has switch case support. In this case, does the value of c stay the same for every shader invocation? If so, you might be able to have the compiler optimize it down. Otherwise, if it changes dynamically, I'm afraid you're going to have to do the comparisons in any case.. are you looking for syntactic sugar or an actual performance improvement? GPU's aren't that good at control flow so any "clever" hacks to minimize the number of comparisons might yield even worse performance. If there's any way you can weave "c" directly into the formula that would be ideal of course, but it doesn't seem possible here.


Edited by Bacterius, 13 June 2013 - 05:54 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 lomateron   Members   -  Reputation: 363

Like
1Likes
Like

Posted 13 June 2013 - 06:05 PM

HLSL supports switches and in my case the "if"s will translate the same way a switch will do to assembly code

don't derail my real question

 

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs



#4 Juliean   GDNet+   -  Reputation: 2693

Like
2Likes
Like

Posted 13 June 2013 - 06:26 PM


Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs

 

Maybe shader permuation with compile defines?

#ifdef SOMETHING
r = a*b;
#else ifdef SOMETHINGELSE
r = a/b;
#else
r = 2*a;
#endif

Pardon me if I got the actual defines wrong. You can now employ a system, which is called "shader permumations", which will generate for each shader a set of possible compile-define combinations (in that case, a maximum of three). I don't know if this is too complicated/sofisticated for you, you might can up with an easier system based on that, but thats AFAIK the only way to fully eliminate the if-statements.


Edited by Juliean, 13 June 2013 - 06:26 PM.


#5 Adam_42   Crossbones+   -  Reputation: 2568

Like
2Likes
Like

Posted 13 June 2013 - 06:29 PM

You can do it with somewhat less code modification like this, but you'll still need to recompile modified source for each shader:

#define c 42
 
float Code(float a,float b)
{
    // original logic stays here
}


#6 lomateron   Members   -  Reputation: 363

Like
2Likes
Like

Posted 13 June 2013 - 06:46 PM

this solves what I was asking



#7 MJP   Moderators   -  Reputation: 11585

Like
1Likes
Like

Posted 13 June 2013 - 07:27 PM

Dynamic linking can definitely be used to implement this, although it's a little wacky to use and will often generate sub-optimal code. Personally I would just do this by pre-compiling several permutations of the shader, with the value of c defined in a a preprocessor macro (similar to what Adam_42 suggests). Doing it this way allows the compiler to completely optimize away the if statement, and also any additional operations performed with the value of c. You can specify the macro definition when compiling the shader using the "pDefines" parameter of D3DCompile, and then just compile the shaders in a for loop.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS