blob: 6e5e80253818103cdd90c671d42f0b352c92ffee [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/CL/CLKernelLibrary.h"
25
steniu0134702472017-07-11 09:22:58 +010026#include "arm_compute/core/CL/CLHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Utils.h"
29
steniu015f910722017-08-23 10:15:22 +010030#include <algorithm>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <fstream>
32#include <iostream>
33#include <utility>
34#include <vector>
35
36using namespace arm_compute;
37
38Program::Program()
39 : _context(), _device(), _is_binary(false), _name(), _source(), _binary()
40{
41}
42
43Program::Program(cl::Context context, std::string name, std::string source)
44 : _context(std::move(context)), _device(), _is_binary(false), _name(std::move(name)), _source(std::move(source)), _binary()
45{
46}
47
48Program::Program(cl::Context context, cl::Device device, std::string name, std::vector<unsigned char> binary)
49 : _context(std::move(context)), _device(std::move(device)), _is_binary(true), _name(std::move(name)), _source(), _binary(std::move(binary))
50{
51}
52
53Program::operator cl::Program() const
54{
55 if(_is_binary)
56 {
57 return cl::Program(_context, { _device }, { _binary });
58 }
59 else
60 {
61 return cl::Program(_context, _source, false);
62 }
63}
64
65bool Program::build(const cl::Program &program, const std::string &build_options)
66{
67 try
68 {
69 return program.build(build_options.c_str()) == CL_SUCCESS;
70 }
71 catch(const cl::Error &e)
72 {
73 cl_int err = CL_SUCCESS;
74 const auto build_info = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(&err);
75
76 for(auto &pair : build_info)
77 {
78 std::cerr << pair.second << std::endl;
79 }
80
81 return false;
82 }
83}
84
85cl::Program Program::build(const std::string &build_options) const
86{
87 cl::Program cl_program = static_cast<cl::Program>(*this);
88 build(cl_program, build_options);
89 return cl_program;
90}
91
92Kernel::Kernel()
93 : _name(), _kernel()
94{
95}
96
97Kernel::Kernel(std::string name, const cl::Program &program)
98 : _name(std::move(name)),
99 _kernel(cl::Kernel(program, _name.c_str()))
100{
101}
102
103const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map =
104{
105 { "absdiff", "absdiff.cl" },
106 { "accumulate", "accumulate.cl" },
107 { "accumulate_squared", "accumulate.cl" },
108 { "accumulate_weighted", "accumulate.cl" },
109 { "activation_layer", "activation_layer.cl" },
110 { "arithmetic_add", "arithmetic_op.cl" },
111 { "arithmetic_sub", "arithmetic_op.cl" },
112 { "bitwise_or", "bitwise_op.cl" },
113 { "bitwise_and", "bitwise_op.cl" },
114 { "bitwise_xor", "bitwise_op.cl" },
115 { "bitwise_not", "bitwise_op.cl" },
116 { "channel_combine_NV", "channel_combine.cl" },
117 { "channel_combine_RGB888", "channel_combine.cl" },
118 { "channel_combine_RGBA8888", "channel_combine.cl" },
119 { "channel_combine_UYVY422", "channel_combine.cl" },
120 { "channel_combine_YUYV422", "channel_combine.cl" },
121 { "channel_extract_NV12", "channel_extract.cl" },
122 { "channel_extract_NV21", "channel_extract.cl" },
123 { "channel_extract_RGB888", "channel_extract.cl" },
124 { "channel_extract_RGBA8888", "channel_extract.cl" },
125 { "channel_extract_UYVY422", "channel_extract.cl" },
126 { "channel_extract_YUYV422", "channel_extract.cl" },
127 { "combine_gradients_L1", "canny.cl" },
128 { "combine_gradients_L2", "canny.cl" },
129 { "concatenate_depth", "concatenate.cl" },
130 { "convolution_rectangle", "convolution_rectangle.cl" },
131 { "col2im", "convolution_layer.cl" },
132 { "convolution3x3_static", "convolution3x3.cl" },
133 { "convolution5x5_static", "convolution5x5.cl" },
134 { "convolution7x7_static", "convolution7x7.cl" },
135 { "convolution9x9_static", "convolution9x9.cl" },
136 { "convolution_separable1x5_static", "convolution5x5.cl" },
137 { "convolution_separable5x1_static", "convolution5x5.cl" },
138 { "convolution_separable1x7_static", "convolution7x7.cl" },
139 { "convolution_separable7x1_static", "convolution7x7.cl" },
140 { "convolution_separable1x9_static", "convolution9x9.cl" },
141 { "convolution_separable9x1_static", "convolution9x9.cl" },
142 { "convert_depth_down", "depth_convert.cl" },
143 { "convert_depth_up", "depth_convert.cl" },
144 { "copy_plane", "channel_extract.cl" },
145 { "copy_planes_3p", "channel_combine.cl" },
146 { "copy_to_keypoint", "fast_corners.cl" },
Giorgio Arena93a690e2017-08-01 16:09:33 +0100147 { "depthwise_convolution_3x3", "depthwise_convolution.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100148 { "depthwise_im2col", "depthwise_convolution.cl" },
149 { "depthwise_vector_to_tensor", "depthwise_convolution.cl" },
150 { "depthwise_weights_reshape", "depthwise_convolution.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100151 { "dequantization_layer", "dequantization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 { "derivative", "derivative.cl" },
153 { "dilate", "dilate.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100154 { "direct_convolution1x1", "direct_convolution1x1.cl" },
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100155 { "direct_convolution1x1_f32_bifrost", "direct_convolution1x1.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100156 { "direct_convolution3x3", "direct_convolution3x3.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100157 { "direct_convolution3x3_f32_bifrost", "direct_convolution3x3.cl" },
steniu01db006682017-08-09 16:26:22 +0100158 { "direct_convolution5x5", "direct_convolution5x5.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100159 { "direct_convolution5x5_f32_bifrost", "direct_convolution5x5.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 { "erode", "erode.cl" },
161 { "fast_corners", "fast_corners.cl" },
162 { "fill_image_borders_constant", "fill_border.cl" },
163 { "fill_image_borders_replicate", "fill_border.cl" },
164 { "finalize", "optical_flow_pyramid_lk.cl" },
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100165 { "floor_layer", "floor.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
167 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100168 { "gemm_accumulate_biases", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 { "gemm_interleave4x4_8bit", "gemm.cl" },
170 { "gemm_interleave4x4_16bit", "gemm.cl" },
171 { "gemm_interleave4x4_32bit", "gemm.cl" },
172 { "gemm_ma_f16", "gemm.cl" },
173 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100174 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100175 { "gemm_ma_qs16", "gemm.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100176 { "gemm_mv", "gemv.cl" },
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100177 { "gemm_mm_interleaved_transposed_u8", "gemm.cl" },
178 { "gemm_mm_interleaved_transposed_f16", "gemm.cl" },
179 { "gemm_mm_interleaved_transposed_f32_midgard", "gemm.cl" },
180 { "gemm_mm_interleaved_transposed_f32_bifrost", "gemm.cl" },
181 { "gemm_mm_interleaved_transposed_qs8", "gemm.cl" },
182 { "gemm_mm_interleaved_transposed_qs16", "gemm.cl" },
183 { "gemm_mm_floating_point", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100184 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100185 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100187 { "gemm_transpose1x16", "gemm.cl" },
188 { "gemm_transpose1x8", "gemm.cl" },
189 { "gemm_transpose1x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 { "harris_score_3x3", "harris_corners.cl" },
191 { "harris_score_5x5", "harris_corners.cl" },
192 { "harris_score_7x7", "harris_corners.cl" },
193 { "hist_border_kernel", "histogram.cl" },
194 { "hist_border_kernel_fixed", "histogram.cl" },
195 { "hist_local_kernel", "histogram.cl" },
196 { "hist_local_kernel_fixed", "histogram.cl" },
197 { "hog_block_normalization", "hog.cl" },
198 { "hog_detector", "hog.cl" },
199 { "hog_orientation_binning", "hog.cl" },
200 { "hysteresis", "canny.cl" },
201 { "im2col_generic", "convolution_layer.cl" },
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100202 { "im2col_kernel3x3_padx0_pady0", "convolution_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 { "im2col_reduced", "convolution_layer.cl" },
204 { "init_level", "optical_flow_pyramid_lk.cl" },
205 { "init_level_max", "optical_flow_pyramid_lk.cl" },
206 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
207 { "integral_horizontal", "integral_image.cl" },
208 { "integral_vertical", "integral_image.cl" },
209 { "IYUV_to_NV12_bt709", "color_convert.cl" },
210 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
211 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
212 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100213 { "l2_normalize", "l2_normalize.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
215 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
216 { "magnitude_phase", "magnitude_phase.cl" },
217 { "mean_stddev_accumulate", "mean_stddev.cl" },
218 { "minmax", "minmaxloc.cl" },
219 { "minmax_border", "minmaxloc.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100220 { "minmax_layer", "minmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 { "minmaxloc", "minmaxloc.cl" },
222 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
223 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
224 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
225 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
226 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
227 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
228 { "non_max_suppression", "nonmax.cl" },
229 { "normalization_layer_cross_map", "normalization_layer.cl" },
230 { "normalization_layer_in_map_1D", "normalization_layer.cl" },
231 { "batchnormalization_layer", "batchnormalization_layer.cl" },
232 { "NV12_to_IYUV_bt709", "color_convert.cl" },
233 { "NV12_to_RGB888_bt709", "color_convert.cl" },
234 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
235 { "NV12_to_YUV444_bt709", "color_convert.cl" },
236 { "NV21_to_IYUV_bt709", "color_convert.cl" },
237 { "NV21_to_RGB888_bt709", "color_convert.cl" },
238 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
239 { "NV21_to_YUV444_bt709", "color_convert.cl" },
240 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
241 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
242 { "pooling_layer_2", "pooling_layer.cl" },
243 { "pooling_layer_3", "pooling_layer.cl" },
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100244 { "pooling_layer_3_optimized", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100245 { "pooling_layer_7", "pooling_layer.cl" },
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100246 { "pooling_layer_N", "pooling_layer.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100247 { "quantization_layer", "quantization_layer.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100248 { "reduction_operation", "reduction_operation.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 { "remap_nearest_neighbour", "remap.cl" },
250 { "remap_bilinear", "remap.cl" },
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100251 { "reshape_layer", "reshape_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252 { "reshape_to_columns", "convolution_layer.cl" },
253 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
254 { "RGB888_to_NV12_bt709", "color_convert.cl" },
255 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
256 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
257 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
258 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
259 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
260 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
SiCong Li3e363692017-07-04 15:02:10 +0100261 { "roi_pooling_layer", "roi_pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262 { "scale_nearest_neighbour", "scale.cl" },
263 { "scale_bilinear", "scale.cl" },
264 { "scharr3x3", "scharr_filter.cl" },
265 { "sobel3x3", "sobel_filter.cl" },
266 { "sobel_separable5x1", "sobel_filter.cl" },
267 { "sobel_separable1x5", "sobel_filter.cl" },
268 { "sobel_separable7x1", "sobel_filter.cl" },
269 { "sobel_separable1x7", "sobel_filter.cl" },
270 { "softmax_layer_max", "softmax_layer.cl" },
271 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
272 { "softmax_layer_norm", "softmax_layer.cl" },
273 { "suppress_non_maximum", "canny.cl" },
274 { "tablelookup_U8", "tablelookup.cl" },
275 { "tablelookup_S16", "tablelookup.cl" },
276 { "threshold_binary", "threshold.cl" },
277 { "threshold_range", "threshold.cl" },
278 { "transpose", "transpose.cl" },
279 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
280 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
281 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
282 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
283 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
284 { "warp_affine_bilinear", "warp_affine.cl" },
285 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
286 { "warp_perspective_bilinear", "warp_perspective.cl" },
287 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
288 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
289 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
290 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
291};
292
293const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
294{
295#ifdef EMBEDDED_KERNELS
296 {
297 "absdiff.cl",
298#include "./cl_kernels/absdiff.clembed"
299 },
300 {
301 "accumulate.cl",
302#include "./cl_kernels/accumulate.clembed"
303 },
304 {
305 "activation_layer.cl",
306#include "./cl_kernels/activation_layer.clembed"
307 },
308 {
309 "arithmetic_op.cl",
310#include "./cl_kernels/arithmetic_op.clembed"
311 },
312 {
313 "bitwise_op.cl",
314#include "./cl_kernels/bitwise_op.clembed"
315 },
316 {
317 "canny.cl",
318#include "./cl_kernels/canny.clembed"
319 },
320 {
321 "channel_combine.cl",
322#include "./cl_kernels/channel_combine.clembed"
323 },
324 {
325 "channel_extract.cl",
326#include "./cl_kernels/channel_extract.clembed"
327 },
328 {
329 "concatenate.cl",
330#include "./cl_kernels/concatenate.clembed"
331 },
332 {
333 "color_convert.cl",
334#include "./cl_kernels/color_convert.clembed"
335 },
336 {
337 "convolution3x3.cl",
338#include "./cl_kernels/convolution3x3.clembed"
339 },
340 {
341 "convolution5x5.cl",
342#include "./cl_kernels/convolution5x5.clembed"
343 },
344 {
345 "convolution7x7.cl",
346#include "./cl_kernels/convolution7x7.clembed"
347 },
348 {
349 "convolution9x9.cl",
350#include "./cl_kernels/convolution9x9.clembed"
351 },
352 {
353 "convolution_layer.cl",
354#include "./cl_kernels/convolution_layer.clembed"
355 },
356 {
357 "convolution_rectangle.cl",
358#include "./cl_kernels/convolution_rectangle.clembed"
359 },
360 {
361 "depth_convert.cl",
362#include "./cl_kernels/depth_convert.clembed"
363 },
364 {
Giorgio Arena93a690e2017-08-01 16:09:33 +0100365 "depthwise_convolution.cl",
366#include "./cl_kernels/depthwise_convolution.clembed"
367 },
368 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100369 "dequantization_layer.cl",
370#include "./cl_kernels/dequantization_layer.clembed"
371 },
372 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373 "derivative.cl",
374#include "./cl_kernels/derivative.clembed"
375 },
376 {
377 "dilate.cl",
378#include "./cl_kernels/dilate.clembed"
379 },
380 {
SiCong Lic51b72f2017-07-28 14:46:20 +0100381 "direct_convolution1x1.cl",
382#include "./cl_kernels/direct_convolution1x1.clembed"
383 },
384 {
385 "direct_convolution3x3.cl",
386#include "./cl_kernels/direct_convolution3x3.clembed"
steniu0127b386c2017-07-18 17:37:43 +0100387 },
388 {
steniu01db006682017-08-09 16:26:22 +0100389 "direct_convolution5x5.cl",
390#include "./cl_kernels/direct_convolution5x5.clembed"
391 },
392 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100393 "erode.cl",
394#include "./cl_kernels/erode.clembed"
395 },
396 {
397 "fast_corners.cl",
398#include "./cl_kernels/fast_corners.clembed"
399 },
400 {
401 "fill_border.cl",
402#include "./cl_kernels/fill_border.clembed"
403 },
404 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100405 "fixed_point.h",
406#include "./cl_kernels/fixed_point.hembed"
407 },
408 {
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100409 "floor.cl",
410#include "./cl_kernels/floor.clembed"
411 },
412 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413 "gaussian_pyramid.cl",
414#include "./cl_kernels/gaussian_pyramid.clembed"
415 },
416 {
417 "gemm.cl",
418#include "./cl_kernels/gemm.clembed"
419 },
420 {
Giorgio Arena9fe41442017-08-23 16:36:24 +0100421 "gemv.cl",
422#include "./cl_kernels/gemv.clembed"
423 },
424 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100425 "harris_corners.cl",
426#include "./cl_kernels/harris_corners.clembed"
427 },
428 {
429 "helpers.h",
430#include "./cl_kernels/helpers.hembed"
431 },
432 {
433 "histogram.cl",
434#include "./cl_kernels/histogram.clembed"
435 },
436 {
437 "hog.cl",
438#include "./cl_kernels/hog.clembed"
439 },
440 {
441 "integral_image.cl",
442#include "./cl_kernels/integral_image.clembed"
443 },
444 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100445 "l2_normalize.cl",
446#include "./cl_kernels/l2_normalize.clembed"
447 },
448 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449 "magnitude_phase.cl",
450#include "./cl_kernels/magnitude_phase.clembed"
451 },
452 {
453 "mean_stddev.cl",
454#include "./cl_kernels/mean_stddev.clembed"
455 },
456 {
457 "minmaxloc.cl",
458#include "./cl_kernels/minmaxloc.clembed"
459 },
460 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100461 "minmax_layer.cl",
462#include "./cl_kernels/minmax_layer.clembed"
463 },
464 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100465 "non_linear_filter3x3.cl",
466#include "./cl_kernels/non_linear_filter3x3.clembed"
467 },
468 {
469 "non_linear_filter5x5.cl",
470#include "./cl_kernels/non_linear_filter5x5.clembed"
471 },
472 {
473 "non_linear_filter_helpers.h",
474#include "./cl_kernels/non_linear_filter_helpers.hembed"
475 },
476 {
477 "nonmax.cl",
478#include "./cl_kernels/nonmax.clembed"
479 },
480 {
481 "normalization_layer.cl",
482#include "./cl_kernels/normalization_layer.clembed"
483 },
484 {
485 "batchnormalization_layer.cl",
486#include "./cl_kernels/batchnormalization_layer.clembed"
487 },
488 {
489 "optical_flow_pyramid_lk.cl",
490#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
491 },
492 {
493 "pixelwise_mul_float.cl",
494#include "./cl_kernels/pixelwise_mul_float.clembed"
495 },
496 {
497 "pixelwise_mul_int.cl",
498#include "./cl_kernels/pixelwise_mul_int.clembed"
499 },
500 {
501 "pooling_layer.cl",
502#include "./cl_kernels/pooling_layer.clembed"
503 },
504 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100505 "quantization_layer.cl",
506#include "./cl_kernels/quantization_layer.clembed"
507 },
508 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100509 "reduction_operation.cl",
510#include "./cl_kernels/reduction_operation.clembed"
511 },
512 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100513 "remap.cl",
514#include "./cl_kernels/remap.clembed"
515 },
516 {
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100517 "reshape_layer.cl",
518#include "./cl_kernels/reshape_layer.clembed"
519 },
520 {
SiCong Li3e363692017-07-04 15:02:10 +0100521 "roi_pooling_layer.cl",
522#include "./cl_kernels/roi_pooling_layer.clembed"
523 },
524 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100525 "scale.cl",
526#include "./cl_kernels/scale.clembed"
527 },
528 {
529 "scharr_filter.cl",
530#include "./cl_kernels/scharr_filter.clembed"
531 },
532 {
533 "sobel_filter.cl",
534#include "./cl_kernels/sobel_filter.clembed"
535 },
536 {
537 "softmax_layer.cl",
538#include "./cl_kernels/softmax_layer.clembed"
539 },
540 {
541 "tablelookup.cl",
542#include "./cl_kernels/tablelookup.clembed"
543 },
544 {
545 "threshold.cl",
546#include "./cl_kernels/threshold.clembed"
547 },
548 {
549 "transpose.cl",
550#include "./cl_kernels/transpose.clembed"
551 },
552 {
553 "types.h",
554#include "./cl_kernels/types.hembed"
555 },
556 {
557 "warp_affine.cl",
558#include "./cl_kernels/warp_affine.clembed"
559 },
560 {
561 "warp_helpers.h",
562#include "./cl_kernels/warp_helpers.hembed"
563 },
564 {
565 "warp_perspective.cl",
566#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100567 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100568#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100569};
570
571CLKernelLibrary::CLKernelLibrary()
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100572 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100573{
574}
575
576CLKernelLibrary &CLKernelLibrary::get()
577{
578 static CLKernelLibrary _kernel_library;
579 return _kernel_library;
580}
581
582Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
583{
584 // Find which program contains the kernel
585 auto kernel_program_it = _kernel_program_map.find(kernel_name);
586
587 if(_kernel_program_map.end() == kernel_program_it)
588 {
589 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
590 }
591
steniu0134702472017-07-11 09:22:58 +0100592 std::string concat_str;
593
594 if(non_uniform_workgroup_support(_device))
595 {
596 concat_str += " -cl-arm-non-uniform-work-group-size ";
597 }
598 else if(get_cl_version(_device) == CLVersion::CL20)
599 {
600 concat_str += " -cl-std=CL2.0 ";
601 }
602 else
603 {
604 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
605 }
606
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100607 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100608 const std::string program_name = kernel_program_it->second;
609 const std::string build_options = stringify_set(build_options_set) + concat_str;
610
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100611 const std::string built_program_name = program_name + "_" + build_options;
612 auto built_program_it = _built_programs_map.find(built_program_name);
613
614 cl::Program cl_program;
615
616 if(_built_programs_map.end() != built_program_it)
617 {
618 // If program has been built, retrieve to create kernel from it
619 cl_program = built_program_it->second;
620 }
621 else
622 {
623 // Get program
624 Program program = load_program(program_name);
625
626 // Build program
627 cl_program = program.build(build_options);
628
629 // Add built program to internal map
630 _built_programs_map.emplace(built_program_name, cl_program);
631 }
632
633 // Create and return kernel
634 return Kernel(kernel_name, cl_program);
635}
636
637const Program &CLKernelLibrary::load_program(const std::string &program_name) const
638{
639 const auto program_it = _programs_map.find(program_name);
640
641 if(program_it != _programs_map.end())
642 {
643 return program_it->second;
644 }
645
646 Program program;
647
648#ifdef EMBEDDED_KERNELS
649 const auto program_source_it = _program_source_map.find(program_name);
650
651 if(_program_source_map.end() == program_source_it)
652 {
653 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
654 }
655
656 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100657#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100658 // Check for binary
659 std::string source_name = _kernel_path + program_name;
660 std::string binary_name = source_name + "bin";
661
662 if(std::ifstream(binary_name).is_open())
663 {
664 const std::string program_binary = read_file(binary_name, true);
665 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
666 }
667 else if(std::ifstream(source_name).is_open())
668 {
669 program = Program(_context, program_name, read_file(source_name, false));
670 }
671 else
672 {
673 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
674 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100675#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100676
677 // Insert program to program map
678 const auto new_program = _programs_map.emplace(program_name, std::move(program));
679
680 return new_program.first->second;
681}
682
683std::string CLKernelLibrary::stringify_set(const StringSet &s) const
684{
steniu0134702472017-07-11 09:22:58 +0100685 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100686
687#ifndef EMBEDDED_KERNELS
688 concat_set += "-I" + _kernel_path + " ";
689#endif /* EMBEDDED_KERNELS */
690
691 // Concatenate set
692 for(const auto &el : s)
693 {
694 concat_set += " " + el;
695 }
696
697 return concat_set;
698}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100699
700std::string CLKernelLibrary::get_program_source(const std::string &program_name)
701{
702 const auto program_source_it = _program_source_map.find(program_name);
703
704 if(program_source_it == _program_source_map.end())
705 {
706 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
707 }
708
709 return program_source_it->second;
710}
steniu015f910722017-08-23 10:15:22 +0100711
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100712size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100713{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100714 size_t result;
steniu015f910722017-08-23 10:15:22 +0100715
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100716 size_t err = kernel.getWorkGroupInfo(_device, CL_KERNEL_WORK_GROUP_SIZE, &result);
717 ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
718 ARM_COMPUTE_UNUSED(err);
719
720 return result;
steniu015f910722017-08-23 10:15:22 +0100721}
722
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100723cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100724{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100725 return cl::NDRange(128u, 1);
steniu015f910722017-08-23 10:15:22 +0100726}