From 84ec50e29dc123b956a85e1dc37dae123323e420 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Sun, 2 Jul 2017 12:10:22 +0200 Subject: Added interface and stubs for the im2col routine --- CMakeLists.txt | 2 +- doc/clblast.md | 60 +++++++++++++++++++++++ include/clblast.h | 7 +++ include/clblast_c.h | 22 +++++++++ include/clblast_netlib_c.h | 14 ++++++ scripts/generator/generator.py | 6 ++- scripts/generator/generator/routine.py | 8 +-- src/clblast.cpp | 37 ++++++++++++++ src/clblast_c.cpp | 67 +++++++++++++++++++++++++ src/clblast_netlib_c.cpp | 90 ++++++++++++++++++++++++++++++++++ src/routines/levelx/xim2col.cpp | 55 +++++++++++++++++++++ src/routines/levelx/xim2col.hpp | 44 +++++++++++++++++ 12 files changed, 406 insertions(+), 6 deletions(-) create mode 100644 src/routines/levelx/xim2col.cpp create mode 100644 src/routines/levelx/xim2col.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 72c03352..a7f1bf0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,7 +164,7 @@ set(LEVEL1_ROUTINES xswap xscal xcopy xaxpy xdot xdotu xdotc xnrm2 xasum xamax) set(LEVEL2_ROUTINES xgemv xgbmv xhemv xhbmv xhpmv xsymv xsbmv xspmv xtrmv xtbmv xtpmv xtrsv xger xgeru xgerc xher xhpr xher2 xhpr2 xsyr xspr xsyr2 xspr2) set(LEVEL3_ROUTINES xgemm xsymm xhemm xsyrk xherk xsyr2k xher2k xtrmm xtrsm) -set(LEVELX_ROUTINES xomatcopy xaxpybatched xgemmbatched) +set(LEVELX_ROUTINES xomatcopy xim2col xaxpybatched xgemmbatched) set(ROUTINES ${LEVEL1_ROUTINES} ${LEVEL2_ROUTINES} ${LEVEL3_ROUTINES} ${LEVELX_ROUTINES}) set(PRECISIONS 32 64 3232 6464 16) diff --git a/doc/clblast.md b/doc/clblast.md index fbd0461e..88563bc1 100644 --- a/doc/clblast.md +++ b/doc/clblast.md @@ -2956,6 +2956,66 @@ Requirements for OMATCOPY: +xIM2COL: Im2col function (non-BLAS function) +------------- + +Performs the im2col algorithm, in which _im_ is the input matrix and _col_ is the output matrix. + +C++ API: +``` +template +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) +``` + +C API: +``` +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) +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) +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) +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) +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) +``` + +Arguments to IM2COL: + +* `const size_t channels`: Integer size argument. This value must be positive. +* `const size_t height`: Integer size argument. This value must be positive. +* `const size_t width`: Integer size argument. This value must be positive. +* `const size_t kernel_h`: Integer size argument. This value must be positive. +* `const size_t kernel_w`: Integer size argument. This value must be positive. +* `const size_t pad_h`: Integer size argument. This value must be positive. +* `const size_t pad_w`: Integer size argument. This value must be positive. +* `const size_t stride_h`: Integer size argument. This value must be positive. +* `const size_t stride_w`: Integer size argument. This value must be positive. +* `const size_t dilation_h`: Integer size argument. This value must be positive. +* `const size_t dilation_w`: Integer size argument. This value must be positive. +* `const cl_mem im_buffer`: OpenCL buffer to store the input im vector. +* `const size_t im_offset`: The offset in elements from the start of the input im vector. +* `cl_mem col_buffer`: OpenCL buffer to store the output col vector. +* `const size_t col_offset`: The offset in elements from the start of the output col vector. +* `cl_command_queue* queue`: Pointer to an OpenCL command queue associated with a context and device to execute the routine on. +* `cl_event* event`: Pointer to an OpenCL event to be able to wait for completion of the routine's OpenCL kernel(s). This is an optional argument. + + + xAXPYBATCHED: Batched version of AXPY ------------- diff --git a/include/clblast.h b/include/clblast.h index 354ca591..e073b211 100644 --- a/include/clblast.h +++ b/include/clblast.h @@ -617,6 +617,13 @@ StatusCode Omatcopy(const Layout layout, const Transpose a_transpose, cl_mem b_buffer, const size_t b_offset, const size_t b_ld, cl_command_queue* queue, cl_event* event = nullptr); +// Im2col function (non-BLAS function): SIM2COL/DIM2COL/CIM2COL/ZIM2COL/HIM2COL +template +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 = nullptr); + // Batched version of AXPY: SAXPYBATCHED/DAXPYBATCHED/CAXPYBATCHED/ZAXPYBATCHED/HAXPYBATCHED template StatusCode AxpyBatched(const size_t n, diff --git a/include/clblast_c.h b/include/clblast_c.h index 323a28df..bd74fe88 100644 --- a/include/clblast_c.h +++ b/include/clblast_c.h @@ -1350,6 +1350,28 @@ CLBlastStatusCode PUBLIC_API CLBlastHomatcopy(const CLBlastLayout layout, const cl_mem b_buffer, const size_t b_offset, const size_t b_ld, cl_command_queue* queue, cl_event* event); +// Im2col function (non-BLAS function): SIM2COL/DIM2COL/CIM2COL/ZIM2COL/HIM2COL +CLBlastStatusCode PUBLIC_API 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); +CLBlastStatusCode PUBLIC_API 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); +CLBlastStatusCode PUBLIC_API 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); +CLBlastStatusCode PUBLIC_API 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); +CLBlastStatusCode PUBLIC_API 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); + // Batched version of AXPY: SAXPYBATCHED/DAXPYBATCHED/CAXPYBATCHED/ZAXPYBATCHED/HAXPYBATCHED CLBlastStatusCode PUBLIC_API CLBlastSaxpyBatched(const size_t n, const float *alphas, diff --git a/include/clblast_netlib_c.h b/include/clblast_netlib_c.h index 4d0d8e1f..8637ac3e 100644 --- a/include/clblast_netlib_c.h +++ b/include/clblast_netlib_c.h @@ -920,6 +920,20 @@ void PUBLIC_API cblas_zomatcopy(const CLBlastLayout layout, const CLBlastTranspo const void* a, const int a_ld, void* b, const int b_ld); +// Im2col function (non-BLAS function): SIM2COL/DIM2COL/CIM2COL/ZIM2COL/HIM2COL +void PUBLIC_API 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); +void PUBLIC_API 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); +void PUBLIC_API 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); +void PUBLIC_API 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); + // ================================================================================================= #ifdef __cplusplus diff --git a/scripts/generator/generator.py b/scripts/generator/generator.py index 74e0815a..df0eaca0 100755 --- a/scripts/generator/generator.py +++ b/scripts/generator/generator.py @@ -42,7 +42,7 @@ FILES = [ "/include/clblast_netlib_c.h", "/src/clblast_netlib_c.cpp", ] -HEADER_LINES = [122, 78, 126, 24, 29, 41, 29, 65, 32] +HEADER_LINES = [122, 79, 126, 24, 29, 41, 29, 65, 32] FOOTER_LINES = [25, 147, 27, 38, 6, 6, 6, 9, 2] HEADER_LINES_DOC = 0 FOOTER_LINES_DOC = 63 @@ -96,10 +96,13 @@ bnma = size_helper(layout_transpose_condition("a"), "n", "m", "b_ld") cmn = size_helper("layout == CLBlastLayoutRowMajor", "m", "n", "c_ld") ammn = size_helper("layout == CLBlastLayoutRowMajor", "m", "((side == CLBlastSideLeft) ? m : n)", "a_ld") bmnn = size_helper("layout == CLBlastLayoutRowMajor", "((side == CLBlastSideLeft) ? m : n)", "n", "b_ld") +im = "height * width * channels" +col = "height * width * channels" # ================================================================================================== # Populates a list of routines +im2col_constants = ["channels", "height", "width", "kernel_h", "kernel_w", "pad_h", "pad_w", "stride_h", "stride_w", "dilation_h", "dilation_w"] ROUTINES = [ [ # Level 1: vector-vector Routine(False, True, False, "1", "rotg", T, [S,D], [], [], [], ["sa","sb","sc","ss"], ["1","1","1","1"], [], "", "Generate givens plane rotation", "", []), @@ -163,6 +166,7 @@ ROUTINES = [ [ # Level X: extra routines (not part of BLAS) # Special routines: Routine(True, True, False, "x", "omatcopy", T, [S,D,C,Z,H], ["m","n"], ["layout","a_transpose"], ["a"], ["b"], [amn,bnma], ["alpha"], "", "Scaling and out-place transpose/copy (non-BLAS function)", "Performs scaling and out-of-place transposition/copying of matrices according to _B = alpha*op(A)_, in which _A_ is an input matrix (_m_ rows by _n_ columns), _B_ an output matrix, and _alpha_ a scalar value. The operation _op_ can be a normal matrix copy, a transposition or a conjugate transposition.", [ald_m, bld_n]), + Routine(True, True, False, "x", "im2col", T, [S,D,C,Z,H], im2col_constants, [], ["im"], ["col"], [im,col], [""], "", "Im2col function (non-BLAS function)", "Performs the im2col algorithm, in which _im_ is the input matrix and _col_ is the output matrix.", []), # Batched routines: Routine(True, True, True, "x", "axpy", T, [S,D,C,Z,H], ["n"], [], ["x"], ["y"], [xn,yn], ["alpha"], "", "Batched version of AXPY", "As AXPY, but multiple operations are batched together for better performance.", []), Routine(True, True, True, "x", "gemm", T, [S,D,C,Z,H], ["m","n","k"], ["layout","a_transpose","b_transpose"], ["a","b"], ["c"], [amk,bkn,cmn], ["alpha","beta"], "", "Batched version of GEMM", "As GEMM, but multiple operations are batched together for better performance.", [ald_transa_m_k, bld_transb_k_n, cld_m]), diff --git a/scripts/generator/generator/routine.py b/scripts/generator/generator/routine.py index b2422dad..cef7db87 100644 --- a/scripts/generator/generator/routine.py +++ b/scripts/generator/generator/routine.py @@ -182,7 +182,7 @@ class Routine: def buffers_without_ld_inc(self): """List of buffers without 'inc' or 'ld'""" - return self.scalar_buffers_first() + self.scalar_buffers_second() + ["ap"] + return self.scalar_buffers_first() + self.scalar_buffers_second() + ["ap", "im", "col"] def get_buffer_type(self, name, flavour): if name in self.index_buffers(): @@ -195,7 +195,7 @@ class Routine: def no_scalars(self): """Determines whether or not this routine has scalar arguments (alpha/beta)""" - return self.scalars == [] + return self.scalars == [] or self.name == "im2col" def has_layout(self): """Determines whether the layout is an argument""" @@ -216,12 +216,12 @@ class Routine: """Determines which buffers go first (between alpha and beta) and which ones go after""" if self.level == "2b": return ["x", "y"] - return ["ap", "a", "b", "x"] + return ["ap", "a", "b", "x", "im"] def buffers_second(self): if self.level == "2b": return ["ap", "a", "b", "c"] - return ["y", "c"] + return ["y", "c", "col"] def buffer(self, name): """Retrieves a variable name for a specific input/output vector/matrix (e.g. 'x')""" 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(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 +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(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(im_buffer), im_offset, + Buffer(col_buffer), col_offset); + return StatusCode::kSuccess; + } catch (...) { return DispatchException(); } +} +template StatusCode PUBLIC_API Im2col(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(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(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(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(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 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(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( + clblast::Im2col(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(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( + clblast::Im2col(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(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( + clblast::Im2col(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(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( + clblast::Im2col(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(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( + clblast::Im2col(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(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(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(context, im_size); + auto col_buffer = clblast::Buffer(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast(im)); + col_buffer.Write(queue, col_size, reinterpret_cast(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col(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(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(context, im_size); + auto col_buffer = clblast::Buffer(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast(im)); + col_buffer.Write(queue, col_size, reinterpret_cast(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col(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(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(context, im_size); + auto col_buffer = clblast::Buffer(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast(im)); + col_buffer.Write(queue, col_size, reinterpret_cast(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col(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(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(context, im_size); + auto col_buffer = clblast::Buffer(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast(im)); + col_buffer.Write(queue, col_size, reinterpret_cast(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col(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(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 +// +// This file implements the Xim2col class (see the header for information about the class). +// +// ================================================================================================= + +#include "routines/levelx/xim2col.hpp" + +#include +#include + +namespace clblast { +// ================================================================================================= + +// Constructor: forwards to base class constructor +template +Xim2col::Xim2col(Queue &queue, EventPointer event, const std::string &name): + Routine(queue, event, name, {}, PrecisionValue(), {}, { +#include "../../kernels/level3/level3.opencl" + }) { +} + +// ================================================================================================= + +// The main routine +template +void Xim2col::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 &im_buffer, const size_t im_offset, + const Buffer &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; +template class Xim2col; +template class Xim2col; +template class Xim2col; +template class Xim2col; + +// ================================================================================================= +} // 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 +// +// 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 +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 &im_buffer, const size_t im_offset, + const Buffer &col_buffer, const size_t col_offset); +}; + +// ================================================================================================= +} // namespace clblast + +// CLBLAST_ROUTINES_XIM2COL_H_ +#endif -- cgit v1.2.3 From 97bcf77d4bc9b31e32a8785787e0497ac5440e44 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Sun, 16 Jul 2017 22:33:49 +0200 Subject: First step towards supporting im2col in the test infrastructure --- src/utilities/utilities.hpp | 25 ++++ test/correctness/routines/levelx/xim2col.cpp | 26 +++++ test/correctness/testblas.hpp | 54 ++++++++- test/correctness/tester.cpp | 5 + test/performance/routines/levelx/xim2col.cpp | 33 ++++++ test/routines/levelx/xim2col.hpp | 165 +++++++++++++++++++++++++++ test/routines/levelx/xomatcopy.hpp | 2 +- 7 files changed, 306 insertions(+), 4 deletions(-) create mode 100644 test/correctness/routines/levelx/xim2col.cpp create mode 100644 test/performance/routines/levelx/xim2col.cpp create mode 100644 test/routines/levelx/xim2col.hpp diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp index 03051354..8daeda08 100644 --- a/src/utilities/utilities.hpp +++ b/src/utilities/utilities.hpp @@ -78,6 +78,19 @@ constexpr auto kArgAlpha = "alpha"; constexpr auto kArgBeta = "beta"; constexpr auto kArgBatchCount = "batch_num"; +// Constants for im2col +constexpr auto kArgChannels = "channels"; +constexpr auto kArgHeight = "height"; +constexpr auto kArgWidth = "width"; +constexpr auto kArgKernelH = "kernelh"; +constexpr auto kArgKernelW = "kernelw"; +constexpr auto kArgPadH = "padh"; +constexpr auto kArgPadW = "padw"; +constexpr auto kArgStrideH = "strideh"; +constexpr auto kArgStrideW = "stridew"; +constexpr auto kArgDilationH = "dilationh"; +constexpr auto kArgDilationW = "dilationw"; + // The tuner-specific arguments in string form constexpr auto kArgFraction = "fraction"; @@ -170,6 +183,18 @@ struct Arguments { size_t imax_offset = 0; T alpha = ConstantOne(); T beta = ConstantOne(); + // Arguments for im2col + size_t channels = 1; + size_t height = 1; + size_t width = 1; + size_t kernel_h = 1; + size_t kernel_w = 1; + size_t pad_h = 1; + size_t pad_w = 1; + size_t stride_h = 1; + size_t stride_w = 1; + size_t dilation_h = 1; + size_t dilation_w = 1; // Batch-specific arguments size_t batch_count = 1; std::vector x_offsets; // = {0}; diff --git a/test/correctness/routines/levelx/xim2col.cpp b/test/correctness/routines/levelx/xim2col.cpp new file mode 100644 index 00000000..fe79b3ef --- /dev/null +++ b/test/correctness/routines/levelx/xim2col.cpp @@ -0,0 +1,26 @@ + +// ================================================================================================= +// 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 +// +// ================================================================================================= + +#include "test/correctness/testblas.hpp" +#include "test/routines/levelx/xim2col.hpp" + +// Main function (not within the clblast namespace) +int main(int argc, char *argv[]) { + auto errors = size_t{0}; + errors += clblast::RunTests, float, float>(argc, argv, false, "SIM2COL"); + errors += clblast::RunTests, double, double>(argc, argv, true, "DIM2COL"); + errors += clblast::RunTests, clblast::float2, clblast::float2>(argc, argv, true, "CIM2COL"); + errors += clblast::RunTests, clblast::double2, clblast::double2>(argc, argv, true, "ZIM2COL"); + errors += clblast::RunTests, clblast::half, clblast::half>(argc, argv, true, "HIM2COL"); + if (errors > 0) { return 1; } else { return 0; } +} + +// ================================================================================================= diff --git a/test/correctness/testblas.hpp b/test/correctness/testblas.hpp index a553687d..577a289e 100644 --- a/test/correctness/testblas.hpp +++ b/test/correctness/testblas.hpp @@ -56,6 +56,8 @@ class TestBlas: public Tester { static const std::vector kMatrixDims; static const std::vector kMatrixVectorDims; static const std::vector kBandSizes; + static const std::vector kPadSizes; + static const std::vector kKernelSizes; static const std::vector kBatchCounts; const std::vector kOffsets; const std::vector kAlphaValues; @@ -130,6 +132,8 @@ template const std::vector TestBlas::kMatr template const std::vector TestBlas::kMatrixVectorDims = { 61, 256 }; template const std::vector TestBlas::kBandSizes = { 4, 19 }; template const std::vector TestBlas::kBatchCounts = { 1, 3 }; +template const std::vector TestBlas::kPadSizes = { 0 }; +template const std::vector TestBlas::kKernelSizes = { 1, 3 }; // Test settings for the invalid tests template const std::vector TestBlas::kInvalidIncrements = { 0, 1 }; @@ -223,6 +227,17 @@ size_t RunTests(int argc, char *argv[], const bool silent, const std::string &na auto imax_offsets = std::vector{args.imax_offset}; auto alphas = std::vector{args.alpha}; auto betas = std::vector{args.beta}; + auto channelss = std::vector{args.channels}; + auto heights = std::vector{args.height}; + auto widths = std::vector{args.width}; + auto kernel_hs = std::vector{args.kernel_h}; + auto kernel_ws = std::vector{args.kernel_w}; + auto pad_hs = std::vector{args.pad_h}; + auto pad_ws = std::vector{args.pad_w}; + auto stride_hs = std::vector{args.stride_h}; + auto stride_ws = std::vector{args.stride_w}; + auto dilation_hs = std::vector{args.dilation_h}; + auto dilation_ws = std::vector{args.dilation_w}; auto batch_counts = std::vector{args.batch_count}; auto x_sizes = std::vector{args.x_size}; auto y_sizes = std::vector{args.y_size}; @@ -267,6 +282,17 @@ size_t RunTests(int argc, char *argv[], const bool silent, const std::string &na if (option == kArgImaxOffset) { imax_offsets = tester.kOffsets; } if (option == kArgAlpha) { alphas = tester.kAlphaValues; } if (option == kArgBeta) { betas = tester.kBetaValues; } + if (option == kArgChannels) { channelss = tester.kMatrixDims; } + if (option == kArgHeight) { heights = tester.kMatrixDims; } + if (option == kArgWidth) { widths = tester.kMatrixDims; } + if (option == kArgKernelH) { kernel_hs = tester.kKernelSizes; } + if (option == kArgKernelW) { kernel_ws = tester.kKernelSizes; } + if (option == kArgPadH) { pad_hs = tester.kPadSizes; } + if (option == kArgPadW) { pad_ws = tester.kPadSizes; } + if (option == kArgStrideH) { stride_hs = tester.kKernelSizes; } + if (option == kArgStrideW) { stride_ws = tester.kKernelSizes; } + if (option == kArgDilationH) { dilation_hs = tester.kKernelSizes; } + if (option == kArgDilationW) { dilation_ws = tester.kKernelSizes; } if (option == kArgBatchCount) { batch_counts = tester.kBatchCounts; } if (option == kArgXOffset) { x_sizes = tester.kVecSizes; } @@ -310,9 +336,31 @@ size_t RunTests(int argc, char *argv[], const bool silent, const std::string &na for (auto &imax_offset: imax_offsets) { r_args.imax_offset = imax_offset; for (auto &alpha: alphas) { r_args.alpha = alpha; for (auto &beta: betas) { r_args.beta = beta; - for (auto &batch_count: batch_counts) { r_args.batch_count = batch_count; - C::SetSizes(r_args); - regular_test_vector.push_back(r_args); + for (auto &channels: channelss) { r_args.channels = channels; + for (auto &height: heights) { r_args.height = height; + for (auto &width: widths) { r_args.width = width; + for (auto &kernel_h: kernel_hs) { r_args.kernel_h = kernel_h; + for (auto &kernel_w: kernel_ws) { r_args.kernel_w = kernel_w; + for (auto &pad_h: pad_hs) { r_args.pad_h = pad_h; + for (auto &pad_w: pad_ws) { r_args.pad_w = pad_w; + for (auto &stride_h: stride_hs) { r_args.stride_h = stride_h; + for (auto &stride_w: stride_ws) { r_args.stride_w = stride_w; + for (auto &dilation_h: dilation_hs) { r_args.dilation_h = dilation_h; + for (auto &dilation_w: dilation_ws) { r_args.dilation_w = dilation_w; + for (auto &batch_count: batch_counts) { r_args.batch_count = batch_count; + C::SetSizes(r_args); + regular_test_vector.push_back(r_args); + } + } + } + } + } + } + } + } + } + } + } } } } diff --git a/test/correctness/tester.cpp b/test/correctness/tester.cpp index fb85e609..1581fbfb 100644 --- a/test/correctness/tester.cpp +++ b/test/correctness/tester.cpp @@ -366,6 +366,11 @@ std::string Tester::GetOptionsString(const Arguments &args) { if (o == kArgAlpha) { result += kArgAlpha + equals + ToString(args.alpha) + " "; } if (o == kArgBeta) { result += kArgBeta + equals + ToString(args.beta) + " "; } if (o == kArgBatchCount){result += kArgBatchCount + equals + ToString(args.batch_count) + " "; } + if (o == kArgChannels) { result += kArgChannels + equals + ToString(args.channels) + " "; } + if (o == kArgHeight) { result += kArgHeight + equals + ToString(args.height) + " "; } + if (o == kArgWidth) { result += kArgWidth + equals + ToString(args.width) + " "; } + if (o == kArgKernelH) { result += kArgKernelH + equals + ToString(args.kernel_h) + " "; } + if (o == kArgKernelW) { result += kArgKernelW + equals + ToString(args.kernel_w) + " "; } } return result; } diff --git a/test/performance/routines/levelx/xim2col.cpp b/test/performance/routines/levelx/xim2col.cpp new file mode 100644 index 00000000..30e224f8 --- /dev/null +++ b/test/performance/routines/levelx/xim2col.cpp @@ -0,0 +1,33 @@ + +// ================================================================================================= +// 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 +// +// ================================================================================================= + +#include "test/performance/client.hpp" +#include "test/routines/levelx/xim2col.hpp" + +// Main function (not within the clblast namespace) +int main(int argc, char *argv[]) { + const auto command_line_args = clblast::RetrieveCommandLineArguments(argc, argv); + switch(clblast::GetPrecision(command_line_args, clblast::Precision::kSingle)) { + case clblast::Precision::kHalf: + clblast::RunClient, clblast::half, clblast::half>(argc, argv); break; + case clblast::Precision::kSingle: + clblast::RunClient, float, float>(argc, argv); break; + case clblast::Precision::kDouble: + clblast::RunClient, double, double>(argc, argv); break; + case clblast::Precision::kComplexSingle: + clblast::RunClient, clblast::float2, clblast::float2>(argc, argv); break; + case clblast::Precision::kComplexDouble: + clblast::RunClient, clblast::double2, clblast::double2>(argc, argv); break; + } + return 0; +} + +// ================================================================================================= diff --git a/test/routines/levelx/xim2col.hpp b/test/routines/levelx/xim2col.hpp new file mode 100644 index 00000000..e5bc56cd --- /dev/null +++ b/test/routines/levelx/xim2col.hpp @@ -0,0 +1,165 @@ + +// ================================================================================================= +// 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 implements a class with static methods to describe the Xim2col routine. Examples of +// such 'descriptions' are how to calculate the size a of buffer or how to run the routine. These +// static methods are used by the correctness tester and the performance tester. +// +// ================================================================================================= + +#ifndef CLBLAST_TEST_ROUTINES_XIM2COL_H_ +#define CLBLAST_TEST_ROUTINES_XIM2COL_H_ + +#include "test/routines/common.hpp" + +namespace clblast { +// ================================================================================================= + +template +StatusCode RunReference(const Arguments &args, BuffersHost &buffers_host) { + for (auto c_id = size_t{0}; c_id < args.channels; ++c_id) { // input channels + for (auto kh_id = size_t{0}; kh_id < args.kernel_h; ++kh_id) { // kernel height + for (auto kw_id = size_t{0}; kw_id < args.kernel_w; ++kw_id) { // kernel width + for (auto h_id = size_t{0}; h_id < args.height; h_id += args.stride_h) { // image height + for (auto w_id = size_t{0}; w_id < args.width; w_id += args.stride_w) { // image width + + // Retrieves the input value + const auto h_index = -args.pad_h + kh_id * args.dilation_h + h_id; + const auto w_index = -args.pad_w + kw_id * args.dilation_w + w_id; + auto val = ConstantZero(); + if (h_index < args.height && w_index < args.width) { + const auto input_index = w_index + args.width * (h_index + args.height * c_id); + val = buffers_host.a_mat[input_index + args.a_offset]; + } + + // Sets the output value + const auto kernel_index = kw_id + args.kernel_w * kh_id; + const auto patch_index = w_id + ((args.width / args.stride_w) * h_id + (args.height / args.stride_h) * c_id); + const auto output_index = kernel_index + args.kernel_h * args.kernel_w * patch_index; + buffers_host.b_mat[output_index + args.b_offset] = val; + } + } + } + } + } + return StatusCode::kSuccess; +} + +// ================================================================================================= + +// See comment at top of file for a description of the class +template +class TestXim2col { +public: + + // The BLAS level: 4 for the extra routines + static size_t BLASLevel() { return 4; } + + // The list of arguments relevant for this routine + static std::vector GetOptions() { + return {kArgChannels, kArgHeight, kArgWidth, kArgKernelH, kArgKernelW, kArgPadH, kArgPadW, + kArgStrideH, kArgStrideW, kArgDilationH, kArgDilationW, + kArgAOffset, kArgBOffset}; + } + static std::vector BuffersIn() { return {kBufMatA, kBufMatB}; } + static std::vector BuffersOut() { return {kBufMatB}; } + + // Describes how to obtain the sizes of the buffers + static size_t NumPatches(const Arguments &args) { + return (args.width / args.stride_w) * (args.height / args.stride_h) * args.channels; + } + static size_t GetSizeA(const Arguments &args) { + return args.height * args.width * args.channels + args.a_offset; + } + static size_t GetSizeB(const Arguments &args) { + return args.kernel_w * args.kernel_h * NumPatches(args) + args.b_offset; + } + + // Describes how to set the sizes of all the buffers + static void SetSizes(Arguments &args) { + args.a_size = GetSizeA(args); + args.b_size = GetSizeB(args); + } + + // Describes what the default values of the leading dimensions of the matrices are + static size_t DefaultLDA(const Arguments &) { return 1; } // N/A for this routine + static size_t DefaultLDB(const Arguments &) { return 1; } // N/A for this routine + static size_t DefaultLDC(const Arguments &) { return 1; } // N/A for this routine + + // Describes which transpose options are relevant for this routine + using Transposes = std::vector; + static Transposes GetATransposes(const Transposes &) { return {}; } // N/A for this routine + static Transposes GetBTransposes(const Transposes &) { return {}; } // N/A for this routine + + // Describes how to prepare the input data + static void PrepareData(const Arguments&, Queue&, const int, std::vector&, + std::vector&, std::vector&, std::vector&, std::vector&, + std::vector&, std::vector&) {} // N/A for this routine + + // Describes how to run the CLBlast routine + static StatusCode RunRoutine(const Arguments &args, Buffers &buffers, Queue &queue) { + auto queue_plain = queue(); + auto event = cl_event{}; + auto status = Im2col(args.channels, args.height, args.width, + args.kernel_h, args.kernel_w, + args.pad_h, args.pad_w, + args.stride_h, args.stride_w, + args.dilation_h, args.dilation_w, + buffers.a_mat(), args.a_offset, + buffers.b_mat(), args.b_offset, + &queue_plain, &event); + if (status == StatusCode::kSuccess) { clWaitForEvents(1, &event); clReleaseEvent(event); } + return status; + } + + // Describes how to run a naive version of the routine (for correctness/performance comparison). + // Note that a proper clBLAS or CPU BLAS comparison is not available for non-BLAS routines. + static StatusCode RunReference1(const Arguments &args, Buffers &buffers, Queue &queue) { + auto buffers_host = BuffersHost(); + DeviceToHost(args, buffers, buffers_host, queue, BuffersIn()); + const auto status = RunReference(args, buffers_host); + HostToDevice(args, buffers, buffers_host, queue, BuffersOut()); + return status; + } + + static StatusCode RunReference2(const Arguments &args, BuffersHost &buffers_host, Queue&) { + return RunReference(args, buffers_host); + } + static StatusCode RunReference3(const Arguments &, BuffersCUDA &, Queue &) { + return StatusCode::kUnknownError; + } + + // Describes how to download the results of the computation (more importantly: which buffer) + static std::vector DownloadResult(const Arguments &args, Buffers &buffers, Queue &queue) { + std::vector result(args.b_size, static_cast(0)); + buffers.b_mat.Read(queue, args.b_size, result); + return result; + } + + // Describes how to compute the indices of the result buffer + static size_t ResultID1(const Arguments &args) { return args.kernel_h * args.kernel_w; } + static size_t ResultID2(const Arguments &args) { return NumPatches(args); } + static size_t GetResultIndex(const Arguments &args, const size_t id1, const size_t id2) { + return id1 + args.kernel_h * args.kernel_w * id2 + args.b_offset; + } + + // Describes how to compute performance metrics + static size_t GetFlops(const Arguments &args) { + return 1; + } + static size_t GetBytes(const Arguments &args) { + return (1) * sizeof(T); + } +}; + +// ================================================================================================= +} // namespace clblast + +// CLBLAST_TEST_ROUTINES_XIM2COL_H_ +#endif diff --git a/test/routines/levelx/xomatcopy.hpp b/test/routines/levelx/xomatcopy.hpp index 4638b61c..2736cf75 100644 --- a/test/routines/levelx/xomatcopy.hpp +++ b/test/routines/levelx/xomatcopy.hpp @@ -114,7 +114,7 @@ class TestXomatcopy { static size_t DefaultLDB(const Arguments &args) { return args.m; } static size_t DefaultLDC(const Arguments &) { return 1; } // N/A for this routine - // Describes which omatcopyose options are relevant for this routine + // Describes which transpose options are relevant for this routine using Transposes = std::vector; static Transposes GetATransposes(const Transposes &all) { return all; } static Transposes GetBTransposes(const Transposes &) { return {}; } // N/A for this routine -- cgit v1.2.3 From 132e62892de91c1dec2ffe1123a106bba0ffd822 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Sat, 19 Aug 2017 16:55:09 +0200 Subject: Implemented proper im2col reference function and completd tests --- test/correctness/testblas.hpp | 10 +++-- test/correctness/tester.cpp | 6 +++ test/routines/levelx/xim2col.hpp | 82 ++++++++++++++++++++++++---------------- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/test/correctness/testblas.hpp b/test/correctness/testblas.hpp index 1c0cf9e3..4e02fd28 100644 --- a/test/correctness/testblas.hpp +++ b/test/correctness/testblas.hpp @@ -57,6 +57,7 @@ class TestBlas: public Tester { static const std::vector kMatrixVectorDims; static const std::vector kBandSizes; static const std::vector kPadSizes; + static const std::vector kDilationSizes; static const std::vector kKernelSizes; static const std::vector kBatchCounts; const std::vector kOffsets; @@ -132,7 +133,8 @@ template const std::vector TestBlas::kMatr template const std::vector TestBlas::kMatrixVectorDims = { 61, 256 }; template const std::vector TestBlas::kBandSizes = { 4, 19 }; template const std::vector TestBlas::kBatchCounts = { 1, 3 }; -template const std::vector TestBlas::kPadSizes = { 0 }; +template const std::vector TestBlas::kPadSizes = { 0, 1 }; +template const std::vector TestBlas::kDilationSizes = { 1, 2 }; template const std::vector TestBlas::kKernelSizes = { 1, 3 }; // Test settings for the invalid tests @@ -282,7 +284,7 @@ size_t RunTests(int argc, char *argv[], const bool silent, const std::string &na if (option == kArgImaxOffset) { imax_offsets = tester.kOffsets; } if (option == kArgAlpha) { alphas = tester.kAlphaValues; } if (option == kArgBeta) { betas = tester.kBetaValues; } - if (option == kArgChannels) { channelss = tester.kMatrixDims; } + if (option == kArgChannels) { channelss = tester.kKernelSizes; } if (option == kArgHeight) { heights = tester.kMatrixDims; } if (option == kArgWidth) { widths = tester.kMatrixDims; } if (option == kArgKernelH) { kernel_hs = tester.kKernelSizes; } @@ -291,8 +293,8 @@ size_t RunTests(int argc, char *argv[], const bool silent, const std::string &na if (option == kArgPadW) { pad_ws = tester.kPadSizes; } if (option == kArgStrideH) { stride_hs = tester.kKernelSizes; } if (option == kArgStrideW) { stride_ws = tester.kKernelSizes; } - if (option == kArgDilationH) { dilation_hs = tester.kKernelSizes; } - if (option == kArgDilationW) { dilation_ws = tester.kKernelSizes; } + if (option == kArgDilationH) { dilation_hs = tester.kDilationSizes; } + if (option == kArgDilationW) { dilation_ws = tester.kDilationSizes; } if (option == kArgBatchCount) { batch_counts = tester.kBatchCounts; } if (option == kArgXOffset) { x_sizes = tester.kVecSizes; } diff --git a/test/correctness/tester.cpp b/test/correctness/tester.cpp index 648aef6e..9dbd8934 100644 --- a/test/correctness/tester.cpp +++ b/test/correctness/tester.cpp @@ -371,6 +371,12 @@ std::string Tester::GetOptionsString(const Arguments &args) { if (o == kArgWidth) { result += kArgWidth + equals + ToString(args.width) + " "; } if (o == kArgKernelH) { result += kArgKernelH + equals + ToString(args.kernel_h) + " "; } if (o == kArgKernelW) { result += kArgKernelW + equals + ToString(args.kernel_w) + " "; } + if (o == kArgPadH) { result += kArgPadH + equals + ToString(args.pad_h) + " "; } + if (o == kArgPadW) { result += kArgPadW + equals + ToString(args.pad_w) + " "; } + if (o == kArgStrideH) { result += kArgStrideH + equals + ToString(args.stride_h) + " "; } + if (o == kArgStrideW) { result += kArgStrideW + equals + ToString(args.stride_w) + " "; } + if (o == kArgDilationH){ result += kArgDilationH + equals + ToString(args.dilation_h) + " "; } + if (o == kArgDilationW){ result += kArgDilationW + equals + ToString(args.dilation_w) + " "; } } return result; } diff --git a/test/routines/levelx/xim2col.hpp b/test/routines/levelx/xim2col.hpp index e5bc56cd..e6aefd9e 100644 --- a/test/routines/levelx/xim2col.hpp +++ b/test/routines/levelx/xim2col.hpp @@ -21,38 +21,6 @@ namespace clblast { // ================================================================================================= -template -StatusCode RunReference(const Arguments &args, BuffersHost &buffers_host) { - for (auto c_id = size_t{0}; c_id < args.channels; ++c_id) { // input channels - for (auto kh_id = size_t{0}; kh_id < args.kernel_h; ++kh_id) { // kernel height - for (auto kw_id = size_t{0}; kw_id < args.kernel_w; ++kw_id) { // kernel width - for (auto h_id = size_t{0}; h_id < args.height; h_id += args.stride_h) { // image height - for (auto w_id = size_t{0}; w_id < args.width; w_id += args.stride_w) { // image width - - // Retrieves the input value - const auto h_index = -args.pad_h + kh_id * args.dilation_h + h_id; - const auto w_index = -args.pad_w + kw_id * args.dilation_w + w_id; - auto val = ConstantZero(); - if (h_index < args.height && w_index < args.width) { - const auto input_index = w_index + args.width * (h_index + args.height * c_id); - val = buffers_host.a_mat[input_index + args.a_offset]; - } - - // Sets the output value - const auto kernel_index = kw_id + args.kernel_w * kh_id; - const auto patch_index = w_id + ((args.width / args.stride_w) * h_id + (args.height / args.stride_h) * c_id); - const auto output_index = kernel_index + args.kernel_h * args.kernel_w * patch_index; - buffers_host.b_mat[output_index + args.b_offset] = val; - } - } - } - } - } - return StatusCode::kSuccess; -} - -// ================================================================================================= - // See comment at top of file for a description of the class template class TestXim2col { @@ -71,8 +39,20 @@ public: static std::vector BuffersOut() { return {kBufMatB}; } // Describes how to obtain the sizes of the buffers + static size_t OutputHeight(const Arguments &args) { + const auto size = args.height + 2 * args.pad_h; + const auto padding = args.dilation_h * (args.kernel_h - 1) + 1; + if (size >= padding) { return (size - padding) / args.stride_h + 1; } + return 1; + } + static size_t OutputWidth(const Arguments &args) { + const auto size = args.width + 2 * args.pad_w; + const auto padding = args.dilation_w * (args.kernel_w - 1) + 1; + if (size >= padding) { return (size - padding) / args.stride_w + 1; } + return 1; + } static size_t NumPatches(const Arguments &args) { - return (args.width / args.stride_w) * (args.height / args.stride_h) * args.channels; + return OutputHeight(args) * OutputWidth(args) * args.channels; } static size_t GetSizeA(const Arguments &args) { return args.height * args.width * args.channels + args.a_offset; @@ -158,6 +138,42 @@ public: } }; +// ================================================================================================= + +template +StatusCode RunReference(const Arguments &args, BuffersHost &buffers_host) { + const auto output_h = TestXim2col::OutputHeight(args); + const auto output_w = TestXim2col::OutputWidth(args); + for (auto c_id = size_t{0}; c_id < args.channels; ++c_id) { // input channels + for (auto kh_id = size_t{0}; kh_id < args.kernel_h; ++kh_id) { // kernel height + for (auto kw_id = size_t{0}; kw_id < args.kernel_w; ++kw_id) { // kernel width + for (auto h_id = size_t{0}; h_id < output_h; ++h_id) { // image height + for (auto w_id = size_t{0}; w_id < output_w; ++w_id) { // image width + + // Retrieves the input value + const auto h_index = -args.pad_h + kh_id * args.dilation_h + args.stride_h * h_id; + const auto w_index = -args.pad_w + kw_id * args.dilation_w + args.stride_w * w_id; + auto val = ConstantZero(); + if (h_index >= 0 && h_index < args.height && + w_index >= 0 && w_index < args.width) { + const auto input_index = w_index + args.width * (h_index + args.height * c_id); + val = buffers_host.a_mat[input_index + args.a_offset]; + } + + // Sets the output value + const auto kernel_index = kw_id + args.kernel_w * kh_id; + const auto patch_index = w_id + output_w * h_id; + const auto output_index = patch_index + kernel_index * output_w * output_h + + c_id * output_w * output_h * args.kernel_h * args.kernel_w; + buffers_host.b_mat[output_index + args.b_offset] = val; + } + } + } + } + } + return StatusCode ::kSuccess; +} + // ================================================================================================= } // namespace clblast -- cgit v1.2.3 From 803ca781f9be56f86a0806689f8886a2428d5b9f Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Sat, 19 Aug 2017 18:25:13 +0200 Subject: First version of im2col kernel, unoptimized but working --- src/kernels/levelx/im2col.opencl | 73 ++++++++++++++++++++++++++++++++++++++++ src/routines/levelx/xim2col.cpp | 38 ++++++++++++++++++++- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/kernels/levelx/im2col.opencl 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 +// +// 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 Xim2col::Xim2col(Queue &queue, EventPointer event, const std::string &name): Routine(queue, event, name, {}, PrecisionValue(), {}, { -#include "../../kernels/level3/level3.opencl" +#include "../../kernels/levelx/im2col.opencl" }) { } @@ -40,6 +40,42 @@ void Xim2col::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(height)); + kernel.SetArgument(1, static_cast(width)); + kernel.SetArgument(2, static_cast(output_h)); + kernel.SetArgument(3, static_cast(output_w)); + kernel.SetArgument(4, static_cast(kernel_h)); + kernel.SetArgument(5, static_cast(kernel_w)); + kernel.SetArgument(6, static_cast(pad_h)); + kernel.SetArgument(7, static_cast(pad_w)); + kernel.SetArgument(8, static_cast(stride_h)); + kernel.SetArgument(9, static_cast(stride_w)); + kernel.SetArgument(10, static_cast(dilation_h)); + kernel.SetArgument(11, static_cast(dilation_w)); + kernel.SetArgument(12, im_buffer()); + kernel.SetArgument(13, static_cast(im_offset)); + kernel.SetArgument(14, col_buffer()); + kernel.SetArgument(15, static_cast(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{h_ceiled, w_ceiled, channels}; + auto local = std::vector{16, 16, 1}; + RunKernel(kernel, queue_, device_, global, local, event_); } // ================================================================================================= -- cgit v1.2.3 From a8c26594d9ee5b735b9d1b0400ac7eba4abf02fb Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Wed, 23 Aug 2017 19:54:09 +0200 Subject: Made the im2col client properly handle the arguments --- src/utilities/utilities.hpp | 8 ++++---- test/performance/client.cpp | 26 +++++++++++++++++++++++++- test/routines/levelx/xim2col.hpp | 4 +++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp index 784e0324..ad6edf3a 100644 --- a/src/utilities/utilities.hpp +++ b/src/utilities/utilities.hpp @@ -171,10 +171,10 @@ struct Arguments { size_t channels = 1; size_t height = 1; size_t width = 1; - size_t kernel_h = 1; - size_t kernel_w = 1; - size_t pad_h = 1; - size_t pad_w = 1; + size_t kernel_h = 3; + size_t kernel_w = 3; + size_t pad_h = 0; + size_t pad_w = 0; size_t stride_h = 1; size_t stride_w = 1; size_t dilation_h = 1; diff --git a/test/performance/client.cpp b/test/performance/client.cpp index dc98ffbd..076481f7 100644 --- a/test/performance/client.cpp +++ b/test/performance/client.cpp @@ -93,7 +93,7 @@ Arguments Client::ParseArguments(int argc, char *argv[], const size_t le if (o == kArgAPOffset) { args.ap_offset= GetArgument(command_line_args, help, kArgAPOffset, size_t{0}); } // Scalar result arguments - if (o == kArgDotOffset) { args.dot_offset = GetArgument(command_line_args, help, kArgDotOffset, size_t{0}); } + if (o == kArgDotOffset) { args.dot_offset = GetArgument(command_line_args, help, kArgDotOffset, size_t{0}); } if (o == kArgNrm2Offset) { args.nrm2_offset = GetArgument(command_line_args, help, kArgNrm2Offset, size_t{0}); } if (o == kArgAsumOffset) { args.asum_offset = GetArgument(command_line_args, help, kArgAsumOffset, size_t{0}); } if (o == kArgImaxOffset) { args.imax_offset = GetArgument(command_line_args, help, kArgImaxOffset, size_t{0}); } @@ -104,6 +104,19 @@ Arguments Client::ParseArguments(int argc, char *argv[], const size_t le // Scalar values if (o == kArgAlpha) { args.alpha = GetArgument(command_line_args, help, kArgAlpha, GetScalar()); } if (o == kArgBeta) { args.beta = GetArgument(command_line_args, help, kArgBeta, GetScalar()); } + + // Arguments for im2col + if (o == kArgChannels) { args.channels = GetArgument(command_line_args, help, kArgChannels, size_t{64}); } + if (o == kArgHeight) { args.height = GetArgument(command_line_args, help, kArgHeight, size_t{64}); } + if (o == kArgWidth) { args.width = GetArgument(command_line_args, help, kArgWidth, size_t{64}); } + if (o == kArgKernelH) { args.kernel_h = GetArgument(command_line_args, help, kArgKernelH, size_t{3}); } + if (o == kArgKernelW) { args.kernel_w = GetArgument(command_line_args, help, kArgKernelW, size_t{3}); } + if (o == kArgPadH) { args.pad_h = GetArgument(command_line_args, help, kArgPadH, size_t{0}); } + if (o == kArgPadW) { args.pad_w = GetArgument(command_line_args, help, kArgPadW, size_t{0}); } + if (o == kArgStrideH) { args.stride_h = GetArgument(command_line_args, help, kArgStrideH, size_t{1}); } + if (o == kArgStrideW) { args.stride_w = GetArgument(command_line_args, help, kArgStrideW, size_t{1}); } + if (o == kArgDilationH) { args.dilation_h = GetArgument(command_line_args, help, kArgDilationH, size_t{1}); } + if (o == kArgDilationW) { args.dilation_w = GetArgument(command_line_args, help, kArgDilationW, size_t{1}); } } // These are the options common to all routines @@ -379,6 +392,17 @@ void Client::PrintTableRow(const Arguments& args, else if (o == kArgAsumOffset){integers.push_back(args.asum_offset); } else if (o == kArgImaxOffset){integers.push_back(args.imax_offset); } else if (o == kArgBatchCount){integers.push_back(args.batch_count); } + else if (o == kArgChannels) {integers.push_back(args.channels); } + else if (o == kArgHeight) {integers.push_back(args.height); } + else if (o == kArgWidth) {integers.push_back(args.width); } + else if (o == kArgKernelH) {integers.push_back(args.kernel_h); } + else if (o == kArgKernelW) {integers.push_back(args.kernel_w); } + else if (o == kArgPadH) {integers.push_back(args.pad_h); } + else if (o == kArgPadW) {integers.push_back(args.pad_w); } + else if (o == kArgStrideH) {integers.push_back(args.stride_h); } + else if (o == kArgStrideW) {integers.push_back(args.stride_w); } + else if (o == kArgDilationH) {integers.push_back(args.dilation_h); } + else if (o == kArgDilationW) {integers.push_back(args.dilation_w); } } auto strings = std::vector{}; for (auto &o: options_) { diff --git a/test/routines/levelx/xim2col.hpp b/test/routines/levelx/xim2col.hpp index e6aefd9e..59be8156 100644 --- a/test/routines/levelx/xim2col.hpp +++ b/test/routines/levelx/xim2col.hpp @@ -134,7 +134,9 @@ public: return 1; } static size_t GetBytes(const Arguments &args) { - return (1) * sizeof(T); + const auto input = args.channels * args.width * args.height; // possibly less with striding + const auto output = args.kernel_h * args.kernel_w * NumPatches(args); + return (input + output) * sizeof(T); } }; -- cgit v1.2.3 From 4d9d03ba512fb404537180a42cf17fb3e10a8033 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Thu, 24 Aug 2017 21:11:12 +0200 Subject: Completed im2col implementation --- CHANGELOG | 6 +++++- README.md | 1 + src/kernels/levelx/im2col.opencl | 17 +++++++++++------ src/routines/levelx/xim2col.cpp | 10 +++++----- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e7e0e9ae..ac323ca0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,10 @@ +Development (next version) +- Added non-BLAS routines: + * SIM2COL/DIM2COL/CIM2COL/ZIM2COL/HIM2COL (im2col transform as used to express convolution as GEMM) + Version 1.0.1 -- Fixed a bug in the direct version of the GEMM kernel +- Fixed a bug in the direct version of the GEMM kernel) Version 1.0.0 - Fixed a bug in the TRSM routine for alpha != 1 diff --git a/README.md b/README.md index 2386cc88..b647f4da 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,7 @@ In addition, some extra non-BLAS routines are also supported by CLBlast, classif | IxMAX | ✔ | ✔ | ✔ | ✔ | ✔ | | IxMIN | ✔ | ✔ | ✔ | ✔ | ✔ | | xOMATCOPY | ✔ | ✔ | ✔ | ✔ | ✔ | +| xIM2COL | ✔ | ✔ | ✔ | ✔ | ✔ | Some less commonly used BLAS routines are not yet supported yet by CLBlast. They are xROTG, xROTMG, xROT, xROTM, xTBSV, and xTPSV. diff --git a/src/kernels/levelx/im2col.opencl b/src/kernels/levelx/im2col.opencl index 3f10881f..a141db41 100644 --- a/src/kernels/levelx/im2col.opencl +++ b/src/kernels/levelx/im2col.opencl @@ -15,12 +15,17 @@ // literal). Comment-out this line for syntax-highlighting when developing. R"( -// ================================================================================================= +// Work-group size parameters re-used from the 'copy' kernel +#ifndef COPY_DIMX + #define COPY_DIMX 8 // Local workgroup size in the first dimension (x) +#endif +#ifndef COPY_DIMY + #define COPY_DIMY 8 // Local workgroup size in the second dimension (y) +#endif -#define WGS1 16 -#define WGS2 16 +// ================================================================================================= -__kernel __attribute__((reqd_work_group_size(WGS1, WGS2, 1))) +__kernel __attribute__((reqd_work_group_size(COPY_DIMX, COPY_DIMY, 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, @@ -31,8 +36,8 @@ void im2col(const int input_h, const int input_w, __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 w_id = get_global_id(0); // image width, max 'output_w' + const int h_id = get_global_id(1); // image height, max 'output_h' const int c_id = get_global_id(2); // input channels if (h_id < output_h && w_id < output_w) { diff --git a/src/routines/levelx/xim2col.cpp b/src/routines/levelx/xim2col.cpp index 10c9c10c..51171eb5 100644 --- a/src/routines/levelx/xim2col.cpp +++ b/src/routines/levelx/xim2col.cpp @@ -22,7 +22,7 @@ namespace clblast { // Constructor: forwards to base class constructor template Xim2col::Xim2col(Queue &queue, EventPointer event, const std::string &name): - Routine(queue, event, name, {}, PrecisionValue(), {}, { + Routine(queue, event, name, {"Copy"}, PrecisionValue(), {}, { #include "../../kernels/levelx/im2col.opencl" }) { } @@ -71,10 +71,10 @@ void Xim2col::DoIm2col(const size_t channels, const size_t height, const size kernel.SetArgument(15, static_cast(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{h_ceiled, w_ceiled, channels}; - auto local = std::vector{16, 16, 1}; + const auto w_ceiled = Ceil(output_w, db_["COPY_DIMY"]); + const auto h_ceiled = Ceil(output_h, db_["COPY_DIMX"]); + const auto global = std::vector{w_ceiled, h_ceiled, channels}; + const auto local = std::vector{db_["COPY_DIMX"], db_["COPY_DIMY"], 1}; RunKernel(kernel, queue_, device_, global, local, event_); } -- cgit v1.2.3