blob: 32199525b082ae46670c66ffb9926c8f575d9de3 [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
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000038CLBuildOptions::CLBuildOptions()
39 : _build_opts()
40{
41}
42
43void CLBuildOptions::add_option(std::string option)
44{
45 _build_opts.emplace(std::move(option));
46}
47
48void CLBuildOptions::add_option_if(bool cond, std::string option)
49{
50 if(cond)
51 {
52 add_option(std::move(option));
53 }
54}
55
56void CLBuildOptions::add_option_if_else(bool cond, std::string option_true, std::string option_false)
57{
58 (cond) ? add_option(std::move(option_true)) : add_option(std::move(option_false));
59}
60
Chunosovd621bca2017-11-03 17:33:15 +070061const CLBuildOptions::StringSet &CLBuildOptions::options() const
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000062{
63 return _build_opts;
64}
65
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066Program::Program()
67 : _context(), _device(), _is_binary(false), _name(), _source(), _binary()
68{
69}
70
71Program::Program(cl::Context context, std::string name, std::string source)
72 : _context(std::move(context)), _device(), _is_binary(false), _name(std::move(name)), _source(std::move(source)), _binary()
73{
74}
75
76Program::Program(cl::Context context, cl::Device device, std::string name, std::vector<unsigned char> binary)
77 : _context(std::move(context)), _device(std::move(device)), _is_binary(true), _name(std::move(name)), _source(), _binary(std::move(binary))
78{
79}
80
81Program::operator cl::Program() const
82{
83 if(_is_binary)
84 {
85 return cl::Program(_context, { _device }, { _binary });
86 }
87 else
88 {
89 return cl::Program(_context, _source, false);
90 }
91}
92
93bool Program::build(const cl::Program &program, const std::string &build_options)
94{
95 try
96 {
97 return program.build(build_options.c_str()) == CL_SUCCESS;
98 }
99 catch(const cl::Error &e)
100 {
101 cl_int err = CL_SUCCESS;
102 const auto build_info = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(&err);
103
104 for(auto &pair : build_info)
105 {
106 std::cerr << pair.second << std::endl;
107 }
108
109 return false;
110 }
111}
112
113cl::Program Program::build(const std::string &build_options) const
114{
115 cl::Program cl_program = static_cast<cl::Program>(*this);
116 build(cl_program, build_options);
117 return cl_program;
118}
119
120Kernel::Kernel()
121 : _name(), _kernel()
122{
123}
124
125Kernel::Kernel(std::string name, const cl::Program &program)
126 : _name(std::move(name)),
127 _kernel(cl::Kernel(program, _name.c_str()))
128{
129}
130
131const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map =
132{
133 { "absdiff", "absdiff.cl" },
134 { "accumulate", "accumulate.cl" },
135 { "accumulate_squared", "accumulate.cl" },
136 { "accumulate_weighted", "accumulate.cl" },
137 { "activation_layer", "activation_layer.cl" },
Michel Iwaniec00633802017-10-12 14:14:15 +0100138 { "activation_layer_qa8", "activation_layer_qa8.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 { "arithmetic_add", "arithmetic_op.cl" },
140 { "arithmetic_sub", "arithmetic_op.cl" },
141 { "bitwise_or", "bitwise_op.cl" },
142 { "bitwise_and", "bitwise_op.cl" },
143 { "bitwise_xor", "bitwise_op.cl" },
144 { "bitwise_not", "bitwise_op.cl" },
145 { "channel_combine_NV", "channel_combine.cl" },
146 { "channel_combine_RGB888", "channel_combine.cl" },
147 { "channel_combine_RGBA8888", "channel_combine.cl" },
148 { "channel_combine_UYVY422", "channel_combine.cl" },
149 { "channel_combine_YUYV422", "channel_combine.cl" },
150 { "channel_extract_NV12", "channel_extract.cl" },
151 { "channel_extract_NV21", "channel_extract.cl" },
152 { "channel_extract_RGB888", "channel_extract.cl" },
153 { "channel_extract_RGBA8888", "channel_extract.cl" },
154 { "channel_extract_UYVY422", "channel_extract.cl" },
155 { "channel_extract_YUYV422", "channel_extract.cl" },
156 { "combine_gradients_L1", "canny.cl" },
157 { "combine_gradients_L2", "canny.cl" },
158 { "concatenate_depth", "concatenate.cl" },
159 { "convolution_rectangle", "convolution_rectangle.cl" },
160 { "col2im", "convolution_layer.cl" },
161 { "convolution3x3_static", "convolution3x3.cl" },
162 { "convolution5x5_static", "convolution5x5.cl" },
163 { "convolution7x7_static", "convolution7x7.cl" },
164 { "convolution9x9_static", "convolution9x9.cl" },
165 { "convolution_separable1x5_static", "convolution5x5.cl" },
166 { "convolution_separable5x1_static", "convolution5x5.cl" },
167 { "convolution_separable1x7_static", "convolution7x7.cl" },
168 { "convolution_separable7x1_static", "convolution7x7.cl" },
169 { "convolution_separable1x9_static", "convolution9x9.cl" },
170 { "convolution_separable9x1_static", "convolution9x9.cl" },
171 { "convert_depth_down", "depth_convert.cl" },
172 { "convert_depth_up", "depth_convert.cl" },
173 { "copy_plane", "channel_extract.cl" },
174 { "copy_planes_3p", "channel_combine.cl" },
175 { "copy_to_keypoint", "fast_corners.cl" },
Giorgio Arena93a690e2017-08-01 16:09:33 +0100176 { "depthwise_convolution_3x3", "depthwise_convolution.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100177 { "depthwise_im2col", "depthwise_convolution.cl" },
178 { "depthwise_vector_to_tensor", "depthwise_convolution.cl" },
179 { "depthwise_weights_reshape", "depthwise_convolution.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100180 { "dequantization_layer", "dequantization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 { "derivative", "derivative.cl" },
182 { "dilate", "dilate.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100183 { "direct_convolution1x1", "direct_convolution1x1.cl" },
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100184 { "direct_convolution1x1_f32_bifrost", "direct_convolution1x1.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100185 { "direct_convolution3x3", "direct_convolution3x3.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100186 { "direct_convolution3x3_f32_bifrost", "direct_convolution3x3.cl" },
steniu01db006682017-08-09 16:26:22 +0100187 { "direct_convolution5x5", "direct_convolution5x5.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100188 { "direct_convolution5x5_f32_bifrost", "direct_convolution5x5.cl" },
Chunosovd621bca2017-11-03 17:33:15 +0700189 { "direct_convolution_1x1_3x3_5x5_quantized", "direct_convolution_1x1_3x3_5x5_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 { "erode", "erode.cl" },
191 { "fast_corners", "fast_corners.cl" },
192 { "fill_image_borders_constant", "fill_border.cl" },
193 { "fill_image_borders_replicate", "fill_border.cl" },
194 { "finalize", "optical_flow_pyramid_lk.cl" },
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100195 { "floor_layer", "floor.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
197 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100198 { "gemm_accumulate_biases", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 { "gemm_interleave4x4_8bit", "gemm.cl" },
200 { "gemm_interleave4x4_16bit", "gemm.cl" },
201 { "gemm_interleave4x4_32bit", "gemm.cl" },
202 { "gemm_ma_f16", "gemm.cl" },
203 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100204 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100205 { "gemm_ma_qs16", "gemm.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100206 { "gemm_mv", "gemv.cl" },
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100207 { "gemm_mm_interleaved_transposed_u8", "gemm.cl" },
208 { "gemm_mm_interleaved_transposed_f16", "gemm.cl" },
209 { "gemm_mm_interleaved_transposed_f32_midgard", "gemm.cl" },
210 { "gemm_mm_interleaved_transposed_f32_bifrost", "gemm.cl" },
211 { "gemm_mm_interleaved_transposed_qs8", "gemm.cl" },
212 { "gemm_mm_interleaved_transposed_qs16", "gemm.cl" },
213 { "gemm_mm_floating_point", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100214 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100215 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100217 { "gemm_transpose1x16", "gemm.cl" },
218 { "gemm_transpose1x8", "gemm.cl" },
219 { "gemm_transpose1x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 { "harris_score_3x3", "harris_corners.cl" },
221 { "harris_score_5x5", "harris_corners.cl" },
222 { "harris_score_7x7", "harris_corners.cl" },
223 { "hist_border_kernel", "histogram.cl" },
224 { "hist_border_kernel_fixed", "histogram.cl" },
225 { "hist_local_kernel", "histogram.cl" },
226 { "hist_local_kernel_fixed", "histogram.cl" },
227 { "hog_block_normalization", "hog.cl" },
228 { "hog_detector", "hog.cl" },
229 { "hog_orientation_binning", "hog.cl" },
230 { "hysteresis", "canny.cl" },
231 { "im2col_generic", "convolution_layer.cl" },
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100232 { "im2col_kernel3x3_padx0_pady0", "convolution_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 { "im2col_reduced", "convolution_layer.cl" },
234 { "init_level", "optical_flow_pyramid_lk.cl" },
235 { "init_level_max", "optical_flow_pyramid_lk.cl" },
236 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
237 { "integral_horizontal", "integral_image.cl" },
238 { "integral_vertical", "integral_image.cl" },
239 { "IYUV_to_NV12_bt709", "color_convert.cl" },
240 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
241 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
242 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100243 { "l2_normalize", "l2_normalize.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
245 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
246 { "magnitude_phase", "magnitude_phase.cl" },
247 { "mean_stddev_accumulate", "mean_stddev.cl" },
248 { "minmax", "minmaxloc.cl" },
249 { "minmax_border", "minmaxloc.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100250 { "minmax_layer", "minmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251 { "minmaxloc", "minmaxloc.cl" },
252 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
253 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
254 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
255 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
256 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
257 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
258 { "non_max_suppression", "nonmax.cl" },
259 { "normalization_layer_cross_map", "normalization_layer.cl" },
260 { "normalization_layer_in_map_1D", "normalization_layer.cl" },
261 { "batchnormalization_layer", "batchnormalization_layer.cl" },
262 { "NV12_to_IYUV_bt709", "color_convert.cl" },
263 { "NV12_to_RGB888_bt709", "color_convert.cl" },
264 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
265 { "NV12_to_YUV444_bt709", "color_convert.cl" },
266 { "NV21_to_IYUV_bt709", "color_convert.cl" },
267 { "NV21_to_RGB888_bt709", "color_convert.cl" },
268 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
269 { "NV21_to_YUV444_bt709", "color_convert.cl" },
270 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
271 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
272 { "pooling_layer_2", "pooling_layer.cl" },
273 { "pooling_layer_3", "pooling_layer.cl" },
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100274 { "pooling_layer_3_optimized", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100275 { "pooling_layer_7", "pooling_layer.cl" },
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100276 { "pooling_layer_N", "pooling_layer.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100277 { "quantization_layer", "quantization_layer.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100278 { "reduction_operation", "reduction_operation.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279 { "remap_nearest_neighbour", "remap.cl" },
280 { "remap_bilinear", "remap.cl" },
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100281 { "reshape_layer", "reshape_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282 { "reshape_to_columns", "convolution_layer.cl" },
283 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
284 { "RGB888_to_NV12_bt709", "color_convert.cl" },
285 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
286 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
287 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
288 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
289 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
290 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
SiCong Li3e363692017-07-04 15:02:10 +0100291 { "roi_pooling_layer", "roi_pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292 { "scale_nearest_neighbour", "scale.cl" },
293 { "scale_bilinear", "scale.cl" },
294 { "scharr3x3", "scharr_filter.cl" },
295 { "sobel3x3", "sobel_filter.cl" },
296 { "sobel_separable5x1", "sobel_filter.cl" },
297 { "sobel_separable1x5", "sobel_filter.cl" },
298 { "sobel_separable7x1", "sobel_filter.cl" },
299 { "sobel_separable1x7", "sobel_filter.cl" },
300 { "softmax_layer_max", "softmax_layer.cl" },
301 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
302 { "softmax_layer_norm", "softmax_layer.cl" },
303 { "suppress_non_maximum", "canny.cl" },
304 { "tablelookup_U8", "tablelookup.cl" },
305 { "tablelookup_S16", "tablelookup.cl" },
306 { "threshold_binary", "threshold.cl" },
307 { "threshold_range", "threshold.cl" },
308 { "transpose", "transpose.cl" },
309 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
310 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
311 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
312 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
313 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
314 { "warp_affine_bilinear", "warp_affine.cl" },
315 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
316 { "warp_perspective_bilinear", "warp_perspective.cl" },
317 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
318 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
319 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
320 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
321};
322
323const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
324{
325#ifdef EMBEDDED_KERNELS
326 {
327 "absdiff.cl",
328#include "./cl_kernels/absdiff.clembed"
329 },
330 {
331 "accumulate.cl",
332#include "./cl_kernels/accumulate.clembed"
333 },
334 {
335 "activation_layer.cl",
336#include "./cl_kernels/activation_layer.clembed"
337 },
338 {
Michel Iwaniec00633802017-10-12 14:14:15 +0100339 "activation_layer_qa8.cl",
340#include "./cl_kernels/activation_layer_qa8.clembed"
341 },
342 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343 "arithmetic_op.cl",
344#include "./cl_kernels/arithmetic_op.clembed"
345 },
346 {
347 "bitwise_op.cl",
348#include "./cl_kernels/bitwise_op.clembed"
349 },
350 {
351 "canny.cl",
352#include "./cl_kernels/canny.clembed"
353 },
354 {
355 "channel_combine.cl",
356#include "./cl_kernels/channel_combine.clembed"
357 },
358 {
359 "channel_extract.cl",
360#include "./cl_kernels/channel_extract.clembed"
361 },
362 {
363 "concatenate.cl",
364#include "./cl_kernels/concatenate.clembed"
365 },
366 {
367 "color_convert.cl",
368#include "./cl_kernels/color_convert.clembed"
369 },
370 {
371 "convolution3x3.cl",
372#include "./cl_kernels/convolution3x3.clembed"
373 },
374 {
375 "convolution5x5.cl",
376#include "./cl_kernels/convolution5x5.clembed"
377 },
378 {
379 "convolution7x7.cl",
380#include "./cl_kernels/convolution7x7.clembed"
381 },
382 {
383 "convolution9x9.cl",
384#include "./cl_kernels/convolution9x9.clembed"
385 },
386 {
387 "convolution_layer.cl",
388#include "./cl_kernels/convolution_layer.clembed"
389 },
390 {
391 "convolution_rectangle.cl",
392#include "./cl_kernels/convolution_rectangle.clembed"
393 },
394 {
395 "depth_convert.cl",
396#include "./cl_kernels/depth_convert.clembed"
397 },
398 {
Giorgio Arena93a690e2017-08-01 16:09:33 +0100399 "depthwise_convolution.cl",
400#include "./cl_kernels/depthwise_convolution.clembed"
401 },
402 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100403 "dequantization_layer.cl",
404#include "./cl_kernels/dequantization_layer.clembed"
405 },
406 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407 "derivative.cl",
408#include "./cl_kernels/derivative.clembed"
409 },
410 {
411 "dilate.cl",
412#include "./cl_kernels/dilate.clembed"
413 },
414 {
SiCong Lic51b72f2017-07-28 14:46:20 +0100415 "direct_convolution1x1.cl",
416#include "./cl_kernels/direct_convolution1x1.clembed"
417 },
418 {
419 "direct_convolution3x3.cl",
420#include "./cl_kernels/direct_convolution3x3.clembed"
steniu0127b386c2017-07-18 17:37:43 +0100421 },
422 {
steniu01db006682017-08-09 16:26:22 +0100423 "direct_convolution5x5.cl",
424#include "./cl_kernels/direct_convolution5x5.clembed"
425 },
426 {
Chunosovd621bca2017-11-03 17:33:15 +0700427 "direct_convolution_1x1_3x3_5x5_quantized.cl",
428#include "./cl_kernels/direct_convolution_1x1_3x3_5x5_quantized.clembed"
429 },
430 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100431 "erode.cl",
432#include "./cl_kernels/erode.clembed"
433 },
434 {
435 "fast_corners.cl",
436#include "./cl_kernels/fast_corners.clembed"
437 },
438 {
439 "fill_border.cl",
440#include "./cl_kernels/fill_border.clembed"
441 },
442 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100443 "fixed_point.h",
444#include "./cl_kernels/fixed_point.hembed"
445 },
446 {
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100447 "floor.cl",
448#include "./cl_kernels/floor.clembed"
449 },
450 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100451 "gaussian_pyramid.cl",
452#include "./cl_kernels/gaussian_pyramid.clembed"
453 },
454 {
455 "gemm.cl",
456#include "./cl_kernels/gemm.clembed"
457 },
458 {
Giorgio Arena9fe41442017-08-23 16:36:24 +0100459 "gemv.cl",
460#include "./cl_kernels/gemv.clembed"
461 },
462 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100463 "harris_corners.cl",
464#include "./cl_kernels/harris_corners.clembed"
465 },
466 {
467 "helpers.h",
468#include "./cl_kernels/helpers.hembed"
469 },
470 {
Chunosovd621bca2017-11-03 17:33:15 +0700471 "helpers_asymm.h",
472#include "./cl_kernels/helpers_asymm.hembed"
473 },
474 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100475 "histogram.cl",
476#include "./cl_kernels/histogram.clembed"
477 },
478 {
479 "hog.cl",
480#include "./cl_kernels/hog.clembed"
481 },
482 {
483 "integral_image.cl",
484#include "./cl_kernels/integral_image.clembed"
485 },
486 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100487 "l2_normalize.cl",
488#include "./cl_kernels/l2_normalize.clembed"
489 },
490 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100491 "magnitude_phase.cl",
492#include "./cl_kernels/magnitude_phase.clembed"
493 },
494 {
495 "mean_stddev.cl",
496#include "./cl_kernels/mean_stddev.clembed"
497 },
498 {
499 "minmaxloc.cl",
500#include "./cl_kernels/minmaxloc.clembed"
501 },
502 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100503 "minmax_layer.cl",
504#include "./cl_kernels/minmax_layer.clembed"
505 },
506 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100507 "non_linear_filter3x3.cl",
508#include "./cl_kernels/non_linear_filter3x3.clembed"
509 },
510 {
511 "non_linear_filter5x5.cl",
512#include "./cl_kernels/non_linear_filter5x5.clembed"
513 },
514 {
515 "non_linear_filter_helpers.h",
516#include "./cl_kernels/non_linear_filter_helpers.hembed"
517 },
518 {
519 "nonmax.cl",
520#include "./cl_kernels/nonmax.clembed"
521 },
522 {
523 "normalization_layer.cl",
524#include "./cl_kernels/normalization_layer.clembed"
525 },
526 {
527 "batchnormalization_layer.cl",
528#include "./cl_kernels/batchnormalization_layer.clembed"
529 },
530 {
531 "optical_flow_pyramid_lk.cl",
532#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
533 },
534 {
535 "pixelwise_mul_float.cl",
536#include "./cl_kernels/pixelwise_mul_float.clembed"
537 },
538 {
539 "pixelwise_mul_int.cl",
540#include "./cl_kernels/pixelwise_mul_int.clembed"
541 },
542 {
543 "pooling_layer.cl",
544#include "./cl_kernels/pooling_layer.clembed"
545 },
546 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100547 "quantization_layer.cl",
548#include "./cl_kernels/quantization_layer.clembed"
549 },
550 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100551 "reduction_operation.cl",
552#include "./cl_kernels/reduction_operation.clembed"
553 },
554 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100555 "remap.cl",
556#include "./cl_kernels/remap.clembed"
557 },
558 {
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100559 "reshape_layer.cl",
560#include "./cl_kernels/reshape_layer.clembed"
561 },
562 {
SiCong Li3e363692017-07-04 15:02:10 +0100563 "roi_pooling_layer.cl",
564#include "./cl_kernels/roi_pooling_layer.clembed"
565 },
566 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100567 "scale.cl",
568#include "./cl_kernels/scale.clembed"
569 },
570 {
571 "scharr_filter.cl",
572#include "./cl_kernels/scharr_filter.clembed"
573 },
574 {
575 "sobel_filter.cl",
576#include "./cl_kernels/sobel_filter.clembed"
577 },
578 {
579 "softmax_layer.cl",
580#include "./cl_kernels/softmax_layer.clembed"
581 },
582 {
583 "tablelookup.cl",
584#include "./cl_kernels/tablelookup.clembed"
585 },
586 {
587 "threshold.cl",
588#include "./cl_kernels/threshold.clembed"
589 },
590 {
591 "transpose.cl",
592#include "./cl_kernels/transpose.clembed"
593 },
594 {
595 "types.h",
596#include "./cl_kernels/types.hembed"
597 },
598 {
599 "warp_affine.cl",
600#include "./cl_kernels/warp_affine.clembed"
601 },
602 {
603 "warp_helpers.h",
604#include "./cl_kernels/warp_helpers.hembed"
605 },
606 {
607 "warp_perspective.cl",
608#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100609 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100610#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100611};
612
613CLKernelLibrary::CLKernelLibrary()
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100614 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100615{
616}
617
618CLKernelLibrary &CLKernelLibrary::get()
619{
620 static CLKernelLibrary _kernel_library;
621 return _kernel_library;
622}
623
624Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
625{
626 // Find which program contains the kernel
627 auto kernel_program_it = _kernel_program_map.find(kernel_name);
628
629 if(_kernel_program_map.end() == kernel_program_it)
630 {
631 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
632 }
633
steniu0134702472017-07-11 09:22:58 +0100634 std::string concat_str;
635
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100636 if(fp16_support(_device))
637 {
638 concat_str += " -DARM_COMPUTE_OPENCL_FP16_ENABLED=1 ";
639 }
640
steniu0134702472017-07-11 09:22:58 +0100641 if(non_uniform_workgroup_support(_device))
642 {
643 concat_str += " -cl-arm-non-uniform-work-group-size ";
644 }
645 else if(get_cl_version(_device) == CLVersion::CL20)
646 {
647 concat_str += " -cl-std=CL2.0 ";
648 }
649 else
650 {
651 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
652 }
653
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100654 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100655 const std::string program_name = kernel_program_it->second;
656 const std::string build_options = stringify_set(build_options_set) + concat_str;
657
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100658 const std::string built_program_name = program_name + "_" + build_options;
659 auto built_program_it = _built_programs_map.find(built_program_name);
660
661 cl::Program cl_program;
662
663 if(_built_programs_map.end() != built_program_it)
664 {
665 // If program has been built, retrieve to create kernel from it
666 cl_program = built_program_it->second;
667 }
668 else
669 {
670 // Get program
671 Program program = load_program(program_name);
672
673 // Build program
674 cl_program = program.build(build_options);
675
676 // Add built program to internal map
677 _built_programs_map.emplace(built_program_name, cl_program);
678 }
679
680 // Create and return kernel
681 return Kernel(kernel_name, cl_program);
682}
683
684const Program &CLKernelLibrary::load_program(const std::string &program_name) const
685{
686 const auto program_it = _programs_map.find(program_name);
687
688 if(program_it != _programs_map.end())
689 {
690 return program_it->second;
691 }
692
693 Program program;
694
695#ifdef EMBEDDED_KERNELS
696 const auto program_source_it = _program_source_map.find(program_name);
697
698 if(_program_source_map.end() == program_source_it)
699 {
700 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
701 }
702
703 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100704#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100705 // Check for binary
706 std::string source_name = _kernel_path + program_name;
707 std::string binary_name = source_name + "bin";
708
709 if(std::ifstream(binary_name).is_open())
710 {
711 const std::string program_binary = read_file(binary_name, true);
712 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
713 }
714 else if(std::ifstream(source_name).is_open())
715 {
716 program = Program(_context, program_name, read_file(source_name, false));
717 }
718 else
719 {
720 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
721 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100722#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100723
724 // Insert program to program map
725 const auto new_program = _programs_map.emplace(program_name, std::move(program));
726
727 return new_program.first->second;
728}
729
730std::string CLKernelLibrary::stringify_set(const StringSet &s) const
731{
steniu0134702472017-07-11 09:22:58 +0100732 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100733
734#ifndef EMBEDDED_KERNELS
735 concat_set += "-I" + _kernel_path + " ";
736#endif /* EMBEDDED_KERNELS */
737
738 // Concatenate set
739 for(const auto &el : s)
740 {
741 concat_set += " " + el;
742 }
743
744 return concat_set;
745}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100746
747std::string CLKernelLibrary::get_program_source(const std::string &program_name)
748{
749 const auto program_source_it = _program_source_map.find(program_name);
750
751 if(program_source_it == _program_source_map.end())
752 {
753 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
754 }
755
756 return program_source_it->second;
757}
steniu015f910722017-08-23 10:15:22 +0100758
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100759size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100760{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100761 size_t result;
steniu015f910722017-08-23 10:15:22 +0100762
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100763 size_t err = kernel.getWorkGroupInfo(_device, CL_KERNEL_WORK_GROUP_SIZE, &result);
764 ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
765 ARM_COMPUTE_UNUSED(err);
766
767 return result;
steniu015f910722017-08-23 10:15:22 +0100768}
769
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100770cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100771{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100772 return cl::NDRange(128u, 1);
steniu015f910722017-08-23 10:15:22 +0100773}