diff options
author | Cedric Nugteren <web@cedricnugteren.nl> | 2017-08-19 18:25:13 +0200 |
---|---|---|
committer | Cedric Nugteren <web@cedricnugteren.nl> | 2017-08-19 18:25:13 +0200 |
commit | 803ca781f9be56f86a0806689f8886a2428d5b9f (patch) | |
tree | 14d2a39fcbe9ee7bd2a8e96ac20647b5993297d1 /src | |
parent | 132e62892de91c1dec2ffe1123a106bba0ffd822 (diff) |
First version of im2col kernel, unoptimized but working
Diffstat (limited to 'src')
-rw-r--r-- | src/kernels/levelx/im2col.opencl | 73 | ||||
-rw-r--r-- | src/routines/levelx/xim2col.cpp | 38 |
2 files changed, 110 insertions, 1 deletions
diff --git a/src/kernels/levelx/im2col.opencl b/src/kernels/levelx/im2col.opencl new file mode 100644 index 00000000..3f10881f --- /dev/null +++ b/src/kernels/levelx/im2col.opencl @@ -0,0 +1,73 @@ + +// ================================================================================================= +// 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 im2col kernel. +// +// ================================================================================================= + +// 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"( + +// ================================================================================================= + +#define WGS1 16 +#define WGS2 16 + +__kernel __attribute__((reqd_work_group_size(WGS1, WGS2, 1))) +void im2col(const int input_h, const int input_w, + const int output_h, const int output_w, + 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 __global real* restrict im_buffer, const int im_offset, + __global real* col_buffer, const int col_offset) { + + // Thread IDs + const int h_id = get_global_id(0); // image height, max 'output_h' + const int w_id = get_global_id(1); // image width, max 'output_w' + const int c_id = get_global_id(2); // input channels + if (h_id < output_h && w_id < output_w) { + + #pragma unroll + for (int kh_id = 0; kh_id < kernel_h; ++kh_id) { // kernel height + #pragma unroll + for (int kw_id = 0; kw_id < kernel_w; ++kw_id) { // kernel width + + // Retrieves the input value + const int h_index = -pad_h + kh_id * dilation_h + stride_h * h_id; + const int w_index = -pad_w + kw_id * dilation_w + stride_w * w_id; + real val; + if (h_index >= 0 && h_index < input_h && + w_index >= 0 && w_index < input_w) { + const int input_index = w_index + input_w * (h_index + input_h * c_id); + val = im_buffer[input_index + im_offset]; + } + else { + SetToZero(val); + } + + // Sets the output value + const int kernel_index = kw_id + kernel_w * kh_id; + const int patch_index = w_id + output_w * h_id; + const int output_index = patch_index + kernel_index * output_w * output_h + + c_id * output_w * output_h * kernel_h * kernel_w; + col_buffer[output_index + col_offset] = val; + } + } + } +} + +// ================================================================================================= + +// End of the C++11 raw string literal +)" + +// ================================================================================================= diff --git a/src/routines/levelx/xim2col.cpp b/src/routines/levelx/xim2col.cpp index 150220d6..10c9c10c 100644 --- a/src/routines/levelx/xim2col.cpp +++ b/src/routines/levelx/xim2col.cpp @@ -23,7 +23,7 @@ namespace clblast { 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" +#include "../../kernels/levelx/im2col.opencl" }) { } @@ -40,6 +40,42 @@ void Xim2col<T>::DoIm2col(const size_t channels, const size_t height, const size // Makes sure all dimensions are larger than zero if ((channels == 0) || (height == 0) || (width == 0)) { throw BLASError(StatusCode::kInvalidDimension); } + + // Sets the output height and width + const auto size_h = height + 2 * pad_h; + const auto padding_h = dilation_h * (kernel_h - 1) + 1; + const auto output_h = (size_h >= padding_h) ? (size_h - padding_h) / stride_h + 1 : 1; + const auto size_w = width + 2 * pad_w; + const auto padding_w = dilation_w * (kernel_w - 1) + 1; + const auto output_w = (size_w >= padding_w) ? (size_w - padding_w) / stride_w + 1 : 1; + + // Retrieves the Xcopy kernel from the compiled binary + auto kernel = Kernel(program_, "im2col"); + + // Sets the kernel arguments + kernel.SetArgument(0, static_cast<int>(height)); + kernel.SetArgument(1, static_cast<int>(width)); + kernel.SetArgument(2, static_cast<int>(output_h)); + kernel.SetArgument(3, static_cast<int>(output_w)); + kernel.SetArgument(4, static_cast<int>(kernel_h)); + kernel.SetArgument(5, static_cast<int>(kernel_w)); + kernel.SetArgument(6, static_cast<int>(pad_h)); + kernel.SetArgument(7, static_cast<int>(pad_w)); + kernel.SetArgument(8, static_cast<int>(stride_h)); + kernel.SetArgument(9, static_cast<int>(stride_w)); + kernel.SetArgument(10, static_cast<int>(dilation_h)); + kernel.SetArgument(11, static_cast<int>(dilation_w)); + kernel.SetArgument(12, im_buffer()); + kernel.SetArgument(13, static_cast<int>(im_offset)); + kernel.SetArgument(14, col_buffer()); + kernel.SetArgument(15, static_cast<int>(col_offset)); + + // Launches the kernel + const auto h_ceiled = Ceil(output_h, 16); + const auto w_ceiled = Ceil(output_w, 16); + auto global = std::vector<size_t>{h_ceiled, w_ceiled, channels}; + auto local = std::vector<size_t>{16, 16, 1}; + RunKernel(kernel, queue_, device_, global, local, event_); } // ================================================================================================= |