Run-time choosing of template specializations using compile-time lookup-tables (LUT). Simply put: compile all possible states of a template function, but execute the optimal one at run-time.
Heavy functions can have many branching scenarios inside loops. Some branches may be dependent on conditions that do not change during execution of the function. Branching prediction works good on CPUs, but in any case, conditions and jumping takes time. Moreover, GPUs do not use branching prediction.
Therefore, the safest thing is that the non-used parts of the code are not even there -- that the compiler deletes them. This is possible by introducing compile-time conditions, if the conditions are immutable during the function execution.
- User defines number of states that each condition (template parameter) has.
- Compiler compiles all possible specializations of the function, and stores the function pointers in a lookup table (LUT).
- When user wants to execute the function with run-time parameters, the optimal one is executed.
Requirements: C++17 compiler.
Test: Run CMake or open the project in an IDE.
Usage: Copy specialut.hpp into your project and include it.
Example:
Include the library:
#include "specialut.hpp"Make a struct with a static function named run:
struct Test
{
template<bool condition, int state>
static void run(double some_param) {
while (loop_condition) {
if constexpr (condition) {
/* ... */
} else {
/* ... */
}
if constexpr (state == SOME_ENUM) {
/* ... */
}
}
}
};Make an instance of Chooser class, with template parameters that specify: the struct containing a template run function, and number of states for each template parameter:
SpeciaLUT::Chooser<Test, 2, 3> test;Find the optimal function based on the immutable run-time conditions (first brackets), pass other parameters and execute the function (second brackets).
test(runtime_bool, int_state)(double_parameter);- CUDA functions
- HIP functions