From dbdb58c6002cbd693f246f1e93919cc32ad4055a Mon Sep 17 00:00:00 2001 From: CNugteren Date: Sun, 9 Aug 2015 15:50:41 +0200 Subject: Refactored the tuners, added JSON output --- src/tuning/copy.cc | 128 ++++++++++++++--------- src/tuning/pad.cc | 124 ++++++++++++++-------- src/tuning/padtranspose.cc | 153 +++++++++++++++++----------- src/tuning/transpose.cc | 139 +++++++++++++++---------- src/tuning/tuning.cc | 249 --------------------------------------------- src/tuning/xaxpy.cc | 133 +++++++++++++++--------- src/tuning/xgemm.cc | 211 ++++++++++++++++++++++---------------- src/tuning/xgemv.cc | 182 +++++++++++++++++++-------------- 8 files changed, 647 insertions(+), 672 deletions(-) delete mode 100644 src/tuning/tuning.cc (limited to 'src') 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) { - - // 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); -} +class TuneCopy { + public: -// ================================================================================================= + // 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 &) { } -// 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; + // 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) { - - // 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}); - - // 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}); - - // 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, {"PAD_DIMX", "PAD_DIMY"}); - tuner.DivGlobalSize(id, {"PAD_WPTX", "PAD_WPTY"}); - - // Sets the function's arguments +class TunePad { + public: + + // 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" + ; + } + + // 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, "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) { - - // 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); -} +class TunePadTranspose { + public: + + // 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}); + } -// 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; + // 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) { - - // 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); -} +class TuneTranspose { + public: + + // 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}); + } -// 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; + // 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) { - - // 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"); +class TuneXaxpy { + public: + + // 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}); - - // 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); -} + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgN, kArgAlpha}; } -// ================================================================================================= + // 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"); + } + } -// 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; + // 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) { - - // 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); -} +class TuneXgemm { + public: + + // 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}; + } -// 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; + // 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) { - - // 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"}); +// See comment at top of file for a description of the class +template +class TuneXgemv { + public: + + // 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" + ; } - 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"}); + + // The list of arguments relevant for this routine + static std::vector GetOptions() { return {kArgM, kArgN, kArgAlpha, kArgBeta}; } + + // 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 + + // 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)}); + } } - 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"}); + 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 } - // Tests for a specific precision - tuner.AddParameter(id, "PRECISION", {static_cast(args.precision)}); - tuner.AddParameterReference("PRECISION", static_cast(args.precision)); - - // 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 -} + // 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 -// 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; +// 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; } } -// ================================================================================================= -} // namespace clblast - // 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; } -- cgit v1.2.3