diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clblast.cpp | 37 | ||||
-rw-r--r-- | src/clblast_c.cpp | 67 | ||||
-rw-r--r-- | src/clblast_netlib_c.cpp | 90 | ||||
-rw-r--r-- | src/routines/levelx/xim2col.cpp | 55 | ||||
-rw-r--r-- | src/routines/levelx/xim2col.hpp | 44 |
5 files changed, 293 insertions, 0 deletions
diff --git a/src/clblast.cpp b/src/clblast.cpp index ca401066..9089b17c 100644 --- a/src/clblast.cpp +++ b/src/clblast.cpp @@ -72,6 +72,7 @@ // Level-x includes (non-BLAS) #include "routines/levelx/xomatcopy.hpp" +#include "routines/levelx/xim2col.hpp" #include "routines/levelx/xaxpybatched.hpp" #include "routines/levelx/xgemmbatched.hpp" @@ -2212,6 +2213,42 @@ template StatusCode PUBLIC_API Omatcopy<half>(const Layout, const Transpose, cl_mem, const size_t, const size_t, cl_command_queue*, cl_event*); +// Im2col function (non-BLAS function): SIM2COL/DIM2COL/CIM2COL/ZIM2COL/HIM2COL +template <typename T> +StatusCode Im2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + auto queue_cpp = Queue(*queue); + auto routine = Xim2col<T>(queue_cpp, event); + routine.DoIm2col(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + Buffer<T>(im_buffer), im_offset, + Buffer<T>(col_buffer), col_offset); + return StatusCode::kSuccess; + } catch (...) { return DispatchException(); } +} +template StatusCode PUBLIC_API Im2col<float>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<double>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<float2>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<double2>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<half>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); + // Batched version of AXPY: SAXPYBATCHED/DAXPYBATCHED/CAXPYBATCHED/ZAXPYBATCHED/HAXPYBATCHED template <typename T> StatusCode AxpyBatched(const size_t n, diff --git a/src/clblast_c.cpp b/src/clblast_c.cpp index d2656274..24697779 100644 --- a/src/clblast_c.cpp +++ b/src/clblast_c.cpp @@ -3515,6 +3515,73 @@ CLBlastStatusCode CLBlastHomatcopy(const CLBlastLayout layout, const CLBlastTran } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } } +// IM2COL +CLBlastStatusCode CLBlastSim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<float>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastDim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<double>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastCim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<float2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastZim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<double2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastHim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<half>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} + // AXPY CLBlastStatusCode CLBlastSaxpyBatched(const size_t n, const float *alphas, diff --git a/src/clblast_netlib_c.cpp b/src/clblast_netlib_c.cpp index d3b9b5e6..7859dddf 100644 --- a/src/clblast_netlib_c.cpp +++ b/src/clblast_netlib_c.cpp @@ -4735,4 +4735,94 @@ void cblas_zomatcopy(const CLBlastLayout layout, const CLBlastTranspose a_transp b_buffer.Read(queue, b_size, reinterpret_cast<double2*>(b)); } +// IM2COL +void cblas_sim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const float* im, + float* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<float>(context, im_size); + auto col_buffer = clblast::Buffer<float>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const float*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<float*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<float>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<float*>(col)); +} +void cblas_dim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const double* im, + double* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<double>(context, im_size); + auto col_buffer = clblast::Buffer<double>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const double*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<double*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<double>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<double*>(col)); +} +void cblas_cim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const void* im, + void* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<float2>(context, im_size); + auto col_buffer = clblast::Buffer<float2>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const float2*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<float2*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<float2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<float2*>(col)); +} +void cblas_zim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const void* im, + void* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<double2>(context, im_size); + auto col_buffer = clblast::Buffer<double2>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const double2*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<double2*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<double2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<double2*>(col)); +} + // ================================================================================================= diff --git a/src/routines/levelx/xim2col.cpp b/src/routines/levelx/xim2col.cpp new file mode 100644 index 00000000..150220d6 --- /dev/null +++ b/src/routines/levelx/xim2col.cpp @@ -0,0 +1,55 @@ + +// ================================================================================================= +// 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 Xim2col class (see the header for information about the class). +// +// ================================================================================================= + +#include "routines/levelx/xim2col.hpp" + +#include <string> +#include <vector> + +namespace clblast { +// ================================================================================================= + +// Constructor: forwards to base class constructor +template <typename T> +Xim2col<T>::Xim2col(Queue &queue, EventPointer event, const std::string &name): + Routine(queue, event, name, {}, PrecisionValue<T>(), {}, { +#include "../../kernels/level3/level3.opencl" + }) { +} + +// ================================================================================================= + +// The main routine +template <typename T> +void Xim2col<T>::DoIm2col(const size_t channels, const size_t height, const size_t width, + const size_t kernel_h, const size_t kernel_w, const size_t pad_h, + const size_t pad_w, const size_t stride_h, const size_t stride_w, + const size_t dilation_h, const size_t dilation_w, + const Buffer<T> &im_buffer, const size_t im_offset, + const Buffer<T> &col_buffer, const size_t col_offset) { + + // Makes sure all dimensions are larger than zero + if ((channels == 0) || (height == 0) || (width == 0)) { throw BLASError(StatusCode::kInvalidDimension); } +} + +// ================================================================================================= + +// Compiles the templated class +template class Xim2col<half>; +template class Xim2col<float>; +template class Xim2col<double>; +template class Xim2col<float2>; +template class Xim2col<double2>; + +// ================================================================================================= +} // namespace clblast diff --git a/src/routines/levelx/xim2col.hpp b/src/routines/levelx/xim2col.hpp new file mode 100644 index 00000000..4448b54e --- /dev/null +++ b/src/routines/levelx/xim2col.hpp @@ -0,0 +1,44 @@ + +// ================================================================================================= +// 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 Xim2col routine. The precision is implemented using a template argument. +// +// ================================================================================================= + +#ifndef CLBLAST_ROUTINES_XIM2COL_H_ +#define CLBLAST_ROUTINES_XIM2COL_H_ + +#include "routine.hpp" + +namespace clblast { +// ================================================================================================= + +// See comment at top of file for a description of the class +template <typename T> +class Xim2col: public Routine { + public: + + // Constructor + Xim2col(Queue &queue, EventPointer event, const std::string &name = "IM2COL"); + + // Templated-precision implementation of the routine + void DoIm2col(const size_t channels, const size_t height, const size_t width, + const size_t kernel_h, const size_t kernel_w, + const size_t pad_h, const size_t pad_w, + const size_t stride_h, const size_t stride_w, + const size_t dilation_h, const size_t dilation_w, + const Buffer<T> &im_buffer, const size_t im_offset, + const Buffer<T> &col_buffer, const size_t col_offset); +}; + +// ================================================================================================= +} // namespace clblast + +// CLBLAST_ROUTINES_XIM2COL_H_ +#endif |