From 4507ba4997cd546418eae0972c018073ac7b36aa Mon Sep 17 00:00:00 2001 From: CNugteren Date: Fri, 18 Sep 2015 15:25:20 +0200 Subject: Added first version of banded matrix-vector multiplication --- src/clblast.cc | 30 +- src/kernels/level2/xgemv.opencl | 395 ++++++++++++++++++++++++ src/kernels/matrix_transforms/gbgemt.opencl | 60 ++++ src/kernels/matrix_transforms/transforms.opencl | 40 +++ src/kernels/xgemv.opencl | 395 ------------------------ src/routines/level2/xgbmv.cc | 117 +++++++ src/routines/level2/xgemv.cc | 6 +- src/tuning/xgemv.cc | 2 +- 8 files changed, 638 insertions(+), 407 deletions(-) create mode 100644 src/kernels/level2/xgemv.opencl create mode 100644 src/kernels/matrix_transforms/gbgemt.opencl create mode 100644 src/kernels/matrix_transforms/transforms.opencl delete mode 100644 src/kernels/xgemv.opencl create mode 100644 src/routines/level2/xgbmv.cc (limited to 'src') 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(const Layout, const Transpose, // General banded matrix-vector multiplication: SGBMV/DGBMV/CGBMV/ZGBMV template -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(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(a_buffer), a_offset, a_ld, + Buffer(x_buffer), x_offset, x_inc, + beta, + Buffer(y_buffer), y_offset, y_inc); } template StatusCode Gbmv(const Layout, const Transpose, const size_t, const size_t, const size_t, const size_t, diff --git a/src/kernels/level2/xgemv.opencl b/src/kernels/level2/xgemv.opencl new file mode 100644 index 00000000..1e12dd78 --- /dev/null +++ b/src/kernels/level2/xgemv.opencl @@ -0,0 +1,395 @@ + +// ================================================================================================= +// 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 contains the Xgemv kernel for matrix-vector multiplication. +// +// ================================================================================================= + +// 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. + +// 1: For the full version of the kernel +#ifndef WGS1 + #define WGS1 64 // The local work-group size +#endif +#ifndef WPT1 + #define WPT1 1 // The amount of work-per-thread +#endif + +// 2: For the fast version +#ifndef WGS2 + #define WGS2 64 // The local work-group size +#endif +#ifndef WPT2 + #define WPT2 1 // The amount of work-per-thread +#endif +#ifndef VW2 + #define VW2 1 // Vector width of matrix A loads +#endif + +// 3: For the fast rotated version +#ifndef WGS3 + #define WGS3 64 // The local work-group size +#endif +#ifndef WPT3 + #define WPT3 1 // The amount of work-per-thread +#endif +#ifndef VW3 + #define VW3 1 // Vector width of matrix A loads +#endif + +// ================================================================================================= + +// Data-widths for the 'fast' kernel +#if VW2 == 1 + typedef real realVF; +#elif VW2 == 2 + typedef real2 realVF; +#elif VW2 == 4 + typedef real4 realVF; +#elif VW2 == 8 + typedef real8 realVF; +#elif VW2 == 16 + typedef real16 realVF; +#endif + +// Data-widths for the 'fast' kernel with rotated matrix +#if VW3 == 1 + typedef real realVFR; +#elif VW3 == 2 + typedef real2 realVFR; +#elif VW3 == 4 + typedef real4 realVFR; +#elif VW3 == 8 + typedef real8 realVFR; +#elif VW3 == 16 + typedef real16 realVFR; +#endif + +// ================================================================================================= +// Defines how to load the input matrix in the regular case + +// Loads a scalar input value +inline real LoadMatrixA(const __global real* restrict agm, const int x, const int y, + const int a_ld, const int a_offset) { + return agm[x + a_ld*y + a_offset]; +} +// Loads a vector input value (1/2) +inline realVF LoadMatrixAVF(const __global realVF* restrict agm, const int x, const int y, + const int a_ld) { + return agm[x + a_ld*y]; +} +// Loads a vector input value (2/2): as before, but different data-type +inline realVFR LoadMatrixAVFR(const __global realVFR* restrict agm, const int x, const int y, + const int a_ld) { + return agm[x + a_ld*y]; +} + +// ================================================================================================= + +// Full version of the kernel +__attribute__((reqd_work_group_size(WGS1, 1, 1))) +__kernel void Xgemv(const int m, const int n, const real alpha, const real beta, + const int a_rotated, + const __global real* restrict agm, const int a_offset, const int a_ld, + const __global real* restrict xgm, const int x_offset, const int x_inc, + __global real* ygm, const int y_offset, const int y_inc, + const int do_conjugate) { + + // Local memory for the vector X + __local real xlm[WGS1]; + + // Initializes the accumulation register + real acc[WPT1]; + #pragma unroll + for (int w=0; w 'm' and 'n' are multiples of WGS2 +// --> 'a_offset' is 0 +// --> 'a_ld' is a multiple of VW2 +// --> 'a_rotated' is 0 +// --> 'do_conjugate' is 0 +__attribute__((reqd_work_group_size(WGS2, 1, 1))) +__kernel void XgemvFast(const int m, const int n, const real alpha, const real beta, + const int a_rotated, + const __global realVF* restrict agm, const int a_offset, const int a_ld, + const __global real* restrict xgm, const int x_offset, const int x_inc, + __global real* ygm, const int y_offset, const int y_inc, + const int do_conjugate) { + // Local memory for the vector X + __local real xlm[WGS2]; + + // Initializes the accumulation register + real acc[WPT2]; + #pragma unroll + for (int w=0; w 'm' and 'n' are multiples of WGS3 +// --> 'a_offset' is 0 +// --> 'a_ld' is a multiple of VW3 +// --> 'a_rotated' is 1 +// --> 'do_conjugate' is 0 +__attribute__((reqd_work_group_size(WGS3, 1, 1))) +__kernel void XgemvFastRot(const int m, const int n, const real alpha, const real beta, + const int a_rotated, + const __global realVFR* restrict agm, const int a_offset, const int a_ld, + const __global real* restrict xgm, const int x_offset, const int x_inc, + __global real* ygm, const int y_offset, const int y_inc, + const int do_conjugate) { + // Local memory for the vector X + __local real xlm[WGS3]; + + // Initializes the accumulation register + real acc[WPT3]; + #pragma unroll + for (int w=0; w +// +// 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= 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 +// +// 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/kernels/xgemv.opencl b/src/kernels/xgemv.opencl deleted file mode 100644 index 1e12dd78..00000000 --- a/src/kernels/xgemv.opencl +++ /dev/null @@ -1,395 +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 contains the Xgemv kernel for matrix-vector multiplication. -// -// ================================================================================================= - -// 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. - -// 1: For the full version of the kernel -#ifndef WGS1 - #define WGS1 64 // The local work-group size -#endif -#ifndef WPT1 - #define WPT1 1 // The amount of work-per-thread -#endif - -// 2: For the fast version -#ifndef WGS2 - #define WGS2 64 // The local work-group size -#endif -#ifndef WPT2 - #define WPT2 1 // The amount of work-per-thread -#endif -#ifndef VW2 - #define VW2 1 // Vector width of matrix A loads -#endif - -// 3: For the fast rotated version -#ifndef WGS3 - #define WGS3 64 // The local work-group size -#endif -#ifndef WPT3 - #define WPT3 1 // The amount of work-per-thread -#endif -#ifndef VW3 - #define VW3 1 // Vector width of matrix A loads -#endif - -// ================================================================================================= - -// Data-widths for the 'fast' kernel -#if VW2 == 1 - typedef real realVF; -#elif VW2 == 2 - typedef real2 realVF; -#elif VW2 == 4 - typedef real4 realVF; -#elif VW2 == 8 - typedef real8 realVF; -#elif VW2 == 16 - typedef real16 realVF; -#endif - -// Data-widths for the 'fast' kernel with rotated matrix -#if VW3 == 1 - typedef real realVFR; -#elif VW3 == 2 - typedef real2 realVFR; -#elif VW3 == 4 - typedef real4 realVFR; -#elif VW3 == 8 - typedef real8 realVFR; -#elif VW3 == 16 - typedef real16 realVFR; -#endif - -// ================================================================================================= -// Defines how to load the input matrix in the regular case - -// Loads a scalar input value -inline real LoadMatrixA(const __global real* restrict agm, const int x, const int y, - const int a_ld, const int a_offset) { - return agm[x + a_ld*y + a_offset]; -} -// Loads a vector input value (1/2) -inline realVF LoadMatrixAVF(const __global realVF* restrict agm, const int x, const int y, - const int a_ld) { - return agm[x + a_ld*y]; -} -// Loads a vector input value (2/2): as before, but different data-type -inline realVFR LoadMatrixAVFR(const __global realVFR* restrict agm, const int x, const int y, - const int a_ld) { - return agm[x + a_ld*y]; -} - -// ================================================================================================= - -// Full version of the kernel -__attribute__((reqd_work_group_size(WGS1, 1, 1))) -__kernel void Xgemv(const int m, const int n, const real alpha, const real beta, - const int a_rotated, - const __global real* restrict agm, const int a_offset, const int a_ld, - const __global real* restrict xgm, const int x_offset, const int x_inc, - __global real* ygm, const int y_offset, const int y_inc, - const int do_conjugate) { - - // Local memory for the vector X - __local real xlm[WGS1]; - - // Initializes the accumulation register - real acc[WPT1]; - #pragma unroll - for (int w=0; w 'm' and 'n' are multiples of WGS2 -// --> 'a_offset' is 0 -// --> 'a_ld' is a multiple of VW2 -// --> 'a_rotated' is 0 -// --> 'do_conjugate' is 0 -__attribute__((reqd_work_group_size(WGS2, 1, 1))) -__kernel void XgemvFast(const int m, const int n, const real alpha, const real beta, - const int a_rotated, - const __global realVF* restrict agm, const int a_offset, const int a_ld, - const __global real* restrict xgm, const int x_offset, const int x_inc, - __global real* ygm, const int y_offset, const int y_inc, - const int do_conjugate) { - // Local memory for the vector X - __local real xlm[WGS2]; - - // Initializes the accumulation register - real acc[WPT2]; - #pragma unroll - for (int w=0; w 'm' and 'n' are multiples of WGS3 -// --> 'a_offset' is 0 -// --> 'a_ld' is a multiple of VW3 -// --> 'a_rotated' is 1 -// --> 'do_conjugate' is 0 -__attribute__((reqd_work_group_size(WGS3, 1, 1))) -__kernel void XgemvFastRot(const int m, const int n, const real alpha, const real beta, - const int a_rotated, - const __global realVFR* restrict agm, const int a_offset, const int a_ld, - const __global real* restrict xgm, const int x_offset, const int x_inc, - __global real* ygm, const int y_offset, const int y_inc, - const int do_conjugate) { - // Local memory for the vector X - __local real xlm[WGS3]; - - // Initializes the accumulation register - real acc[WPT3]; - #pragma unroll - for (int w=0; w +// +// This file implements the Xgbmv class (see the header for information about the class). +// +// ================================================================================================= + +#include "internal/routines/level2/xgbmv.h" + +#include +#include + +namespace clblast { +// ================================================================================================= + +// Constructor: forwards to base class constructor +template +Xgbmv::Xgbmv(Queue &queue, Event &event, const std::string &name): + Xgemv(queue, event, name) { +} + +// ================================================================================================= + +// The main routine +template +StatusCode Xgbmv::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 &a_buffer, const size_t a_offset, const size_t a_ld, + const Buffer &x_buffer, const size_t x_offset, const size_t x_inc, + const T beta, + const Buffer &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(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(a_one)); + kernel.SetArgument(1, static_cast(a_two)); + kernel.SetArgument(2, static_cast(a_ld)); + kernel.SetArgument(3, static_cast(a_offset)); + kernel.SetArgument(4, a_buffer()); + kernel.SetArgument(5, static_cast(t_one)); + kernel.SetArgument(6, static_cast(t_two)); + kernel.SetArgument(7, static_cast(t_one)); + kernel.SetArgument(8, static_cast(0)); + kernel.SetArgument(9, t_buffer()); + kernel.SetArgument(10, static_cast(layout)); + if (rotated) { + kernel.SetArgument(11, static_cast(ku)); + kernel.SetArgument(12, static_cast(kl)); + } + else { + kernel.SetArgument(11, static_cast(kl)); + kernel.SetArgument(12, static_cast(ku)); + } + + // Uses the common matrix-transforms thread configuration + auto global = std::vector{Ceil(CeilDiv(t_one, db_["PAD_WPTX"]), db_["PAD_DIMX"]), + Ceil(CeilDiv(t_two, db_["PAD_WPTY"]), db_["PAD_DIMY"])}; + auto local = std::vector{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; +template class Xgbmv; +template class Xgbmv; +template class Xgbmv; + +// ================================================================================================= +} // 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 Xgemv::Xgemv(Queue &queue, Event &event, const std::string &name): Routine(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" ; } -- cgit v1.2.3