summaryrefslogtreecommitdiff
path: root/src/tuning/xgemv.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/tuning/xgemv.cc')
-rw-r--r--src/tuning/xgemv.cc182
1 files changed, 104 insertions, 78 deletions
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 <www.cedricnugteren.nl>
//
-// 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 <string>
#include <vector>
-#include <stdexcept>
#include "internal/utilities.h"
#include "internal/tuning.h"
@@ -25,93 +23,121 @@
namespace clblast {
// =================================================================================================
-// The Xgemv auto-tuner
-template <typename T>
-void XgemvTune(const Arguments<T> &args, const size_t variation,
- const std::vector<T> &a_mat, const std::vector<T> &x_vec, std::vector<T> &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<size_t> 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 <typename T, int V>
+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<std::string> GetOptions() { return {kArgM, kArgN, kArgAlpha, kArgBeta}; }
+
+ // Tests for valid arguments
+ static void TestValidArguments(const Arguments<T> &) { }
+
+ // 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<T> &args) { return args.n; }
+ static size_t GetSizeY(const Arguments<T> &args) { return args.m; }
+ static size_t GetSizeA(const Arguments<T> &args) { return args.m * args.n; }
+ static size_t GetSizeB(const Arguments<T> &) { return 1; } // N/A for this kernel
+ static size_t GetSizeC(const Arguments<T> &) { 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<size_t> 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<T> &) { }
+
+ // Sets the base thread configuration
+ static std::vector<size_t> GlobalSize(const Arguments<T> &args) { return {args.m}; }
+ static std::vector<size_t> LocalSize() { return {1}; }
+ static std::vector<size_t> LocalSizeRef() { return {64}; }
+
+ // Transforms the thread configuration based on the parameters
+ using TransformVector = std::vector<std::vector<std::string>>;
+ 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<T> &args,
+ std::vector<T> &x_vec, std::vector<T> &y_vec,
+ std::vector<T> &a_mat, std::vector<T> &, std::vector<T> &) {
+ auto a_rotated = (V==3) ? 1 : 0;
+ tuner.AddArgumentScalar(static_cast<int>(args.m));
+ tuner.AddArgumentScalar(static_cast<int>(args.n));
+ tuner.AddArgumentScalar(args.alpha);
+ tuner.AddArgumentScalar(args.beta);
+ tuner.AddArgumentScalar(static_cast<int>(a_rotated));
+ tuner.AddArgumentInput(a_mat);
+ tuner.AddArgumentScalar(0);
+ tuner.AddArgumentScalar(static_cast<int>(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<size_t>(args.precision)});
- tuner.AddParameterReference("PRECISION", static_cast<size_t>(args.precision));
-
- // Sets the function's arguments
- tuner.AddArgumentScalar(static_cast<int>(args.m));
- tuner.AddArgumentScalar(static_cast<int>(args.n));
- tuner.AddArgumentScalar(args.alpha);
- tuner.AddArgumentScalar(args.beta);
- tuner.AddArgumentScalar(static_cast<int>(a_rotated));
- tuner.AddArgumentInput(a_mat);
- tuner.AddArgumentScalar(0);
- tuner.AddArgumentScalar(static_cast<int>(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<T> &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<float>(argc, argv, num_variations, XgemvTune<float>); break;
- case Precision::kDouble: TunerAXY<double>(argc, argv, num_variations, XgemvTune<double>); break;
- case Precision::kComplexSingle: TunerAXY<float2>(argc, argv, num_variations, XgemvTune<float2>); break;
- case Precision::kComplexDouble: TunerAXY<double2>(argc, argv, num_variations, XgemvTune<double2>); 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 <int V>
+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<clblast::TuneXgemv<float,V>, float>(argc, argv); break;
+ case clblast::Precision::kDouble: clblast::Tuner<clblast::TuneXgemv<double,V>, double>(argc, argv); break;
+ case clblast::Precision::kComplexSingle: clblast::Tuner<clblast::TuneXgemv<float2,V>, float2>(argc, argv); break;
+ case clblast::Precision::kComplexDouble: clblast::Tuner<clblast::TuneXgemv<double2,V>, 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;
}