0

In Cuda you can specify template parameters that are used to automatically create completely different versions of kernels. The catch is that you can only pass const values to the functions so that the compiler knows ahead of time exactly which versions of the kernel need to be created. For instance, you can have a template parameter int X, then use an if(X==4){this}else{that} and you'll get two separate functions created, neither of which have the overhead of the 'if' statement.

I've found this to be invaluable in allowing great flexibility and code re-usability without sacrificing performance.

Bonus points if you can point out that branches don't have that much overhead, I never knew that! ;)

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
GLJeff
  • 139
  • 10
  • You mean like [this](https://stackoverflow.com/a/6179580/681865)? – talonmies Oct 15 '20 at 02:56
  • It's not clear what you mean here. Templates in C++ *always* produce "separate compilations". When a template is instantiated, the compiler effectively compiles the existing code with those template arguments substituted in, resulting in a different entity from any other instantiation. `vector` is as different a type from `vector` as `int` is from `float`. – Nicol Bolas Oct 15 '20 at 03:25
  • Or [this](https://stackoverflow.com/q/35562342/681865)? – talonmies Oct 15 '20 at 03:30
  • 1
    The reason CUDA (C++) has this capability is *because* C++ has this capability, not the other way around. CUDA (C++) attempts to be a mostly complete implementation of C++ with various restrictions and additions. You don't need C++11 or anything else special. – Robert Crovella Oct 15 '20 at 04:37
  • Does this answer your question? [Should I unify two similar kernels with an 'if' statement, risking performance loss?](https://stackoverflow.com/questions/6179295/should-i-unify-two-similar-kernels-with-an-if-statement-risking-performance-l) – gotqn Nov 05 '20 at 06:20

1 Answers1

2

Something like this?

#include <iostream>

template <int x>
void function() {
  if constexpr (x == 1) {
    std::cout << "hello\n";
  } else {
    std::cout << "world\n";
  }
}

int main() {
  function<3>();
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • 1
    You don't even need the `if constexpr` bit; most compilers will optimize out obvious conditions like that as a matter of course. – Nicol Bolas Oct 15 '20 at 03:30