summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCNugteren <web@cedricnugteren.nl>2015-09-18 15:25:20 +0200
committerCNugteren <web@cedricnugteren.nl>2015-09-18 15:25:20 +0200
commit4507ba4997cd546418eae0972c018073ac7b36aa (patch)
tree08e549a9e4f174a85eb7d9a8efd3735b1daae44a /src
parent42db8ea968d9d2972446aa4fd73515a3d7aa093e (diff)
Added first version of banded matrix-vector multiplication
Diffstat (limited to 'src')
-rw-r--r--src/clblast.cc30
-rw-r--r--src/kernels/level2/xgemv.opencl (renamed from src/kernels/xgemv.opencl)0
-rw-r--r--src/kernels/matrix_transforms/gbgemt.opencl60
-rw-r--r--src/kernels/matrix_transforms/transforms.opencl40
-rw-r--r--src/routines/level2/xgbmv.cc117
-rw-r--r--src/routines/level2/xgemv.cc6
-rw-r--r--src/tuning/xgemv.cc2
7 files changed, 243 insertions, 12 deletions
diff --git a/src/clblast.cc b/src/clblast.cc
index a0dd8c70..ad5e354d 100644
--- a/src/clblast.cc
+++ b/src/clblast.cc
@@ -28,6 +28,7 @@
// BLAS level-2 includes
#include "internal/routines/level2/xgemv.h"
+#include "internal/routines/level2/xgbmv.h"
#include "internal/routines/level2/xhemv.h"
#include "internal/routines/level2/xsymv.h"
@@ -327,15 +328,26 @@ template StatusCode Gemv<double2>(const Layout, const Transpose,
// General banded matrix-vector multiplication: SGBMV/DGBMV/CGBMV/ZGBMV
template <typename T>
-StatusCode Gbmv(const Layout, const Transpose,
- const size_t, const size_t, const size_t, const size_t,
- const T,
- const cl_mem, const size_t, const size_t,
- const cl_mem, const size_t, const size_t,
- const T,
- cl_mem, const size_t, const size_t,
- cl_command_queue*, cl_event*) {
- return StatusCode::kNotImplemented;
+StatusCode Gbmv(const Layout layout, const Transpose a_transpose,
+ const size_t m, const size_t n, const size_t kl, const size_t ku,
+ const T alpha,
+ const cl_mem a_buffer, const size_t a_offset, const size_t a_ld,
+ const cl_mem x_buffer, const size_t x_offset, const size_t x_inc,
+ const T beta,
+ cl_mem y_buffer, const size_t y_offset, const size_t y_inc,
+ cl_command_queue* queue, cl_event* event) {
+ auto queue_cpp = Queue(*queue);
+ auto event_cpp = Event(*event);
+ auto routine = Xgbmv<T>(queue_cpp, event_cpp);
+ auto status = routine.SetUp();
+ if (status != StatusCode::kSuccess) { return status; }
+ return routine.DoGbmv(layout, a_transpose,
+ m, n, kl, ku,
+ alpha,
+ Buffer<T>(a_buffer), a_offset, a_ld,
+ Buffer<T>(x_buffer), x_offset, x_inc,
+ beta,
+ Buffer<T>(y_buffer), y_offset, y_inc);
}
template StatusCode Gbmv<float>(const Layout, const Transpose,
const size_t, const size_t, const size_t, const size_t,
diff --git a/src/kernels/xgemv.opencl b/src/kernels/level2/xgemv.opencl
index 1e12dd78..1e12dd78 100644
--- a/src/kernels/xgemv.opencl
+++ b/src/kernels/level2/xgemv.opencl
diff --git a/src/kernels/matrix_transforms/gbgemt.opencl b/src/kernels/matrix_transforms/gbgemt.opencl
new file mode 100644
index 00000000..e46e3a59
--- /dev/null
+++ b/src/kernels/matrix_transforms/gbgemt.opencl
@@ -0,0 +1,60 @@
+
+// =================================================================================================
+// 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 contains the general banded (gb) to general (ge) matrix transforms.
+//
+// This kernel uses the matrix-transforms common tuning parameters.
+//
+// =================================================================================================
+
+// Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string
+// literal). Comment-out this line for syntax-highlighting when developing.
+R"(
+
+// =================================================================================================
+#if defined(ROUTINE_GBMV)
+
+// Kernel to transform a general banded matrix into a general matrix
+__attribute__((reqd_work_group_size(PAD_DIMX, PAD_DIMY, 1)))
+__kernel void GeneralBandedToGeneral(const int src_one, const int src_two,
+ const int src_ld, const int src_offset,
+ __global const real* restrict src,
+ const int dest_one, const int dest_two,
+ const int dest_ld, const int dest_offset,
+ __global real* dest,
+ const int layout,
+ const int kl, const int ku) {
+
+ // Loops over the work per thread in both dimensions
+ #pragma unroll
+ for (int w_one=0; w_one<PAD_WPTX; ++w_one) {
+ const int id_one = (get_group_id(0)*PAD_WPTX + w_one) * PAD_DIMX + get_local_id(0);
+ #pragma unroll
+ for (int w_two=0; w_two<PAD_WPTY; ++w_two) {
+ const int id_two = (get_group_id(1)*PAD_WPTY + w_two) * PAD_DIMY + get_local_id(1);
+ if (id_two < dest_two && id_one < dest_one) {
+ real result;
+ SetToZero(result);
+ const int k = ku - id_two + id_one;
+ if ((id_one >= id_two - ku) && (id_one < id_two + kl + 1)) {
+ result = src[id_two*src_ld + k + src_offset];
+ }
+ dest[id_two*dest_ld + id_one + dest_offset] = result;
+ }
+ }
+ }
+}
+
+#endif
+// =================================================================================================
+
+// End of the C++11 raw string literal
+)"
+
+// =================================================================================================
diff --git a/src/kernels/matrix_transforms/transforms.opencl b/src/kernels/matrix_transforms/transforms.opencl
new file mode 100644
index 00000000..01889a13
--- /dev/null
+++ b/src/kernels/matrix_transforms/transforms.opencl
@@ -0,0 +1,40 @@
+
+// =================================================================================================
+// 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 contains the common functions and parameters specific for matrix-transform kernels.
+//
+// =================================================================================================
+
+// Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string
+// literal). Comment-out this line for syntax-highlighting when developing.
+R"(
+
+// =================================================================================================
+
+// Parameters set by the tuner or by the database. Here they are given a basic default value in case
+// this kernel file is used outside of the CLBlast library.
+#ifndef PAD_DIMX
+ #define PAD_DIMX 8 // Local workgroup size in the first dimension (x)
+#endif
+#ifndef PAD_DIMY
+ #define PAD_DIMY 8 // Local workgroup size in the second dimension (y)
+#endif
+#ifndef PAD_WPTX
+ #define PAD_WPTX 1 // Work per thread in the first dimension (x)
+#endif
+#ifndef PAD_WPTY
+ #define PAD_WPTY 1 // Work per thread in the second dimension (y)
+#endif
+
+// =================================================================================================
+
+// End of the C++11 raw string literal
+)"
+
+// =================================================================================================
diff --git a/src/routines/level2/xgbmv.cc b/src/routines/level2/xgbmv.cc
new file mode 100644
index 00000000..eac208b3
--- /dev/null
+++ b/src/routines/level2/xgbmv.cc
@@ -0,0 +1,117 @@
+
+// =================================================================================================
+// 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 Xgbmv class (see the header for information about the class).
+//
+// =================================================================================================
+
+#include "internal/routines/level2/xgbmv.h"
+
+#include <string>
+#include <vector>
+
+namespace clblast {
+// =================================================================================================
+
+// Constructor: forwards to base class constructor
+template <typename T>
+Xgbmv<T>::Xgbmv(Queue &queue, Event &event, const std::string &name):
+ Xgemv<T>(queue, event, name) {
+}
+
+// =================================================================================================
+
+// The main routine
+template <typename T>
+StatusCode Xgbmv<T>::DoGbmv(const Layout layout, const Transpose a_transpose,
+ const size_t m, const size_t n, const size_t kl, const size_t ku,
+ 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) {
+
+ // Makes sure all dimensions are larger than zero
+ if (n == 0 || m == 0) { return StatusCode::kInvalidDimension; }
+
+ //
+ auto rotated = (layout == Layout::kRowMajor);
+ auto t_one = (rotated) ? n : m;
+ auto t_two = (rotated) ? m : n;
+ auto a_one = kl+ku+1;
+ auto a_two = (rotated) ? m : n;
+
+ // Checks for validity of the A matrix
+ auto status = StatusCode::kSuccess;
+ if (a_ld < a_one) { return StatusCode::kInvalidLeadDimA; }
+ try {
+ auto required_size = (a_ld*a_two + a_offset)*sizeof(T);
+ auto buffer_size = a_buffer.GetSize();
+ if (buffer_size < required_size) { return StatusCode::kInsufficientMemoryA; }
+ } catch (...) { return StatusCode::kInvalidMatrixA; }
+
+ // Temporary buffer to generalize the input matrix
+ try {
+ auto t_buffer = Buffer<T>(context_, t_one*t_two);
+
+ // Creates a general matrix from the input to be able to run the regular Xgemv routine
+ try {
+ auto& program = GetProgramFromCache();
+ auto kernel = Kernel(program, "GeneralBandedToGeneral");
+
+ // Sets the arguments for the matrix transform kernel
+ kernel.SetArgument(0, static_cast<int>(a_one));
+ kernel.SetArgument(1, static_cast<int>(a_two));
+ kernel.SetArgument(2, static_cast<int>(a_ld));
+ kernel.SetArgument(3, static_cast<int>(a_offset));
+ kernel.SetArgument(4, a_buffer());
+ kernel.SetArgument(5, static_cast<int>(t_one));
+ kernel.SetArgument(6, static_cast<int>(t_two));
+ kernel.SetArgument(7, static_cast<int>(t_one));
+ kernel.SetArgument(8, static_cast<int>(0));
+ kernel.SetArgument(9, t_buffer());
+ kernel.SetArgument(10, static_cast<int>(layout));
+ if (rotated) {
+ kernel.SetArgument(11, static_cast<int>(ku));
+ kernel.SetArgument(12, static_cast<int>(kl));
+ }
+ else {
+ kernel.SetArgument(11, static_cast<int>(kl));
+ kernel.SetArgument(12, static_cast<int>(ku));
+ }
+
+ // Uses the common matrix-transforms thread configuration
+ auto global = std::vector<size_t>{Ceil(CeilDiv(t_one, db_["PAD_WPTX"]), db_["PAD_DIMX"]),
+ Ceil(CeilDiv(t_two, db_["PAD_WPTY"]), db_["PAD_DIMY"])};
+ auto local = std::vector<size_t>{db_["PAD_DIMX"], db_["PAD_DIMY"]};
+ status = RunKernel(kernel, global, local);
+ if (ErrorIn(status)) { return status; }
+
+ // Runs the regular Xgemv code
+ status = DoGemv(layout, a_transpose, m, n, alpha,
+ t_buffer, 0, t_one,
+ x_buffer, x_offset, x_inc, beta,
+ y_buffer, y_offset, y_inc);
+
+ // Return the status of the Xgemv routine
+ return status;
+ } catch (...) { return StatusCode::kInvalidKernel; }
+ } catch (...) { return StatusCode::kTempBufferAllocFailure; }
+}
+
+// =================================================================================================
+
+// Compiles the templated class
+template class Xgbmv<float>;
+template class Xgbmv<double>;
+template class Xgbmv<float2>;
+template class Xgbmv<double2>;
+
+// =================================================================================================
+} // namespace clblast
diff --git a/src/routines/level2/xgemv.cc b/src/routines/level2/xgemv.cc
index f95a9957..e52d2f20 100644
--- a/src/routines/level2/xgemv.cc
+++ b/src/routines/level2/xgemv.cc
@@ -32,8 +32,10 @@ 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/pad.opencl" // For {Herm,Symm}{Upper,Lower}ToSquared (for HEMV/SYMV)
- #include "../../kernels/xgemv.opencl"
+ #include "../../kernels/pad.opencl" // TODO: replace
+ #include "../../kernels/matrix_transforms/transforms.opencl"
+ #include "../../kernels/matrix_transforms/gbgemt.opencl"
+ #include "../../kernels/level2/xgemv.opencl"
;
}
diff --git a/src/tuning/xgemv.cc b/src/tuning/xgemv.cc
index 3d6fe595..6a066518 100644
--- a/src/tuning/xgemv.cc
+++ b/src/tuning/xgemv.cc
@@ -34,7 +34,7 @@ class TuneXgemv {
static std::string GetSources() {
return
#include "../src/kernels/common.opencl"
- #include "../src/kernels/xgemv.opencl"
+ #include "../src/kernels/level2/xgemv.opencl"
;
}