47 #endif // HAVE_VIENNACL
55 namespace implementation
61 template <enum Backend,
class Matrix>
65 typedef typename Matrix::Scalar
T;
83 static void compute(Matrix X, Matrix W, Matrix Y,
bool flip ,
84 bool overwrite, int32_t stride_x, int32_t stride_y);
90 template <
class Matrix>
94 typedef typename Matrix::Scalar
T;
116 bool overwrite, int32_t stride_x, int32_t stride_y)
124 int32_t rx = (kx-1)/2;
125 int32_t ry = (ky-1)/2;
127 for (int32_t x=0; x<width; x+=stride_x)
129 int32_t xout = x/stride_x;
131 for (int32_t y=0; y<height; y+=stride_y)
133 int32_t yout = y/stride_y;
135 T
sum = overwrite ? 0 : Y(yout,xout);
136 for (int32_t x1=x-rx; x1<=x+rx; x1++)
138 int32_t wx = flip ? x1-x+rx : rx-x1+x;
139 for (int32_t y1=y-ry; y1<=y+ry; y1++)
141 if (x1>=0 && y1>=0 && x1<width && y1<height)
144 sum += W(y1-y+ry,wx)*X(y1,x1);
146 sum += W(ry-y1+y,wx)*X(y1,x1);
155 #endif // HAVE_EIGEN3
160 template <
class Matrix>
161 struct convolve<Backend::VIENNACL, Matrix>
164 typedef typename Matrix::Scalar
T;
168 static viennacl::ocl::kernel& generate_kernel_unity_stride(
169 int32_t radius_x, int32_t radius_y,
bool flip,
bool overwrite)
171 std::string kernel_name =
172 "convolve_unity_stride_" + ocl::get_type_string<T>() +
"_" +
173 std::to_string(radius_x) +
"_" + std::to_string(radius_y);
175 if (flip) kernel_name.append(
"_flip");
176 if (overwrite) kernel_name.append(
"_overwrite");
178 if (ocl::kernel_exists(kernel_name))
179 return ocl::get_kernel(kernel_name);
181 std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
183 if (flip) source.append(
"#define FLIP\n");
184 if (overwrite) source.append(
"#define OVERWRITE\n");
186 source.append(
"#define RADIUS_X " + std::to_string(radius_x) +
"\n");
187 source.append(
"#define RADIUS_Y " + std::to_string(radius_y) +
"\n");
191 #define W_WIDTH (2*RADIUS_X+1)
192 #define W_HEIGHT (2*RADIUS_Y+1)
194 #define X_LOCAL_WIDTH (WORK_GROUP_SIZE_2D+2*RADIUS_X)
195 #define X_LOCAL_HEIGHT (WORK_GROUP_SIZE_2D+2*RADIUS_Y)
197 inline DATATYPE readX(read_only __global DATATYPE* X, int x, int y,
198 int X_width, int X_height, int X_offset)
200 if (x>=0 && y>=0 && x<X_width && y<X_height)
201 return X[y + x*X_height + X_offset];
206 __kernel void KERNEL_NAME(
207 read_only __global DATATYPE* X, int X_width, int X_height, int X_offset,
208 __constant DATATYPE* W, int W_offset,
209 __global DATATYPE* Y, int Y_offset)
211 __local DATATYPE X_local[X_LOCAL_WIDTH][X_LOCAL_HEIGHT];
213 int x = get_global_id(0);
214 int y = get_global_id(1);
216 int xl = get_local_id(0);
217 int yl = get_local_id(1);
219 if (xl==WORK_GROUP_SIZE_2D-1 && yl == WORK_GROUP_SIZE_2D-1)
221 for (int rx=0; rx<=2*RADIUS_X; rx++)
222 for (int ry=0; ry<=2*RADIUS_Y; ry++)
223 X_local[xl+rx][yl+ry] = readX(X, x-RADIUS_X+rx, y-RADIUS_Y+ry, X_width, X_height, X_offset);
225 else if (xl==WORK_GROUP_SIZE_2D-1)
227 for (int rx=0; rx<=2*RADIUS_X; rx++)
228 X_local[xl+rx][yl] = readX(X, x-RADIUS_X+rx, y-RADIUS_Y, X_width, X_height, X_offset);
230 else if (yl == WORK_GROUP_SIZE_2D-1)
232 for (int ry=0; ry<=2*RADIUS_Y; ry++)
233 X_local[xl][yl+ry] = readX(X, x-RADIUS_X, y-RADIUS_Y+ry, X_width, X_height, X_offset);
236 X_local[xl][yl] = readX(X, x-RADIUS_X, y-RADIUS_Y, X_width, X_height, X_offset);
238 barrier(CLK_LOCAL_MEM_FENCE);
240 if (x>=X_width || y>=X_height)
244 for (int x1=0; x1<W_WIDTH; x1++)
247 int wx = x1*W_HEIGHT+W_offset;
249 int wx = (2*RADIUS_X-x1)*W_HEIGHT+W_offset;
252 for (int y1=0; y1<W_HEIGHT; y1++)
256 sum += W[y1+wx]*X_local[inx][iny];
258 sum += W[2*RADIUS_Y-y1+wx]*X_local[inx][iny];
263 Y[y+X_height*x + Y_offset] = sum;
265 Y[y+X_height*x + Y_offset] += sum;
271 viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
273 kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
274 kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
281 static viennacl::ocl::kernel& generate_kernel_arbitrary_stride(
282 int32_t radius_x, int32_t radius_y,
bool flip,
bool overwrite)
284 std::string kernel_name =
285 "convolve_arbitrary_stride_" + ocl::get_type_string<T>() +
"_" +
286 std::to_string(radius_x) +
"_" + std::to_string(radius_y);
288 if (flip) kernel_name.append(
"_flip");
289 if (overwrite) kernel_name.append(
"_overwrite");
291 if (ocl::kernel_exists(kernel_name))
292 return ocl::get_kernel(kernel_name);
294 std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
296 if (flip) source.append(
"#define FLIP\n");
297 if (overwrite) source.append(
"#define OVERWRITE\n");
299 source.append(
"#define RADIUS_X " + std::to_string(radius_x) +
"\n");
300 source.append(
"#define RADIUS_Y " + std::to_string(radius_y) +
"\n");
304 #define W_WIDTH (2*RADIUS_X+1)
305 #define W_HEIGHT (2*RADIUS_Y+1)
307 #define X_LOCAL_WIDTH (WORK_GROUP_SIZE_2D+2*RADIUS_X)
308 #define X_LOCAL_HEIGHT (WORK_GROUP_SIZE_2D+2*RADIUS_Y)
310 __kernel void KERNEL_NAME(
311 read_only __global DATATYPE* X, int X_width, int X_height, int X_offset,
312 __constant DATATYPE* W, int W_offset,
313 __global DATATYPE* Y, int Y_offset,
314 int stride_x, int stride_y)
316 __local DATATYPE X_local[WORK_GROUP_SIZE_2D][WORK_GROUP_SIZE_2D];
318 int x = get_global_id(0)*stride_x;
319 int y = get_global_id(1)*stride_y;
321 int Y_width = X_width/stride_x;
322 int Y_height = X_height/stride_y;
324 if (get_global_id(0)>=Y_width || get_global_id(1)>=Y_height)
328 for (int x1=0; x1<W_WIDTH; x1++)
331 int wx = x1*W_HEIGHT+W_offset;
333 int wx = (2*RADIUS_X-x1)*W_HEIGHT+W_offset;
335 int inx = x1+x-RADIUS_X;
336 for (int y1=0; y1<W_HEIGHT; y1++)
338 int iny = y1+y-RADIUS_Y;
339 if (inx>=0 && iny>=0 && inx<X_width && iny<X_height)
342 sum += W[y1+wx]*X[iny+inx*X_height+X_offset];
344 sum += W[2*RADIUS_Y-y1+wx]*X[iny+inx*X_height+X_offset];
350 Y[get_global_id(1)+Y_height*get_global_id(0) + Y_offset] = sum;
352 Y[get_global_id(1)+Y_height*get_global_id(0) + Y_offset] += sum;
358 viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
360 kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
361 kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
382 static void compute(CGPUMatrix<T> X, CGPUMatrix<T> W, CGPUMatrix<T> Y,
bool flip ,
383 bool overwrite, int32_t stride_x, int32_t stride_y)
385 if (stride_x==1 && stride_y==1)
387 viennacl::ocl::kernel& kernel = generate_kernel_unity_stride<T>(
388 (W.num_cols-1)/2, (W.num_rows-1)/2, flip, overwrite);
390 kernel.global_work_size(0, ocl::align_to_multiple_2d(Y.num_cols));
391 kernel.global_work_size(1, ocl::align_to_multiple_2d(Y.num_rows));
393 viennacl::ocl::enqueue(kernel(
394 X.vcl_matrix(), cl_int(X.num_cols), cl_int(X.num_rows), cl_int(X.offset),
395 W.vcl_matrix(), cl_int(W.offset),
396 Y.vcl_matrix(), cl_int(Y.offset)));
400 viennacl::ocl::kernel& kernel = generate_kernel_arbitrary_stride<T>(
401 (W.num_cols-1)/2, (W.num_rows-1)/2, flip, overwrite);
403 kernel.global_work_size(0, ocl::align_to_multiple_2d(Y.num_cols));
404 kernel.global_work_size(1, ocl::align_to_multiple_2d(Y.num_rows));
406 viennacl::ocl::enqueue(kernel(
407 X.vcl_matrix(), cl_int(X.num_cols), cl_int(X.num_rows), cl_int(X.offset),
408 W.vcl_matrix(), cl_int(W.offset),
409 Y.vcl_matrix(), cl_int(Y.offset),
410 cl_int(stride_x), cl_int(stride_y)));
415 #endif // HAVE_VIENNACL
422 #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