summaryrefslogtreecommitdiff
path: root/src/tuning/xgemv.cc
blob: e2d547290006ea65a263809ee5d663c5f0ed8cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// =================================================================================================
// 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 <www.cedricnugteren.nl>
//
// This file implements an auto-tuner to tune the Xgemv OpenCL kernel. It uses the CLTune library.
//
// =================================================================================================

#include <string>
#include <vector>
#include <stdexcept>

#include "internal/utilities.h"
#include "internal/tuning.h"

namespace clblast {
// =================================================================================================

// The Xgemv auto-tuner
template <typename T>
void XgemvTune(const Arguments<T> &args,
               const std::vector<T> &a_mat, const std::vector<T> &x_vec, std::vector<T> &y_vec,
               cltune::Tuner &tuner) {

  // This points to the Xgemv kernel as found in the CLBlast library
  std::string common_source =
  #include "../src/kernels/common.opencl"
  std::string kernel_source =
  #include "../src/kernels/xgemv.opencl"
  auto sources = common_source + kernel_source;
  auto id = tuner.AddKernelFromString(sources, "XgemvFast", {args.m}, {1});
  tuner.SetReferenceFromString(sources, "Xgemv", {args.m}, {64});

  // Sets the tunable parameters and their possible values
  tuner.AddParameter(id, "WGS", {64, 128, 256, 512, 1024, 1536, 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<size_t>(args.precision)});
  tuner.AddParameterReference("PRECISION", static_cast<size_t>(args.precision));

  // Sets the constraints
  auto MultipleOfX = [] (std::vector<size_t> v) { return IsMultiple(v[0], v[1]); };
  tuner.AddConstraint(id, MultipleOfX, {"WGS", "VW"});
  tuner.AddConstraint(id, MultipleOfX, {"WPT", "VW"});

  // Modifies the thread-sizes (local) based on the parameters
  tuner.MulLocalSize(id, {"WGS"});
  tuner.DivGlobalSize(id, {"WPT"});

  // 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>(args.layout));
  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);
}

// =================================================================================================

// Main function which calls the common client code with the routine-specific function as argument.
void TunerXgemv(int argc, char *argv[]) {
  switch(GetPrecision(argc, argv)) {
    case Precision::kHalf: throw std::runtime_error("Unsupported precision mode");
    case Precision::kSingle: TunerAXY<float>(argc, argv, XgemvTune<float>); break;
    case Precision::kDouble: TunerAXY<double>(argc, argv, XgemvTune<double>); break;
    case Precision::kComplexSingle: TunerAXY<float2>(argc, argv, XgemvTune<float2>); break;
    case Precision::kComplexDouble: TunerAXY<double2>(argc, argv, XgemvTune<double2>); break;
  }
}

// =================================================================================================
} // namespace clblast

// Main function (not within the clblast namespace)
int main(int argc, char *argv[]) {
  clblast::TunerXgemv(argc, argv);
  return 0;
}

// =================================================================================================