From 8be95fd156e8e756fc1aa70858ca03c4f0b43e86 Mon Sep 17 00:00:00 2001 From: Sriram Katta Date: Fri, 6 Feb 2026 16:16:29 +0100 Subject: [PATCH 1/2] refactor register_kernel to use type traits for integer and floating point types --- examples/examples_common.hpp | 124 +++++++++++++++++------------------ 1 file changed, 60 insertions(+), 64 deletions(-) diff --git a/examples/examples_common.hpp b/examples/examples_common.hpp index ceef0ec..d3b5668 100644 --- a/examples/examples_common.hpp +++ b/examples/examples_common.hpp @@ -168,74 +168,70 @@ Only type supported by this helper function at this time: - int, long int, long long int, unsigned long long int - float, double */ -// TODO: Clean the mess up! +template +constexpr bool always_false_v = false; + +template +struct smax_int_type{ + static_assert(always_false_v, "Unsupported integer type"); +}; + +template <> +struct smax_int_type { + static constexpr SMAX::IntType value = SMAX::IntType::INT32; +}; + +template <> +struct smax_int_type { + static constexpr SMAX::IntType value = SMAX::IntType::UINT32; +}; + +template <> +struct smax_int_type { + static constexpr SMAX::IntType value = SMAX::IntType::INT64; +}; + +template <> +struct smax_int_type { + static constexpr SMAX::IntType value = SMAX::IntType::INT64; +}; + +template <> +struct smax_int_type { + static constexpr SMAX::IntType value = SMAX::IntType::UINT64; +}; + +template +struct smax_float_type{ + static_assert(always_false_v, "Unsupported integer type"); +}; + +template <> +struct smax_float_type { + static constexpr SMAX::FloatType value = SMAX::FloatType::FLOAT32; +}; + +template <> +struct smax_float_type { + static constexpr SMAX::FloatType value = SMAX::FloatType::FLOAT64; +}; + + template void register_kernel(SMAX::Interface *smax, std::string kernel_name, SMAX::KernelType KernelType, SMAX::PlatformType PlatformType) { - if constexpr (std::is_same_v) { - if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::INT32, - SMAX::FloatType::FLOAT32); - } else if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::INT32, - SMAX::FloatType::FLOAT64); - } else { - std::cout << "VT not recognized" << std::endl; - } - } else if constexpr (std::is_same_v) { - if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::INT64, - SMAX::FloatType::FLOAT32); - } else if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::INT64, - SMAX::FloatType::FLOAT64); - } else { - std::cout << "VT not recognized" << std::endl; - } - } else if constexpr (std::is_same_v) { - if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::UINT32, - SMAX::FloatType::FLOAT32); - } else if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::UINT32, - SMAX::FloatType::FLOAT64); - } else { - std::cout << "VT not recognized" << std::endl; - } - } else if constexpr (std::is_same_v) { - if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::INT64, - SMAX::FloatType::FLOAT32); - } else if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::INT64, - SMAX::FloatType::FLOAT64); - } else { - std::cout << "VT not recognized" << std::endl; - } - } else if constexpr (std::is_same_v) { - if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::UINT64, - SMAX::FloatType::FLOAT32); - } else if constexpr (std::is_same_v) { - smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, - SMAX::IntType::UINT64, - SMAX::FloatType::FLOAT64); - } else { - std::cout << "VT not recognized" << std::endl; - } - } else { - std::cout << "IT not recognized" << std::endl; - } + static_assert(std::is_integral_v, "IT must be an integral type"); + static_assert(std::is_floating_point_v, "VT must be a floating point type"); + + constexpr SMAX::IntType itype = smax_int_type::value; + constexpr SMAX::FloatType ftype = smax_float_type::value; + + smax->register_kernel(kernel_name.c_str(), + KernelType, + PlatformType, + itype, + ftype); }; double compute_euclid_dist(const ULL n_rows, const double *y_SMAX, From f0263f6e222d38655d8cd737bb4b0aa9579ad534 Mon Sep 17 00:00:00 2001 From: Sriram Katta Date: Fri, 6 Feb 2026 16:17:54 +0100 Subject: [PATCH 2/2] refactor: simplify template struct definitions and improve formatting in examples_common.hpp --- examples/examples_common.hpp | 43 +++++++++++++----------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/examples/examples_common.hpp b/examples/examples_common.hpp index d3b5668..a23b5f7 100644 --- a/examples/examples_common.hpp +++ b/examples/examples_common.hpp @@ -168,69 +168,56 @@ Only type supported by this helper function at this time: - int, long int, long long int, unsigned long long int - float, double */ -template -constexpr bool always_false_v = false; +template constexpr bool always_false_v = false; -template -struct smax_int_type{ +template struct smax_int_type { static_assert(always_false_v, "Unsupported integer type"); }; -template <> -struct smax_int_type { +template <> struct smax_int_type { static constexpr SMAX::IntType value = SMAX::IntType::INT32; }; -template <> -struct smax_int_type { +template <> struct smax_int_type { static constexpr SMAX::IntType value = SMAX::IntType::UINT32; }; -template <> -struct smax_int_type { +template <> struct smax_int_type { static constexpr SMAX::IntType value = SMAX::IntType::INT64; }; -template <> -struct smax_int_type { +template <> struct smax_int_type { static constexpr SMAX::IntType value = SMAX::IntType::INT64; }; -template <> -struct smax_int_type { +template <> struct smax_int_type { static constexpr SMAX::IntType value = SMAX::IntType::UINT64; }; -template -struct smax_float_type{ +template struct smax_float_type { static_assert(always_false_v, "Unsupported integer type"); }; -template <> -struct smax_float_type { +template <> struct smax_float_type { static constexpr SMAX::FloatType value = SMAX::FloatType::FLOAT32; }; -template <> -struct smax_float_type { +template <> struct smax_float_type { static constexpr SMAX::FloatType value = SMAX::FloatType::FLOAT64; }; - template void register_kernel(SMAX::Interface *smax, std::string kernel_name, SMAX::KernelType KernelType, SMAX::PlatformType PlatformType) { - static_assert(std::is_integral_v, "IT must be an integral type"); - static_assert(std::is_floating_point_v, "VT must be a floating point type"); + static_assert(std::is_integral_v, "IT must be an integral type"); + static_assert(std::is_floating_point_v, + "VT must be a floating point type"); - constexpr SMAX::IntType itype = smax_int_type::value; + constexpr SMAX::IntType itype = smax_int_type::value; constexpr SMAX::FloatType ftype = smax_float_type::value; - smax->register_kernel(kernel_name.c_str(), - KernelType, - PlatformType, - itype, + smax->register_kernel(kernel_name.c_str(), KernelType, PlatformType, itype, ftype); };