Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Source/Api/Configuration/KernelConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (std::holds_alternative<std::string>(pair.GetValue()))
{
result += " " + pair.GetName() + "=" + pair.GetValueString();
}
else if (std::holds_alternative<bool>(pair.GetValue()) && std::get<bool>(pair.GetValue()))
{
result += " " + pair.GetName();
}
}

return result;
Expand Down
7 changes: 7 additions & 0 deletions Source/Api/Configuration/KernelConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...".
Expand Down
20 changes: 18 additions & 2 deletions Source/Api/Configuration/ParameterPair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ ParameterPair::ParameterPair() :
m_Value(static_cast<uint64_t>(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)
Expand Down Expand Up @@ -46,6 +47,10 @@ std::string ParameterPair::GetValueString() const
return std::get<bool>(m_Value) ? "true" : "false";
case ParameterValueType::String:
return std::get<std::string>(m_Value);
case ParameterValueType::CompilerParameter:
if (std::holds_alternative<std::string>(m_Value))
return std::get<std::string>(m_Value);
return std::get<bool>(m_Value) ? "set" : "notSet";
default:
KttError("Unhandled parameter value type");
return "";
Expand All @@ -69,9 +74,18 @@ uint64_t ParameterPair::GetValueUint() const

ParameterValueType ParameterPair::GetValueType() const
{
if (this->IsCompilerParameter())
{
return ParameterValueType::CompilerParameter;
}
return GetTypeFromValue(m_Value);
}

bool ParameterPair::IsCompilerParameter() const
{
return m_IsCompilerParameter;
}

bool ParameterPair::HasSameValue(const ParameterPair& other) const
{
if (GetValueType() != other.GetValueType())
Expand All @@ -91,6 +105,8 @@ bool ParameterPair::HasSameValue(const ParameterPair& other) const
return std::get<bool>(m_Value) == std::get<bool>(other.GetValue());
case ParameterValueType::String:
return GetValueString() == other.GetValueString();
case ParameterValueType::CompilerParameter:
return GetValueString() == other.GetValueString();
default:
KttError("Unhandled parameter value type");
return false;
Expand Down
12 changes: 10 additions & 2 deletions Source/Api/Configuration/ParameterPair.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ 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.
* @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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -110,6 +117,7 @@ class KTT_API ParameterPair
private:
std::string m_Name;
ParameterValue m_Value;
bool m_IsCompilerParameter;
};

} // namespace ktt
Expand Down
33 changes: 13 additions & 20 deletions Source/ComputeEngine/Cpp/CppCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,36 @@ class CppCompiler::Impl
sourceFile.close();

// Compile with configured compiler
std::string command = m_Compiler + " " + compilerOptions;

#if defined(_MSC_VER)
// MSVC compiler command
// /LD - create DLL
// /MD - use multi-threaded DLL runtime
// /std:c++17 - C++17 standard
// /EHsc - exception handling
// /Fe - output executable/library name
std::string command = m_Compiler + " /LD /MD /std:c++17 /EHsc ";
command += compilerOptions;
command += " /Fe:";
command += libraryPath.string();
command += " ";
command += sourcePath.string();
command += " /LD /MD /EHsc";

// /Fe - output name
command += " /Fe:" + libraryPath.string();

// Link with OpenMP if the flag is present in compilerOptions
if (compilerOptions.find("/openmp") != std::string::npos || compilerOptions.find("-fopenmp") != std::string::npos)
if (compilerOptions.find("/openmp") != std::string::npos)
{
command += " /openmp";
}
command += " 2>&1";
#else
// GCC/Clang compiler command
// Only use essential flags here. Optimization flags (-O2, etc.) should be passed via compilerOptions.
command += " -shared -fPIC";
command += " -o " + libraryPath.string();
// Note: compilerOptions may include flags like -fopenmp that need to be passed to both compiler and linker
std::string command = m_Compiler + " -shared -fPIC -std=c++11 ";
command += compilerOptions;
command += " -o ";
command += libraryPath.string();
command += " ";
command += sourcePath.string();
// Link with OpenMP if the flag is present in compilerOptions
if (compilerOptions.find("-fopenmp") != std::string::npos)
{
command += " -fopenmp";
}
command += " 2>&1";
#endif

command += " " + sourcePath.string();
command += " 2>&1";

Logger::LogDebug("Compiling kernel with command: " + command);

FILE* pipe = popen(command.c_str(), "r");
Expand Down
21 changes: 19 additions & 2 deletions Source/ComputeEngine/Cpp/CppEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ CppEngine::CppEngine(const PlatformIndex platformIndex, const DeviceIndex device

// Set device info
m_DeviceInfo = DeviceInfo(0, "CPU");

// Set default compiler options
SetCompilerOptions("");
}

CppEngine::CppEngine(const ComputeApiInitializer& initializer, std::vector<QueueId>& assignedQueueIds) :
Expand All @@ -66,6 +69,7 @@ ComputeActionId CppEngine::RunKernelAsync(const KernelComputeData& data, const Q
Timer overheadTimer;
overheadTimer.Start();

m_Configuration.SetTuningCompilerOptions(data.GetCompilerOptions());
// Load kernel (cached)
auto kernel = LoadKernel(data);
// Set kernel arguments based on current buffers
Expand Down Expand Up @@ -613,10 +617,10 @@ void CppEngine::SetCompilerOptions(const std::string& options, const bool overri
finalOptions += " ";
}

finalOptions += m_Configuration.GetDefaultCompilerOptions();
finalOptions += GetDefaultCompilerOptions();
}

m_Configuration.SetCompilerOptions(finalOptions);
m_Configuration.SetStaticCompilerOptions(finalOptions);
ClearKernelCache();
}

Expand Down Expand Up @@ -800,6 +804,19 @@ void CppEngine::ClearQueueActions(const QueueId id)
// TODO: implement if we track per-queue actions
}

std::string CppEngine::GetDefaultCompilerOptions() const
{
std::string result;

#if defined(_MSC_VER)
result = " /std:c++17";
#else
result = " -std=c++17";
#endif

return result;
}

} // namespace ktt

#endif // KTT_API_CPP
1 change: 1 addition & 0 deletions Source/ComputeEngine/Cpp/CppEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class CppEngine : public ComputeEngine
std::unique_ptr<CppBuffer> CreateBuffer(KernelArgument& argument);
std::unique_ptr<CppBuffer> CreateUserBuffer(KernelArgument& argument, ComputeBuffer buffer);
void ClearQueueActions(const QueueId id);
std::string GetDefaultCompilerOptions() const;
};

} // namespace ktt
Expand Down
4 changes: 3 additions & 1 deletion Source/ComputeEngine/Cuda/CudaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ ComputeActionId CudaEngine::RunKernelAsync(const KernelComputeData& data, const
Timer timer;
timer.Start();

m_Configuration.SetTuningCompilerOptions(data.GetCompilerOptions());

auto kernel = LoadKernel(data);
std::vector<CUdeviceptr*> arguments = GetKernelArguments(data.GetArguments());
const size_t sharedMemorySize = GetSharedMemorySize(data.GetArguments());
Expand Down Expand Up @@ -786,7 +788,7 @@ void CudaEngine::SetCompilerOptions(const std::string& options, const bool overr
finalOptions += GetDefaultCompilerOptions();
}

m_Configuration.SetCompilerOptions(finalOptions);
m_Configuration.SetStaticCompilerOptions(finalOptions);
ClearKernelCache();
}

Expand Down
23 changes: 9 additions & 14 deletions Source/ComputeEngine/EngineConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -53,14 +58,4 @@ bool EngineConfiguration::IsProfilingActive() const
return m_ProfilingFlag;
}

void EngineConfiguration::SetDefaultCompilerOptions(const std::string& options)
{
m_DefaultCompilerOptions = options;
}

const std::string& EngineConfiguration::GetDefaultCompilerOptions() const
{
return m_DefaultCompilerOptions;
}

} // namespace ktt
12 changes: 5 additions & 7 deletions Source/ComputeEngine/EngineConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@ 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;

bool IsProfilingActive() const;
void SetProfiling(const bool profiling);

void SetDefaultCompilerOptions(const std::string& options);
const std::string& GetDefaultCompilerOptions() const;

private:
std::string m_CompilerOptions;
std::string m_DefaultCompilerOptions;
std::string m_StaticCompilerOptions;
std::string m_TuningCompilerOptions;
GlobalSizeType m_GlobalSizeType;
bool m_GlobalSizeCorrection;
bool m_ProfilingFlag;
Expand Down
8 changes: 7 additions & 1 deletion Source/ComputeEngine/KernelComputeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -156,4 +157,9 @@ const std::vector<KernelArgument*>& KernelComputeData::GetArguments() const
return m_Arguments;
}

const std::string& KernelComputeData::GetCompilerOptions() const
{
return m_CompilerOptions;
}

} // namespace ktt
2 changes: 2 additions & 0 deletions Source/ComputeEngine/KernelComputeData.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class KernelComputeData
const KernelConfiguration& GetConfiguration() const;
size_t GetArgumentIndex(const ArgumentId& id) const;
const std::vector<KernelArgument*>& GetArguments() const;
const std::string& GetCompilerOptions() const;

private:
std::string m_Name;
Expand All @@ -47,6 +48,7 @@ class KernelComputeData
DimensionVector m_LocalSize;
const KernelConfiguration* m_Configuration;
std::vector<KernelArgument*> m_Arguments;
std::string m_CompilerOptions;
};

} // namespace ktt
Loading