SHOGUN  4.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Convolve.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Khaled Nasr
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
31 #ifndef CONVOLVE_H_
32 #define CONVOLVE_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
37 
38 #include <shogun/io/SGIO.h>
39 
40 #ifdef HAVE_EIGEN3
42 #endif // HAVE_EIGEN3
43 
44 #ifdef HAVE_VIENNACL
45 #include <shogun/lib/GPUMatrix.h>
47 #endif // HAVE_VIENNACL
48 
49 namespace shogun
50 {
51 
52 namespace linalg
53 {
54 
55 namespace implementation
56 {
57 
61 template <enum Backend, class Matrix>
62 struct convolve
63 {
65  typedef typename Matrix::Scalar T;
66 
83  static void compute(Matrix X, Matrix W, Matrix Y, bool flip ,
84  bool overwrite, int32_t stride_x, int32_t stride_y);
85 };
86 
87 #ifdef HAVE_EIGEN3
88 
90 template <> template <class Matrix>
91 struct convolve<Backend::EIGEN3, Matrix>
92 {
93  typedef typename Matrix::Scalar T;
96 
110  static void compute(SGMatrix<T> X, SGMatrix<T> W, SGMatrix<T> Y, bool flip ,
111  bool overwrite, int32_t stride_x, int32_t stride_y)
112  {
113  int32_t width = X.num_cols;
114  int32_t height = X.num_rows;
115 
116  int32_t kx = W.num_cols;
117  int32_t ky = W.num_rows;
118 
119  int32_t rx = (kx-1)/2;
120  int32_t ry = (ky-1)/2;
121 
122  for (int32_t x=0; x<width; x+=stride_x)
123  {
124  int32_t xout = x/stride_x;
125 
126  for (int32_t y=0; y<height; y+=stride_y)
127  {
128  int32_t yout = y/stride_y;
129 
130  T sum = overwrite ? 0 : Y(yout,xout);
131  for (int32_t x1=x-rx; x1<=x+rx; x1++)
132  {
133  int32_t wx = flip ? x1-x+rx : rx-x1+x;
134  for (int32_t y1=y-ry; y1<=y+ry; y1++)
135  {
136  if (x1>=0 && y1>=0 && x1<width && y1<height)
137  {
138  if (flip)
139  sum += W(y1-y+ry,wx)*X(y1,x1);
140  else
141  sum += W(ry-y1+y,wx)*X(y1,x1);
142  }
143  }
144  }
145  Y(yout,xout) = sum;
146  }
147  }
148  }
149 };
150 #endif // HAVE_EIGEN3
151 
152 #ifdef HAVE_VIENNACL
153 
155 template <> template <class Matrix>
156 struct convolve<Backend::VIENNACL, Matrix>
157 {
158  typedef typename Matrix::Scalar T;
159 
161  template <class T>
162  static viennacl::ocl::kernel& generate_kernel_unity_stride(
163  int32_t radius_x, int32_t radius_y, bool flip, bool overwrite)
164  {
165  std::string kernel_name =
166  "convolve_unity_stride_" + ocl::get_type_string<T>() + "_" +
167  std::to_string(radius_x) + "_" + std::to_string(radius_y);
168 
169  if (flip) kernel_name.append("_flip");
170  if (overwrite) kernel_name.append("_overwrite");
171 
172  if (ocl::kernel_exists(kernel_name))
173  return ocl::get_kernel(kernel_name);
174 
175  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
176 
177  if (flip) source.append("#define FLIP\n");
178  if (overwrite) source.append("#define OVERWRITE\n");
179 
180  source.append("#define RADIUS_X " + std::to_string(radius_x) + "\n");
181  source.append("#define RADIUS_Y " + std::to_string(radius_y) + "\n");
182 
183  source.append(
184  R"(
185  #define W_WIDTH (2*RADIUS_X+1)
186  #define W_HEIGHT (2*RADIUS_Y+1)
187 
188  #define X_LOCAL_WIDTH (WORK_GROUP_SIZE_2D+2*RADIUS_X)
189  #define X_LOCAL_HEIGHT (WORK_GROUP_SIZE_2D+2*RADIUS_Y)
190 
191  inline DATATYPE readX(read_only __global DATATYPE* X, int x, int y,
192  int X_width, int X_height, int X_offset)
193  {
194  if (x>=0 && y>=0 && x<X_width && y<X_height)
195  return X[y + x*X_height + X_offset];
196  else
197  return 0;
198  }
199 
200  __kernel void KERNEL_NAME(
201  read_only __global DATATYPE* X, int X_width, int X_height, int X_offset,
202  __constant DATATYPE* W, int W_offset,
203  __global DATATYPE* Y, int Y_offset)
204  {
205  __local DATATYPE X_local[X_LOCAL_WIDTH][X_LOCAL_HEIGHT];
206 
207  int x = get_global_id(0);
208  int y = get_global_id(1);
209 
210  int xl = get_local_id(0);
211  int yl = get_local_id(1);
212 
213  if (xl==WORK_GROUP_SIZE_2D-1 && yl == WORK_GROUP_SIZE_2D-1)
214  {
215  for (int rx=0; rx<=2*RADIUS_X; rx++)
216  for (int ry=0; ry<=2*RADIUS_Y; ry++)
217  X_local[xl+rx][yl+ry] = readX(X, x-RADIUS_X+rx, y-RADIUS_Y+ry, X_width, X_height, X_offset);
218  }
219  else if (xl==WORK_GROUP_SIZE_2D-1)
220  {
221  for (int rx=0; rx<=2*RADIUS_X; rx++)
222  X_local[xl+rx][yl] = readX(X, x-RADIUS_X+rx, y-RADIUS_Y, X_width, X_height, X_offset);
223  }
224  else if (yl == WORK_GROUP_SIZE_2D-1)
225  {
226  for (int ry=0; ry<=2*RADIUS_Y; ry++)
227  X_local[xl][yl+ry] = readX(X, x-RADIUS_X, y-RADIUS_Y+ry, X_width, X_height, X_offset);
228  }
229  else
230  X_local[xl][yl] = readX(X, x-RADIUS_X, y-RADIUS_Y, X_width, X_height, X_offset);
231 
232  barrier(CLK_LOCAL_MEM_FENCE);
233 
234  if (x>=X_width || y>=X_height)
235  return;
236 
237  DATATYPE sum = 0;
238  for (int x1=0; x1<W_WIDTH; x1++)
239  {
240  #ifdef FLIP
241  int wx = x1*W_HEIGHT+W_offset;
242  #else
243  int wx = (2*RADIUS_X-x1)*W_HEIGHT+W_offset;
244  #endif
245  int inx = x1+xl;
246  for (int y1=0; y1<W_HEIGHT; y1++)
247  {
248  int iny = y1+yl;
249  #ifdef FLIP
250  sum += W[y1+wx]*X_local[inx][iny];
251  #else
252  sum += W[2*RADIUS_Y-y1+wx]*X_local[inx][iny];
253  #endif
254  }
255  }
256  #ifdef OVERWRITE
257  Y[y+X_height*x + Y_offset] = sum;
258  #else
259  Y[y+X_height*x + Y_offset] += sum;
260  #endif
261  }
262  )"
263  );
264 
265  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
266 
267  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
268  kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
269 
270  return kernel;
271  }
272 
274  template <class T>
275  static viennacl::ocl::kernel& generate_kernel_arbitrary_stride(
276  int32_t radius_x, int32_t radius_y, bool flip, bool overwrite)
277  {
278  std::string kernel_name =
279  "convolve_arbitrary_stride_" + ocl::get_type_string<T>() + "_" +
280  std::to_string(radius_x) + "_" + std::to_string(radius_y);
281 
282  if (flip) kernel_name.append("_flip");
283  if (overwrite) kernel_name.append("_overwrite");
284 
285  if (ocl::kernel_exists(kernel_name))
286  return ocl::get_kernel(kernel_name);
287 
288  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
289 
290  if (flip) source.append("#define FLIP\n");
291  if (overwrite) source.append("#define OVERWRITE\n");
292 
293  source.append("#define RADIUS_X " + std::to_string(radius_x) + "\n");
294  source.append("#define RADIUS_Y " + std::to_string(radius_y) + "\n");
295 
296  source.append(
297  R"(
298  #define W_WIDTH (2*RADIUS_X+1)
299  #define W_HEIGHT (2*RADIUS_Y+1)
300 
301  #define X_LOCAL_WIDTH (WORK_GROUP_SIZE_2D+2*RADIUS_X)
302  #define X_LOCAL_HEIGHT (WORK_GROUP_SIZE_2D+2*RADIUS_Y)
303 
304  __kernel void KERNEL_NAME(
305  read_only __global DATATYPE* X, int X_width, int X_height, int X_offset,
306  __constant DATATYPE* W, int W_offset,
307  __global DATATYPE* Y, int Y_offset,
308  int stride_x, int stride_y)
309  {
310  __local DATATYPE X_local[WORK_GROUP_SIZE_2D][WORK_GROUP_SIZE_2D];
311 
312  int x = get_global_id(0)*stride_x;
313  int y = get_global_id(1)*stride_y;
314 
315  int Y_width = X_width/stride_x;
316  int Y_height = X_height/stride_y;
317 
318  if (get_global_id(0)>=Y_width || get_global_id(1)>=Y_height)
319  return;
320 
321  DATATYPE sum = 0;
322  for (int x1=0; x1<W_WIDTH; x1++)
323  {
324  #ifdef FLIP
325  int wx = x1*W_HEIGHT+W_offset;
326  #else
327  int wx = (2*RADIUS_X-x1)*W_HEIGHT+W_offset;
328  #endif
329  int inx = x1+x-RADIUS_X;
330  for (int y1=0; y1<W_HEIGHT; y1++)
331  {
332  int iny = y1+y-RADIUS_Y;
333  if (inx>=0 && iny>=0 && inx<X_width && iny<X_height)
334  {
335  #ifdef FLIP
336  sum += W[y1+wx]*X[iny+inx*X_height+X_offset];
337  #else
338  sum += W[2*RADIUS_Y-y1+wx]*X[iny+inx*X_height+X_offset];
339  #endif
340  }
341  }
342  }
343  #ifdef OVERWRITE
344  Y[get_global_id(1)+Y_height*get_global_id(0) + Y_offset] = sum;
345  #else
346  Y[get_global_id(1)+Y_height*get_global_id(0) + Y_offset] += sum;
347  #endif
348  }
349  )"
350  );
351 
352  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
353 
354  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
355  kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
356 
357  return kernel;
358  }
359 
376  static void compute(CGPUMatrix<T> X, CGPUMatrix<T> W, CGPUMatrix<T> Y, bool flip ,
377  bool overwrite, int32_t stride_x, int32_t stride_y)
378  {
379  if (stride_x==1 && stride_y==1)
380  {
381  viennacl::ocl::kernel& kernel = generate_kernel_unity_stride<T>(
382  (W.num_cols-1)/2, (W.num_rows-1)/2, flip, overwrite);
383 
384  kernel.global_work_size(0, ocl::align_to_multiple_2d(Y.num_cols));
385  kernel.global_work_size(1, ocl::align_to_multiple_2d(Y.num_rows));
386 
387  viennacl::ocl::enqueue(kernel(
388  X.vcl_matrix(), cl_int(X.num_cols), cl_int(X.num_rows), cl_int(X.offset),
389  W.vcl_matrix(), cl_int(W.offset),
390  Y.vcl_matrix(), cl_int(Y.offset)));
391  }
392  else
393  {
394  viennacl::ocl::kernel& kernel = generate_kernel_arbitrary_stride<T>(
395  (W.num_cols-1)/2, (W.num_rows-1)/2, flip, overwrite);
396 
397  kernel.global_work_size(0, ocl::align_to_multiple_2d(Y.num_cols));
398  kernel.global_work_size(1, ocl::align_to_multiple_2d(Y.num_rows));
399 
400  viennacl::ocl::enqueue(kernel(
401  X.vcl_matrix(), cl_int(X.num_cols), cl_int(X.num_rows), cl_int(X.offset),
402  W.vcl_matrix(), cl_int(W.offset),
403  Y.vcl_matrix(), cl_int(Y.offset),
404  cl_int(stride_x), cl_int(stride_y)));
405  }
406  }
407 };
408 
409 #endif // HAVE_VIENNACL
410 
411 }
412 
413 }
414 
415 }
416 #endif // CONVOLVE_H_
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Convolve.h:94
static void compute(SGMatrix< T > X, SGMatrix< T > W, SGMatrix< T > Y, bool flip, bool overwrite, int32_t stride_x, int32_t stride_y)
Definition: Convolve.h:110
Generic class sum which provides a static compute method. This class is specialized for different typ...
Definition: Sum.h:71
index_t num_rows
Definition: SGMatrix.h:329
index_t num_cols
Definition: SGMatrix.h:331
static void compute(Matrix X, Matrix W, Matrix Y, bool flip, bool overwrite, int32_t stride_x, int32_t stride_y)
shogun matrix
Definition: Parameter.h:26

SHOGUN Machine Learning Toolbox - Documentation