45 #endif // HAVE_VIENNACL
53 namespace implementation
59 template <enum Backend,
class Matrix>
63 typedef typename Matrix::Scalar
T;
81 static void compute(Matrix X, Matrix W, Matrix Y,
bool flip ,
82 bool overwrite, int32_t stride_x, int32_t stride_y);
87 template <
class Matrix>
91 typedef typename Matrix::Scalar
T;
113 bool overwrite, int32_t stride_x, int32_t stride_y)
121 int32_t rx = (kx-1)/2;
122 int32_t ry = (ky-1)/2;
124 for (int32_t x=0; x<width; x+=stride_x)
126 int32_t xout = x/stride_x;
128 for (int32_t y=0; y<height; y+=stride_y)
130 int32_t yout = y/stride_y;
132 T
sum = overwrite ? 0 : Y(yout,xout);
133 for (int32_t x1=x-rx; x1<=x+rx; x1++)
135 int32_t wx = flip ? x1-x+rx : rx-x1+x;
136 for (int32_t y1=y-ry; y1<=y+ry; y1++)
138 if (x1>=0 && y1>=0 && x1<width && y1<height)
141 sum += W(y1-y+ry,wx)*X(y1,x1);
143 sum += W(ry-y1+y,wx)*X(y1,x1);
156 template <
class Matrix>
157 struct convolve<Backend::VIENNACL, Matrix>
160 typedef typename Matrix::Scalar
T;
164 static viennacl::ocl::kernel& generate_kernel_unity_stride(
165 int32_t radius_x, int32_t radius_y,
bool flip,
bool overwrite)
167 std::string kernel_name =
168 "convolve_unity_stride_" + ocl::get_type_string<T>() +
"_" +
169 std::to_string(radius_x) +
"_" + std::to_string(radius_y);
171 if (flip) kernel_name.append(
"_flip");
172 if (overwrite) kernel_name.append(
"_overwrite");
174 if (ocl::kernel_exists(kernel_name))
175 return ocl::get_kernel(kernel_name);
177 std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
179 if (flip) source.append(
"#define FLIP\n");
180 if (overwrite) source.append(
"#define OVERWRITE\n");
182 source.append(
"#define RADIUS_X " + std::to_string(radius_x) +
"\n");
183 source.append(
"#define RADIUS_Y " + std::to_string(radius_y) +
"\n");
187 #define W_WIDTH (2*RADIUS_X+1)
188 #define W_HEIGHT (2*RADIUS_Y+1)
190 #define X_LOCAL_WIDTH (WORK_GROUP_SIZE_2D+2*RADIUS_X)
191 #define X_LOCAL_HEIGHT (WORK_GROUP_SIZE_2D+2*RADIUS_Y)
193 inline DATATYPE readX(read_only __global DATATYPE* X, int x, int y,
194 int X_width, int X_height, int X_offset)
196 if (x>=0 && y>=0 && x<X_width && y<X_height)
197 return X[y + x*X_height + X_offset];
202 __kernel void KERNEL_NAME(
203 read_only __global DATATYPE* X, int X_width, int X_height, int X_offset,
204 __constant DATATYPE* W, int W_offset,
205 __global DATATYPE* Y, int Y_offset)
207 __local DATATYPE X_local[X_LOCAL_WIDTH][X_LOCAL_HEIGHT];
209 int x = get_global_id(0);
210 int y = get_global_id(1);
212 int xl = get_local_id(0);
213 int yl = get_local_id(1);
215 if (xl==WORK_GROUP_SIZE_2D-1 && yl == WORK_GROUP_SIZE_2D-1)
217 for (int rx=0; rx<=2*RADIUS_X; rx++)
218 for (int ry=0; ry<=2*RADIUS_Y; ry++)
219 X_local[xl+rx][yl+ry] = readX(X, x-RADIUS_X+rx, y-RADIUS_Y+ry, X_width, X_height, X_offset);
221 else if (xl==WORK_GROUP_SIZE_2D-1)
223 for (int rx=0; rx<=2*RADIUS_X; rx++)
224 X_local[xl+rx][yl] = readX(X, x-RADIUS_X+rx, y-RADIUS_Y, X_width, X_height, X_offset);
226 else if (yl == WORK_GROUP_SIZE_2D-1)
228 for (int ry=0; ry<=2*RADIUS_Y; ry++)
229 X_local[xl][yl+ry] = readX(X, x-RADIUS_X, y-RADIUS_Y+ry, X_width, X_height, X_offset);
232 X_local[xl][yl] = readX(X, x-RADIUS_X, y-RADIUS_Y, X_width, X_height, X_offset);
234 barrier(CLK_LOCAL_MEM_FENCE);
236 if (x>=X_width || y>=X_height)
240 for (int x1=0; x1<W_WIDTH; x1++)
243 int wx = x1*W_HEIGHT+W_offset;
245 int wx = (2*RADIUS_X-x1)*W_HEIGHT+W_offset;
248 for (int y1=0; y1<W_HEIGHT; y1++)
252 sum += W[y1+wx]*X_local[inx][iny];
254 sum += W[2*RADIUS_Y-y1+wx]*X_local[inx][iny];
259 Y[y+X_height*x + Y_offset] = sum;
261 Y[y+X_height*x + Y_offset] += sum;
267 viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
269 kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
270 kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
277 static viennacl::ocl::kernel& generate_kernel_arbitrary_stride(
278 int32_t radius_x, int32_t radius_y,
bool flip,
bool overwrite)
280 std::string kernel_name =
281 "convolve_arbitrary_stride_" + ocl::get_type_string<T>() +
"_" +
282 std::to_string(radius_x) +
"_" + std::to_string(radius_y);
284 if (flip) kernel_name.append(
"_flip");
285 if (overwrite) kernel_name.append(
"_overwrite");
287 if (ocl::kernel_exists(kernel_name))
288 return ocl::get_kernel(kernel_name);
290 std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
292 if (flip) source.append(
"#define FLIP\n");
293 if (overwrite) source.append(
"#define OVERWRITE\n");
295 source.append(
"#define RADIUS_X " + std::to_string(radius_x) +
"\n");
296 source.append(
"#define RADIUS_Y " + std::to_string(radius_y) +
"\n");
300 #define W_WIDTH (2*RADIUS_X+1)
301 #define W_HEIGHT (2*RADIUS_Y+1)
303 #define X_LOCAL_WIDTH (WORK_GROUP_SIZE_2D+2*RADIUS_X)
304 #define X_LOCAL_HEIGHT (WORK_GROUP_SIZE_2D+2*RADIUS_Y)
306 __kernel void KERNEL_NAME(
307 read_only __global DATATYPE* X, int X_width, int X_height, int X_offset,
308 __constant DATATYPE* W, int W_offset,
309 __global DATATYPE* Y, int Y_offset,
310 int stride_x, int stride_y)
312 __local DATATYPE X_local[WORK_GROUP_SIZE_2D][WORK_GROUP_SIZE_2D];
314 int x = get_global_id(0)*stride_x;
315 int y = get_global_id(1)*stride_y;
317 int Y_width = X_width/stride_x;
318 int Y_height = X_height/stride_y;
320 if (get_global_id(0)>=Y_width || get_global_id(1)>=Y_height)
324 for (int x1=0; x1<W_WIDTH; x1++)
327 int wx = x1*W_HEIGHT+W_offset;
329 int wx = (2*RADIUS_X-x1)*W_HEIGHT+W_offset;
331 int inx = x1+x-RADIUS_X;
332 for (int y1=0; y1<W_HEIGHT; y1++)
334 int iny = y1+y-RADIUS_Y;
335 if (inx>=0 && iny>=0 && inx<X_width && iny<X_height)
338 sum += W[y1+wx]*X[iny+inx*X_height+X_offset];
340 sum += W[2*RADIUS_Y-y1+wx]*X[iny+inx*X_height+X_offset];
346 Y[get_global_id(1)+Y_height*get_global_id(0) + Y_offset] = sum;
348 Y[get_global_id(1)+Y_height*get_global_id(0) + Y_offset] += sum;
354 viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
356 kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
357 kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
378 static void compute(CGPUMatrix<T> X, CGPUMatrix<T> W, CGPUMatrix<T> Y,
bool flip ,
379 bool overwrite, int32_t stride_x, int32_t stride_y)
381 if (stride_x==1 && stride_y==1)
383 viennacl::ocl::kernel& kernel = generate_kernel_unity_stride<T>(
384 (W.num_cols-1)/2, (W.num_rows-1)/2, flip, overwrite);
386 kernel.global_work_size(0, ocl::align_to_multiple_2d(Y.num_cols));
387 kernel.global_work_size(1, ocl::align_to_multiple_2d(Y.num_rows));
389 viennacl::ocl::enqueue(kernel(
390 X.vcl_matrix(), cl_int(X.num_cols), cl_int(X.num_rows), cl_int(X.offset),
391 W.vcl_matrix(), cl_int(W.offset),
392 Y.vcl_matrix(), cl_int(Y.offset)));
396 viennacl::ocl::kernel& kernel = generate_kernel_arbitrary_stride<T>(
397 (W.num_cols-1)/2, (W.num_rows-1)/2, flip, overwrite);
399 kernel.global_work_size(0, ocl::align_to_multiple_2d(Y.num_cols));
400 kernel.global_work_size(1, ocl::align_to_multiple_2d(Y.num_rows));
402 viennacl::ocl::enqueue(kernel(
403 X.vcl_matrix(), cl_int(X.num_cols), cl_int(X.num_rows), cl_int(X.offset),
404 W.vcl_matrix(), cl_int(W.offset),
405 Y.vcl_matrix(), cl_int(Y.offset),
406 cl_int(stride_x), cl_int(stride_y)));
411 #endif // HAVE_VIENNACL
418 #endif // CONVOLVE_H_
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
static void compute(SGMatrix< T > X, SGMatrix< T > W, SGMatrix< T > Y, bool flip, bool overwrite, int32_t stride_x, int32_t stride_y)
Generic class sum which provides a static compute method. This class is specialized for different typ...
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
static void compute(Matrix X, Matrix W, Matrix Y, bool flip, bool overwrite, int32_t stride_x, int32_t stride_y)
all of classes and functions are contained in the shogun namespace