summaryrefslogtreecommitdiff
path: root/src/routines/level2/xgemv.cc
blob: 79cf8248472a49f4ad48c6823f1cdf52cee463ad (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// =================================================================================================
// 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 the Xgemv class (see the header for information about the class).
//
// =================================================================================================

#include "internal/routines/level2/xgemv.h"

#include <string>
#include <vector>

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

// Specific implementations to get the memory-type based on a template argument
template <> const Precision Xgemv<float>::precision_ = Precision::kSingle;
template <> const Precision Xgemv<double>::precision_ = Precision::kDouble;
template <> const Precision Xgemv<float2>::precision_ = Precision::kComplexSingle;
template <> const Precision Xgemv<double2>::precision_ = Precision::kComplexDouble;

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

// Constructor: forwards to base class constructor
template <typename T>
Xgemv<T>::Xgemv(Queue &queue, Event &event, const std::string &name):
    Routine<T>(queue, event, name, {"Pad", "Xgemv"}, precision_) {
  source_string_ =
    #include "../../kernels/level2/xgemv.opencl"
    #include "../../kernels/level2/xgemv_fast.opencl"
  ;
}

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

// The main routine
template <typename T>
StatusCode Xgemv<T>::DoGemv(const Layout layout, const Transpose a_transpose,
                            const size_t m, const size_t n,
                            const T alpha,
                            const Buffer<T> &a_buffer, const size_t a_offset, const size_t a_ld,
                            const Buffer<T> &x_buffer, const size_t x_offset, const size_t x_inc,
                            const T beta,
                            const Buffer<T> &y_buffer, const size_t y_offset, const size_t y_inc) {

  // Performs the matrix-vector multiplication
  return MatVec(layout, a_transpose,
                m, n, alpha,
                a_buffer, a_offset, a_ld,
                x_buffer, x_offset, x_inc, beta,
                y_buffer, y_offset, y_inc,
                true, true,
                0, false, 0, 0); // N/A for this routine
}

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

// The generic implementation, also suited for other (non general) matrix-vector multiplications
template <typename T>
StatusCode Xgemv<T>::MatVec(const Layout layout, const Transpose a_transpose,
                            const size_t m, const size_t n,
                            const T alpha,
                            const Buffer<T> &a_buffer, const size_t a_offset, const size_t a_ld,
                            const Buffer<T> &x_buffer, const size_t x_offset, const size_t x_inc,
                            const T beta,
                            const Buffer<T> &y_buffer, const size_t y_offset, const size_t y_inc,
                            bool fast_kernel, bool fast_kernel_rot,
                            const size_t parameter, const bool packed,
                            const size_t kl, const size_t ku) {

  // Makes sure all dimensions are larger than zero
  if (m == 0 || n == 0) { return StatusCode::kInvalidDimension; }

  // Computes whether or not the matrix has an alternative layout (row or column-major).
  auto a_altlayout = (layout == Layout::kRowMajor);
  auto a_one = (a_altlayout) ? n : m;
  auto a_two = (a_altlayout) ? m : n;

  // Swap m and n if the matrix is transposed
  auto a_transposed = (a_transpose != Transpose::kNo);
  auto m_real = (a_transposed) ? n : m;
  auto n_real = (a_transposed) ? m : n;

  // Special adjustments for banded matrices
  if (kl != 0 || ku != 0) {
    a_one = kl+ku+1;
  }

  // Determines whether the kernel needs to perform rotated access ('^' is the XOR operator)
  auto a_rotated = a_transposed ^ a_altlayout;

  // In case of complex data-types, the transpose can also become a conjugate transpose
  auto a_conjugate = (a_transpose == Transpose::kConjugate);

  // Tests the matrix and the vectors for validity
  auto status = StatusCode::kSuccess;
  if (packed) { status = TestMatrixAP(n, a_buffer, a_offset, sizeof(T)); }
  else { status = TestMatrixA(a_one, a_two, a_buffer, a_offset, a_ld, sizeof(T)); }
  if (ErrorIn(status)) { return status; }
  status = TestVectorX(n_real, x_buffer, x_offset, x_inc, sizeof(T));
  if (ErrorIn(status)) { return status; }
  status = TestVectorY(m_real, y_buffer, y_offset, y_inc, sizeof(T));
  if (ErrorIn(status)) { return status; }

  // Determines whether or not the fast-version can be used
  fast_kernel = fast_kernel && (a_offset == 0) && (a_rotated == 0) && (a_conjugate == 0) &&
                IsMultiple(m, db_["WGS2"]*db_["WPT2"]) &&
                IsMultiple(n, db_["WGS2"]) &&
                IsMultiple(a_ld, db_["VW2"]);
  fast_kernel_rot = fast_kernel_rot && (a_offset == 0) && (a_rotated == 1) && (a_conjugate == 0) &&
                    IsMultiple(m, db_["WGS3"]*db_["WPT3"]) &&
                    IsMultiple(n, db_["WGS3"]) &&
                    IsMultiple(a_ld, db_["VW3"]);

  // If possible, run the fast-version (rotated or non-rotated) of the kernel
  auto kernel_name = "Xgemv";
  auto m_ceiled = Ceil(m_real, db_["WGS1"]*db_["WPT1"]);
  auto global_size = m_ceiled / db_["WPT1"];
  auto local_size = db_["WGS1"];
  if (fast_kernel) {
    kernel_name = "XgemvFast";
    global_size = m_real / db_["WPT2"];
    local_size = db_["WGS2"];
  }
  if (fast_kernel_rot) {
    kernel_name = "XgemvFastRot";
    global_size = m_real / db_["WPT3"];
    local_size = db_["WGS3"];
  }

  // Retrieves the Xgemv kernel from the compiled binary
  try {
    auto& program = GetProgramFromCache();
    auto kernel = Kernel(program, kernel_name);

    // Sets the kernel arguments
    kernel.SetArgument(0, static_cast<int>(m_real));
    kernel.SetArgument(1, static_cast<int>(n_real));
    kernel.SetArgument(2, alpha);
    kernel.SetArgument(3, beta);
    kernel.SetArgument(4, static_cast<int>(a_rotated));
    kernel.SetArgument(5, a_buffer());
    kernel.SetArgument(6, static_cast<int>(a_offset));
    kernel.SetArgument(7, static_cast<int>(a_ld));
    kernel.SetArgument(8, x_buffer());
    kernel.SetArgument(9, static_cast<int>(x_offset));
    kernel.SetArgument(10, static_cast<int>(x_inc));
    kernel.SetArgument(11, y_buffer());
    kernel.SetArgument(12, static_cast<int>(y_offset));
    kernel.SetArgument(13, static_cast<int>(y_inc));
    kernel.SetArgument(14, static_cast<int>(a_conjugate));
    kernel.SetArgument(15, static_cast<int>(parameter)); // extra parameter used for symm/herm
    kernel.SetArgument(16, static_cast<int>(kl)); // only used for banded matrices
    kernel.SetArgument(17, static_cast<int>(ku)); // only used for banded matrices

    // Launches the kernel
    auto global = std::vector<size_t>{global_size};
    auto local = std::vector<size_t>{local_size};
    status = RunKernel(kernel, global, local);
    if (ErrorIn(status)) { return status; }

    // Waits for all kernels to finish
    queue_.Finish();

    // Succesfully finished the computation
    return StatusCode::kSuccess;
  } catch (...) { return StatusCode::kInvalidKernel; }
}

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

// Compiles the templated class
template class Xgemv<float>;
template class Xgemv<double>;
template class Xgemv<float2>;
template class Xgemv<double2>;

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