blob: 6cc5a9a6b53d3452d7fb6b11e8acec6a22870ef8 [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
Chunosovf450caa2017-11-08 16:09:35 +070061void CLBuildOptions::add_options(const StringSet &options)
62{
63 _build_opts.insert(options.begin(), options.end());
64}
65
66void CLBuildOptions::add_options_if(bool cond, const StringSet &options)
67{
68 if(cond)
69 {
70 add_options(options);
71 }
72}
73
Chunosovd621bca2017-11-03 17:33:15 +070074const CLBuildOptions::StringSet &CLBuildOptions::options() const
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000075{
76 return _build_opts;
77}
78
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079Program::Program()
80 : _context(), _device(), _is_binary(false), _name(), _source(), _binary()
81{
82}
83
84Program::Program(cl::Context context, std::string name, std::string source)
85 : _context(std::move(context)), _device(), _is_binary(false), _name(std::move(name)), _source(std::move(source)), _binary()
86{
87}
88
89Program::Program(cl::Context context, cl::Device device, std::string name, std::vector<unsigned char> binary)
90 : _context(std::move(context)), _device(std::move(device)), _is_binary(true), _name(std::move(name)), _source(), _binary(std::move(binary))
91{
92}
93
94Program::operator cl::Program() const
95{
96 if(_is_binary)
97 {
98 return cl::Program(_context, { _device }, { _binary });
99 }
100 else
101 {
102 return cl::Program(_context, _source, false);
103 }
104}
105
106bool Program::build(const cl::Program &program, const std::string &build_options)
107{
108 try
109 {
110 return program.build(build_options.c_str()) == CL_SUCCESS;
111 }
112 catch(const cl::Error &e)
113 {
114 cl_int err = CL_SUCCESS;
115 const auto build_info = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(&err);
116
117 for(auto &pair : build_info)
118 {
119 std::cerr << pair.second << std::endl;
120 }
121
122 return false;
123 }
124}
125
126cl::Program Program::build(const std::string &build_options) const
127{
128 cl::Program cl_program = static_cast<cl::Program>(*this);
129 build(cl_program, build_options);
130 return cl_program;
131}
132
133Kernel::Kernel()
134 : _name(), _kernel()
135{
136}
137
138Kernel::Kernel(std::string name, const cl::Program &program)
139 : _name(std::move(name)),
140 _kernel(cl::Kernel(program, _name.c_str()))
141{
142}
143
144const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map =
145{
146 { "absdiff", "absdiff.cl" },
147 { "accumulate", "accumulate.cl" },
148 { "accumulate_squared", "accumulate.cl" },
149 { "accumulate_weighted", "accumulate.cl" },
150 { "activation_layer", "activation_layer.cl" },
Michel Iwaniec00633802017-10-12 14:14:15 +0100151 { "activation_layer_qa8", "activation_layer_qa8.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 { "arithmetic_add", "arithmetic_op.cl" },
153 { "arithmetic_sub", "arithmetic_op.cl" },
154 { "bitwise_or", "bitwise_op.cl" },
155 { "bitwise_and", "bitwise_op.cl" },
156 { "bitwise_xor", "bitwise_op.cl" },
157 { "bitwise_not", "bitwise_op.cl" },
158 { "channel_combine_NV", "channel_combine.cl" },
159 { "channel_combine_RGB888", "channel_combine.cl" },
160 { "channel_combine_RGBA8888", "channel_combine.cl" },
161 { "channel_combine_UYVY422", "channel_combine.cl" },
162 { "channel_combine_YUYV422", "channel_combine.cl" },
163 { "channel_extract_NV12", "channel_extract.cl" },
164 { "channel_extract_NV21", "channel_extract.cl" },
165 { "channel_extract_RGB888", "channel_extract.cl" },
166 { "channel_extract_RGBA8888", "channel_extract.cl" },
167 { "channel_extract_UYVY422", "channel_extract.cl" },
168 { "channel_extract_YUYV422", "channel_extract.cl" },
169 { "combine_gradients_L1", "canny.cl" },
170 { "combine_gradients_L2", "canny.cl" },
171 { "concatenate_depth", "concatenate.cl" },
172 { "convolution_rectangle", "convolution_rectangle.cl" },
173 { "col2im", "convolution_layer.cl" },
174 { "convolution3x3_static", "convolution3x3.cl" },
175 { "convolution5x5_static", "convolution5x5.cl" },
176 { "convolution7x7_static", "convolution7x7.cl" },
177 { "convolution9x9_static", "convolution9x9.cl" },
178 { "convolution_separable1x5_static", "convolution5x5.cl" },
179 { "convolution_separable5x1_static", "convolution5x5.cl" },
180 { "convolution_separable1x7_static", "convolution7x7.cl" },
181 { "convolution_separable7x1_static", "convolution7x7.cl" },
182 { "convolution_separable1x9_static", "convolution9x9.cl" },
183 { "convolution_separable9x1_static", "convolution9x9.cl" },
184 { "convert_depth_down", "depth_convert.cl" },
185 { "convert_depth_up", "depth_convert.cl" },
186 { "copy_plane", "channel_extract.cl" },
187 { "copy_planes_3p", "channel_combine.cl" },
188 { "copy_to_keypoint", "fast_corners.cl" },
Giorgio Arena93a690e2017-08-01 16:09:33 +0100189 { "depthwise_convolution_3x3", "depthwise_convolution.cl" },
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700190 { "depthwise_convolution_3x3_quantized", "depthwise_convolution_quantized.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100191 { "depthwise_im2col", "depthwise_convolution.cl" },
192 { "depthwise_vector_to_tensor", "depthwise_convolution.cl" },
193 { "depthwise_weights_reshape", "depthwise_convolution.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100194 { "dequantization_layer", "dequantization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 { "derivative", "derivative.cl" },
196 { "dilate", "dilate.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100197 { "direct_convolution1x1", "direct_convolution1x1.cl" },
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100198 { "direct_convolution1x1_f32_bifrost", "direct_convolution1x1.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100199 { "direct_convolution3x3", "direct_convolution3x3.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100200 { "direct_convolution3x3_f32_bifrost", "direct_convolution3x3.cl" },
steniu01db006682017-08-09 16:26:22 +0100201 { "direct_convolution5x5", "direct_convolution5x5.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100202 { "direct_convolution5x5_f32_bifrost", "direct_convolution5x5.cl" },
Chunosovd621bca2017-11-03 17:33:15 +0700203 { "direct_convolution_1x1_3x3_5x5_quantized", "direct_convolution_1x1_3x3_5x5_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 { "erode", "erode.cl" },
205 { "fast_corners", "fast_corners.cl" },
206 { "fill_image_borders_constant", "fill_border.cl" },
207 { "fill_image_borders_replicate", "fill_border.cl" },
208 { "finalize", "optical_flow_pyramid_lk.cl" },
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100209 { "floor_layer", "floor.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
211 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100212 { "gemm_accumulate_biases", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 { "gemm_interleave4x4_8bit", "gemm.cl" },
214 { "gemm_interleave4x4_16bit", "gemm.cl" },
215 { "gemm_interleave4x4_32bit", "gemm.cl" },
216 { "gemm_ma_f16", "gemm.cl" },
217 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100218 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100219 { "gemm_ma_qs16", "gemm.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100220 { "gemm_mv", "gemv.cl" },
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100221 { "gemm_mm_interleaved_transposed_u8", "gemm.cl" },
222 { "gemm_mm_interleaved_transposed_f16", "gemm.cl" },
223 { "gemm_mm_interleaved_transposed_f32_midgard", "gemm.cl" },
224 { "gemm_mm_interleaved_transposed_f32_bifrost", "gemm.cl" },
225 { "gemm_mm_interleaved_transposed_qs8", "gemm.cl" },
226 { "gemm_mm_interleaved_transposed_qs16", "gemm.cl" },
227 { "gemm_mm_floating_point", "gemm.cl" },
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000228 { "gemm_mm_floating_point_f32_bifrost", "gemm.cl" },
229 { "gemm_mm_floating_point_f32_bifrost_1000", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100230 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100231 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100233 { "gemm_transpose1x16", "gemm.cl" },
234 { "gemm_transpose1x8", "gemm.cl" },
235 { "gemm_transpose1x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236 { "harris_score_3x3", "harris_corners.cl" },
237 { "harris_score_5x5", "harris_corners.cl" },
238 { "harris_score_7x7", "harris_corners.cl" },
239 { "hist_border_kernel", "histogram.cl" },
240 { "hist_border_kernel_fixed", "histogram.cl" },
241 { "hist_local_kernel", "histogram.cl" },
242 { "hist_local_kernel_fixed", "histogram.cl" },
243 { "hog_block_normalization", "hog.cl" },
244 { "hog_detector", "hog.cl" },
245 { "hog_orientation_binning", "hog.cl" },
246 { "hysteresis", "canny.cl" },
247 { "im2col_generic", "convolution_layer.cl" },
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100248 { "im2col_kernel3x3_padx0_pady0", "convolution_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 { "im2col_reduced", "convolution_layer.cl" },
250 { "init_level", "optical_flow_pyramid_lk.cl" },
251 { "init_level_max", "optical_flow_pyramid_lk.cl" },
252 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
253 { "integral_horizontal", "integral_image.cl" },
254 { "integral_vertical", "integral_image.cl" },
255 { "IYUV_to_NV12_bt709", "color_convert.cl" },
256 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
257 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
258 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100259 { "l2_normalize", "l2_normalize.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100260 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
261 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
262 { "magnitude_phase", "magnitude_phase.cl" },
263 { "mean_stddev_accumulate", "mean_stddev.cl" },
264 { "minmax", "minmaxloc.cl" },
265 { "minmax_border", "minmaxloc.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100266 { "minmax_layer", "minmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267 { "minmaxloc", "minmaxloc.cl" },
268 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
269 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
270 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
271 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
272 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
273 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
274 { "non_max_suppression", "nonmax.cl" },
275 { "normalization_layer_cross_map", "normalization_layer.cl" },
276 { "normalization_layer_in_map_1D", "normalization_layer.cl" },
277 { "batchnormalization_layer", "batchnormalization_layer.cl" },
278 { "NV12_to_IYUV_bt709", "color_convert.cl" },
279 { "NV12_to_RGB888_bt709", "color_convert.cl" },
280 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
281 { "NV12_to_YUV444_bt709", "color_convert.cl" },
282 { "NV21_to_IYUV_bt709", "color_convert.cl" },
283 { "NV21_to_RGB888_bt709", "color_convert.cl" },
284 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
285 { "NV21_to_YUV444_bt709", "color_convert.cl" },
286 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
287 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
288 { "pooling_layer_2", "pooling_layer.cl" },
289 { "pooling_layer_3", "pooling_layer.cl" },
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000290 { "pooling_layer_optimized_3", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100291 { "pooling_layer_7", "pooling_layer.cl" },
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100292 { "pooling_layer_N", "pooling_layer.cl" },
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000293 { "pooling_layer_N_quantized", "pooling_layer_quantized.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100294 { "quantization_layer", "quantization_layer.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100295 { "reduction_operation", "reduction_operation.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100296 { "remap_nearest_neighbour", "remap.cl" },
297 { "remap_bilinear", "remap.cl" },
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100298 { "reshape_layer", "reshape_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299 { "reshape_to_columns", "convolution_layer.cl" },
300 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
301 { "RGB888_to_NV12_bt709", "color_convert.cl" },
302 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
303 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
304 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
305 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
306 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
307 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
SiCong Li3e363692017-07-04 15:02:10 +0100308 { "roi_pooling_layer", "roi_pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 { "scale_nearest_neighbour", "scale.cl" },
310 { "scale_bilinear", "scale.cl" },
311 { "scharr3x3", "scharr_filter.cl" },
312 { "sobel3x3", "sobel_filter.cl" },
313 { "sobel_separable5x1", "sobel_filter.cl" },
314 { "sobel_separable1x5", "sobel_filter.cl" },
315 { "sobel_separable7x1", "sobel_filter.cl" },
316 { "sobel_separable1x7", "sobel_filter.cl" },
317 { "softmax_layer_max", "softmax_layer.cl" },
Chunosovf450caa2017-11-08 16:09:35 +0700318 { "softmax_layer_max_quantized", "softmax_layer_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
Chunosovf450caa2017-11-08 16:09:35 +0700320 { "softmax_layer_shift_exp_sum_quantized", "softmax_layer_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321 { "softmax_layer_norm", "softmax_layer.cl" },
Chunosovf450caa2017-11-08 16:09:35 +0700322 { "softmax_layer_norm_quantized", "softmax_layer_quantized.cl" },
Chunosovd6afedc2017-11-06 22:09:45 +0700323 { "softmax_layer_max_shift_exp_sum_serial", "softmax_layer.cl" },
324 { "softmax_layer_max_shift_exp_sum_parallel", "softmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100325 { "suppress_non_maximum", "canny.cl" },
326 { "tablelookup_U8", "tablelookup.cl" },
327 { "tablelookup_S16", "tablelookup.cl" },
328 { "threshold_binary", "threshold.cl" },
329 { "threshold_range", "threshold.cl" },
330 { "transpose", "transpose.cl" },
331 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
332 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
333 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
334 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
335 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
336 { "warp_affine_bilinear", "warp_affine.cl" },
337 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
338 { "warp_perspective_bilinear", "warp_perspective.cl" },
339 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
340 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
341 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
342 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
343};
344
345const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
346{
347#ifdef EMBEDDED_KERNELS
348 {
349 "absdiff.cl",
350#include "./cl_kernels/absdiff.clembed"
351 },
352 {
353 "accumulate.cl",
354#include "./cl_kernels/accumulate.clembed"
355 },
356 {
357 "activation_layer.cl",
358#include "./cl_kernels/activation_layer.clembed"
359 },
360 {
Michel Iwaniec00633802017-10-12 14:14:15 +0100361 "activation_layer_qa8.cl",
362#include "./cl_kernels/activation_layer_qa8.clembed"
363 },
364 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365 "arithmetic_op.cl",
366#include "./cl_kernels/arithmetic_op.clembed"
367 },
368 {
369 "bitwise_op.cl",
370#include "./cl_kernels/bitwise_op.clembed"
371 },
372 {
373 "canny.cl",
374#include "./cl_kernels/canny.clembed"
375 },
376 {
377 "channel_combine.cl",
378#include "./cl_kernels/channel_combine.clembed"
379 },
380 {
381 "channel_extract.cl",
382#include "./cl_kernels/channel_extract.clembed"
383 },
384 {
385 "concatenate.cl",
386#include "./cl_kernels/concatenate.clembed"
387 },
388 {
389 "color_convert.cl",
390#include "./cl_kernels/color_convert.clembed"
391 },
392 {
393 "convolution3x3.cl",
394#include "./cl_kernels/convolution3x3.clembed"
395 },
396 {
397 "convolution5x5.cl",
398#include "./cl_kernels/convolution5x5.clembed"
399 },
400 {
401 "convolution7x7.cl",
402#include "./cl_kernels/convolution7x7.clembed"
403 },
404 {
405 "convolution9x9.cl",
406#include "./cl_kernels/convolution9x9.clembed"
407 },
408 {
409 "convolution_layer.cl",
410#include "./cl_kernels/convolution_layer.clembed"
411 },
412 {
413 "convolution_rectangle.cl",
414#include "./cl_kernels/convolution_rectangle.clembed"
415 },
416 {
417 "depth_convert.cl",
418#include "./cl_kernels/depth_convert.clembed"
419 },
420 {
Giorgio Arena93a690e2017-08-01 16:09:33 +0100421 "depthwise_convolution.cl",
422#include "./cl_kernels/depthwise_convolution.clembed"
423 },
424 {
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700425 "depthwise_convolution_quantized.cl",
426#include "./cl_kernels/depthwise_convolution_quantized.clembed"
427 },
428 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100429 "dequantization_layer.cl",
430#include "./cl_kernels/dequantization_layer.clembed"
431 },
432 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433 "derivative.cl",
434#include "./cl_kernels/derivative.clembed"
435 },
436 {
437 "dilate.cl",
438#include "./cl_kernels/dilate.clembed"
439 },
440 {
SiCong Lic51b72f2017-07-28 14:46:20 +0100441 "direct_convolution1x1.cl",
442#include "./cl_kernels/direct_convolution1x1.clembed"
443 },
444 {
445 "direct_convolution3x3.cl",
446#include "./cl_kernels/direct_convolution3x3.clembed"
steniu0127b386c2017-07-18 17:37:43 +0100447 },
448 {
steniu01db006682017-08-09 16:26:22 +0100449 "direct_convolution5x5.cl",
450#include "./cl_kernels/direct_convolution5x5.clembed"
451 },
452 {
Chunosovd621bca2017-11-03 17:33:15 +0700453 "direct_convolution_1x1_3x3_5x5_quantized.cl",
454#include "./cl_kernels/direct_convolution_1x1_3x3_5x5_quantized.clembed"
455 },
456 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100457 "erode.cl",
458#include "./cl_kernels/erode.clembed"
459 },
460 {
461 "fast_corners.cl",
462#include "./cl_kernels/fast_corners.clembed"
463 },
464 {
465 "fill_border.cl",
466#include "./cl_kernels/fill_border.clembed"
467 },
468 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100469 "fixed_point.h",
470#include "./cl_kernels/fixed_point.hembed"
471 },
472 {
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100473 "floor.cl",
474#include "./cl_kernels/floor.clembed"
475 },
476 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100477 "gaussian_pyramid.cl",
478#include "./cl_kernels/gaussian_pyramid.clembed"
479 },
480 {
481 "gemm.cl",
482#include "./cl_kernels/gemm.clembed"
483 },
484 {
Giorgio Arena9fe41442017-08-23 16:36:24 +0100485 "gemv.cl",
486#include "./cl_kernels/gemv.clembed"
487 },
488 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100489 "harris_corners.cl",
490#include "./cl_kernels/harris_corners.clembed"
491 },
492 {
493 "helpers.h",
494#include "./cl_kernels/helpers.hembed"
495 },
496 {
Chunosovd621bca2017-11-03 17:33:15 +0700497 "helpers_asymm.h",
498#include "./cl_kernels/helpers_asymm.hembed"
499 },
500 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501 "histogram.cl",
502#include "./cl_kernels/histogram.clembed"
503 },
504 {
505 "hog.cl",
506#include "./cl_kernels/hog.clembed"
507 },
508 {
509 "integral_image.cl",
510#include "./cl_kernels/integral_image.clembed"
511 },
512 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100513 "l2_normalize.cl",
514#include "./cl_kernels/l2_normalize.clembed"
515 },
516 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100517 "magnitude_phase.cl",
518#include "./cl_kernels/magnitude_phase.clembed"
519 },
520 {
521 "mean_stddev.cl",
522#include "./cl_kernels/mean_stddev.clembed"
523 },
524 {
525 "minmaxloc.cl",
526#include "./cl_kernels/minmaxloc.clembed"
527 },
528 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100529 "minmax_layer.cl",
530#include "./cl_kernels/minmax_layer.clembed"
531 },
532 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100533 "non_linear_filter3x3.cl",
534#include "./cl_kernels/non_linear_filter3x3.clembed"
535 },
536 {
537 "non_linear_filter5x5.cl",
538#include "./cl_kernels/non_linear_filter5x5.clembed"
539 },
540 {
541 "non_linear_filter_helpers.h",
542#include "./cl_kernels/non_linear_filter_helpers.hembed"
543 },
544 {
545 "nonmax.cl",
546#include "./cl_kernels/nonmax.clembed"
547 },
548 {
549 "normalization_layer.cl",
550#include "./cl_kernels/normalization_layer.clembed"
551 },
552 {
553 "batchnormalization_layer.cl",
554#include "./cl_kernels/batchnormalization_layer.clembed"
555 },
556 {
557 "optical_flow_pyramid_lk.cl",
558#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
559 },
560 {
561 "pixelwise_mul_float.cl",
562#include "./cl_kernels/pixelwise_mul_float.clembed"
563 },
564 {
565 "pixelwise_mul_int.cl",
566#include "./cl_kernels/pixelwise_mul_int.clembed"
567 },
568 {
569 "pooling_layer.cl",
570#include "./cl_kernels/pooling_layer.clembed"
571 },
572 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000573 "pooling_layer_quantized.cl",
574#include "./cl_kernels/pooling_layer_quantized.clembed"
575 },
576 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100577 "quantization_layer.cl",
578#include "./cl_kernels/quantization_layer.clembed"
579 },
580 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100581 "reduction_operation.cl",
582#include "./cl_kernels/reduction_operation.clembed"
583 },
584 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100585 "remap.cl",
586#include "./cl_kernels/remap.clembed"
587 },
588 {
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100589 "reshape_layer.cl",
590#include "./cl_kernels/reshape_layer.clembed"
591 },
592 {
SiCong Li3e363692017-07-04 15:02:10 +0100593 "roi_pooling_layer.cl",
594#include "./cl_kernels/roi_pooling_layer.clembed"
595 },
596 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100597 "scale.cl",
598#include "./cl_kernels/scale.clembed"
599 },
600 {
601 "scharr_filter.cl",
602#include "./cl_kernels/scharr_filter.clembed"
603 },
604 {
605 "sobel_filter.cl",
606#include "./cl_kernels/sobel_filter.clembed"
607 },
608 {
609 "softmax_layer.cl",
610#include "./cl_kernels/softmax_layer.clembed"
611 },
612 {
Chunosovf450caa2017-11-08 16:09:35 +0700613 "softmax_layer_quantized.cl",
614#include "./cl_kernels/softmax_layer_quantized.clembed"
615 },
616 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100617 "tablelookup.cl",
618#include "./cl_kernels/tablelookup.clembed"
619 },
620 {
621 "threshold.cl",
622#include "./cl_kernels/threshold.clembed"
623 },
624 {
625 "transpose.cl",
626#include "./cl_kernels/transpose.clembed"
627 },
628 {
629 "types.h",
630#include "./cl_kernels/types.hembed"
631 },
632 {
633 "warp_affine.cl",
634#include "./cl_kernels/warp_affine.clembed"
635 },
636 {
637 "warp_helpers.h",
638#include "./cl_kernels/warp_helpers.hembed"
639 },
640 {
641 "warp_perspective.cl",
642#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100643 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100644#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100645};
646
647CLKernelLibrary::CLKernelLibrary()
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100648 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100649{
650}
651
652CLKernelLibrary &CLKernelLibrary::get()
653{
654 static CLKernelLibrary _kernel_library;
655 return _kernel_library;
656}
657
658Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
659{
660 // Find which program contains the kernel
661 auto kernel_program_it = _kernel_program_map.find(kernel_name);
662
663 if(_kernel_program_map.end() == kernel_program_it)
664 {
665 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
666 }
667
steniu0134702472017-07-11 09:22:58 +0100668 std::string concat_str;
669
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100670 if(fp16_support(_device))
671 {
672 concat_str += " -DARM_COMPUTE_OPENCL_FP16_ENABLED=1 ";
673 }
674
steniu0134702472017-07-11 09:22:58 +0100675 if(non_uniform_workgroup_support(_device))
676 {
677 concat_str += " -cl-arm-non-uniform-work-group-size ";
678 }
679 else if(get_cl_version(_device) == CLVersion::CL20)
680 {
681 concat_str += " -cl-std=CL2.0 ";
682 }
683 else
684 {
685 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
686 }
687
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100688 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100689 const std::string program_name = kernel_program_it->second;
690 const std::string build_options = stringify_set(build_options_set) + concat_str;
691
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100692 const std::string built_program_name = program_name + "_" + build_options;
693 auto built_program_it = _built_programs_map.find(built_program_name);
694
695 cl::Program cl_program;
696
697 if(_built_programs_map.end() != built_program_it)
698 {
699 // If program has been built, retrieve to create kernel from it
700 cl_program = built_program_it->second;
701 }
702 else
703 {
704 // Get program
705 Program program = load_program(program_name);
706
707 // Build program
708 cl_program = program.build(build_options);
709
710 // Add built program to internal map
711 _built_programs_map.emplace(built_program_name, cl_program);
712 }
713
714 // Create and return kernel
715 return Kernel(kernel_name, cl_program);
716}
717
718const Program &CLKernelLibrary::load_program(const std::string &program_name) const
719{
720 const auto program_it = _programs_map.find(program_name);
721
722 if(program_it != _programs_map.end())
723 {
724 return program_it->second;
725 }
726
727 Program program;
728
729#ifdef EMBEDDED_KERNELS
730 const auto program_source_it = _program_source_map.find(program_name);
731
732 if(_program_source_map.end() == program_source_it)
733 {
734 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
735 }
736
737 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100738#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100739 // Check for binary
740 std::string source_name = _kernel_path + program_name;
741 std::string binary_name = source_name + "bin";
742
743 if(std::ifstream(binary_name).is_open())
744 {
745 const std::string program_binary = read_file(binary_name, true);
746 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
747 }
748 else if(std::ifstream(source_name).is_open())
749 {
750 program = Program(_context, program_name, read_file(source_name, false));
751 }
752 else
753 {
754 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
755 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100756#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100757
758 // Insert program to program map
759 const auto new_program = _programs_map.emplace(program_name, std::move(program));
760
761 return new_program.first->second;
762}
763
764std::string CLKernelLibrary::stringify_set(const StringSet &s) const
765{
steniu0134702472017-07-11 09:22:58 +0100766 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100767
768#ifndef EMBEDDED_KERNELS
769 concat_set += "-I" + _kernel_path + " ";
770#endif /* EMBEDDED_KERNELS */
771
772 // Concatenate set
773 for(const auto &el : s)
774 {
775 concat_set += " " + el;
776 }
777
778 return concat_set;
779}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100780
781std::string CLKernelLibrary::get_program_source(const std::string &program_name)
782{
783 const auto program_source_it = _program_source_map.find(program_name);
784
785 if(program_source_it == _program_source_map.end())
786 {
787 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
788 }
789
790 return program_source_it->second;
791}
steniu015f910722017-08-23 10:15:22 +0100792
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100793size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100794{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100795 size_t result;
steniu015f910722017-08-23 10:15:22 +0100796
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100797 size_t err = kernel.getWorkGroupInfo(_device, CL_KERNEL_WORK_GROUP_SIZE, &result);
798 ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
799 ARM_COMPUTE_UNUSED(err);
800
801 return result;
steniu015f910722017-08-23 10:15:22 +0100802}
803
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100804cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100805{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100806 return cl::NDRange(128u, 1);
steniu015f910722017-08-23 10:15:22 +0100807}