From dbdb58c6002cbd693f246f1e93919cc32ad4055a Mon Sep 17 00:00:00 2001 From: CNugteren Date: Sun, 9 Aug 2015 15:50:41 +0200 Subject: [PATCH 1/8] Refactored the tuners, added JSON output --- CMakeLists.txt | 5 +- include/internal/tuning.h | 119 +++++++++++++----- src/tuning/copy.cc | 130 ++++++++++++------- src/tuning/pad.cc | 116 +++++++++++------ src/tuning/padtranspose.cc | 155 ++++++++++++++--------- src/tuning/transpose.cc | 141 +++++++++++++-------- src/tuning/tuning.cc | 249 ------------------------------------- src/tuning/xaxpy.cc | 133 ++++++++++++-------- src/tuning/xgemm.cc | 213 +++++++++++++++++-------------- src/tuning/xgemv.cc | 180 +++++++++++++++------------ 10 files changed, 736 insertions(+), 705 deletions(-) delete mode 100644 src/tuning/tuning.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ca225b2..72cfe52f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,12 +154,9 @@ if(TUNERS) # Includes CLTune include_directories(${CLTUNE_INCLUDE_DIRS}) - # Creates the common tuner objects (requires CMake 2.8.8) - add_library(tuners_common OBJECT src/tuning/tuning.cc) - # Adds tuning executables foreach(KERNEL ${KERNELS}) - add_executable(tuner_${KERNEL} $ src/tuning/${KERNEL}.cc) + add_executable(tuner_${KERNEL} src/tuning/${KERNEL}.cc) target_link_libraries(tuner_${KERNEL} clblast ${CLTUNE_LIBRARIES} ${OPENCL_LIBRARIES}) install(TARGETS tuner_${KERNEL} DESTINATION bin) endforeach() diff --git a/include/internal/tuning.h b/include/internal/tuning.h index d0cf6b5d..40ce74bb 100644 --- a/include/internal/tuning.h +++ b/include/internal/tuning.h @@ -7,9 +7,8 @@ // Author(s): // Cedric Nugteren // -// This file implements the header for the tuner functions. This is only used for the optional -// and stand-alone tuner binaries and not part of the core of CLBlast. The convention used here is -// that X and Y are vectors, while A, B, and C are matrices. +// This file implements the interface to the CLTune auto-tuner. This is only used for the optional +// and stand-alone tuner binaries and not part of the core of CLBlast. // // ================================================================================================= @@ -17,44 +16,104 @@ #define CLBLAST_TUNING_H_ #include -#include +#include #include namespace clblast { // ================================================================================================= -// Functions with two or three OpenCL memory buffers -template -using Tuner2 = std::function&, - const std::vector&, std::vector&, - cltune::Tuner&)>; -template -using Tuner3 = std::function&, - const std::vector&, const std::vector&, std::vector&, - cltune::Tuner&)>; +// Function to get command-line argument, set-up the input buffers, configure the tuner, and collect +// the results. Used for all types of kernel families. Note that this is a header-only function so +// that it is automatically compiled for the various kernels (given as the 'C' template argument). +template +void Tuner(int argc, char* argv[]) { -// As above, but now with an additional ID for the variation -template -using Tuner3V = std::function&, const size_t, - const std::vector&, const std::vector&, std::vector&, - cltune::Tuner&)>; + // Sets the parameters and platform/device for which to tune (command-line options) + auto help = std::string{"* Options given/available:\n"}; + auto args = Arguments{}; + args.platform_id = GetArgument(argc, argv, help, kArgPlatform, size_t{0}); + args.device_id = GetArgument(argc, argv, help, kArgDevice, size_t{0}); + args.precision = GetArgument(argc, argv, help, kArgPrecision, Precision::kSingle); + for (auto &o: C::GetOptions()) { + if (o == kArgM) { args.m = GetArgument(argc, argv, help, kArgM, C::DefaultM()); } + if (o == kArgN) { args.n = GetArgument(argc, argv, help, kArgN, C::DefaultN()); } + if (o == kArgK) { args.k = GetArgument(argc, argv, help, kArgK, C::DefaultK()); } + if (o == kArgAlpha) { args.alpha = GetArgument(argc, argv, help, kArgAlpha, GetScalar()); } + if (o == kArgBeta) { args.beta = GetArgument(argc, argv, help, kArgBeta, GetScalar()); } + if (o == kArgFraction) { args.fraction = GetArgument(argc, argv, help, kArgFraction, C::DefaultFraction()); } + } + fprintf(stdout, "%s\n", help.c_str()); -// Tuner for vector-vector input -template -void TunerXY(int argc, char* argv[], const Tuner2 &tune_function); + // Tests validity of the given arguments + C::TestValidArguments(args); -// Tuner for matrix-vector-vector input -template -void TunerAXY(int argc, char* argv[], const size_t num_variations, const Tuner3V &tune_function); + // Creates input buffers with random data + auto x_vec = std::vector(C::GetSizeX(args)); + auto y_vec = std::vector(C::GetSizeY(args)); + auto a_mat = std::vector(C::GetSizeA(args)); + auto b_mat = std::vector(C::GetSizeB(args)); + auto c_mat = std::vector(C::GetSizeC(args)); + PopulateVector(x_vec); + PopulateVector(y_vec); + PopulateVector(a_mat); + PopulateVector(b_mat); + PopulateVector(c_mat); -// Tuner for matrix-matrix input -template -void TunerAB(int argc, char* argv[], const Tuner2 &tune_function); + // Initializes the tuner for the chosen device + cltune::Tuner tuner(args.platform_id, args.device_id); -// Tuner for matrix-matrix-matrix input -template -void TunerABC(int argc, char* argv[], const Tuner3 &tune_function); + // Use full-search to explore all parameter combinations or random-search to search only a part of + // the parameter values. The fraction is set as a command-line argument. + if (args.fraction == 1.0 || args.fraction == 0.0) { + tuner.UseFullSearch(); + } + else { + tuner.UseRandomSearch(1.0/args.fraction); + } + + // Loads the kernel sources and defines the kernel to tune + auto sources = C::GetSources(); + auto id = tuner.AddKernelFromString(sources, C::KernelName(), C::GlobalSize(args), C::LocalSize()); + tuner.SetReferenceFromString(sources, C::KernelName(), C::GlobalSize(args), C::LocalSizeRef()); + + // Sets the tunable parameters and their possible values + C::SetParameters(tuner, id); + C::SetConstraints(tuner, id); + C::SetLocalMemorySize(tuner, id, args); + + // Tests for a specific precision + tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); + tuner.AddParameterReference("PRECISION", static_cast(args.precision)); + + // Modifies the thread-sizes (both global and local) based on the parameters + for (auto ¶meters: C::MulLocal()) { tuner.MulLocalSize(id, parameters); } + for (auto ¶meters: C::DivLocal()) { tuner.DivLocalSize(id, parameters); } + for (auto ¶meters: C::MulGlobal()) { tuner.MulGlobalSize(id, parameters); } + for (auto ¶meters: C::DivGlobal()) { tuner.DivGlobalSize(id, parameters); } + + // Sets the function's arguments + C::SetArguments(tuner, args, x_vec, y_vec, a_mat, b_mat, c_mat); + + // Starts the tuning process + tuner.Tune(); + + // Prints the results to screen + auto time_ms = tuner.PrintToScreen(); + tuner.PrintFormatted(); + + // Also prints the performance of the best-case in terms of GB/s or GFLOPS + if (time_ms != 0.0) { + printf("[ -------> ] %.1lf ms", time_ms); + printf(" or %.1lf %s\n", C::GetMetric(args)/(time_ms*1.0e6), C::PerformanceUnit().c_str()); + } + + // Outputs the results as JSON to disk + tuner.PrintJSON("clblast_"+C::KernelFamily()+".json", { + {"kernel_family", C::KernelFamily()}, + {"precision", std::to_string(static_cast(args.precision))} + }); +} // ================================================================================================= } // namespace clblast diff --git a/src/tuning/copy.cc b/src/tuning/copy.cc index 125b076e..f38a28f3 100644 --- a/src/tuning/copy.cc +++ b/src/tuning/copy.cc @@ -7,13 +7,12 @@ // Author(s): // Cedric Nugteren // -// This file implements an auto-tuner to tune the copy OpenCL kernels. It uses CLTune. +// This file uses the CLTune auto-tuner to tune the copy OpenCL kernels. // // ================================================================================================= #include #include -#include #include "internal/utilities.h" #include "internal/tuning.h" @@ -21,61 +20,96 @@ namespace clblast { // ================================================================================================= -// The copy auto-tuner +// See comment at top of file for a description of the class template -void CopyTune(const Arguments &args, - const std::vector &a_mat, std::vector &b_mat, - cltune::Tuner &tuner) { +class TuneCopy { + public: - // This points to the CopyMatrix kernel as found in the CLBlast library. This is just one example - // of a copy kernel. However, all copy-kernels use the same tuning parameters, so one has to be - // chosen as a representative. - std::string sources = - #include "../src/kernels/common.opencl" - #include "../src/kernels/copy.opencl" - ; - auto id = tuner.AddKernelFromString(sources, "CopyMatrix", {args.m, args.n}, {1, 1}); - tuner.SetReferenceFromString(sources, "CopyMatrix", {args.m, args.n}, {8, 8}); - - // Sets the tunable parameters and their possible values - tuner.AddParameter(id, "COPY_DIMX", {8, 16, 32}); - tuner.AddParameter(id, "COPY_DIMY", {8, 16, 32}); - tuner.AddParameter(id, "COPY_WPT", {1, 2, 4, 8}); - tuner.AddParameter(id, "COPY_VW", {1, 2, 4, 8}); - - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); - - // Modifies the thread-sizes (both global and local) based on the parameters - tuner.MulLocalSize(id, {"COPY_DIMX", "COPY_DIMY"}); - tuner.DivGlobalSize(id, {"COPY_VW", "COPY_WPT"}); - - // Sets the function's arguments - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentInput(a_mat); - tuner.AddArgumentOutput(b_mat); -} - -// ================================================================================================= - -// Main function which calls the common client code with the routine-specific function as argument. -void TunerCopy(int argc, char *argv[]) { - switch(GetPrecision(argc, argv)) { - case Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); - case Precision::kSingle: TunerAB(argc, argv, CopyTune); break; - case Precision::kDouble: TunerAB(argc, argv, CopyTune); break; - case Precision::kComplexSingle: TunerAB(argc, argv, CopyTune); break; - case Precision::kComplexDouble: TunerAB(argc, argv, CopyTune); break; + // The representative kernel and the source code + static std::string KernelFamily() { return "copy"; } + static std::string KernelName() { return "CopyMatrix"; } + static std::string GetSources() { + return + #include "../src/kernels/common.opencl" + #include "../src/kernels/copy.opencl" + ; } -} + + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgM, kArgN}; } + + // Tests for valid arguments + static void TestValidArguments(const Arguments &) { } + + // Sets the default values for the arguments + static size_t DefaultM() { return 1024; } + static size_t DefaultN() { return 1024; } + static size_t DefaultK() { return 1; } // N/A for this kernel + static double DefaultFraction() { return 1.0; } // N/A for this kernel + + // Describes how to obtain the sizes of the buffers + static size_t GetSizeX(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeY(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeA(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeB(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeC(const Arguments &) { return 1; } // N/A for this kernel + + // Sets the tuning parameters and their possible values + static void SetParameters(cltune::Tuner &tuner, const size_t id) { + tuner.AddParameter(id, "COPY_DIMX", {8, 16, 32}); + tuner.AddParameter(id, "COPY_DIMY", {8, 16, 32}); + tuner.AddParameter(id, "COPY_WPT", {1, 2, 4, 8}); + tuner.AddParameter(id, "COPY_VW", {1, 2, 4, 8}); + } + + // Sets the constraints and local memory size + static void SetConstraints(cltune::Tuner &, const size_t) { } + static void SetLocalMemorySize(cltune::Tuner &, const size_t, const Arguments &) { } + + // Sets the base thread configuration + static std::vector GlobalSize(const Arguments &args) { return {args.m, args.n}; } + static std::vector LocalSize() { return {1, 1}; } + static std::vector LocalSizeRef() { return {8, 8}; } + + // Transforms the thread configuration based on the parameters + using TransformVector = std::vector>; + static TransformVector MulLocal() { return {{"COPY_DIMX", "COPY_DIMY"}}; } + static TransformVector DivLocal() { return {}; } + static TransformVector MulGlobal() { return {}; } + static TransformVector DivGlobal() { return {{"COPY_VW", "COPY_WPT"}}; } + + // Sets the kernel's arguments + static void SetArguments(cltune::Tuner &tuner, const Arguments &args, + std::vector &, std::vector &, + std::vector &a_mat, std::vector &b_mat, std::vector &) { + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentInput(a_mat); + tuner.AddArgumentOutput(b_mat); + } + + // Describes how to compute the performance metrics + static size_t GetMetric(const Arguments &args) { + return 2 * args.m * args.n * GetBytes(args.precision); + } + static std::string PerformanceUnit() { return "GB/s"; } +}; // ================================================================================================= } // namespace clblast +// Shortcuts to the clblast namespace +using float2 = clblast::float2; +using double2 = clblast::double2; + // Main function (not within the clblast namespace) int main(int argc, char *argv[]) { - clblast::TunerCopy(argc, argv); + switch(clblast::GetPrecision(argc, argv)) { + case clblast::Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); + case clblast::Precision::kSingle: clblast::Tuner, float>(argc, argv); break; + case clblast::Precision::kDouble: clblast::Tuner, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: clblast::Tuner, float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: clblast::Tuner, double2>(argc, argv); break; + } return 0; } diff --git a/src/tuning/pad.cc b/src/tuning/pad.cc index 584415c7..2ce566fb 100644 --- a/src/tuning/pad.cc +++ b/src/tuning/pad.cc @@ -7,13 +7,12 @@ // Author(s): // Cedric Nugteren // -// This file implements an auto-tuner to tune the pad-copy OpenCL kernels. It uses CLTune. +// This file uses the CLTune auto-tuner to tune the pad OpenCL kernels. // // ================================================================================================= #include #include -#include #include "internal/utilities.h" #include "internal/tuning.h" @@ -21,37 +20,68 @@ namespace clblast { // ================================================================================================= -// The pad auto-tuner +// See comment at top of file for a description of the class template -void PadTune(const Arguments &args, - const std::vector &a_mat, std::vector &b_mat, - cltune::Tuner &tuner) { +class TunePad { + public: - // This points to the PadMatrix kernel as found in the CLBlast library. This is just one - // example of a pad kernel. However, all pad-kernels use the same tuning parameters, so one has - // to be chosen as a representative. - std::string sources = - #include "../src/kernels/common.opencl" - #include "../src/kernels/pad.opencl" - ; - auto id = tuner.AddKernelFromString(sources, "PadMatrix", {args.m, args.n}, {1, 1}); - tuner.SetReferenceFromString(sources, "PadMatrix", {args.m, args.n}, {8, 8}); + // The representative kernel and the source code + static std::string KernelFamily() { return "pad"; } + static std::string KernelName() { return "PadMatrix"; } + static std::string GetSources() { + return + #include "../src/kernels/common.opencl" + #include "../src/kernels/pad.opencl" + ; + } - // Sets the tunable parameters and their possible values - tuner.AddParameter(id, "PAD_DIMX", {8, 16, 32}); - tuner.AddParameter(id, "PAD_DIMY", {8, 16, 32}); - tuner.AddParameter(id, "PAD_WPTX", {1, 2, 4}); - tuner.AddParameter(id, "PAD_WPTY", {1, 2, 4}); + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgM, kArgN}; } - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); + // Tests for valid arguments + static void TestValidArguments(const Arguments &) { } - // Modifies the thread-sizes (both global and local) based on the parameters - tuner.MulLocalSize(id, {"PAD_DIMX", "PAD_DIMY"}); - tuner.DivGlobalSize(id, {"PAD_WPTX", "PAD_WPTY"}); + // Sets the default values for the arguments + static size_t DefaultM() { return 1024; } + static size_t DefaultN() { return 1024; } + static size_t DefaultK() { return 1; } // N/A for this kernel + static double DefaultFraction() { return 1.0; } // N/A for this kernel - // Sets the function's arguments + // Describes how to obtain the sizes of the buffers + static size_t GetSizeX(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeY(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeA(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeB(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeC(const Arguments &) { return 1; } // N/A for this kernel + + // Sets the tuning parameters and their possible values + static void SetParameters(cltune::Tuner &tuner, const size_t id) { + tuner.AddParameter(id, "PAD_DIMX", {8, 16, 32}); + tuner.AddParameter(id, "PAD_DIMY", {8, 16, 32}); + tuner.AddParameter(id, "PAD_WPTX", {1, 2, 4}); + tuner.AddParameter(id, "PAD_WPTY", {1, 2, 4}); + } + + // Sets the constraints and local memory size + static void SetConstraints(cltune::Tuner &, const size_t) { } + static void SetLocalMemorySize(cltune::Tuner &, const size_t, const Arguments &) { } + + // Sets the base thread configuration + static std::vector GlobalSize(const Arguments &args) { return {args.m, args.n}; } + static std::vector LocalSize() { return {1, 1}; } + static std::vector LocalSizeRef() { return {8, 8}; } + + // Transforms the thread configuration based on the parameters + using TransformVector = std::vector>; + static TransformVector MulLocal() { return {{"PAD_DIMX", "PAD_DIMY"}}; } + static TransformVector DivLocal() { return {}; } + static TransformVector MulGlobal() { return {}; } + static TransformVector DivGlobal() { return {{"PAD_WPTX", "PAD_WPTY"}}; } + + // Sets the kernel's arguments + static void SetArguments(cltune::Tuner &tuner, const Arguments &args, + std::vector &, std::vector &, + std::vector &a_mat, std::vector &b_mat, std::vector &) { tuner.AddArgumentScalar(static_cast(args.m)); tuner.AddArgumentScalar(static_cast(args.n)); tuner.AddArgumentScalar(static_cast(args.m)); @@ -63,27 +93,31 @@ void PadTune(const Arguments &args, tuner.AddArgumentScalar(0); tuner.AddArgumentOutput(b_mat); tuner.AddArgumentScalar(0); -} - -// ================================================================================================= - -// Main function which calls the common client code with the routine-specific function as argument. -void TunerPad(int argc, char *argv[]) { - switch(GetPrecision(argc, argv)) { - case Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); - case Precision::kSingle: TunerAB(argc, argv, PadTune); break; - case Precision::kDouble: TunerAB(argc, argv, PadTune); break; - case Precision::kComplexSingle: TunerAB(argc, argv, PadTune); break; - case Precision::kComplexDouble: TunerAB(argc, argv, PadTune); break; } -} + + // Describes how to compute the performance metrics + static size_t GetMetric(const Arguments &args) { + return 2 * args.m * args.n * GetBytes(args.precision); + } + static std::string PerformanceUnit() { return "GB/s"; } +}; // ================================================================================================= } // namespace clblast +// Shortcuts to the clblast namespace +using float2 = clblast::float2; +using double2 = clblast::double2; + // Main function (not within the clblast namespace) int main(int argc, char *argv[]) { - clblast::TunerPad(argc, argv); + switch(clblast::GetPrecision(argc, argv)) { + case clblast::Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); + case clblast::Precision::kSingle: clblast::Tuner, float>(argc, argv); break; + case clblast::Precision::kDouble: clblast::Tuner, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: clblast::Tuner, float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: clblast::Tuner, double2>(argc, argv); break; + } return 0; } diff --git a/src/tuning/padtranspose.cc b/src/tuning/padtranspose.cc index 25044556..8d494745 100644 --- a/src/tuning/padtranspose.cc +++ b/src/tuning/padtranspose.cc @@ -7,13 +7,12 @@ // Author(s): // Cedric Nugteren // -// This file implements an auto-tuner to tune the pad-transpose OpenCL kernels. It uses CLTune. +// This file uses the CLTune auto-tuner to tune the padtranspose OpenCL kernels. // // ================================================================================================= #include #include -#include #include "internal/utilities.h" #include "internal/tuning.h" @@ -21,74 +20,108 @@ namespace clblast { // ================================================================================================= -// The transpose auto-tuner +// See comment at top of file for a description of the class template -void PadTransposeTune(const Arguments &args, - const std::vector &a_mat, std::vector &b_mat, - cltune::Tuner &tuner) { +class TunePadTranspose { + public: - // This points to the PadTransposeMatrix kernel as found in the CLBlast library. This is just one - // example of a transpose kernel. However, all kernels use the same tuning parameters, so one has - // to be chosen as a representative. - std::string sources = - #include "../src/kernels/common.opencl" - #include "../src/kernels/padtranspose.opencl" - ; - auto id = tuner.AddKernelFromString(sources, "PadTransposeMatrix", {args.m, args.n}, {1, 1}); - tuner.SetReferenceFromString(sources, "PadTransposeMatrix", {args.m, args.n}, {8, 8}); - - // Sets the tunable parameters and their possible values - tuner.AddParameter(id, "PADTRA_TILE", {8, 16, 32, 64}); - tuner.AddParameter(id, "PADTRA_WPT", {1, 2, 4, 8, 16}); - tuner.AddParameter(id, "PADTRA_PAD", {0, 1}); - - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); - - // Sets the constraints for local memory size limitations - auto LocalMemorySize = [args] (std::vector v) { - return ((v[0]*v[1]*(v[0]*v[1]+v[2]))*GetBytes(args.precision)); - }; - tuner.SetLocalMemoryUsage(id, LocalMemorySize, {"PADTRA_TILE", "PADTRA_WPT", "PADTRA_PAD"}); - - // Modifies the thread-sizes (both global and local) based on the parameters - tuner.DivGlobalSize(id, {"PADTRA_WPT", "PADTRA_WPT"}); - tuner.MulLocalSize(id, {"PADTRA_TILE", "PADTRA_TILE"}); - - // Sets the function's arguments - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentScalar(static_cast(args.n)); - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentScalar(0); - tuner.AddArgumentInput(a_mat); - tuner.AddArgumentScalar(static_cast(args.n)); - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentScalar(static_cast(args.n)); - tuner.AddArgumentScalar(0); - tuner.AddArgumentOutput(b_mat); - tuner.AddArgumentScalar(0); -} - -// ================================================================================================= - -// Main function which calls the common client code with the routine-specific function as argument. -void TunerPadTranspose(int argc, char *argv[]) { - switch(GetPrecision(argc, argv)) { - case Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); - case Precision::kSingle: TunerAB(argc, argv, PadTransposeTune); break; - case Precision::kDouble: TunerAB(argc, argv, PadTransposeTune); break; - case Precision::kComplexSingle: TunerAB(argc, argv, PadTransposeTune); break; - case Precision::kComplexDouble: TunerAB(argc, argv, PadTransposeTune); break; + // The representative kernel and the source code + static std::string KernelFamily() { return "padtranspose"; } + static std::string KernelName() { return "PadTransposeMatrix"; } + static std::string GetSources() { + return + #include "../src/kernels/common.opencl" + #include "../src/kernels/padtranspose.opencl" + ; } -} + + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgM, kArgN}; } + + // Tests for valid arguments + static void TestValidArguments(const Arguments &) { } + + // Sets the default values for the arguments + static size_t DefaultM() { return 1024; } + static size_t DefaultN() { return 1024; } + static size_t DefaultK() { return 1; } // N/A for this kernel + static double DefaultFraction() { return 1.0; } // N/A for this kernel + + // Describes how to obtain the sizes of the buffers + static size_t GetSizeX(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeY(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeA(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeB(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeC(const Arguments &) { return 1; } // N/A for this kernel + + // Sets the tuning parameters and their possible values + static void SetParameters(cltune::Tuner &tuner, const size_t id) { + tuner.AddParameter(id, "PADTRA_TILE", {8, 16, 32, 64}); + tuner.AddParameter(id, "PADTRA_WPT", {1, 2, 4, 8, 16}); + tuner.AddParameter(id, "PADTRA_PAD", {0, 1}); + } + + // Sets the constraints and local memory size + static void SetConstraints(cltune::Tuner &, const size_t) { } + static void SetLocalMemorySize(cltune::Tuner &tuner, const size_t id, const Arguments &args) { + auto LocalMemorySize = [args] (std::vector v) { + return ((v[0]*v[1]*(v[0]*v[1]+v[2]))*GetBytes(args.precision)); + }; + tuner.SetLocalMemoryUsage(id, LocalMemorySize, {"PADTRA_TILE", "PADTRA_WPT", "PADTRA_PAD"}); + } + + // Sets the base thread configuration + static std::vector GlobalSize(const Arguments &args) { return {args.m, args.n}; } + static std::vector LocalSize() { return {1, 1}; } + static std::vector LocalSizeRef() { return {8, 8}; } + + // Transforms the thread configuration based on the parameters + using TransformVector = std::vector>; + static TransformVector MulLocal() { return {{"PADTRA_TILE", "PADTRA_TILE"}}; } + static TransformVector DivLocal() { return {}; } + static TransformVector MulGlobal() { return {}; } + static TransformVector DivGlobal() { return {{"PADTRA_WPT", "PADTRA_WPT"}}; } + + // Sets the kernel's arguments + static void SetArguments(cltune::Tuner &tuner, const Arguments &args, + std::vector &, std::vector &, + std::vector &a_mat, std::vector &b_mat, std::vector &) { + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentScalar(static_cast(args.n)); + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentScalar(0); + tuner.AddArgumentInput(a_mat); + tuner.AddArgumentScalar(static_cast(args.n)); + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentScalar(static_cast(args.n)); + tuner.AddArgumentScalar(0); + tuner.AddArgumentOutput(b_mat); + tuner.AddArgumentScalar(0); + } + + // Describes how to compute the performance metrics + static size_t GetMetric(const Arguments &args) { + return 2 * args.m * args.n * GetBytes(args.precision); + } + static std::string PerformanceUnit() { return "GB/s"; } +}; // ================================================================================================= } // namespace clblast +// Shortcuts to the clblast namespace +using float2 = clblast::float2; +using double2 = clblast::double2; + // Main function (not within the clblast namespace) int main(int argc, char *argv[]) { - clblast::TunerPadTranspose(argc, argv); + switch(clblast::GetPrecision(argc, argv)) { + case clblast::Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); + case clblast::Precision::kSingle: clblast::Tuner, float>(argc, argv); break; + case clblast::Precision::kDouble: clblast::Tuner, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: clblast::Tuner, float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: clblast::Tuner, double2>(argc, argv); break; + } return 0; } diff --git a/src/tuning/transpose.cc b/src/tuning/transpose.cc index 8963a688..2ffdb7aa 100644 --- a/src/tuning/transpose.cc +++ b/src/tuning/transpose.cc @@ -7,13 +7,12 @@ // Author(s): // Cedric Nugteren // -// This file implements an auto-tuner to tune the transpose OpenCL kernels. It uses CLTune. +// This file uses the CLTune auto-tuner to tune the transpose OpenCL kernels. // // ================================================================================================= #include #include -#include #include "internal/utilities.h" #include "internal/tuning.h" @@ -21,67 +20,101 @@ namespace clblast { // ================================================================================================= -// The transpose auto-tuner +// See comment at top of file for a description of the class template -void TransposeTune(const Arguments &args, - const std::vector &a_mat, std::vector &b_mat, - cltune::Tuner &tuner) { +class TuneTranspose { + public: - // This points to the PadTransposeMatrix kernel as found in the CLBlast library. This is just one - // example of a transpose kernel. However, all kernels use the same tuning parameters, so one has - // to be chosen as a representative. - std::string sources = - #include "../src/kernels/common.opencl" - #include "../src/kernels/transpose.opencl" - ; - auto id = tuner.AddKernelFromString(sources, "TransposeMatrix", {args.m, args.n}, {1, 1}); - tuner.SetReferenceFromString(sources, "TransposeMatrix", {args.m, args.n}, {8, 8}); - - // Sets the tunable parameters and their possible values - tuner.AddParameter(id, "TRA_DIM", {4, 8, 16, 32, 64}); - tuner.AddParameter(id, "TRA_WPT", {1, 2, 4, 8, 16}); - tuner.AddParameter(id, "TRA_PAD", {0, 1}); - tuner.AddParameter(id, "TRA_SHUFFLE", {0, 1}); - - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); - - // Sets the constraints for local memory size limitations - auto LocalMemorySize = [args] (std::vector v) { - return ((v[0]*v[1]*(v[0]*v[1]+v[2]))*GetBytes(args.precision)); - }; - tuner.SetLocalMemoryUsage(id, LocalMemorySize, {"TRA_DIM", "TRA_WPT", "TRA_PAD"}); - - // Modifies the thread-sizes (both global and local) based on the parameters - tuner.DivGlobalSize(id, {"TRA_WPT", "TRA_WPT"}); - tuner.MulLocalSize(id, {"TRA_DIM", "TRA_DIM"}); - - // Sets the function's arguments - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentInput(a_mat); - tuner.AddArgumentOutput(b_mat); -} - -// ================================================================================================= - -// Main function which calls the common client code with the routine-specific function as argument. -void TunerTranspose(int argc, char *argv[]) { - switch(GetPrecision(argc, argv)) { - case Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); - case Precision::kSingle: TunerAB(argc, argv, TransposeTune); break; - case Precision::kDouble: TunerAB(argc, argv, TransposeTune); break; - case Precision::kComplexSingle: TunerAB(argc, argv, TransposeTune); break; - case Precision::kComplexDouble: TunerAB(argc, argv, TransposeTune); break; + // The representative kernel and the source code + static std::string KernelFamily() { return "transpose"; } + static std::string KernelName() { return "TransposeMatrix"; } + static std::string GetSources() { + return + #include "../src/kernels/common.opencl" + #include "../src/kernels/transpose.opencl" + ; } -} + + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgM, kArgN}; } + + // Tests for valid arguments + static void TestValidArguments(const Arguments &) { } + + // Sets the default values for the arguments + static size_t DefaultM() { return 1024; } + static size_t DefaultN() { return 1024; } + static size_t DefaultK() { return 1; } // N/A for this kernel + static double DefaultFraction() { return 1.0; } // N/A for this kernel + + // Describes how to obtain the sizes of the buffers + static size_t GetSizeX(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeY(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeA(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeB(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeC(const Arguments &) { return 1; } // N/A for this kernel + + // Sets the tuning parameters and their possible values + static void SetParameters(cltune::Tuner &tuner, const size_t id) { + tuner.AddParameter(id, "TRA_DIM", {4, 8, 16, 32, 64}); + tuner.AddParameter(id, "TRA_WPT", {1, 2, 4, 8, 16}); + tuner.AddParameter(id, "TRA_PAD", {0, 1}); + tuner.AddParameter(id, "TRA_SHUFFLE", {0, 1}); + } + + // Sets the constraints and local memory size + static void SetConstraints(cltune::Tuner &, const size_t) { } + static void SetLocalMemorySize(cltune::Tuner &tuner, const size_t id, const Arguments &args) { + auto LocalMemorySize = [args] (std::vector v) { + return ((v[0]*v[1]*(v[0]*v[1]+v[2]))*GetBytes(args.precision)); + }; + tuner.SetLocalMemoryUsage(id, LocalMemorySize, {"TRA_DIM", "TRA_WPT", "TRA_PAD"}); + } + + // Sets the base thread configuration + static std::vector GlobalSize(const Arguments &args) { return {args.m, args.n}; } + static std::vector LocalSize() { return {1, 1}; } + static std::vector LocalSizeRef() { return {8, 8}; } + + // Transforms the thread configuration based on the parameters + using TransformVector = std::vector>; + static TransformVector MulLocal() { return {{"TRA_DIM", "TRA_DIM"}}; } + static TransformVector DivLocal() { return {}; } + static TransformVector MulGlobal() { return {}; } + static TransformVector DivGlobal() { return {{"TRA_WPT", "TRA_WPT"}}; } + + // Sets the kernel's arguments + static void SetArguments(cltune::Tuner &tuner, const Arguments &args, + std::vector &, std::vector &, + std::vector &a_mat, std::vector &b_mat, std::vector &) { + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentInput(a_mat); + tuner.AddArgumentOutput(b_mat); + } + + // Describes how to compute the performance metrics + static size_t GetMetric(const Arguments &args) { + return 2 * args.m * args.n * GetBytes(args.precision); + } + static std::string PerformanceUnit() { return "GB/s"; } +}; // ================================================================================================= } // namespace clblast +// Shortcuts to the clblast namespace +using float2 = clblast::float2; +using double2 = clblast::double2; + // Main function (not within the clblast namespace) int main(int argc, char *argv[]) { - clblast::TunerTranspose(argc, argv); + switch(clblast::GetPrecision(argc, argv)) { + case clblast::Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); + case clblast::Precision::kSingle: clblast::Tuner, float>(argc, argv); break; + case clblast::Precision::kDouble: clblast::Tuner, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: clblast::Tuner, float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: clblast::Tuner, double2>(argc, argv); break; + } return 0; } diff --git a/src/tuning/tuning.cc b/src/tuning/tuning.cc deleted file mode 100644 index 2dcb11d5..00000000 --- a/src/tuning/tuning.cc +++ /dev/null @@ -1,249 +0,0 @@ - -// ================================================================================================= -// 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 common auto-tuning code to interface with the CLTune library. -// -// ================================================================================================= - -#include -#include - -#include "internal/utilities.h" -#include "internal/tuning.h" - -namespace clblast { -// ================================================================================================= - -// Function to get command-line argument, set-up the input buffers, configure the tuner, and collect -// the results. Used for vector-vector routines. -template -void TunerXY(int argc, char* argv[], const Tuner2 &tune_function) { - - // Sets the parameters and platform/device for which to tune (command-line options) - auto help = std::string{"* Options given/available:\n"}; - auto args = Arguments{}; - args.platform_id = GetArgument(argc, argv, help, kArgPlatform, size_t{0}); - args.device_id = GetArgument(argc, argv, help, kArgDevice, size_t{0}); - args.precision = GetArgument(argc, argv, help, kArgPrecision, Precision::kSingle); - args.n = GetArgument(argc, argv, help, kArgN, size_t{4096*1024}); - args.alpha = GetArgument(argc, argv, help, kArgAlpha, GetScalar()); - fprintf(stdout, "%s\n", help.c_str()); - - // Creates input buffers with random data - auto x_vec = std::vector(args.n); - auto y_vec = std::vector(args.n); - PopulateVector(x_vec); - PopulateVector(y_vec); - - // Initializes the tuner for the chosen device - cltune::Tuner tuner(args.platform_id, args.device_id); - - // Use full-search to explore all parameter combinations. - tuner.UseFullSearch(); - - // Configures the tuning parameters (kernel specific) - tune_function(args, x_vec, y_vec, tuner); - - // Starts the tuning process - tuner.Tune(); - - // Prints the results to screen - auto time_ms = tuner.PrintToScreen(); - tuner.PrintFormatted(); - - // Also prints the performance of the best-case in terms of GB/s - const auto mega_bytes = (3*args.n*GetBytes(args.precision)) * 1.0e-6; - if (time_ms != 0.0) { - printf("[ -------> ] %.1lf ms or %.1lf GB/s\n", time_ms, mega_bytes/time_ms); - } -} - -// Compiles the above function -template void TunerXY(int, char**, const Tuner2&); -template void TunerXY(int, char**, const Tuner2&); -template void TunerXY(int, char**, const Tuner2&); -template void TunerXY(int, char**, const Tuner2&); - -// ================================================================================================= - -// Function to get command-line argument, set-up the input buffers, configure the tuner, and collect -// the results. Used for matrix-vector-vector routines. -template -void TunerAXY(int argc, char* argv[], const size_t num_variations, - const Tuner3V &tune_function) { - - // Sets the parameters and platform/device for which to tune (command-line options) - auto help = std::string{"* Options given/available:\n"}; - auto args = Arguments{}; - args.platform_id = GetArgument(argc, argv, help, kArgPlatform, size_t{0}); - args.device_id = GetArgument(argc, argv, help, kArgDevice, size_t{0}); - args.precision = GetArgument(argc, argv, help, kArgPrecision, Precision::kSingle); - args.m = GetArgument(argc, argv, help, kArgM, size_t{2048}); - args.n = GetArgument(argc, argv, help, kArgN, size_t{2048}); - args.alpha = GetArgument(argc, argv, help, kArgAlpha, GetScalar()); - args.beta = GetArgument(argc, argv, help, kArgBeta, GetScalar()); - fprintf(stdout, "%s\n", help.c_str()); - - // Creates input buffers with random data - auto a_mat = std::vector(args.m * args.n); - auto x_vec = std::vector(args.n); - auto y_vec = std::vector(args.m); - PopulateVector(a_mat); - PopulateVector(x_vec); - PopulateVector(y_vec); - - // Loop over the different variations of the kernel - for (auto variation=size_t{1}; variation<=num_variations; ++variation) { - - // Initializes the tuner for the chosen device - cltune::Tuner tuner(args.platform_id, args.device_id); - - // Use full-search to explore all parameter combinations. - tuner.UseFullSearch(); - - // Configures the tuning parameters (kernel specific) - tune_function(args, variation, a_mat, x_vec, y_vec, tuner); - - // Starts the tuning process - tuner.Tune(); - - // Prints the results to screen - auto time_ms = tuner.PrintToScreen(); - tuner.PrintFormatted(); - - // Also prints the performance of the best-case in terms of GB/s and GFLOPS - const auto mega_bytes = ((args.m*args.n + 2*args.m + args.n)*GetBytes(args.precision)) * 1.0e-6; - const auto mega_flops = (2*args.m*args.n) * 1.0e-6; - if (time_ms != 0.0) { - printf("[ -------> ] %.1lf ms or %.1lf GB/s or %.1lf GFLOPS\n", - time_ms, mega_bytes/time_ms, mega_flops/time_ms); - } - } -} - -// Compiles the above function -template void TunerAXY(int, char**, const size_t, const Tuner3V&); -template void TunerAXY(int, char**, const size_t, const Tuner3V&); -template void TunerAXY(int, char**, const size_t, const Tuner3V&); -template void TunerAXY(int, char**, const size_t, const Tuner3V&); - -// ================================================================================================= - -// Function to get command-line argument, set-up the input buffers, configure the tuner, and collect -// the results. Used for matrix-matrix routines. -template -void TunerAB(int argc, char* argv[], const Tuner2 &tune_function) { - - // Sets the parameters and platform/device for which to tune (command-line options) - auto help = std::string{"* Options given/available:\n"}; - auto args = Arguments{}; - args.platform_id = GetArgument(argc, argv, help, kArgPlatform, size_t{0}); - args.device_id = GetArgument(argc, argv, help, kArgDevice, size_t{0}); - args.precision = GetArgument(argc, argv, help, kArgPrecision, Precision::kSingle); - args.m = GetArgument(argc, argv, help, kArgM, size_t{1024}); - args.n = GetArgument(argc, argv, help, kArgN, size_t{1024}); - args.fraction = GetArgument(argc, argv, help, kArgFraction, 2048.0); - fprintf(stdout, "%s\n", help.c_str()); - - // Creates input buffers with random data - auto a_mat = std::vector(args.m * args.n); - auto b_mat = std::vector(args.m * args.n); - PopulateVector(a_mat); - PopulateVector(b_mat); - - // Initializes the tuner for the chosen device - cltune::Tuner tuner(args.platform_id, args.device_id); - - // Use full-search to explore all parameter combinations. - tuner.UseFullSearch(); - - // Configures the tuning parameters (kernel specific) - tune_function(args, a_mat, b_mat, tuner); - - // Starts the tuning process - tuner.Tune(); - - // Prints the results to screen - auto time_ms = tuner.PrintToScreen(); - tuner.PrintFormatted(); - - // Also prints the performance of the best-case in terms of GB/s - const auto mega_bytes = (2*args.m*args.n*GetBytes(args.precision)) * 1.0e-6; - if (time_ms != 0.0) { - printf("[ -------> ] %.1lf ms or %.1lf GB/s\n", time_ms, mega_bytes/time_ms); - } -} - -// Compiles the above function -template void TunerAB(int, char**, const Tuner2&); -template void TunerAB(int, char**, const Tuner2&); -template void TunerAB(int, char**, const Tuner2&); -template void TunerAB(int, char**, const Tuner2&); - -// ================================================================================================= - -// Function to get command-line argument, set-up the input buffers, configure the tuner, and collect -// the results. Used for matrix-matrix-matrix routines. -template -void TunerABC(int argc, char* argv[], const Tuner3 &tune_function) { - - // Sets the parameters and platform/device for which to tune (command-line options) - auto help = std::string{"* Options given/available:\n"}; - auto args = Arguments{}; - args.platform_id = GetArgument(argc, argv, help, kArgPlatform, size_t{0}); - args.device_id = GetArgument(argc, argv, help, kArgDevice, size_t{0}); - args.precision = GetArgument(argc, argv, help, kArgPrecision, Precision::kSingle); - args.m = GetArgument(argc, argv, help, kArgM, size_t{1024}); - args.n = GetArgument(argc, argv, help, kArgN, size_t{1024}); - args.k = GetArgument(argc, argv, help, kArgK, size_t{1024}); - args.alpha = GetArgument(argc, argv, help, kArgAlpha, GetScalar()); - args.beta = GetArgument(argc, argv, help, kArgBeta, GetScalar()); - args.fraction = GetArgument(argc, argv, help, kArgFraction, 2048.0); - fprintf(stdout, "%s\n", help.c_str()); - - // Creates input buffers with random data - auto a_mat = std::vector(args.m * args.k); - auto b_mat = std::vector(args.n * args.k); - auto c_mat = std::vector(args.m * args.n); - PopulateVector(a_mat); - PopulateVector(b_mat); - PopulateVector(c_mat); - - // Initializes the tuner for the chosen device - cltune::Tuner tuner(args.platform_id, args.device_id); - - // Use random-search to search only a part of the parameter values. The fraction of the search- - // space to explore is set as a command-line argument. - tuner.UseRandomSearch(1.0/args.fraction); - - // Configures the tuning parameters (kernel specific) - tune_function(args, a_mat, b_mat, c_mat, tuner); - - // Starts the tuning process - tuner.Tune(); - - // Prints the results to screen - auto time_ms = tuner.PrintToScreen(); - tuner.PrintFormatted(); - - // Also prints the performance of the best-case in terms of GFLOPS - const auto mega_flops = (2*args.m*args.n*args.k) * 1.0e-6; - if (time_ms != 0.0) { - printf("[ -------> ] %.1lf ms or %.1lf GFLOPS\n", time_ms, mega_flops/time_ms); - } -} - -// Compiles the above function -template void TunerABC(int, char**, const Tuner3&); -template void TunerABC(int, char**, const Tuner3&); -template void TunerABC(int, char**, const Tuner3&); -template void TunerABC(int, char**, const Tuner3&); - -// ================================================================================================= -} // namespace clblast diff --git a/src/tuning/xaxpy.cc b/src/tuning/xaxpy.cc index 20b5978e..cc9e81d3 100644 --- a/src/tuning/xaxpy.cc +++ b/src/tuning/xaxpy.cc @@ -7,13 +7,12 @@ // Author(s): // Cedric Nugteren // -// This file implements an auto-tuner to tune the Xaxpy OpenCL kernel. It uses the CLTune library. +// This file uses the CLTune auto-tuner to tune the xaxpy OpenCL kernels. // // ================================================================================================= #include #include -#include #include "internal/utilities.h" #include "internal/tuning.h" @@ -21,66 +20,100 @@ namespace clblast { // ================================================================================================= -// The Xaxpy auto-tuner +// See comment at top of file for a description of the class template -void XaxpyTune(const Arguments &args, - const std::vector &x_vec, std::vector &y_vec, - cltune::Tuner &tuner) { +class TuneXaxpy { + public: - // The XaxpyFast kernel only works under certain conditions. Check here whether the condition is - // true for the reference kernel - if (!IsMultiple(args.n, 64)) { - throw std::runtime_error("The 'XaxpyFast' kernel requires 'n' to be a multiple of WGS*WPT*VW"); + // The representative kernel and the source code + static std::string KernelFamily() { return "xaxpy"; } + static std::string KernelName() { return "XaxpyFast"; } + static std::string GetSources() { + return + #include "../src/kernels/common.opencl" + #include "../src/kernels/xaxpy.opencl" + ; } - // This points to the XaxpyFast kernel as found in the CLBlast library - std::string sources = - #include "../src/kernels/common.opencl" - #include "../src/kernels/xaxpy.opencl" - ; - auto id = tuner.AddKernelFromString(sources, "XaxpyFast", {args.n}, {1}); - tuner.SetReferenceFromString(sources, "XaxpyFast", {args.n}, {64}); + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgN, kArgAlpha}; } - // Sets the tunable parameters and their possible values - tuner.AddParameter(id, "WGS", {64, 128, 256, 512, 1024, 2048}); - tuner.AddParameter(id, "WPT", {1, 2, 4, 8}); - tuner.AddParameter(id, "VW", {1, 2, 4, 8}); - - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); - - // Modifies the thread-sizes (local) based on the parameters - tuner.MulLocalSize(id, {"WGS"}); - tuner.DivGlobalSize(id, {"WPT"}); - tuner.DivGlobalSize(id, {"VW"}); - - // Sets the function's arguments - tuner.AddArgumentScalar(static_cast(args.n)); - tuner.AddArgumentScalar(args.alpha); - tuner.AddArgumentInput(x_vec); - tuner.AddArgumentOutput(y_vec); -} - -// ================================================================================================= - -// Main function which calls the common client code with the routine-specific function as argument. -void TunerXaxpy(int argc, char *argv[]) { - switch(GetPrecision(argc, argv)) { - case Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); - case Precision::kSingle: TunerXY(argc, argv, XaxpyTune); break; - case Precision::kDouble: TunerXY(argc, argv, XaxpyTune); break; - case Precision::kComplexSingle: TunerXY(argc, argv, XaxpyTune); break; - case Precision::kComplexDouble: TunerXY(argc, argv, XaxpyTune); break; + // Tests for valid arguments + static void TestValidArguments(const Arguments &args) { + if (!IsMultiple(args.n, 64)) { + throw std::runtime_error("'XaxpyFast' requires 'n' to be a multiple of WGS*WPT*VW"); + } } -} + + // Sets the default values for the arguments + static size_t DefaultM() { return 1; } // N/A for this kernel + static size_t DefaultN() { return 4096*1024; } + static size_t DefaultK() { return 1; } // N/A for this kernel + static double DefaultFraction() { return 1.0; } // N/A for this kernel + + // Describes how to obtain the sizes of the buffers + static size_t GetSizeX(const Arguments &args) { return args.n; } // N/A for this kernel + static size_t GetSizeY(const Arguments &args) { return args.n; } // N/A for this kernel + static size_t GetSizeA(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeB(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeC(const Arguments &) { return 1; } // N/A for this kernel + + // Sets the tuning parameters and their possible values + static void SetParameters(cltune::Tuner &tuner, const size_t id) { + tuner.AddParameter(id, "WGS", {64, 128, 256, 512, 1024, 2048}); + tuner.AddParameter(id, "WPT", {1, 2, 4, 8}); + tuner.AddParameter(id, "VW", {1, 2, 4, 8}); + } + + // Sets the constraints and local memory size + static void SetConstraints(cltune::Tuner &, const size_t) { } + static void SetLocalMemorySize(cltune::Tuner &, const size_t, const Arguments &) { } + + // Sets the base thread configuration + static std::vector GlobalSize(const Arguments &args) { return {args.n}; } + static std::vector LocalSize() { return {1}; } + static std::vector LocalSizeRef() { return {64}; } + + // Transforms the thread configuration based on the parameters + using TransformVector = std::vector>; + static TransformVector MulLocal() { return {{"WGS"}}; } + static TransformVector DivLocal() { return {}; } + static TransformVector MulGlobal() { return {}; } + static TransformVector DivGlobal() { return {{"WPT"},{"VW"}}; } + + // Sets the kernel's arguments + static void SetArguments(cltune::Tuner &tuner, const Arguments &args, + std::vector &x_vec, std::vector &y_vec, + std::vector &, std::vector &, std::vector &) { + tuner.AddArgumentScalar(static_cast(args.n)); + tuner.AddArgumentScalar(args.alpha); + tuner.AddArgumentInput(x_vec); + tuner.AddArgumentOutput(y_vec); + } + + // Describes how to compute the performance metrics + static size_t GetMetric(const Arguments &args) { + return 3 * args.n * GetBytes(args.precision); + } + static std::string PerformanceUnit() { return "GB/s"; } +}; // ================================================================================================= } // namespace clblast +// Shortcuts to the clblast namespace +using float2 = clblast::float2; +using double2 = clblast::double2; + // Main function (not within the clblast namespace) int main(int argc, char *argv[]) { - clblast::TunerXaxpy(argc, argv); + switch(clblast::GetPrecision(argc, argv)) { + case clblast::Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); + case clblast::Precision::kSingle: clblast::Tuner, float>(argc, argv); break; + case clblast::Precision::kDouble: clblast::Tuner, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: clblast::Tuner, float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: clblast::Tuner, double2>(argc, argv); break; + } return 0; } diff --git a/src/tuning/xgemm.cc b/src/tuning/xgemm.cc index 3fe58ed5..302f2bd5 100644 --- a/src/tuning/xgemm.cc +++ b/src/tuning/xgemm.cc @@ -7,15 +7,12 @@ // Author(s): // Cedric Nugteren // -// This file implements an auto-tuner to tune the Xgemm OpenCL kernel. It uses the CLTune library. -// Note that this tuner uses random-search: running it multiple times or with a larger fraction -// argument might be neccessary to obtain good results. +// This file uses the CLTune auto-tuner to tune the xgemm OpenCL kernels. // // ================================================================================================= #include #include -#include #include "internal/utilities.h" #include "internal/tuning.h" @@ -23,102 +20,136 @@ namespace clblast { // ================================================================================================= -// The Xgemm auto-tuner +// See comment at top of file for a description of the class template -void XgemmTune(const Arguments &args, - const std::vector &a_mat, const std::vector &b_mat, std::vector &c_mat, - cltune::Tuner &tuner) { +class TuneXgemm { + public: - // This points to the Xgemm kernel as found in the CLBlast library and its golden reference - std::string sources = - #include "../src/kernels/common.opencl" - #include "../src/kernels/xgemm.opencl" - ; - auto id = tuner.AddKernelFromString(sources, "Xgemm", {args.m, args.n}, {1, 1}); - tuner.SetReferenceFromString(sources, "Xgemm", {args.m, args.n}, {8, 8}); - - // Sets the tunable parameters and their possible values - tuner.AddParameter(id, "MWG", {16, 32, 64, 128}); - tuner.AddParameter(id, "NWG", {16, 32, 64, 128}); - tuner.AddParameter(id, "KWG", {16, 32}); - tuner.AddParameter(id, "MDIMC", {8, 16, 32}); - tuner.AddParameter(id, "NDIMC", {8, 16, 32}); - tuner.AddParameter(id, "MDIMA", {8, 16, 32}); - tuner.AddParameter(id, "NDIMB", {8, 16, 32}); - tuner.AddParameter(id, "KWI", {2, 8}); - tuner.AddParameter(id, "VWM", {1, 2, 4, 8}); - tuner.AddParameter(id, "VWN", {1, 2, 4, 8}); - tuner.AddParameter(id, "STRM", {0, 1}); - tuner.AddParameter(id, "STRN", {0, 1}); - tuner.AddParameter(id, "SA", {0, 1}); - tuner.AddParameter(id, "SB", {0, 1}); - - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); - - // Sets the helper functions to implement the constraints below - auto MultipleOfX = [] (std::vector v) { return IsMultiple(v[0], v[1]); }; - auto MultipleOfXMulY = [] (std::vector v) { return IsMultiple(v[0], v[1]*v[2]); }; - auto MultipleOfXMulYDivZ = [] (std::vector v) { return IsMultiple(v[0], (v[1]*v[2])/v[3]); }; - - // Sets constraints: Requirement for unrolling the KWG loop - tuner.AddConstraint(id, MultipleOfX, {"KWG", "KWI"}); - - // Sets constraints: Required for integer MWI and NWI - tuner.AddConstraint(id, MultipleOfXMulY, {"MWG", "MDIMC", "VWM"}); - tuner.AddConstraint(id, MultipleOfXMulY, {"NWG", "NDIMC", "VWN"}); - - // Sets constraints: Required for integer MWIA and NWIB - tuner.AddConstraint(id, MultipleOfXMulY, {"MWG", "MDIMA", "VWM"}); - tuner.AddConstraint(id, MultipleOfXMulY, {"NWG", "NDIMB", "VWN"}); - - // Sets constraints: KWG has to be a multiple of KDIMA = ((MDIMC*NDIMC)/(MDIMA)) and KDIMB = (...) - tuner.AddConstraint(id, MultipleOfXMulYDivZ, {"KWG", "MDIMC", "NDIMC", "MDIMA"}); - tuner.AddConstraint(id, MultipleOfXMulYDivZ, {"KWG", "MDIMC", "NDIMC", "NDIMB"}); - - // Sets the constraints for local memory size limitations - auto LocalMemorySize = [args] (std::vector v) { - return (((v[0]*v[1]*v[2]/v[3]) + (v[4]*v[5]*v[6]/v[7]))*GetBytes(args.precision)); - }; - tuner.SetLocalMemoryUsage(id, LocalMemorySize, {"SA", "KWG", "MWG", "VWM", - "SB", "KWG", "NWG", "VWN"}); - - // Modifies the thread-sizes (both global and local) based on the parameters - tuner.MulLocalSize(id, {"MDIMC", "NDIMC"}); - tuner.MulGlobalSize(id, {"MDIMC", "NDIMC"}); - tuner.DivGlobalSize(id, {"MWG", "NWG"}); - - // Sets the function's arguments - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentScalar(static_cast(args.n)); - tuner.AddArgumentScalar(static_cast(args.k)); - tuner.AddArgumentScalar(args.alpha); - tuner.AddArgumentScalar(args.beta); - tuner.AddArgumentInput(a_mat); - tuner.AddArgumentInput(b_mat); - tuner.AddArgumentOutput(c_mat); -} - -// ================================================================================================= - -// Main function which calls the common client code with the routine-specific function as argument. -void TunerXgemm(int argc, char *argv[]) { - switch(GetPrecision(argc, argv)) { - case Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); - case Precision::kSingle: TunerABC(argc, argv, XgemmTune); break; - case Precision::kDouble: TunerABC(argc, argv, XgemmTune); break; - case Precision::kComplexSingle: TunerABC(argc, argv, XgemmTune); break; - case Precision::kComplexDouble: TunerABC(argc, argv, XgemmTune); break; + // The representative kernel and the source code + static std::string KernelFamily() { return "xgemm"; } + static std::string KernelName() { return "Xgemm"; } + static std::string GetSources() { + return + #include "../src/kernels/common.opencl" + #include "../src/kernels/xgemm.opencl" + ; } -} + + // The list of arguments relevant for this routine + static std::vector GetOptions() { + return {kArgM, kArgN, kArgK, kArgAlpha, kArgBeta, kArgFraction}; + } + + // Tests for valid arguments + static void TestValidArguments(const Arguments &) { } + + // Sets the default values for the arguments + static size_t DefaultM() { return 1024; } + static size_t DefaultN() { return 1024; } + static size_t DefaultK() { return 1024; } + static double DefaultFraction() { return 2048.0; } + + // Describes how to obtain the sizes of the buffers + static size_t GetSizeX(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeY(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeA(const Arguments &args) { return args.m * args.k; } + static size_t GetSizeB(const Arguments &args) { return args.n * args.k; } + static size_t GetSizeC(const Arguments &args) { return args.m * args.n; } + + // Sets the tuning parameters and their possible values + static void SetParameters(cltune::Tuner &tuner, const size_t id) { + tuner.AddParameter(id, "MWG", {16, 32, 64, 128}); + tuner.AddParameter(id, "NWG", {16, 32, 64, 128}); + tuner.AddParameter(id, "KWG", {16, 32}); + tuner.AddParameter(id, "MDIMC", {8, 16, 32}); + tuner.AddParameter(id, "NDIMC", {8, 16, 32}); + tuner.AddParameter(id, "MDIMA", {8, 16, 32}); + tuner.AddParameter(id, "NDIMB", {8, 16, 32}); + tuner.AddParameter(id, "KWI", {2, 8}); + tuner.AddParameter(id, "VWM", {1, 2, 4, 8}); + tuner.AddParameter(id, "VWN", {1, 2, 4, 8}); + tuner.AddParameter(id, "STRM", {0, 1}); + tuner.AddParameter(id, "STRN", {0, 1}); + tuner.AddParameter(id, "SA", {0, 1}); + tuner.AddParameter(id, "SB", {0, 1}); + } + + // Sets the constraints + static void SetConstraints(cltune::Tuner &tuner, const size_t id) { + auto MultipleOfX = [] (std::vector v) { return IsMultiple(v[0], v[1]); }; + auto MultipleOfXMulY = [] (std::vector v) { return IsMultiple(v[0], v[1]*v[2]); }; + auto MultipleOfXMulYDivZ = [] (std::vector v) { return IsMultiple(v[0], (v[1]*v[2])/v[3]); }; + // Requirement for unrolling the KWG loop + tuner.AddConstraint(id, MultipleOfX, {"KWG", "KWI"}); + // Required for integer MWI and NWI + tuner.AddConstraint(id, MultipleOfXMulY, {"MWG", "MDIMC", "VWM"}); + tuner.AddConstraint(id, MultipleOfXMulY, {"NWG", "NDIMC", "VWN"}); + // Required for integer MWIA and NWIB + tuner.AddConstraint(id, MultipleOfXMulY, {"MWG", "MDIMA", "VWM"}); + tuner.AddConstraint(id, MultipleOfXMulY, {"NWG", "NDIMB", "VWN"}); + // KWG has to be a multiple of KDIMA = ((MDIMC*NDIMC)/(MDIMA)) and KDIMB = (...) + tuner.AddConstraint(id, MultipleOfXMulYDivZ, {"KWG", "MDIMC", "NDIMC", "MDIMA"}); + tuner.AddConstraint(id, MultipleOfXMulYDivZ, {"KWG", "MDIMC", "NDIMC", "NDIMB"}); + } + + // Sets the local memory size + static void SetLocalMemorySize(cltune::Tuner &tuner, const size_t id, const Arguments &args) { + auto LocalMemorySize = [args] (std::vector v) { + return (((v[0]*v[1]*v[2]/v[3]) + (v[4]*v[5]*v[6]/v[7]))*GetBytes(args.precision)); + }; + tuner.SetLocalMemoryUsage(id, LocalMemorySize, {"SA", "KWG", "MWG", "VWM", + "SB", "KWG", "NWG", "VWN"}); + } + + // Sets the base thread configuration + static std::vector GlobalSize(const Arguments &args) { return {args.m, args.n}; } + static std::vector LocalSize() { return {1, 1}; } + static std::vector LocalSizeRef() { return {8, 8}; } + + // Transforms the thread configuration based on the parameters + using TransformVector = std::vector>; + static TransformVector MulLocal() { return {{"MDIMC", "NDIMC"}}; } + static TransformVector DivLocal() { return {}; } + static TransformVector MulGlobal() { return {{"MDIMC", "NDIMC"}}; } + static TransformVector DivGlobal() { return {{"MWG", "NWG"}}; } + + // Sets the kernel's arguments + static void SetArguments(cltune::Tuner &tuner, const Arguments &args, + std::vector &, std::vector &, + std::vector &a_mat, std::vector &b_mat, std::vector &c_mat) { + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentScalar(static_cast(args.n)); + tuner.AddArgumentScalar(static_cast(args.k)); + tuner.AddArgumentScalar(args.alpha); + tuner.AddArgumentScalar(args.beta); + tuner.AddArgumentInput(a_mat); + tuner.AddArgumentInput(b_mat); + tuner.AddArgumentOutput(c_mat); + } + + // Describes how to compute the performance metrics + static size_t GetMetric(const Arguments &args) { + return 2 * args.m * args.n * args.k; + } + static std::string PerformanceUnit() { return "GFLOPS"; } +}; // ================================================================================================= } // namespace clblast +// Shortcuts to the clblast namespace +using float2 = clblast::float2; +using double2 = clblast::double2; + // Main function (not within the clblast namespace) int main(int argc, char *argv[]) { - clblast::TunerXgemm(argc, argv); + switch(clblast::GetPrecision(argc, argv)) { + case clblast::Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); + case clblast::Precision::kSingle: clblast::Tuner, float>(argc, argv); break; + case clblast::Precision::kDouble: clblast::Tuner, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: clblast::Tuner, float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: clblast::Tuner, double2>(argc, argv); break; + } return 0; } diff --git a/src/tuning/xgemv.cc b/src/tuning/xgemv.cc index a9d88e4b..e22b5103 100644 --- a/src/tuning/xgemv.cc +++ b/src/tuning/xgemv.cc @@ -7,8 +7,7 @@ // Author(s): // Cedric Nugteren // -// This file implements an auto-tuner to tune the Xgemv OpenCL kernel. It uses the CLTune library. -// Three variations of the kernel are tuned: +// This file uses the CLTune auto-tuner to tune the xgemv OpenCL kernels. Three variants are tuned: // 1: The full version of the kernel // 2: The fast version for non-transposed matrices // 3: The fast version for transposed matrices @@ -17,7 +16,6 @@ #include #include -#include #include "internal/utilities.h" #include "internal/tuning.h" @@ -25,93 +23,121 @@ namespace clblast { // ================================================================================================= -// The Xgemv auto-tuner -template -void XgemvTune(const Arguments &args, const size_t variation, - const std::vector &a_mat, const std::vector &x_vec, std::vector &y_vec, - cltune::Tuner &tuner) { +// See comment at top of file for a description of the class +template +class TuneXgemv { + public: - // Sets the kernel name and the layout argument - auto kernel_name = (variation == 1) ? "Xgemv" : ((variation == 2) ? "XgemvFast" : "XgemvFastRot"); - auto a_rotated = (variation == 3) ? 1 : 0; - - // This points to the Xgemv kernel as found in the CLBlast library - std::string sources = - #include "../src/kernels/common.opencl" - #include "../src/kernels/xgemv.opencl" - ; - auto id = tuner.AddKernelFromString(sources, kernel_name, {args.m}, {1}); - tuner.SetReferenceFromString(sources, "Xgemv", {args.m}, {64}); - - // Helper for the constraints - auto MultipleOfX = [] (std::vector v) { return IsMultiple(v[0], v[1]); }; - - // Sets the tunable parameters, their possible values, the adjusted thread sizes, and constraints - if (variation == 1) { - tuner.AddParameter(id, "WGS1", {64, 128, 256, 512, 1024, 1536, 2048}); - tuner.AddParameter(id, "WPT1", {1, 2, 4, 8}); - tuner.MulLocalSize(id, {"WGS1"}); - tuner.DivGlobalSize(id, {"WPT1"}); - } - else if (variation == 2) { - tuner.AddParameter(id, "WGS2", {64, 128, 256, 512, 1024, 1536, 2048}); - tuner.AddParameter(id, "WPT2", {1, 2, 4, 8}); - tuner.AddParameter(id, "VW2", {1, 2, 4, 8}); - tuner.MulLocalSize(id, {"WGS2"}); - tuner.DivGlobalSize(id, {"WPT2"}); - tuner.AddConstraint(id, MultipleOfX, {"WPT2", "VW2"}); - } - else if (variation == 3) { - tuner.AddParameter(id, "WGS3", {64, 128, 256, 512, 1024, 1536, 2048}); - tuner.AddParameter(id, "WPT3", {1, 2, 4, 8}); - tuner.AddParameter(id, "VW3", {1, 2, 4, 8}); - tuner.MulLocalSize(id, {"WGS3"}); - tuner.DivGlobalSize(id, {"WPT3"}); - tuner.AddConstraint(id, MultipleOfX, {"WGS3", "VW3"}); + // The representative kernel and the source code + static std::string KernelFamily() { return "xgemv_"+std::to_string(V); } + static std::string KernelName() { return (V==1) ? "Xgemv" : ((V==2) ? "XgemvFast" : "XgemvFastRot"); } + static std::string GetSources() { + return + #include "../src/kernels/common.opencl" + #include "../src/kernels/xgemv.opencl" + ; } - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgM, kArgN, kArgAlpha, kArgBeta}; } - // Sets the function's arguments - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentScalar(static_cast(args.n)); - tuner.AddArgumentScalar(args.alpha); - tuner.AddArgumentScalar(args.beta); - tuner.AddArgumentScalar(static_cast(a_rotated)); - tuner.AddArgumentInput(a_mat); - tuner.AddArgumentScalar(0); - tuner.AddArgumentScalar(static_cast(args.m)); - tuner.AddArgumentInput(x_vec); - tuner.AddArgumentScalar(0); - tuner.AddArgumentScalar(1); - tuner.AddArgumentOutput(y_vec); - tuner.AddArgumentScalar(0); - tuner.AddArgumentScalar(1); - tuner.AddArgumentScalar(0); // Conjugate transpose -} + // Tests for valid arguments + static void TestValidArguments(const Arguments &) { } -// ================================================================================================= + // Sets the default values for the arguments + static size_t DefaultM() { return 2048; } + static size_t DefaultN() { return 2048; } + static size_t DefaultK() { return 1; } // N/A for this kernel + static double DefaultFraction() { return 1.0; } // N/A for this kernel -// Main function which calls the common client code with the routine-specific function as argument. -void TunerXgemv(int argc, char *argv[]) { - auto num_variations = size_t{3}; - switch(GetPrecision(argc, argv)) { - case Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); - case Precision::kSingle: TunerAXY(argc, argv, num_variations, XgemvTune); break; - case Precision::kDouble: TunerAXY(argc, argv, num_variations, XgemvTune); break; - case Precision::kComplexSingle: TunerAXY(argc, argv, num_variations, XgemvTune); break; - case Precision::kComplexDouble: TunerAXY(argc, argv, num_variations, XgemvTune); break; + // Describes how to obtain the sizes of the buffers + static size_t GetSizeX(const Arguments &args) { return args.n; } + static size_t GetSizeY(const Arguments &args) { return args.m; } + static size_t GetSizeA(const Arguments &args) { return args.m * args.n; } + static size_t GetSizeB(const Arguments &) { return 1; } // N/A for this kernel + static size_t GetSizeC(const Arguments &) { return 1; } // N/A for this kernel + + // Sets the tuning parameters and their possible values + static void SetParameters(cltune::Tuner &tuner, const size_t id) { + tuner.AddParameter(id, "WGS"+std::to_string(V), {64, 128, 256, 512, 1024, 1536, 2048}); + tuner.AddParameter(id, "WPT"+std::to_string(V), {1, 2, 4, 8}); + if (V==2 || V==3) { tuner.AddParameter(id, "VW"+std::to_string(V), {1, 2, 4, 8}); } } -} + + // Sets the constraints and local memory size + static void SetConstraints(cltune::Tuner &tuner, const size_t id) { + auto MultipleOfX = [] (std::vector v) { return IsMultiple(v[0], v[1]); }; + if (V==2 || V==3) { + tuner.AddConstraint(id, MultipleOfX, {"WPT"+std::to_string(V), "VW"+std::to_string(V)}); + } + } + static void SetLocalMemorySize(cltune::Tuner &, const size_t, const Arguments &) { } + + // Sets the base thread configuration + static std::vector GlobalSize(const Arguments &args) { return {args.m}; } + static std::vector LocalSize() { return {1}; } + static std::vector LocalSizeRef() { return {64}; } + + // Transforms the thread configuration based on the parameters + using TransformVector = std::vector>; + static TransformVector MulLocal() { return {{"WGS"+std::to_string(V)}}; } + static TransformVector DivLocal() { return {}; } + static TransformVector MulGlobal() { return {}; } + static TransformVector DivGlobal() { return {{"WPT"+std::to_string(V)}}; } + + // Sets the kernel's arguments + static void SetArguments(cltune::Tuner &tuner, const Arguments &args, + std::vector &x_vec, std::vector &y_vec, + std::vector &a_mat, std::vector &, std::vector &) { + auto a_rotated = (V==3) ? 1 : 0; + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentScalar(static_cast(args.n)); + tuner.AddArgumentScalar(args.alpha); + tuner.AddArgumentScalar(args.beta); + tuner.AddArgumentScalar(static_cast(a_rotated)); + tuner.AddArgumentInput(a_mat); + tuner.AddArgumentScalar(0); + tuner.AddArgumentScalar(static_cast(args.m)); + tuner.AddArgumentInput(x_vec); + tuner.AddArgumentScalar(0); + tuner.AddArgumentScalar(1); + tuner.AddArgumentOutput(y_vec); + tuner.AddArgumentScalar(0); + tuner.AddArgumentScalar(1); + tuner.AddArgumentScalar(0); // Conjugate transpose + } + + // Describes how to compute the performance metrics + static size_t GetMetric(const Arguments &args) { + return (args.m*args.n + 2*args.m + args.n) * GetBytes(args.precision); + } + static std::string PerformanceUnit() { return "GB/s"; } +}; // ================================================================================================= } // namespace clblast +// Shortcuts to the clblast namespace +using float2 = clblast::float2; +using double2 = clblast::double2; + +// Function to tune a specific variation V (not within the clblast namespace) +template +void StartVariation(int argc, char *argv[]) { + switch(clblast::GetPrecision(argc, argv)) { + case clblast::Precision::kHalf: throw std::runtime_error("Unsupported precision mode"); + case clblast::Precision::kSingle: clblast::Tuner, float>(argc, argv); break; + case clblast::Precision::kDouble: clblast::Tuner, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: clblast::Tuner, float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: clblast::Tuner, double2>(argc, argv); break; + } +} + // Main function (not within the clblast namespace) int main(int argc, char *argv[]) { - clblast::TunerXgemv(argc, argv); + StartVariation<1>(argc, argv); + StartVariation<2>(argc, argv); + StartVariation<3>(argc, argv); return 0; } From f85d44f6020eb8062166c7593b63b3adfe44c00b Mon Sep 17 00:00:00 2001 From: CNugteren Date: Thu, 13 Aug 2015 08:33:04 +0200 Subject: [PATCH 2/8] Added argument m,n,k metadata to JSON files --- include/internal/tuning.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/internal/tuning.h b/include/internal/tuning.h index 40ce74bb..3c596d69 100644 --- a/include/internal/tuning.h +++ b/include/internal/tuning.h @@ -108,11 +108,17 @@ void Tuner(int argc, char* argv[]) { printf(" or %.1lf %s\n", C::GetMetric(args)/(time_ms*1.0e6), C::PerformanceUnit().c_str()); } - // Outputs the results as JSON to disk - tuner.PrintJSON("clblast_"+C::KernelFamily()+".json", { + // Outputs the results as JSON to disk, including some meta-data + auto metadata = std::vector>{ {"kernel_family", C::KernelFamily()}, {"precision", std::to_string(static_cast(args.precision))} - }); + }; + for (auto &o: C::GetOptions()) { + if (o == kArgM) { metadata.push_back({"arg_m", std::to_string(args.m)}); } + if (o == kArgN) { metadata.push_back({"arg_n", std::to_string(args.n)}); } + if (o == kArgK) { metadata.push_back({"arg_k", std::to_string(args.k)}); } + } + tuner.PrintJSON("clblast_"+C::KernelFamily()+".json", metadata); } // ================================================================================================= From cbd25bffea7e4aef8a17e2b5a7e121caad5e5125 Mon Sep 17 00:00:00 2001 From: CNugteren Date: Wed, 19 Aug 2015 11:12:16 +0200 Subject: [PATCH 3/8] Added hotfix 8eeb7f721ff8811521147cfe5ae9796164286b77 --- src/kernels/transpose.opencl | 60 ++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/kernels/transpose.opencl b/src/kernels/transpose.opencl index 2aa53bb8..d726f7ec 100644 --- a/src/kernels/transpose.opencl +++ b/src/kernels/transpose.opencl @@ -97,39 +97,39 @@ __kernel void TransposeMatrix(const int ld, #if TRA_WPT == 1 results[0] = v[0]; #elif TRA_WPT == 2 - results[0] = (realT) (v[0].x, v[1].x); - results[1] = (realT) (v[0].y, v[1].y); + results[0] = (realT) {v[0].x, v[1].x}; + results[1] = (realT) {v[0].y, v[1].y}; #elif TRA_WPT == 4 - results[0] = (realT) (v[0].x, v[1].x, v[2].x, v[3].x); - results[1] = (realT) (v[0].y, v[1].y, v[2].y, v[3].y); - results[2] = (realT) (v[0].z, v[1].z, v[2].z, v[3].z); - results[3] = (realT) (v[0].w, v[1].w, v[2].w, v[3].w); + results[0] = (realT) {v[0].x, v[1].x, v[2].x, v[3].x}; + results[1] = (realT) {v[0].y, v[1].y, v[2].y, v[3].y}; + results[2] = (realT) {v[0].z, v[1].z, v[2].z, v[3].z}; + results[3] = (realT) {v[0].w, v[1].w, v[2].w, v[3].w}; #elif TRA_WPT == 8 - results[0] = (realT) (v[0].s0, v[1].s0, v[2].s0, v[3].s0, v[4].s0, v[5].s0, v[6].s0, v[7].s0); - results[1] = (realT) (v[0].s1, v[1].s1, v[2].s1, v[3].s1, v[4].s1, v[5].s1, v[6].s1, v[7].s1); - results[2] = (realT) (v[0].s2, v[1].s2, v[2].s2, v[3].s2, v[4].s2, v[5].s2, v[6].s2, v[7].s2); - results[3] = (realT) (v[0].s3, v[1].s3, v[2].s3, v[3].s3, v[4].s3, v[5].s3, v[6].s3, v[7].s3); - results[4] = (realT) (v[0].s4, v[1].s4, v[2].s4, v[3].s4, v[4].s4, v[5].s4, v[6].s4, v[7].s4); - results[5] = (realT) (v[0].s5, v[1].s5, v[2].s5, v[3].s5, v[4].s5, v[5].s5, v[6].s5, v[7].s5); - results[6] = (realT) (v[0].s6, v[1].s6, v[2].s6, v[3].s6, v[4].s6, v[5].s6, v[6].s6, v[7].s6); - results[7] = (realT) (v[0].s7, v[1].s7, v[2].s7, v[3].s7, v[4].s7, v[5].s7, v[6].s7, v[7].s7); + results[0] = (realT) {v[0].s0, v[1].s0, v[2].s0, v[3].s0, v[4].s0, v[5].s0, v[6].s0, v[7].s0}; + results[1] = (realT) {v[0].s1, v[1].s1, v[2].s1, v[3].s1, v[4].s1, v[5].s1, v[6].s1, v[7].s1}; + results[2] = (realT) {v[0].s2, v[1].s2, v[2].s2, v[3].s2, v[4].s2, v[5].s2, v[6].s2, v[7].s2}; + results[3] = (realT) {v[0].s3, v[1].s3, v[2].s3, v[3].s3, v[4].s3, v[5].s3, v[6].s3, v[7].s3}; + results[4] = (realT) {v[0].s4, v[1].s4, v[2].s4, v[3].s4, v[4].s4, v[5].s4, v[6].s4, v[7].s4}; + results[5] = (realT) {v[0].s5, v[1].s5, v[2].s5, v[3].s5, v[4].s5, v[5].s5, v[6].s5, v[7].s5}; + results[6] = (realT) {v[0].s6, v[1].s6, v[2].s6, v[3].s6, v[4].s6, v[5].s6, v[6].s6, v[7].s6}; + results[7] = (realT) {v[0].s7, v[1].s7, v[2].s7, v[3].s7, v[4].s7, v[5].s7, v[6].s7, v[7].s7}; #elif TRA_WPT == 16 - results[ 0] = (realT) (v[0].s0, v[1].s0, v[2].s0, v[3].s0, v[4].s0, v[5].s0, v[6].s0, v[7].s0, v[8].s0, v[9].s0, v[10].s0, v[11].s0, v[12].s0, v[13].s0, v[14].s0, v[15].s0); - results[ 1] = (realT) (v[0].s1, v[1].s1, v[2].s1, v[3].s1, v[4].s1, v[5].s1, v[6].s1, v[7].s1, v[8].s1, v[9].s1, v[10].s1, v[11].s1, v[12].s1, v[13].s1, v[14].s1, v[15].s1); - results[ 2] = (realT) (v[0].s2, v[1].s2, v[2].s2, v[3].s2, v[4].s2, v[5].s2, v[6].s2, v[7].s2, v[8].s2, v[9].s2, v[10].s2, v[11].s2, v[12].s2, v[13].s2, v[14].s2, v[15].s2); - results[ 3] = (realT) (v[0].s3, v[1].s3, v[2].s3, v[3].s3, v[4].s3, v[5].s3, v[6].s3, v[7].s3, v[8].s3, v[9].s3, v[10].s3, v[11].s3, v[12].s3, v[13].s3, v[14].s3, v[15].s3); - results[ 4] = (realT) (v[0].s4, v[1].s4, v[2].s4, v[3].s4, v[4].s4, v[5].s4, v[6].s4, v[7].s4, v[8].s4, v[9].s4, v[10].s4, v[11].s4, v[12].s4, v[13].s4, v[14].s4, v[15].s4); - results[ 5] = (realT) (v[0].s5, v[1].s5, v[2].s5, v[3].s5, v[4].s5, v[5].s5, v[6].s5, v[7].s5, v[8].s5, v[9].s5, v[10].s5, v[11].s5, v[12].s5, v[13].s5, v[14].s5, v[15].s5); - results[ 6] = (realT) (v[0].s6, v[1].s6, v[2].s6, v[3].s6, v[4].s6, v[5].s6, v[6].s6, v[7].s6, v[8].s6, v[9].s6, v[10].s6, v[11].s6, v[12].s6, v[13].s6, v[14].s6, v[15].s6); - results[ 7] = (realT) (v[0].s7, v[1].s7, v[2].s7, v[3].s7, v[4].s7, v[5].s7, v[6].s7, v[7].s7, v[8].s7, v[9].s7, v[10].s7, v[11].s7, v[12].s7, v[13].s7, v[14].s7, v[15].s7); - results[ 8] = (realT) (v[0].s8, v[1].s8, v[2].s8, v[3].s8, v[4].s8, v[5].s8, v[6].s8, v[7].s8, v[8].s8, v[9].s8, v[10].s8, v[11].s8, v[12].s8, v[13].s8, v[14].s8, v[15].s8); - results[ 9] = (realT) (v[0].s9, v[1].s9, v[2].s9, v[3].s9, v[4].s9, v[5].s9, v[6].s9, v[7].s9, v[8].s9, v[9].s9, v[10].s9, v[11].s9, v[12].s9, v[13].s9, v[14].s9, v[15].s9); - results[10] = (realT) (v[0].sA, v[1].sA, v[2].sA, v[3].sA, v[4].sA, v[5].sA, v[6].sA, v[7].sA, v[8].sA, v[9].sA, v[10].sA, v[11].sA, v[12].sA, v[13].sA, v[14].sA, v[15].sA); - results[11] = (realT) (v[0].sB, v[1].sB, v[2].sB, v[3].sB, v[4].sB, v[5].sB, v[6].sB, v[7].sB, v[8].sB, v[9].sB, v[10].sB, v[11].sB, v[12].sB, v[13].sB, v[14].sB, v[15].sB); - results[12] = (realT) (v[0].sC, v[1].sC, v[2].sC, v[3].sC, v[4].sC, v[5].sC, v[6].sC, v[7].sC, v[8].sC, v[9].sC, v[10].sC, v[11].sC, v[12].sC, v[13].sC, v[14].sC, v[15].sC); - results[13] = (realT) (v[0].sD, v[1].sD, v[2].sD, v[3].sD, v[4].sD, v[5].sD, v[6].sD, v[7].sD, v[8].sD, v[9].sD, v[10].sD, v[11].sD, v[12].sD, v[13].sD, v[14].sD, v[15].sD); - results[14] = (realT) (v[0].sE, v[1].sE, v[2].sE, v[3].sE, v[4].sE, v[5].sE, v[6].sE, v[7].sE, v[8].sE, v[9].sE, v[10].sE, v[11].sE, v[12].sE, v[13].sE, v[14].sE, v[15].sE); - results[15] = (realT) (v[0].sF, v[1].sF, v[2].sF, v[3].sF, v[4].sF, v[5].sF, v[6].sF, v[7].sF, v[8].sF, v[9].sF, v[10].sF, v[11].sF, v[12].sF, v[13].sF, v[14].sF, v[15].sF); + results[ 0] = (realT) {v[0].s0, v[1].s0, v[2].s0, v[3].s0, v[4].s0, v[5].s0, v[6].s0, v[7].s0, v[8].s0, v[9].s0, v[10].s0, v[11].s0, v[12].s0, v[13].s0, v[14].s0, v[15].s0}; + results[ 1] = (realT) {v[0].s1, v[1].s1, v[2].s1, v[3].s1, v[4].s1, v[5].s1, v[6].s1, v[7].s1, v[8].s1, v[9].s1, v[10].s1, v[11].s1, v[12].s1, v[13].s1, v[14].s1, v[15].s1}; + results[ 2] = (realT) {v[0].s2, v[1].s2, v[2].s2, v[3].s2, v[4].s2, v[5].s2, v[6].s2, v[7].s2, v[8].s2, v[9].s2, v[10].s2, v[11].s2, v[12].s2, v[13].s2, v[14].s2, v[15].s2}; + results[ 3] = (realT) {v[0].s3, v[1].s3, v[2].s3, v[3].s3, v[4].s3, v[5].s3, v[6].s3, v[7].s3, v[8].s3, v[9].s3, v[10].s3, v[11].s3, v[12].s3, v[13].s3, v[14].s3, v[15].s3}; + results[ 4] = (realT) {v[0].s4, v[1].s4, v[2].s4, v[3].s4, v[4].s4, v[5].s4, v[6].s4, v[7].s4, v[8].s4, v[9].s4, v[10].s4, v[11].s4, v[12].s4, v[13].s4, v[14].s4, v[15].s4}; + results[ 5] = (realT) {v[0].s5, v[1].s5, v[2].s5, v[3].s5, v[4].s5, v[5].s5, v[6].s5, v[7].s5, v[8].s5, v[9].s5, v[10].s5, v[11].s5, v[12].s5, v[13].s5, v[14].s5, v[15].s5}; + results[ 6] = (realT) {v[0].s6, v[1].s6, v[2].s6, v[3].s6, v[4].s6, v[5].s6, v[6].s6, v[7].s6, v[8].s6, v[9].s6, v[10].s6, v[11].s6, v[12].s6, v[13].s6, v[14].s6, v[15].s6}; + results[ 7] = (realT) {v[0].s7, v[1].s7, v[2].s7, v[3].s7, v[4].s7, v[5].s7, v[6].s7, v[7].s7, v[8].s7, v[9].s7, v[10].s7, v[11].s7, v[12].s7, v[13].s7, v[14].s7, v[15].s7}; + results[ 8] = (realT) {v[0].s8, v[1].s8, v[2].s8, v[3].s8, v[4].s8, v[5].s8, v[6].s8, v[7].s8, v[8].s8, v[9].s8, v[10].s8, v[11].s8, v[12].s8, v[13].s8, v[14].s8, v[15].s8}; + results[ 9] = (realT) {v[0].s9, v[1].s9, v[2].s9, v[3].s9, v[4].s9, v[5].s9, v[6].s9, v[7].s9, v[8].s9, v[9].s9, v[10].s9, v[11].s9, v[12].s9, v[13].s9, v[14].s9, v[15].s9}; + results[10] = (realT) {v[0].sA, v[1].sA, v[2].sA, v[3].sA, v[4].sA, v[5].sA, v[6].sA, v[7].sA, v[8].sA, v[9].sA, v[10].sA, v[11].sA, v[12].sA, v[13].sA, v[14].sA, v[15].sA}; + results[11] = (realT) {v[0].sB, v[1].sB, v[2].sB, v[3].sB, v[4].sB, v[5].sB, v[6].sB, v[7].sB, v[8].sB, v[9].sB, v[10].sB, v[11].sB, v[12].sB, v[13].sB, v[14].sB, v[15].sB}; + results[12] = (realT) {v[0].sC, v[1].sC, v[2].sC, v[3].sC, v[4].sC, v[5].sC, v[6].sC, v[7].sC, v[8].sC, v[9].sC, v[10].sC, v[11].sC, v[12].sC, v[13].sC, v[14].sC, v[15].sC}; + results[13] = (realT) {v[0].sD, v[1].sD, v[2].sD, v[3].sD, v[4].sD, v[5].sD, v[6].sD, v[7].sD, v[8].sD, v[9].sD, v[10].sD, v[11].sD, v[12].sD, v[13].sD, v[14].sD, v[15].sD}; + results[14] = (realT) {v[0].sE, v[1].sE, v[2].sE, v[3].sE, v[4].sE, v[5].sE, v[6].sE, v[7].sE, v[8].sE, v[9].sE, v[10].sE, v[11].sE, v[12].sE, v[13].sE, v[14].sE, v[15].sE}; + results[15] = (realT) {v[0].sF, v[1].sF, v[2].sF, v[3].sF, v[4].sF, v[5].sF, v[6].sF, v[7].sF, v[8].sF, v[9].sF, v[10].sF, v[11].sF, v[12].sF, v[13].sF, v[14].sF, v[15].sF}; #endif // Stores the results into the destination matrix From 8a02db0746896011d6ea42cf6b9c4910d9ddda13 Mon Sep 17 00:00:00 2001 From: CNugteren Date: Wed, 19 Aug 2015 11:12:42 +0200 Subject: [PATCH 4/8] Added precision to the JSON output --- include/internal/tuning.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/internal/tuning.h b/include/internal/tuning.h index 3c596d69..929658c6 100644 --- a/include/internal/tuning.h +++ b/include/internal/tuning.h @@ -109,16 +109,17 @@ void Tuner(int argc, char* argv[]) { } // Outputs the results as JSON to disk, including some meta-data + auto precision_string = std::to_string(static_cast(args.precision)); auto metadata = std::vector>{ {"kernel_family", C::KernelFamily()}, - {"precision", std::to_string(static_cast(args.precision))} + {"precision", precision_string} }; for (auto &o: C::GetOptions()) { if (o == kArgM) { metadata.push_back({"arg_m", std::to_string(args.m)}); } if (o == kArgN) { metadata.push_back({"arg_n", std::to_string(args.n)}); } if (o == kArgK) { metadata.push_back({"arg_k", std::to_string(args.k)}); } } - tuner.PrintJSON("clblast_"+C::KernelFamily()+".json", metadata); + tuner.PrintJSON("clblast_"+C::KernelFamily()+"_"+precision_string+".json", metadata); } // ================================================================================================= From b46de2243390d3f773b8a66da41d3d0bf61dbcee Mon Sep 17 00:00:00 2001 From: CNugteren Date: Wed, 19 Aug 2015 19:34:29 +0200 Subject: [PATCH 5/8] Moved precision tester to utilities --- include/internal/utilities.h | 6 ++++++ src/utilities.cc | 14 ++++++++++++++ test/correctness/tester.cc | 14 -------------- test/correctness/tester.h | 4 ---- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/include/internal/utilities.h b/include/internal/utilities.h index 6dba24e1..d9fdb9ab 100644 --- a/include/internal/utilities.h +++ b/include/internal/utilities.h @@ -197,6 +197,12 @@ bool IsMultiple(const size_t a, const size_t b); // Convert the precision enum into bytes, e.g. a double takes up 8 bytes size_t GetBytes(const Precision precision); +// ================================================================================================= + +// Returns false is this precision is not supported by the device +template +bool PrecisionSupported(const Device &device); + // ================================================================================================= } // namespace clblast diff --git a/src/utilities.cc b/src/utilities.cc index 62abbb91..042b3116 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -270,5 +270,19 @@ size_t GetBytes(const Precision precision) { } } +// ================================================================================================= + +// Returns false is this precision is not supported by the device +template <> bool PrecisionSupported(const Device &) { return true; } +template <> bool PrecisionSupported(const Device &) { return true; } +template <> bool PrecisionSupported(const Device &device) { + auto extensions = device.Capabilities(); + return (extensions.find(kKhronosDoublePrecision) == std::string::npos) ? false : true; +} +template <> bool PrecisionSupported(const Device &device) { + auto extensions = device.Capabilities(); + return (extensions.find(kKhronosDoublePrecision) == std::string::npos) ? false : true; +} + // ================================================================================================= } // namespace clblast diff --git a/test/correctness/tester.cc b/test/correctness/tester.cc index 002cb1a6..a52142c4 100644 --- a/test/correctness/tester.cc +++ b/test/correctness/tester.cc @@ -335,20 +335,6 @@ template <> const std::vector GetExampleScalars(const bool full_test) { // ================================================================================================= -// Returns false is this precision is not supported by the device -template <> bool PrecisionSupported(const Device &) { return true; } -template <> bool PrecisionSupported(const Device &) { return true; } -template <> bool PrecisionSupported(const Device &device) { - auto extensions = device.Capabilities(); - return (extensions.find(kKhronosDoublePrecision) == std::string::npos) ? false : true; -} -template <> bool PrecisionSupported(const Device &device) { - auto extensions = device.Capabilities(); - return (extensions.find(kKhronosDoublePrecision) == std::string::npos) ? false : true; -} - -// ================================================================================================= - // Compiles the templated class template class Tester; template class Tester; diff --git a/test/correctness/tester.h b/test/correctness/tester.h index 06f4afbe..db714f3d 100644 --- a/test/correctness/tester.h +++ b/test/correctness/tester.h @@ -140,10 +140,6 @@ bool TestSimilarity(const T val1, const T val2); template const std::vector GetExampleScalars(const bool full_test); -// Returns false is this precision is not supported by the device -template -bool PrecisionSupported(const Device &device); - // ================================================================================================= } // namespace clblast From 798a3b6101d09a87c615f2e1e91de2d947b911da Mon Sep 17 00:00:00 2001 From: CNugteren Date: Wed, 19 Aug 2015 19:35:08 +0200 Subject: [PATCH 6/8] Add check for supported precision to the tuners --- include/internal/tuning.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/internal/tuning.h b/include/internal/tuning.h index 929658c6..f029c704 100644 --- a/include/internal/tuning.h +++ b/include/internal/tuning.h @@ -48,6 +48,16 @@ void Tuner(int argc, char* argv[]) { // Tests validity of the given arguments C::TestValidArguments(args); + // Tests for validity of the precision + { + auto platform = Platform(args.platform_id); + auto device = Device(platform, args.device_id); + if (!PrecisionSupported(device)) { + printf("* Unsupported precision, skipping this tuning run\n\n"); + return; + } + } + // Creates input buffers with random data auto x_vec = std::vector(C::GetSizeX(args)); auto y_vec = std::vector(C::GetSizeY(args)); From 07e393cce4f03184853486ecae711e8575d0c177 Mon Sep 17 00:00:00 2001 From: CNugteren Date: Wed, 19 Aug 2015 19:35:56 +0200 Subject: [PATCH 7/8] Added target to run all tuners --- CMakeLists.txt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 72cfe52f..51db02da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ set(LEVEL1_ROUTINES xaxpy) set(LEVEL2_ROUTINES xgemv xhemv xsymv) set(LEVEL3_ROUTINES xgemm xsymm xhemm xsyrk xherk xsyr2k xher2k xtrmm) set(ROUTINES ${LEVEL1_ROUTINES} ${LEVEL2_ROUTINES} ${LEVEL3_ROUTINES}) +set(PRECISIONS 32 3232 64 6464) # ================================================================================================== @@ -133,6 +134,17 @@ install(FILES include/clblast.h DESTINATION include) # ================================================================================================== +# Sets a default platform and device to run tuners and/or tests on +set(DEVICEPLATFORM ) +if(DEFINED ENV{DEFAULT_DEVICE}) + set(DEVICEPLATFORM ${DEVICEPLATFORM} -device $ENV{DEFAULT_DEVICE}) +endif() +if(DEFINED ENV{DEFAULT_PLATFORM}) + set(DEVICEPLATFORM ${DEVICEPLATFORM} -platform $ENV{DEFAULT_PLATFORM}) +endif() + +# ================================================================================================== + # This section contains all the code related to the examples if(SAMPLES) @@ -161,6 +173,17 @@ if(TUNERS) install(TARGETS tuner_${KERNEL} DESTINATION bin) endforeach() + # Adds 'alltuners' target: runs all tuners for all precisions + set(ALLTUNERS ) + set(ALLTUNERSDEPENDS ) + foreach(KERNEL ${KERNELS}) + foreach(PRECISION ${PRECISIONS}) + set(ALLTUNERS ${ALLTUNERS} COMMAND tuner_${KERNEL} -precision ${PRECISION} ${DEVICEPLATFORM}) + endforeach() + set(ALLTUNERSDEPENDS tuner_${KERNEL}) + endforeach() + add_custom_target(alltuners ${ALLTUNERS} DEPENDS ${ALLTUNERSDEPENDS}) + endif() # ================================================================================================== From 15db2bcc208d8e5bccf0464396431c7d8e6f3f28 Mon Sep 17 00:00:00 2001 From: CNugteren Date: Thu, 20 Aug 2015 08:30:51 +0200 Subject: [PATCH 8/8] Added initial version of tuner-database Python script --- src/database.py | 208 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 src/database.py diff --git a/src/database.py b/src/database.py new file mode 100644 index 00000000..2852b54c --- /dev/null +++ b/src/database.py @@ -0,0 +1,208 @@ +#!/usr/bin/env python + +# ================================================================================================== +# 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 max-width of 100 characters per line. +# +# Author(s): +# Cedric Nugteren +# +# ================================================================================================== + +# System modules +import sys +import os.path +import glob +import re +import json + +# Additional modules +import pandas as pd + +# Constants +ATTRIBUTES = ["device", "type", "vendor", "precision", "kernel_family", "arg_m", "arg_n", "arg_k"] + +# Pandas options +pd.set_option('display.width', 1000) + +# ================================================================================================== +# Database operations +# ================================================================================================== + +# Loads the database from disk +def LoadDatabase(filename): + return pd.read_pickle(filename) + +# Saves the database to disk +def SaveDatabase(df, filename): + df.to_pickle(filename) + +# Loads JSON data from file +def ImportDataFromFile(filename): + with open(filename) as f: + data = json.load(f) + json_data = pd.DataFrame(data) + df = pd.io.json.json_normalize(json_data["results"]) + for attribute in ATTRIBUTES: + if attribute == "kernel_family": + df[attribute] = re.sub(r'_\d+', '', data[attribute]) + elif attribute in data: + df[attribute] = data[attribute] + else: + df[attribute] = 0 + return df + +# Returns the row-wise concatenation of two dataframes +def ConcatenateData(df1, df2): + return pd.concat([df1, df2]) + +# Removes duplicates from a dataframe +def RemoveDuplicates(df): + return df.drop_duplicates() + +# Bests +def GetBestResults(df): + dfbest = pd.DataFrame() + grouped = df.groupby(ATTRIBUTES+["kernel"]) + for name, dfgroup in grouped: + bestcase = dfgroup.loc[[dfgroup["time"].idxmin()]] + dfbest = ConcatenateData(dfbest, bestcase) + return dfbest + +# ================================================================================================== +# C++ header generation +# ================================================================================================== + +# The C++ header +def GetHeader(family): + return(""" +// ================================================================================================= +// 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): +// Database generator +// +// This file populates the database with best-found tuning parameters for the '%s' kernels. +// +// ================================================================================================= + +namespace clblast { +// =================================================================================================""" + % family.title()) + +# The C++ footer +def GetFooter(): + return("\n} // namespace clblast\n") + +# The start of a new C++ precision entry +def GetPrecision(family, precision): + precisionstring = "Single" + if precision == "64": + precisionstring = "Double" + elif precision == "3232": + precisionstring = "ComplexSingle" + elif precision == "6464": + precisionstring = "ComplexDouble" + return("\n\nconst Database::DatabaseEntry Database::%s%s = {\n \"%s\", Precision::k%s, {\n" + % (family.title(), precisionstring, family.title(), precisionstring)) + +# The C++ device type and vendor +def GetDeviceVendor(vendor, devtype): + return(" { // %s %ss\n kDeviceType%s, kDeviceVendor%s, {\n" + % (vendor, devtype, devtype, vendor)) + +# Prints the data to a C++ database +def PrintData(df): + + # Iterates over the kernel families: creates a new file per family + for family, dffamily in df.groupby(["kernel_family"]): + dffamily = dffamily.dropna(axis=1, how='all') + f = open(family+'.h', 'w+') + f.write(GetHeader(family)) + + # Loops over the different entries for this family and prints their headers + for precision, dfprecision in dffamily.groupby(["precision"]): + f.write(GetPrecision(family, precision)) + for vendor, dfvendor in dfprecision.groupby(["vendor"]): + for devtype, dfdevtype in dfvendor.groupby(["type"]): + f.write(GetDeviceVendor(vendor, devtype)) + for device, dfdevice in dfdevtype.groupby(["device"]): + devicename = "\"%s\"," % device + f.write(" { %-20s { " % devicename) + + # Collects the paramaters for this case and prints them + parameters = [] + for kernel, dfkernel in dfdevice.groupby(["kernel"]): + dfkernel = dfkernel.dropna(axis=1) + col_names = [col for col in list(dfkernel) if col.startswith('parameters.') and col != "parameters.PRECISION"] + parameters += ["{\"%s\",%d}" % (p.replace("parameters.",""), dfkernel[p].iloc[0]) for p in col_names] + f.write(", ".join(parameters)) + f.write(" } },\n") + + # Prints the footers + f.write(" }\n },\n") + f.write(" }\n};\n\n// =================================================================================================") + f.write(GetFooter()) + +# ================================================================================================== +# Command-line arguments parsing and verification +# ================================================================================================== + +# Checks for the number of command-line arguments +if len(sys.argv) != 3: + print "[ERROR] Usage: database.py " + sys.exit() + +# Parses the command-line arguments +path_json = sys.argv[1] +path_clblast = sys.argv[2] +file_db = path_clblast+"/src/database.db" +glob_json = path_json+"/*.json" + +# Checks whether the command-line arguments are valid; exists otherwise +clblast_h = path_clblast+"/include/clblast.h" # Not used but just for validation +if not os.path.isfile(clblast_h): + print "[ERROR] The path '"+path_clblast+"' does not point to the root of the CLBlast library" + sys.exit() +if len(glob.glob(glob_json)) < 1: + print "[ERROR] The path '"+path_json+"' does not contain any JSON files" + sys.exit() + +# ================================================================================================== +# The main body of the script +# ================================================================================================== + +# Loads the database if it exists. If not, a new database is initialized +db_exists = os.path.isfile(file_db) +database = LoadDatabase(file_db) if db_exists else pd.DataFrame() + +# Loops over all JSON files in the supplied folder +for file_json in glob.glob(glob_json): + + # Loads the newly imported data + print "## Processing '"+file_json+"'", + imported_data = ImportDataFromFile(file_json) + + # Adds the new data to the database + old_size = len(database.index) + database = ConcatenateData(database, imported_data) + database = RemoveDuplicates(database) + new_size = len(database.index) + print "with "+str(new_size-old_size)+" new items" + +# Stores the new database back to disk +SaveDatabase(database, file_db) + +# Retrieves the best performing results +bests = GetBestResults(database) + +# TODO: Determines the defaults for other vendors and per vendor +#defaults = CalculateDefaults(bests) +#bests = ConcatenateData(bests, defaults) + +# Outputs the data as a C++ database +PrintData(bests) + +# ==================================================================================================