GLJeff
GLJeff

Reputation: 147

Does C++ have a way to do Cuda style kernel templates, where parameters produce separate compilations?

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! ;)

Upvotes: 0

Views: 342

Answers (1)

Bill Lynch
Bill Lynch

Reputation: 81936

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>();
}

Upvotes: 2

Related Questions