From f94d498a3773f838bcffb90fd56993a1583ad8ae Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Fri, 17 Nov 2017 20:57:46 +0100 Subject: Moved compilation function to separate file; removed dependency of tuners of the CLBlast library --- src/utilities/compile.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++ src/utilities/compile.hpp | 36 +++++++++++++++++ src/utilities/timing.hpp | 1 - 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/utilities/compile.cpp create mode 100644 src/utilities/compile.hpp (limited to 'src/utilities') diff --git a/src/utilities/compile.cpp b/src/utilities/compile.cpp new file mode 100644 index 00000000..3c02d316 --- /dev/null +++ b/src/utilities/compile.cpp @@ -0,0 +1,99 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren +// +// This file implements the kernel compilation functions (see the header for more information). +// +// ================================================================================================= + +#include +#include + +#include "routines/common.hpp" + +namespace clblast { +// ================================================================================================= + +// Compiles a program from source code +Program CompileFromSource(const std::string &source_string, const Precision precision, + const std::string &routine_name, + const Device& device, const Context& context, + std::vector& options) { + auto header_string = std::string{""}; + + header_string += "#define PRECISION " + ToString(static_cast(precision)) + "\n"; + + // Adds the name of the routine as a define + header_string += "#define ROUTINE_" + routine_name + "\n"; + + // Not all OpenCL compilers support the 'inline' keyword. The keyword is only used for devices on + // which it is known to work with all OpenCL platforms. + if (device.IsNVIDIA() || device.IsARM()) { + header_string += "#define USE_INLINE_KEYWORD 1\n"; + } + + // For specific devices, use the non-IEE754 compliant OpenCL mad() instruction. This can improve + // performance, but might result in a reduced accuracy. + if (device.IsAMD() && device.IsGPU()) { + header_string += "#define USE_CL_MAD 1\n"; + } + + // For specific devices, use staggered/shuffled workgroup indices. + if (device.IsAMD() && device.IsGPU()) { + header_string += "#define USE_STAGGERED_INDICES 1\n"; + } + + // For specific devices add a global synchronisation barrier to the GEMM kernel to optimize + // performance through better cache behaviour + if (device.IsARM() && device.IsGPU()) { + header_string += "#define GLOBAL_MEM_FENCE 1\n"; + } + + // Optionally adds a translation header from OpenCL kernels to CUDA kernels + #ifdef CUDA_API + source_string += + #include "kernels/opencl_to_cuda.h" + ; + #endif + + // Loads the common header (typedefs and defines and such) + header_string += + #include "kernels/common.opencl" + ; + + // Prints details of the routine to compile in case of debugging in verbose mode + #ifdef VERBOSE + printf("[DEBUG] Compiling routine '%s-%s'\n", + routine_name.c_str(), ToString(precision).c_str()); + const auto start_time = std::chrono::steady_clock::now(); + #endif + + // Compiles the kernel + auto program = Program(context, header_string + source_string); + try { + program.Build(device, options); + } catch (const CLCudaAPIBuildError &e) { + if (program.StatusIsCompilationWarningOrError(e.status())) { + fprintf(stdout, "OpenCL compiler error/warning: %s\n", + program.GetBuildInfo(device).c_str()); + } + throw; + } + + // Prints the elapsed compilation time in case of debugging in verbose mode + #ifdef VERBOSE + const auto elapsed_time = std::chrono::steady_clock::now() - start_time; + const auto timing = std::chrono::duration(elapsed_time).count(); + printf("[DEBUG] Completed compilation in %.2lf ms\n", timing); + #endif + + return program; +} + +// ================================================================================================= +} // namespace clblast diff --git a/src/utilities/compile.hpp b/src/utilities/compile.hpp new file mode 100644 index 00000000..bd4686eb --- /dev/null +++ b/src/utilities/compile.hpp @@ -0,0 +1,36 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren +// +// This file contains the CLBlast way to compile a kernel from source, used for the library and for +// the auto-tuners. +// +// ================================================================================================= + +#ifndef CLBLAST_UTILITIES_COMPILE_H_ +#define CLBLAST_UTILITIES_COMPILE_H_ + +#include +#include + +#include "utilities/utilities.hpp" + +namespace clblast { +// ================================================================================================= + +// Compiles a program from source code +Program CompileFromSource(const std::string &source_string, const Precision precision, + const std::string &routine_name, + const Device& device, const Context& context, + std::vector& options); + +// ================================================================================================= +} // namespace clblast + +// CLBLAST_UTILITIES_COMPILE_H_ +#endif diff --git a/src/utilities/timing.hpp b/src/utilities/timing.hpp index 3a5e2cff..e8040058 100644 --- a/src/utilities/timing.hpp +++ b/src/utilities/timing.hpp @@ -21,7 +21,6 @@ #include #include "utilities/utilities.hpp" -#include "routines/common.hpp" namespace clblast { // ================================================================================================= -- cgit v1.2.3