From a1ca46dab058dec916e8c347091f3937c9bf1ba8 Mon Sep 17 00:00:00 2001 From: Peter-Pis Date: Tue, 10 Feb 2026 03:36:26 +0100 Subject: [PATCH 1/3] Add tuning of compiler options --- .../Api/Configuration/KernelConfiguration.cpp | 29 ++++++++++++++++++- .../Api/Configuration/KernelConfiguration.h | 7 +++++ Source/Api/Configuration/ParameterPair.cpp | 10 +++++-- Source/Api/Configuration/ParameterPair.h | 10 ++++++- Source/ComputeEngine/Cuda/CudaEngine.cpp | 2 ++ Source/ComputeEngine/EngineConfiguration.cpp | 13 ++++++--- Source/ComputeEngine/EngineConfiguration.h | 8 +++-- Source/ComputeEngine/KernelComputeData.cpp | 8 ++++- Source/ComputeEngine/KernelComputeData.h | 2 ++ Source/ComputeEngine/OpenCl/OpenClEngine.cpp | 7 ++++- Source/ComputeEngine/Vulkan/VulkanEngine.cpp | 2 ++ Source/Kernel/Kernel.cpp | 2 +- Source/Kernel/KernelManager.cpp | 5 ++-- Source/Kernel/KernelManager.h | 3 +- Source/Kernel/KernelParameter.cpp | 16 +++++++--- Source/Kernel/KernelParameter.h | 5 +++- Source/KernelRunner/KernelRunner.cpp | 1 + Source/Tuner.cpp | 24 +++++++++++++-- Source/Tuner.h | 19 +++++++++++- Source/Tuner.inl | 3 +- Source/TunerCore.cpp | 5 ++-- Source/TunerCore.h | 3 +- 22 files changed, 155 insertions(+), 29 deletions(-) diff --git a/Source/Api/Configuration/KernelConfiguration.cpp b/Source/Api/Configuration/KernelConfiguration.cpp index 11d22660..ebc0da99 100644 --- a/Source/Api/Configuration/KernelConfiguration.cpp +++ b/Source/Api/Configuration/KernelConfiguration.cpp @@ -28,7 +28,34 @@ std::string KernelConfiguration::GeneratePrefix() const for (const auto& pair : m_Pairs) { - result += std::string("#define ") + pair.GetString() + std::string("\n"); + if (!pair.IsCompilerParameter()) + { + result += std::string("#define ") + pair.GetString() + std::string("\n"); + } + } + + return result; +} + +std::string KernelConfiguration::GetCompilerOptions() const +{ + std::string result; + + for (const auto& pair : m_Pairs) + { + if (!pair.IsCompilerParameter()) + { + continue; + } + + if (pair.GetValueType() == ParameterValueType::String) + { + result += " " + pair.GetName() + "=" + pair.GetValueString(); + } + else if (pair.GetValueType() == ParameterValueType::Bool && std::get(pair.GetValue())) + { + result += " " + pair.GetName(); + } } return result; diff --git a/Source/Api/Configuration/KernelConfiguration.h b/Source/Api/Configuration/KernelConfiguration.h index 901e69ea..26db7ae6 100644 --- a/Source/Api/Configuration/KernelConfiguration.h +++ b/Source/Api/Configuration/KernelConfiguration.h @@ -43,10 +43,17 @@ class KTT_API KernelConfiguration /** @fn std::string GeneratePrefix() const * Generates kernel source preprocessor definitions from configuration. + * Parameters where IsCompilerParameter() is true are excluded. * @return Kernel source preprocessor definitions. */ std::string GeneratePrefix() const; + /** @fn std::string GetCompilerOptions() const + * Returns compiler options for parameters where IsCompilerParameter() is true. + * @return Compiler options for current configuration. + */ + std::string GetCompilerOptions() const; + /** @fn std::string GetString() const * Converts configuration to string. * @return String in format "parameter1String, parameter2String, ...". diff --git a/Source/Api/Configuration/ParameterPair.cpp b/Source/Api/Configuration/ParameterPair.cpp index 672015ee..2dcd4b03 100644 --- a/Source/Api/Configuration/ParameterPair.cpp +++ b/Source/Api/Configuration/ParameterPair.cpp @@ -11,9 +11,10 @@ ParameterPair::ParameterPair() : m_Value(static_cast(0)) {} -ParameterPair::ParameterPair(const std::string& name, const ParameterValue& value) : +ParameterPair::ParameterPair(const std::string& name, const ParameterValue& value, const bool isCompilerParameter) : m_Name(name), - m_Value(value) + m_Value(value), + m_IsCompilerParameter(isCompilerParameter) {} void ParameterPair::SetValue(const ParameterValue& value) @@ -72,6 +73,11 @@ ParameterValueType ParameterPair::GetValueType() const return GetTypeFromValue(m_Value); } +bool ParameterPair::IsCompilerParameter() const +{ + return m_IsCompilerParameter; +} + bool ParameterPair::HasSameValue(const ParameterPair& other) const { if (GetValueType() != other.GetValueType()) diff --git a/Source/Api/Configuration/ParameterPair.h b/Source/Api/Configuration/ParameterPair.h index da46b708..d1b92386 100644 --- a/Source/Api/Configuration/ParameterPair.h +++ b/Source/Api/Configuration/ParameterPair.h @@ -30,8 +30,9 @@ class KTT_API ParameterPair * Constructor which creates a parameter pair with the specified value. * @param name Name of a kernel parameter tied to the pair. * @param value Value of a parameter. + * @param isCompilerParameter If true, this paramater is a compiler option rather than in the program prefix. */ - explicit ParameterPair(const std::string& name, const ParameterValue& value); + explicit ParameterPair(const std::string& name, const ParameterValue& value, const bool isCompilerParameter = false); /** @fn void SetValue(const ParameterValue& value) * Setter for parameter value. @@ -75,6 +76,12 @@ class KTT_API ParameterPair */ ParameterValueType GetValueType() const; + /** @fn bool IsCompilerParameter() const + * Returns whether this parameter is a compiler parameter + * @return True if parameter is a compiler parameter, false otherwise. + */ + bool IsCompilerParameter() const; + /** @fn bool HasSameValue(const ParameterPair& other) const * Checks if parameter value is same as other parameter value. * @param other Source for other value. @@ -110,6 +117,7 @@ class KTT_API ParameterPair private: std::string m_Name; ParameterValue m_Value; + bool m_IsCompilerParameter; }; } // namespace ktt diff --git a/Source/ComputeEngine/Cuda/CudaEngine.cpp b/Source/ComputeEngine/Cuda/CudaEngine.cpp index 6253be3b..534c208a 100644 --- a/Source/ComputeEngine/Cuda/CudaEngine.cpp +++ b/Source/ComputeEngine/Cuda/CudaEngine.cpp @@ -122,6 +122,8 @@ ComputeActionId CudaEngine::RunKernelAsync(const KernelComputeData& data, const ExceptionReason::DeviceLimitsExceeded); } + m_Configuration.SetTuningCompilerOptions(data.GetCompilerOptions()); + Timer timer; timer.Start(); diff --git a/Source/ComputeEngine/EngineConfiguration.cpp b/Source/ComputeEngine/EngineConfiguration.cpp index c103b095..16505c06 100644 --- a/Source/ComputeEngine/EngineConfiguration.cpp +++ b/Source/ComputeEngine/EngineConfiguration.cpp @@ -13,9 +13,14 @@ EngineConfiguration::EngineConfiguration(const GlobalSizeType sizeType) : m_ProfilingFlag(false) {} -void EngineConfiguration::SetCompilerOptions(const std::string& options) +void EngineConfiguration::SetStaticCompilerOptions(const std::string& options) { - m_CompilerOptions = options; + m_StaticCompilerOptions = options; +} + +void EngineConfiguration::SetTuningCompilerOptions(const std::string& options) +{ + m_TuningCompilerOptions = options; } void EngineConfiguration::SetGlobalSizeType(const GlobalSizeType sizeType) @@ -28,9 +33,9 @@ void EngineConfiguration::SetGlobalSizeCorrection(const bool sizeCorrection) m_GlobalSizeCorrection = sizeCorrection; } -const std::string& EngineConfiguration::GetCompilerOptions() const +std::string EngineConfiguration::GetCompilerOptions() const { - return m_CompilerOptions; + return m_StaticCompilerOptions + m_TuningCompilerOptions; } GlobalSizeType EngineConfiguration::GetGlobalSizeType() const diff --git a/Source/ComputeEngine/EngineConfiguration.h b/Source/ComputeEngine/EngineConfiguration.h index 4b6e352a..842a171a 100644 --- a/Source/ComputeEngine/EngineConfiguration.h +++ b/Source/ComputeEngine/EngineConfiguration.h @@ -13,11 +13,12 @@ class EngineConfiguration EngineConfiguration(); explicit EngineConfiguration(const GlobalSizeType sizeType); - void SetCompilerOptions(const std::string& options); + void SetStaticCompilerOptions(const std::string& options); + void SetTuningCompilerOptions(const std::string& options); void SetGlobalSizeType(const GlobalSizeType sizeType); void SetGlobalSizeCorrection(const bool sizeCorrection); - const std::string& GetCompilerOptions() const; + std::string GetCompilerOptions() const; GlobalSizeType GetGlobalSizeType() const; bool GetGlobalSizeCorrection() const; @@ -25,7 +26,8 @@ class EngineConfiguration void SetProfiling(const bool profiling); private: - std::string m_CompilerOptions; + std::string m_StaticCompilerOptions; + std::string m_TuningCompilerOptions; GlobalSizeType m_GlobalSizeType; bool m_GlobalSizeCorrection; bool m_ProfilingFlag; diff --git a/Source/ComputeEngine/KernelComputeData.cpp b/Source/ComputeEngine/KernelComputeData.cpp index 66bd91e9..b21d5184 100644 --- a/Source/ComputeEngine/KernelComputeData.cpp +++ b/Source/ComputeEngine/KernelComputeData.cpp @@ -19,7 +19,8 @@ KernelComputeData::KernelComputeData(const Kernel& kernel, const KernelDefinitio m_ConfigurationPrefix(configuration.GeneratePrefix()), m_TemplatedName(definition.GetTemplatedName()), m_Configuration(&configuration), - m_Arguments(definition.GetArguments()) + m_Arguments(definition.GetArguments()), + m_CompilerOptions(configuration.GetCompilerOptions()) { const auto id = definition.GetId(); const auto& pairs = configuration.GetPairs(); @@ -156,4 +157,9 @@ const std::vector& KernelComputeData::GetArguments() const return m_Arguments; } +const std::string& KernelComputeData::GetCompilerOptions() const +{ + return m_CompilerOptions; +} + } // namespace ktt diff --git a/Source/ComputeEngine/KernelComputeData.h b/Source/ComputeEngine/KernelComputeData.h index 66b11f7c..b09af15d 100644 --- a/Source/ComputeEngine/KernelComputeData.h +++ b/Source/ComputeEngine/KernelComputeData.h @@ -37,6 +37,7 @@ class KernelComputeData const KernelConfiguration& GetConfiguration() const; size_t GetArgumentIndex(const ArgumentId& id) const; const std::vector& GetArguments() const; + const std::string& GetCompilerOptions() const; private: std::string m_Name; @@ -47,6 +48,7 @@ class KernelComputeData DimensionVector m_LocalSize; const KernelConfiguration* m_Configuration; std::vector m_Arguments; + std::string m_CompilerOptions; }; } // namespace ktt diff --git a/Source/ComputeEngine/OpenCl/OpenClEngine.cpp b/Source/ComputeEngine/OpenCl/OpenClEngine.cpp index 947fb101..99bdc7c2 100644 --- a/Source/ComputeEngine/OpenCl/OpenClEngine.cpp +++ b/Source/ComputeEngine/OpenCl/OpenClEngine.cpp @@ -120,6 +120,8 @@ ComputeActionId OpenClEngine::RunKernelAsync(const KernelComputeData& data, cons ExceptionReason::DeviceLimitsExceeded); } + m_Configuration.SetTuningCompilerOptions(data.GetCompilerOptions()); + Timer timer; timer.Start(); @@ -650,7 +652,7 @@ GlobalSizeType OpenClEngine::GetGlobalSizeType() const void OpenClEngine::SetCompilerOptions(const std::string& options, [[maybe_unused]] const bool overrideDefault) { - m_Configuration.SetCompilerOptions(options); + m_Configuration.SetStaticCompilerOptions(options); ClearKernelCache(); } @@ -687,6 +689,9 @@ std::shared_ptr OpenClEngine::LoadKernel(const KernelComputeData& } auto program = std::make_unique(*m_Context, data.GetSource()); + // TODO remove + Logger::LogInfo( "|" + data.GetCompilerOptions() + "|" ); + Logger::LogInfo( "|" + m_Configuration.GetCompilerOptions() + "|" ); program->Build(m_Configuration.GetCompilerOptions()); auto kernel = std::make_shared(std::move(program), data.GetName(), m_ComputeIdGenerator, m_Configuration); diff --git a/Source/ComputeEngine/Vulkan/VulkanEngine.cpp b/Source/ComputeEngine/Vulkan/VulkanEngine.cpp index 77e687d1..5cbc7a3b 100644 --- a/Source/ComputeEngine/Vulkan/VulkanEngine.cpp +++ b/Source/ComputeEngine/Vulkan/VulkanEngine.cpp @@ -67,6 +67,8 @@ ComputeActionId VulkanEngine::RunKernelAsync(const KernelComputeData& data, cons ExceptionReason::DeviceLimitsExceeded); } + m_Configuration.SetTuningCompilerOptions(data.GetCompilerOptions()); + Timer timer; timer.Start(); diff --git a/Source/Kernel/Kernel.cpp b/Source/Kernel/Kernel.cpp index 27a6188f..b49607c3 100644 --- a/Source/Kernel/Kernel.cpp +++ b/Source/Kernel/Kernel.cpp @@ -216,7 +216,7 @@ KernelConfiguration Kernel::CreateConfiguration(const ParameterInput& parameters throw KttException("Value type mismatch for parameter with name " + pair.first); } - pairs.emplace_back(parameter.GetName(), pair.second); + pairs.emplace_back(parameter.GetName(), pair.second, parameter.IsCompilerParameter()); } for (const auto& parameter : m_Parameters) diff --git a/Source/Kernel/KernelManager.cpp b/Source/Kernel/KernelManager.cpp index 970ab2fc..065bff0b 100644 --- a/Source/Kernel/KernelManager.cpp +++ b/Source/Kernel/KernelManager.cpp @@ -93,10 +93,11 @@ void KernelManager::RemoveKernel(const KernelId id) } } -void KernelManager::AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group) +void KernelManager::AddParameter(const KernelId id, const std::string& name, const std::vector& values, + const std::string& group, const bool isCompilerParameter) { auto& kernel = GetKernel(id); - kernel.AddParameter(KernelParameter(name, values, group)); + kernel.AddParameter(KernelParameter(name, values, group, isCompilerParameter)); } void KernelManager::AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, diff --git a/Source/Kernel/KernelManager.h b/Source/Kernel/KernelManager.h index 4ee72bc2..1daa806a 100644 --- a/Source/Kernel/KernelManager.h +++ b/Source/Kernel/KernelManager.h @@ -28,7 +28,8 @@ class KernelManager KernelId CreateKernel(const std::string& name, const std::vector& definitionIds); void RemoveKernel(const KernelId id); - void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group); + void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group, + const bool isCompilerParameter); void AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, const std::string& group); void AddConstraint(const KernelId id, const std::vector& parameters, ConstraintFunction function); diff --git a/Source/Kernel/KernelParameter.cpp b/Source/Kernel/KernelParameter.cpp index 706180dd..55cf22e6 100644 --- a/Source/Kernel/KernelParameter.cpp +++ b/Source/Kernel/KernelParameter.cpp @@ -13,10 +13,12 @@ namespace ktt static const std::string DefaultGroup = "KTTDefaultGroup"; -KernelParameter::KernelParameter(const std::string& name, const std::vector& values, const std::string& group) : +KernelParameter::KernelParameter(const std::string& name, const std::vector& values, const std::string& group, + const bool isCompilerParameter) : m_Name(name), m_Group(group), - m_Values(values) + m_Values(values), + m_IsCompilerParameter(isCompilerParameter) { if (values.empty()) { @@ -31,7 +33,8 @@ KernelParameter::KernelParameter(const std::string& name, const std::vector KernelParameter::GeneratePairs() const @@ -81,6 +84,11 @@ std::vector KernelParameter::GeneratePairs() const return result; } +bool KernelParameter::IsCompilerParameter() const +{ + return m_IsCompilerParameter; +} + bool KernelParameter::operator==(const KernelParameter& other) const { return m_Name == other.m_Name; diff --git a/Source/Kernel/KernelParameter.h b/Source/Kernel/KernelParameter.h index 49d6e54c..27f71f57 100644 --- a/Source/Kernel/KernelParameter.h +++ b/Source/Kernel/KernelParameter.h @@ -14,7 +14,8 @@ namespace ktt class KernelParameter { public: - explicit KernelParameter(const std::string& name, const std::vector& values, const std::string& group); + explicit KernelParameter(const std::string& name, const std::vector& values, const std::string& group, + const bool isCompilerParameter); explicit KernelParameter(const std::string& name, const ParameterValueType valueType, const std::string& valueScript, const std::string& group); @@ -25,6 +26,7 @@ class KernelParameter ParameterValueType GetValueType() const; ParameterPair GeneratePair(const size_t valueIndex) const; std::vector GeneratePairs() const; + bool IsCompilerParameter() const; bool operator==(const KernelParameter& other) const; bool operator!=(const KernelParameter& other) const; @@ -34,6 +36,7 @@ class KernelParameter std::string m_Name; std::string m_Group; std::vector m_Values; + bool m_IsCompilerParameter; static std::vector GetValuesFromScript(const ParameterValueType valueType, const std::string& valueScript); }; diff --git a/Source/KernelRunner/KernelRunner.cpp b/Source/KernelRunner/KernelRunner.cpp index f6c8bfde..5f7ad9e3 100644 --- a/Source/KernelRunner/KernelRunner.cpp +++ b/Source/KernelRunner/KernelRunner.cpp @@ -321,6 +321,7 @@ void KernelRunner::ValidateResult(const Kernel& kernel, KernelResult& result, co { Logger::LogInfo("Kernel run completed successfully in " + std::to_string(duration) + tag + ", kernel duration was " + std::to_string(kernelDuration) + tag); + Logger::LogInfo("\n"); //TODO remove return; } diff --git a/Source/Tuner.cpp b/Source/Tuner.cpp index 1ca8be92..e721fb72 100644 --- a/Source/Tuner.cpp +++ b/Source/Tuner.cpp @@ -1024,12 +1024,32 @@ ArgumentId Tuner::AddUserArgument(ComputeBuffer buffer, const size_t elementSize } } -void Tuner::AddParameterInternal(const KernelId id, const std::string& name, const std::vector& values, +void Tuner::AddCompilerParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group) +{ + std::vector parameterValues; + + for (const auto& value : values) + { + parameterValues.push_back(value); + } + + if (parameterValues.empty()) + { + parameterValues.push_back(true); + parameterValues.push_back(false); + } + + bool isCompilerParameter = true; + AddParameterInternal(id, name, parameterValues, group, isCompilerParameter); +} + +void Tuner::AddParameterInternal(const KernelId id, const std::string& name, const std::vector& values, + const std::string& group, const bool isCompilerParameter) { try { - m_Tuner->AddParameter(id, name, values, group); + m_Tuner->AddParameter(id, name, values, group, isCompilerParameter); } catch (const KttException& exception) { diff --git a/Source/Tuner.h b/Source/Tuner.h index 84e2c0b2..036b3a21 100644 --- a/Source/Tuner.h +++ b/Source/Tuner.h @@ -230,6 +230,23 @@ class KTT_API Tuner template void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group = ""); + // TODO: function description (@param) + /** @fn void AddParameter(const KernelId id, const std::string& name, const std::vector& values, + * const std::string& group = "") + * Adds new compiler parameter for the specified kernel, providing parameter name and optionally list of allowed values. + * Parameters will be added to the compiler as compiler options. + * During the tuning process, tuner will generate configurations for combinations of kernel parameters and their values. + * @param id Id of kernel for which the parameter will be added. + * @param name Name of a parameter. Parameter names for a single kernel must be unique. + * @param values Allowed values for the parameter. Value type is a string. If left empty, parameter with be either + * included without any value or completely excluded. + * @param group Optional group inside which the parameter will be added. Tuning configurations are generated separately for each + * group. This is useful when kernels contain groups of parameters that can be tuned independently. In this way, the total number + * of generated configurations can be significantly reduced. + */ + void AddCompilerParameter(const KernelId id, const std::string& name, const std::vector& values = {}, + const std::string& group = ""); + /** @fn void AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, * const std::string& valueScript, const std::string& group = "") * Adds new parameter for the specified kernel, providing parameter name, value type and a script which generates list of allowed @@ -1006,7 +1023,7 @@ class KTT_API Tuner const ArgumentMemoryLocation memoryLocation, const ArgumentAccessType accessType, const size_t dataSize, const ArgumentId& customId = ""); KTT_VIRTUAL_API void AddParameterInternal(const KernelId id, const std::string& name, const std::vector& values, - const std::string& group); + const std::string& group, const bool isCompilerParameter); template ArgumentDataType DeriveArgumentDataType() const; diff --git a/Source/Tuner.inl b/Source/Tuner.inl index 40d54afb..c92f843b 100644 --- a/Source/Tuner.inl +++ b/Source/Tuner.inl @@ -20,7 +20,8 @@ void Tuner::AddParameter(const KernelId id, const std::string& name, const std:: parameterValues.emplace_back(value); } - AddParameterInternal(id, name, parameterValues, group); + bool isCompilerParameter = false; + AddParameterInternal(id, name, parameterValues, group, isCompilerParameter); } template diff --git a/Source/TunerCore.cpp b/Source/TunerCore.cpp index 99c89836..91115f5b 100644 --- a/Source/TunerCore.cpp +++ b/Source/TunerCore.cpp @@ -89,9 +89,10 @@ void TunerCore::SetLauncher(const KernelId id, KernelLauncher launcher) m_KernelManager->SetLauncher(id, launcher); } -void TunerCore::AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group) +void TunerCore::AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group, + const bool isCompilerParameter) { - m_KernelManager->AddParameter(id, name, values, group); + m_KernelManager->AddParameter(id, name, values, group, isCompilerParameter); } void TunerCore::AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, diff --git a/Source/TunerCore.h b/Source/TunerCore.h index 6947cc87..3d7caec2 100644 --- a/Source/TunerCore.h +++ b/Source/TunerCore.h @@ -38,7 +38,8 @@ class TunerCore KernelId CreateKernel(const std::string& name, const std::vector& definitionIds, KernelLauncher launcher); void RemoveKernel(const KernelId id); void SetLauncher(const KernelId id, KernelLauncher launcher); - void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group); + void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group, + const bool isCompilerParameter); void AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, const std::string& group); void AddConstraint(const KernelId id, const std::vector& parameters, ConstraintFunction function); From 45b6827eb46098e30f124af271971624615d70e1 Mon Sep 17 00:00:00 2001 From: Peter-Pis Date: Tue, 10 Feb 2026 10:05:01 +0100 Subject: [PATCH 2/3] small fixes --- Source/Api/Configuration/ParameterPair.h | 2 +- Source/ComputeEngine/Cuda/CudaEngine.cpp | 2 +- Source/ComputeEngine/Vulkan/VulkanEngine.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Api/Configuration/ParameterPair.h b/Source/Api/Configuration/ParameterPair.h index d1b92386..328fd79c 100644 --- a/Source/Api/Configuration/ParameterPair.h +++ b/Source/Api/Configuration/ParameterPair.h @@ -26,7 +26,7 @@ class KTT_API ParameterPair */ ParameterPair(); - /** @fn explicit ParameterPair(const std::string& name, const ParameterValue& value) + /** @fn explicit ParameterPair(const std::string& name, const ParameterValue& value, const bool isCompilerParameter = false) * Constructor which creates a parameter pair with the specified value. * @param name Name of a kernel parameter tied to the pair. * @param value Value of a parameter. diff --git a/Source/ComputeEngine/Cuda/CudaEngine.cpp b/Source/ComputeEngine/Cuda/CudaEngine.cpp index 534c208a..d4c4090e 100644 --- a/Source/ComputeEngine/Cuda/CudaEngine.cpp +++ b/Source/ComputeEngine/Cuda/CudaEngine.cpp @@ -792,7 +792,7 @@ void CudaEngine::SetCompilerOptions(const std::string& options, const bool overr finalOptions += GetDefaultCompilerOptions(); } - m_Configuration.SetCompilerOptions(finalOptions); + m_Configuration.SetStaticCompilerOptions(finalOptions); ClearKernelCache(); } diff --git a/Source/ComputeEngine/Vulkan/VulkanEngine.cpp b/Source/ComputeEngine/Vulkan/VulkanEngine.cpp index 5cbc7a3b..ce1a65dc 100644 --- a/Source/ComputeEngine/Vulkan/VulkanEngine.cpp +++ b/Source/ComputeEngine/Vulkan/VulkanEngine.cpp @@ -419,7 +419,7 @@ GlobalSizeType VulkanEngine::GetGlobalSizeType() const void VulkanEngine::SetCompilerOptions(const std::string& options, [[maybe_unused]] const bool overrideDefault) { - m_Configuration.SetCompilerOptions(options); + m_Configuration.SetStaticCompilerOptions(options); ClearKernelCache(); } From e8ca73872a78fc6200de147863c65454a5f3e17b Mon Sep 17 00:00:00 2001 From: Peter-Pis Date: Tue, 17 Feb 2026 23:14:12 +0100 Subject: [PATCH 3/3] Add compiler parameters support for json --- .../Api/Configuration/KernelConfiguration.cpp | 4 ++-- Source/Api/Configuration/ParameterPair.cpp | 10 ++++++++ Source/Kernel/Kernel.cpp | 2 +- Source/Kernel/KernelManager.cpp | 4 ++-- Source/Kernel/KernelManager.h | 2 +- Source/Kernel/KernelParameter.cpp | 9 ++++--- Source/Kernel/KernelParameter.h | 2 +- Source/Kernel/ParameterValueType.h | 6 ++++- Source/KernelRunner/KernelRunner.cpp | 1 - Source/Output/JsonConverters.cpp | 18 ++++++++++++++ Source/Output/JsonConverters.h | 3 ++- Source/Tuner.cpp | 17 ++++++++++++- Source/Tuner.h | 24 ++++++++++++++++--- Source/TunerCore.cpp | 4 ++-- Source/TunerCore.h | 2 +- 15 files changed, 88 insertions(+), 20 deletions(-) diff --git a/Source/Api/Configuration/KernelConfiguration.cpp b/Source/Api/Configuration/KernelConfiguration.cpp index ebc0da99..401c7e13 100644 --- a/Source/Api/Configuration/KernelConfiguration.cpp +++ b/Source/Api/Configuration/KernelConfiguration.cpp @@ -48,11 +48,11 @@ std::string KernelConfiguration::GetCompilerOptions() const continue; } - if (pair.GetValueType() == ParameterValueType::String) + if (std::holds_alternative(pair.GetValue())) { result += " " + pair.GetName() + "=" + pair.GetValueString(); } - else if (pair.GetValueType() == ParameterValueType::Bool && std::get(pair.GetValue())) + else if (std::holds_alternative(pair.GetValue()) && std::get(pair.GetValue())) { result += " " + pair.GetName(); } diff --git a/Source/Api/Configuration/ParameterPair.cpp b/Source/Api/Configuration/ParameterPair.cpp index 2dcd4b03..dd3fa821 100644 --- a/Source/Api/Configuration/ParameterPair.cpp +++ b/Source/Api/Configuration/ParameterPair.cpp @@ -47,6 +47,10 @@ std::string ParameterPair::GetValueString() const return std::get(m_Value) ? "true" : "false"; case ParameterValueType::String: return std::get(m_Value); + case ParameterValueType::CompilerParameter: + if (std::holds_alternative(m_Value)) + return std::get(m_Value); + return std::get(m_Value) ? "set" : "notSet"; default: KttError("Unhandled parameter value type"); return ""; @@ -70,6 +74,10 @@ uint64_t ParameterPair::GetValueUint() const ParameterValueType ParameterPair::GetValueType() const { + if (this->IsCompilerParameter()) + { + return ParameterValueType::CompilerParameter; + } return GetTypeFromValue(m_Value); } @@ -97,6 +105,8 @@ bool ParameterPair::HasSameValue(const ParameterPair& other) const return std::get(m_Value) == std::get(other.GetValue()); case ParameterValueType::String: return GetValueString() == other.GetValueString(); + case ParameterValueType::CompilerParameter: + return GetValueString() == other.GetValueString(); default: KttError("Unhandled parameter value type"); return false; diff --git a/Source/Kernel/Kernel.cpp b/Source/Kernel/Kernel.cpp index b49607c3..1d1cf0ed 100644 --- a/Source/Kernel/Kernel.cpp +++ b/Source/Kernel/Kernel.cpp @@ -211,7 +211,7 @@ KernelConfiguration Kernel::CreateConfiguration(const ParameterInput& parameters { const auto& parameter = GetParamater(pair.first); - if (parameter.GetValueType() != ParameterPair::GetTypeFromValue(pair.second)) + if (ParameterPair::GetTypeFromValue(parameter.GetValues()[0]) != ParameterPair::GetTypeFromValue(pair.second)) { throw KttException("Value type mismatch for parameter with name " + pair.first); } diff --git a/Source/Kernel/KernelManager.cpp b/Source/Kernel/KernelManager.cpp index 065bff0b..47f3c9f6 100644 --- a/Source/Kernel/KernelManager.cpp +++ b/Source/Kernel/KernelManager.cpp @@ -101,10 +101,10 @@ void KernelManager::AddParameter(const KernelId id, const std::string& name, con } void KernelManager::AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, - const std::string& group) + const std::string& group, const bool isCompilerParameter) { auto& kernel = GetKernel(id); - kernel.AddParameter(KernelParameter(name, valueType, valueScript, group)); + kernel.AddParameter(KernelParameter(name, valueType, valueScript, group, isCompilerParameter)); } void KernelManager::AddConstraint(const KernelId id, const std::vector& parameters, ConstraintFunction function) diff --git a/Source/Kernel/KernelManager.h b/Source/Kernel/KernelManager.h index 1daa806a..835a5d77 100644 --- a/Source/Kernel/KernelManager.h +++ b/Source/Kernel/KernelManager.h @@ -31,7 +31,7 @@ class KernelManager void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group, const bool isCompilerParameter); void AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, - const std::string& group); + const std::string& group, const bool isCompilerParameter); void AddConstraint(const KernelId id, const std::vector& parameters, ConstraintFunction function); void AddGenericConstraint(const KernelId id, const std::vector& parameters, GenericConstraintFunction function); void AddScriptConstraint(const KernelId id, const std::vector& parameters, const std::string& script); diff --git a/Source/Kernel/KernelParameter.cpp b/Source/Kernel/KernelParameter.cpp index 55cf22e6..089f4f71 100644 --- a/Source/Kernel/KernelParameter.cpp +++ b/Source/Kernel/KernelParameter.cpp @@ -32,9 +32,8 @@ KernelParameter::KernelParameter(const std::string& name, const std::vector& KernelParameter::GetValues() const ParameterValueType KernelParameter::GetValueType() const { + if (this->IsCompilerParameter()) + { + return ParameterValueType::CompilerParameter; + } return ParameterPair::GetTypeFromValue(m_Values[0]); } diff --git a/Source/Kernel/KernelParameter.h b/Source/Kernel/KernelParameter.h index 27f71f57..c33284fe 100644 --- a/Source/Kernel/KernelParameter.h +++ b/Source/Kernel/KernelParameter.h @@ -17,7 +17,7 @@ class KernelParameter explicit KernelParameter(const std::string& name, const std::vector& values, const std::string& group, const bool isCompilerParameter); explicit KernelParameter(const std::string& name, const ParameterValueType valueType, const std::string& valueScript, - const std::string& group); + const std::string& group, const bool isCompilerParameter); const std::string& GetName() const; const std::string& GetGroup() const; diff --git a/Source/Kernel/ParameterValueType.h b/Source/Kernel/ParameterValueType.h index 80e95f35..b1f46528 100644 --- a/Source/Kernel/ParameterValueType.h +++ b/Source/Kernel/ParameterValueType.h @@ -33,7 +33,11 @@ enum class ParameterValueType /** Parameter has string type. */ - String + String, + + /** Parameter is a compiler parameter. + */ + CompilerParameter }; } // namespace ktt diff --git a/Source/KernelRunner/KernelRunner.cpp b/Source/KernelRunner/KernelRunner.cpp index 5f7ad9e3..f6c8bfde 100644 --- a/Source/KernelRunner/KernelRunner.cpp +++ b/Source/KernelRunner/KernelRunner.cpp @@ -321,7 +321,6 @@ void KernelRunner::ValidateResult(const Kernel& kernel, KernelResult& result, co { Logger::LogInfo("Kernel run completed successfully in " + std::to_string(duration) + tag + ", kernel duration was " + std::to_string(kernelDuration) + tag); - Logger::LogInfo("\n"); //TODO remove return; } diff --git a/Source/Output/JsonConverters.cpp b/Source/Output/JsonConverters.cpp index d04cec1f..c11a68a9 100644 --- a/Source/Output/JsonConverters.cpp +++ b/Source/Output/JsonConverters.cpp @@ -72,6 +72,9 @@ void to_json(json& j, const ParameterPair& pair) case ParameterValueType::String: j["Value"] = pair.GetValueString(); break; + case ParameterValueType::CompilerParameter: + j["Value"] = pair.GetValueString(); + break; default: KttError("Unhandled parameter value type"); } @@ -122,6 +125,21 @@ void from_json(const json& j, ParameterPair& pair) pair = ParameterPair(name, valueString); break; } + case ParameterValueType::CompilerParameter: + { + bool isCompilerParameter = true; + std::string valueString; + j.at("Value").get_to(valueString); + if (valueString == "set" || valueString == "notSet") + { + pair = ParameterPair(name, valueString == "set", isCompilerParameter); + } + else + { + pair = ParameterPair(name, valueString, isCompilerParameter); + } + break; + } default: KttError("Unhandled parameter value type"); } diff --git a/Source/Output/JsonConverters.h b/Source/Output/JsonConverters.h index 816b140a..4b481fbf 100644 --- a/Source/Output/JsonConverters.h +++ b/Source/Output/JsonConverters.h @@ -50,7 +50,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(ParameterValueType, {ParameterValueType::Double, "Double"}, {ParameterValueType::Float, "Float"}, {ParameterValueType::Bool, "Bool"}, - {ParameterValueType::String, "String"} + {ParameterValueType::String, "String"}, + {ParameterValueType::CompilerParameter, "CompilerParameter"} }); NLOHMANN_JSON_SERIALIZE_ENUM(ProfilingCounterType, diff --git a/Source/Tuner.cpp b/Source/Tuner.cpp index e721fb72..03e3a10e 100644 --- a/Source/Tuner.cpp +++ b/Source/Tuner.cpp @@ -170,7 +170,22 @@ void Tuner::AddScriptParameter(const KernelId id, const std::string& name, const { try { - m_Tuner->AddScriptParameter(id, name, valueType, valueScript, group); + bool isCompilerParameter = false; + m_Tuner->AddScriptParameter(id, name, valueType, valueScript, group, isCompilerParameter); + } + catch (const KttException& exception) + { + TunerCore::Log(LoggingLevel::Error, exception.what()); + } +} + +void Tuner::AddScriptCompilerParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, + const std::string& valueScript, const std::string& group) +{ + try + { + bool isCompilerParameter = true; + m_Tuner->AddScriptParameter(id, name, valueType, valueScript, group, isCompilerParameter); } catch (const KttException& exception) { diff --git a/Source/Tuner.h b/Source/Tuner.h index 036b3a21..902fae9e 100644 --- a/Source/Tuner.h +++ b/Source/Tuner.h @@ -230,15 +230,15 @@ class KTT_API Tuner template void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group = ""); - // TODO: function description (@param) - /** @fn void AddParameter(const KernelId id, const std::string& name, const std::vector& values, + /** @fn void AddCompilerParameter(const KernelId id, const std::string& name, const std::vector& values = {}, * const std::string& group = "") + * Works exactly as AddParameter(), but for compiler parameters. * Adds new compiler parameter for the specified kernel, providing parameter name and optionally list of allowed values. * Parameters will be added to the compiler as compiler options. * During the tuning process, tuner will generate configurations for combinations of kernel parameters and their values. * @param id Id of kernel for which the parameter will be added. * @param name Name of a parameter. Parameter names for a single kernel must be unique. - * @param values Allowed values for the parameter. Value type is a string. If left empty, parameter with be either + * @param values Optional allowed values for the parameter. Value type is a string. If left empty, parameter with be either * included without any value or completely excluded. * @param group Optional group inside which the parameter will be added. Tuning configurations are generated separately for each * group. This is useful when kernels contain groups of parameters that can be tuned independently. In this way, the total number @@ -264,6 +264,24 @@ class KTT_API Tuner void AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, const std::string& group = ""); + /** @fn void AddScriptCompilerParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, + * const std::string& valueScript, const std::string& group = "") + * Works exactly as AddScriptParameter(), but for compiler parameters. + * Adds new compiler parameter for the specified kernel, providing parameter name, value type and a script which generates list of allowed + * values. Compiler parameters will be added as a compiler option. During the tuning process, tuner will + * generate configurations for combinations of kernel parameters and their values. + * @param id Id of kernel for which the parameter will be added. + * @param name Name of a parameter. Parameter names for a single kernel must be unique. + * @param valueType Type of parameter values. + * @param valueScript Python script which will be executed to generate a list of parameter values. The values of the tuning parameters + * can be utilized by the script. The default thread size can be accessed from script through variable named "defaultSize". + * @param group Optional group inside which the parameter will be added. Tuning configurations are generated separately for each + * group. This is useful when kernels contain groups of parameters that can be tuned independently. In this way, the total number + * of generated configurations can be significantly reduced. + */ + void AddScriptCompilerParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, + const std::string& group = ""); + /** @fn void AddThreadModifier(const KernelId id, const std::vector& definitionIds, const ModifierType type, * const ModifierDimension dimension, const std::vector& parameters, ModifierFunction function) * Adds thread modifier function for the specified kernel. The function receives thread size in the specified dimension and diff --git a/Source/TunerCore.cpp b/Source/TunerCore.cpp index 91115f5b..fb039d3a 100644 --- a/Source/TunerCore.cpp +++ b/Source/TunerCore.cpp @@ -96,9 +96,9 @@ void TunerCore::AddParameter(const KernelId id, const std::string& name, const s } void TunerCore::AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, - const std::string& group) + const std::string& group, const bool isCompilerParameter) { - m_KernelManager->AddScriptParameter(id, name, valueType, valueScript, group); + m_KernelManager->AddScriptParameter(id, name, valueType, valueScript, group, isCompilerParameter); } void TunerCore::AddConstraint(const KernelId id, const std::vector& parameters, ConstraintFunction function) diff --git a/Source/TunerCore.h b/Source/TunerCore.h index 3d7caec2..37d24c91 100644 --- a/Source/TunerCore.h +++ b/Source/TunerCore.h @@ -41,7 +41,7 @@ class TunerCore void AddParameter(const KernelId id, const std::string& name, const std::vector& values, const std::string& group, const bool isCompilerParameter); void AddScriptParameter(const KernelId id, const std::string& name, const ParameterValueType valueType, const std::string& valueScript, - const std::string& group); + const std::string& group, const bool isCompilerParameter); void AddConstraint(const KernelId id, const std::vector& parameters, ConstraintFunction function); void AddGenericConstraint(const KernelId id, const std::vector& parameters, GenericConstraintFunction function); void AddScriptConstraint(const KernelId id, const std::vector& parameters, const std::string& script);