blob: 2589bd12b5f9258256efd880dcf46aa1f1970e21 [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" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 { "derivative", "derivative.cl" },
149 { "dilate", "dilate.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100150 { "direct_convolution1x1", "direct_convolution1x1.cl" },
151 { "direct_convolution3x3", "direct_convolution3x3.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100152 { "direct_convolution3x3_f32_bifrost", "direct_convolution3x3.cl" },
steniu01db006682017-08-09 16:26:22 +0100153 { "direct_convolution5x5", "direct_convolution5x5.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100154 { "direct_convolution5x5_f32_bifrost", "direct_convolution5x5.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 { "erode", "erode.cl" },
156 { "fast_corners", "fast_corners.cl" },
157 { "fill_image_borders_constant", "fill_border.cl" },
158 { "fill_image_borders_replicate", "fill_border.cl" },
159 { "finalize", "optical_flow_pyramid_lk.cl" },
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100160 { "floor_layer", "floor.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
162 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100163 { "gemm_accumulate_biases", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 { "gemm_interleave4x4_8bit", "gemm.cl" },
165 { "gemm_interleave4x4_16bit", "gemm.cl" },
166 { "gemm_interleave4x4_32bit", "gemm.cl" },
167 { "gemm_ma_f16", "gemm.cl" },
168 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100169 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100170 { "gemm_ma_qs16", "gemm.cl" },
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100171 { "gemm_mm_interleaved_transposed_u8", "gemm.cl" },
172 { "gemm_mm_interleaved_transposed_f16", "gemm.cl" },
173 { "gemm_mm_interleaved_transposed_f32_midgard", "gemm.cl" },
174 { "gemm_mm_interleaved_transposed_f32_bifrost", "gemm.cl" },
175 { "gemm_mm_interleaved_transposed_qs8", "gemm.cl" },
176 { "gemm_mm_interleaved_transposed_qs16", "gemm.cl" },
177 { "gemm_mm_floating_point", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100178 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100179 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100181 { "gemm_transpose1x16", "gemm.cl" },
182 { "gemm_transpose1x8", "gemm.cl" },
183 { "gemm_transpose1x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184 { "harris_score_3x3", "harris_corners.cl" },
185 { "harris_score_5x5", "harris_corners.cl" },
186 { "harris_score_7x7", "harris_corners.cl" },
187 { "hist_border_kernel", "histogram.cl" },
188 { "hist_border_kernel_fixed", "histogram.cl" },
189 { "hist_local_kernel", "histogram.cl" },
190 { "hist_local_kernel_fixed", "histogram.cl" },
191 { "hog_block_normalization", "hog.cl" },
192 { "hog_detector", "hog.cl" },
193 { "hog_orientation_binning", "hog.cl" },
194 { "hysteresis", "canny.cl" },
195 { "im2col_generic", "convolution_layer.cl" },
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100196 { "im2col_kernel3x3_padx0_pady0", "convolution_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 { "im2col_reduced", "convolution_layer.cl" },
198 { "init_level", "optical_flow_pyramid_lk.cl" },
199 { "init_level_max", "optical_flow_pyramid_lk.cl" },
200 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
201 { "integral_horizontal", "integral_image.cl" },
202 { "integral_vertical", "integral_image.cl" },
203 { "IYUV_to_NV12_bt709", "color_convert.cl" },
204 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
205 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
206 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
207 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
208 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
209 { "magnitude_phase", "magnitude_phase.cl" },
210 { "mean_stddev_accumulate", "mean_stddev.cl" },
211 { "minmax", "minmaxloc.cl" },
212 { "minmax_border", "minmaxloc.cl" },
213 { "minmaxloc", "minmaxloc.cl" },
214 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
215 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
216 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
217 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
218 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
219 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
220 { "non_max_suppression", "nonmax.cl" },
221 { "normalization_layer_cross_map", "normalization_layer.cl" },
222 { "normalization_layer_in_map_1D", "normalization_layer.cl" },
223 { "batchnormalization_layer", "batchnormalization_layer.cl" },
224 { "NV12_to_IYUV_bt709", "color_convert.cl" },
225 { "NV12_to_RGB888_bt709", "color_convert.cl" },
226 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
227 { "NV12_to_YUV444_bt709", "color_convert.cl" },
228 { "NV21_to_IYUV_bt709", "color_convert.cl" },
229 { "NV21_to_RGB888_bt709", "color_convert.cl" },
230 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
231 { "NV21_to_YUV444_bt709", "color_convert.cl" },
232 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
233 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
234 { "pooling_layer_2", "pooling_layer.cl" },
235 { "pooling_layer_3", "pooling_layer.cl" },
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100236 { "pooling_layer_3_optimized", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100237 { "pooling_layer_7", "pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 { "remap_nearest_neighbour", "remap.cl" },
239 { "remap_bilinear", "remap.cl" },
240 { "reshape_to_columns", "convolution_layer.cl" },
241 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
242 { "RGB888_to_NV12_bt709", "color_convert.cl" },
243 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
244 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
245 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
246 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
247 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
248 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
249 { "scale_nearest_neighbour", "scale.cl" },
250 { "scale_bilinear", "scale.cl" },
251 { "scharr3x3", "scharr_filter.cl" },
252 { "sobel3x3", "sobel_filter.cl" },
253 { "sobel_separable5x1", "sobel_filter.cl" },
254 { "sobel_separable1x5", "sobel_filter.cl" },
255 { "sobel_separable7x1", "sobel_filter.cl" },
256 { "sobel_separable1x7", "sobel_filter.cl" },
257 { "softmax_layer_max", "softmax_layer.cl" },
258 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
259 { "softmax_layer_norm", "softmax_layer.cl" },
260 { "suppress_non_maximum", "canny.cl" },
261 { "tablelookup_U8", "tablelookup.cl" },
262 { "tablelookup_S16", "tablelookup.cl" },
263 { "threshold_binary", "threshold.cl" },
264 { "threshold_range", "threshold.cl" },
265 { "transpose", "transpose.cl" },
266 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
267 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
268 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
269 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
270 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
271 { "warp_affine_bilinear", "warp_affine.cl" },
272 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
273 { "warp_perspective_bilinear", "warp_perspective.cl" },
274 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
275 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
276 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
277 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
278};
279
280const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
281{
282#ifdef EMBEDDED_KERNELS
283 {
284 "absdiff.cl",
285#include "./cl_kernels/absdiff.clembed"
286 },
287 {
288 "accumulate.cl",
289#include "./cl_kernels/accumulate.clembed"
290 },
291 {
292 "activation_layer.cl",
293#include "./cl_kernels/activation_layer.clembed"
294 },
295 {
296 "arithmetic_op.cl",
297#include "./cl_kernels/arithmetic_op.clembed"
298 },
299 {
300 "bitwise_op.cl",
301#include "./cl_kernels/bitwise_op.clembed"
302 },
303 {
304 "canny.cl",
305#include "./cl_kernels/canny.clembed"
306 },
307 {
308 "channel_combine.cl",
309#include "./cl_kernels/channel_combine.clembed"
310 },
311 {
312 "channel_extract.cl",
313#include "./cl_kernels/channel_extract.clembed"
314 },
315 {
316 "concatenate.cl",
317#include "./cl_kernels/concatenate.clembed"
318 },
319 {
320 "color_convert.cl",
321#include "./cl_kernels/color_convert.clembed"
322 },
323 {
324 "convolution3x3.cl",
325#include "./cl_kernels/convolution3x3.clembed"
326 },
327 {
328 "convolution5x5.cl",
329#include "./cl_kernels/convolution5x5.clembed"
330 },
331 {
332 "convolution7x7.cl",
333#include "./cl_kernels/convolution7x7.clembed"
334 },
335 {
336 "convolution9x9.cl",
337#include "./cl_kernels/convolution9x9.clembed"
338 },
339 {
340 "convolution_layer.cl",
341#include "./cl_kernels/convolution_layer.clembed"
342 },
343 {
344 "convolution_rectangle.cl",
345#include "./cl_kernels/convolution_rectangle.clembed"
346 },
347 {
348 "depth_convert.cl",
349#include "./cl_kernels/depth_convert.clembed"
350 },
351 {
Giorgio Arena93a690e2017-08-01 16:09:33 +0100352 "depthwise_convolution.cl",
353#include "./cl_kernels/depthwise_convolution.clembed"
354 },
355 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 "derivative.cl",
357#include "./cl_kernels/derivative.clembed"
358 },
359 {
360 "dilate.cl",
361#include "./cl_kernels/dilate.clembed"
362 },
363 {
SiCong Lic51b72f2017-07-28 14:46:20 +0100364 "direct_convolution1x1.cl",
365#include "./cl_kernels/direct_convolution1x1.clembed"
366 },
367 {
368 "direct_convolution3x3.cl",
369#include "./cl_kernels/direct_convolution3x3.clembed"
steniu0127b386c2017-07-18 17:37:43 +0100370 },
371 {
steniu01db006682017-08-09 16:26:22 +0100372 "direct_convolution5x5.cl",
373#include "./cl_kernels/direct_convolution5x5.clembed"
374 },
375 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100376 "erode.cl",
377#include "./cl_kernels/erode.clembed"
378 },
379 {
380 "fast_corners.cl",
381#include "./cl_kernels/fast_corners.clembed"
382 },
383 {
384 "fill_border.cl",
385#include "./cl_kernels/fill_border.clembed"
386 },
387 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100388 "fixed_point.h",
389#include "./cl_kernels/fixed_point.hembed"
390 },
391 {
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100392 "floor.cl",
393#include "./cl_kernels/floor.clembed"
394 },
395 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100396 "gaussian_pyramid.cl",
397#include "./cl_kernels/gaussian_pyramid.clembed"
398 },
399 {
400 "gemm.cl",
401#include "./cl_kernels/gemm.clembed"
402 },
403 {
404 "harris_corners.cl",
405#include "./cl_kernels/harris_corners.clembed"
406 },
407 {
408 "helpers.h",
409#include "./cl_kernels/helpers.hembed"
410 },
411 {
412 "histogram.cl",
413#include "./cl_kernels/histogram.clembed"
414 },
415 {
416 "hog.cl",
417#include "./cl_kernels/hog.clembed"
418 },
419 {
420 "integral_image.cl",
421#include "./cl_kernels/integral_image.clembed"
422 },
423 {
424 "magnitude_phase.cl",
425#include "./cl_kernels/magnitude_phase.clembed"
426 },
427 {
428 "mean_stddev.cl",
429#include "./cl_kernels/mean_stddev.clembed"
430 },
431 {
432 "minmaxloc.cl",
433#include "./cl_kernels/minmaxloc.clembed"
434 },
435 {
436 "non_linear_filter3x3.cl",
437#include "./cl_kernels/non_linear_filter3x3.clembed"
438 },
439 {
440 "non_linear_filter5x5.cl",
441#include "./cl_kernels/non_linear_filter5x5.clembed"
442 },
443 {
444 "non_linear_filter_helpers.h",
445#include "./cl_kernels/non_linear_filter_helpers.hembed"
446 },
447 {
448 "nonmax.cl",
449#include "./cl_kernels/nonmax.clembed"
450 },
451 {
452 "normalization_layer.cl",
453#include "./cl_kernels/normalization_layer.clembed"
454 },
455 {
456 "batchnormalization_layer.cl",
457#include "./cl_kernels/batchnormalization_layer.clembed"
458 },
459 {
460 "optical_flow_pyramid_lk.cl",
461#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
462 },
463 {
464 "pixelwise_mul_float.cl",
465#include "./cl_kernels/pixelwise_mul_float.clembed"
466 },
467 {
468 "pixelwise_mul_int.cl",
469#include "./cl_kernels/pixelwise_mul_int.clembed"
470 },
471 {
472 "pooling_layer.cl",
473#include "./cl_kernels/pooling_layer.clembed"
474 },
475 {
476 "remap.cl",
477#include "./cl_kernels/remap.clembed"
478 },
479 {
480 "scale.cl",
481#include "./cl_kernels/scale.clembed"
482 },
483 {
484 "scharr_filter.cl",
485#include "./cl_kernels/scharr_filter.clembed"
486 },
487 {
488 "sobel_filter.cl",
489#include "./cl_kernels/sobel_filter.clembed"
490 },
491 {
492 "softmax_layer.cl",
493#include "./cl_kernels/softmax_layer.clembed"
494 },
495 {
496 "tablelookup.cl",
497#include "./cl_kernels/tablelookup.clembed"
498 },
499 {
500 "threshold.cl",
501#include "./cl_kernels/threshold.clembed"
502 },
503 {
504 "transpose.cl",
505#include "./cl_kernels/transpose.clembed"
506 },
507 {
508 "types.h",
509#include "./cl_kernels/types.hembed"
510 },
511 {
512 "warp_affine.cl",
513#include "./cl_kernels/warp_affine.clembed"
514 },
515 {
516 "warp_helpers.h",
517#include "./cl_kernels/warp_helpers.hembed"
518 },
519 {
520 "warp_perspective.cl",
521#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100522 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100523#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100524};
525
526CLKernelLibrary::CLKernelLibrary()
steniu015f910722017-08-23 10:15:22 +0100527 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map(), _max_workgroup_size(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100528{
529}
530
531CLKernelLibrary &CLKernelLibrary::get()
532{
533 static CLKernelLibrary _kernel_library;
534 return _kernel_library;
535}
536
537Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
538{
539 // Find which program contains the kernel
540 auto kernel_program_it = _kernel_program_map.find(kernel_name);
541
542 if(_kernel_program_map.end() == kernel_program_it)
543 {
544 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
545 }
546
steniu0134702472017-07-11 09:22:58 +0100547 std::string concat_str;
548
549 if(non_uniform_workgroup_support(_device))
550 {
551 concat_str += " -cl-arm-non-uniform-work-group-size ";
552 }
553 else if(get_cl_version(_device) == CLVersion::CL20)
554 {
555 concat_str += " -cl-std=CL2.0 ";
556 }
557 else
558 {
559 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
560 }
561
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100562 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100563 const std::string program_name = kernel_program_it->second;
564 const std::string build_options = stringify_set(build_options_set) + concat_str;
565
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100566 const std::string built_program_name = program_name + "_" + build_options;
567 auto built_program_it = _built_programs_map.find(built_program_name);
568
569 cl::Program cl_program;
570
571 if(_built_programs_map.end() != built_program_it)
572 {
573 // If program has been built, retrieve to create kernel from it
574 cl_program = built_program_it->second;
575 }
576 else
577 {
578 // Get program
579 Program program = load_program(program_name);
580
581 // Build program
582 cl_program = program.build(build_options);
583
584 // Add built program to internal map
585 _built_programs_map.emplace(built_program_name, cl_program);
586 }
587
588 // Create and return kernel
589 return Kernel(kernel_name, cl_program);
590}
591
592const Program &CLKernelLibrary::load_program(const std::string &program_name) const
593{
594 const auto program_it = _programs_map.find(program_name);
595
596 if(program_it != _programs_map.end())
597 {
598 return program_it->second;
599 }
600
601 Program program;
602
603#ifdef EMBEDDED_KERNELS
604 const auto program_source_it = _program_source_map.find(program_name);
605
606 if(_program_source_map.end() == program_source_it)
607 {
608 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
609 }
610
611 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100612#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100613 // Check for binary
614 std::string source_name = _kernel_path + program_name;
615 std::string binary_name = source_name + "bin";
616
617 if(std::ifstream(binary_name).is_open())
618 {
619 const std::string program_binary = read_file(binary_name, true);
620 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
621 }
622 else if(std::ifstream(source_name).is_open())
623 {
624 program = Program(_context, program_name, read_file(source_name, false));
625 }
626 else
627 {
628 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
629 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100630#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100631
632 // Insert program to program map
633 const auto new_program = _programs_map.emplace(program_name, std::move(program));
634
635 return new_program.first->second;
636}
637
638std::string CLKernelLibrary::stringify_set(const StringSet &s) const
639{
steniu0134702472017-07-11 09:22:58 +0100640 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100641
642#ifndef EMBEDDED_KERNELS
643 concat_set += "-I" + _kernel_path + " ";
644#endif /* EMBEDDED_KERNELS */
645
646 // Concatenate set
647 for(const auto &el : s)
648 {
649 concat_set += " " + el;
650 }
651
652 return concat_set;
653}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100654
655std::string CLKernelLibrary::get_program_source(const std::string &program_name)
656{
657 const auto program_source_it = _program_source_map.find(program_name);
658
659 if(program_source_it == _program_source_map.end())
660 {
661 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
662 }
663
664 return program_source_it->second;
665}
steniu015f910722017-08-23 10:15:22 +0100666
667size_t CLKernelLibrary::max_local_workgroup_size()
668{
669 if(_max_workgroup_size == 0)
670 {
671 size_t err = clGetDeviceInfo(_device.get(), CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &_max_workgroup_size, nullptr);
672 ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetDeviceInfo failed to return valid information");
673 ARM_COMPUTE_UNUSED(err);
674 }
675
676 return _max_workgroup_size;
677}
678
679cl::NDRange CLKernelLibrary::default_ndrange()
680{
681 return cl::NDRange(std::min<size_t>(_max_workgroup_size, 128u), 1);
682}