blob: 9c8be36b499260f2728299d1ea3ea740b672d864 [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
30#include <fstream>
31#include <iostream>
32#include <utility>
33#include <vector>
34
35using namespace arm_compute;
36
37Program::Program()
38 : _context(), _device(), _is_binary(false), _name(), _source(), _binary()
39{
40}
41
42Program::Program(cl::Context context, std::string name, std::string source)
43 : _context(std::move(context)), _device(), _is_binary(false), _name(std::move(name)), _source(std::move(source)), _binary()
44{
45}
46
47Program::Program(cl::Context context, cl::Device device, std::string name, std::vector<unsigned char> binary)
48 : _context(std::move(context)), _device(std::move(device)), _is_binary(true), _name(std::move(name)), _source(), _binary(std::move(binary))
49{
50}
51
52Program::operator cl::Program() const
53{
54 if(_is_binary)
55 {
56 return cl::Program(_context, { _device }, { _binary });
57 }
58 else
59 {
60 return cl::Program(_context, _source, false);
61 }
62}
63
64bool Program::build(const cl::Program &program, const std::string &build_options)
65{
66 try
67 {
68 return program.build(build_options.c_str()) == CL_SUCCESS;
69 }
70 catch(const cl::Error &e)
71 {
72 cl_int err = CL_SUCCESS;
73 const auto build_info = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(&err);
74
75 for(auto &pair : build_info)
76 {
77 std::cerr << pair.second << std::endl;
78 }
79
80 return false;
81 }
82}
83
84cl::Program Program::build(const std::string &build_options) const
85{
86 cl::Program cl_program = static_cast<cl::Program>(*this);
87 build(cl_program, build_options);
88 return cl_program;
89}
90
91Kernel::Kernel()
92 : _name(), _kernel()
93{
94}
95
96Kernel::Kernel(std::string name, const cl::Program &program)
97 : _name(std::move(name)),
98 _kernel(cl::Kernel(program, _name.c_str()))
99{
100}
101
102const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map =
103{
104 { "absdiff", "absdiff.cl" },
105 { "accumulate", "accumulate.cl" },
106 { "accumulate_squared", "accumulate.cl" },
107 { "accumulate_weighted", "accumulate.cl" },
108 { "activation_layer", "activation_layer.cl" },
109 { "arithmetic_add", "arithmetic_op.cl" },
110 { "arithmetic_sub", "arithmetic_op.cl" },
111 { "bitwise_or", "bitwise_op.cl" },
112 { "bitwise_and", "bitwise_op.cl" },
113 { "bitwise_xor", "bitwise_op.cl" },
114 { "bitwise_not", "bitwise_op.cl" },
115 { "channel_combine_NV", "channel_combine.cl" },
116 { "channel_combine_RGB888", "channel_combine.cl" },
117 { "channel_combine_RGBA8888", "channel_combine.cl" },
118 { "channel_combine_UYVY422", "channel_combine.cl" },
119 { "channel_combine_YUYV422", "channel_combine.cl" },
120 { "channel_extract_NV12", "channel_extract.cl" },
121 { "channel_extract_NV21", "channel_extract.cl" },
122 { "channel_extract_RGB888", "channel_extract.cl" },
123 { "channel_extract_RGBA8888", "channel_extract.cl" },
124 { "channel_extract_UYVY422", "channel_extract.cl" },
125 { "channel_extract_YUYV422", "channel_extract.cl" },
126 { "combine_gradients_L1", "canny.cl" },
127 { "combine_gradients_L2", "canny.cl" },
128 { "concatenate_depth", "concatenate.cl" },
129 { "convolution_rectangle", "convolution_rectangle.cl" },
130 { "col2im", "convolution_layer.cl" },
131 { "convolution3x3_static", "convolution3x3.cl" },
132 { "convolution5x5_static", "convolution5x5.cl" },
133 { "convolution7x7_static", "convolution7x7.cl" },
134 { "convolution9x9_static", "convolution9x9.cl" },
135 { "convolution_separable1x5_static", "convolution5x5.cl" },
136 { "convolution_separable5x1_static", "convolution5x5.cl" },
137 { "convolution_separable1x7_static", "convolution7x7.cl" },
138 { "convolution_separable7x1_static", "convolution7x7.cl" },
139 { "convolution_separable1x9_static", "convolution9x9.cl" },
140 { "convolution_separable9x1_static", "convolution9x9.cl" },
141 { "convert_depth_down", "depth_convert.cl" },
142 { "convert_depth_up", "depth_convert.cl" },
143 { "copy_plane", "channel_extract.cl" },
144 { "copy_planes_3p", "channel_combine.cl" },
145 { "copy_to_keypoint", "fast_corners.cl" },
146 { "derivative", "derivative.cl" },
147 { "dilate", "dilate.cl" },
steniu0127b386c2017-07-18 17:37:43 +0100148 { "direct_convolution3x3", "direct_convolution.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 { "erode", "erode.cl" },
150 { "fast_corners", "fast_corners.cl" },
151 { "fill_image_borders_constant", "fill_border.cl" },
152 { "fill_image_borders_replicate", "fill_border.cl" },
153 { "finalize", "optical_flow_pyramid_lk.cl" },
154 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
155 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100156 { "gemm_accumulate_biases", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 { "gemm_interleave4x4_8bit", "gemm.cl" },
158 { "gemm_interleave4x4_16bit", "gemm.cl" },
159 { "gemm_interleave4x4_32bit", "gemm.cl" },
160 { "gemm_ma_f16", "gemm.cl" },
161 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100162 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100163 { "gemm_ma_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 { "gemm_mm_u8", "gemm.cl" },
165 { "gemm_mm_f16", "gemm.cl" },
166 { "gemm_mm_f32_midgard", "gemm.cl" },
167 { "gemm_mm_f32_bifrost", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100168 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100169 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 { "gemm_vm_f16", "gemm.cl" },
171 { "gemm_vm_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100172 { "gemm_vm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100173 { "gemm_vm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100175 { "gemm_transpose1x16", "gemm.cl" },
176 { "gemm_transpose1x8", "gemm.cl" },
177 { "gemm_transpose1x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 { "harris_score_3x3", "harris_corners.cl" },
179 { "harris_score_5x5", "harris_corners.cl" },
180 { "harris_score_7x7", "harris_corners.cl" },
181 { "hist_border_kernel", "histogram.cl" },
182 { "hist_border_kernel_fixed", "histogram.cl" },
183 { "hist_local_kernel", "histogram.cl" },
184 { "hist_local_kernel_fixed", "histogram.cl" },
185 { "hog_block_normalization", "hog.cl" },
186 { "hog_detector", "hog.cl" },
187 { "hog_orientation_binning", "hog.cl" },
188 { "hysteresis", "canny.cl" },
189 { "im2col_generic", "convolution_layer.cl" },
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100190 { "im2col_kernel3x3_padx0_pady0", "convolution_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 { "im2col_reduced", "convolution_layer.cl" },
192 { "init_level", "optical_flow_pyramid_lk.cl" },
193 { "init_level_max", "optical_flow_pyramid_lk.cl" },
194 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
195 { "integral_horizontal", "integral_image.cl" },
196 { "integral_vertical", "integral_image.cl" },
197 { "IYUV_to_NV12_bt709", "color_convert.cl" },
198 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
199 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
200 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
201 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
202 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
203 { "magnitude_phase", "magnitude_phase.cl" },
204 { "mean_stddev_accumulate", "mean_stddev.cl" },
205 { "minmax", "minmaxloc.cl" },
206 { "minmax_border", "minmaxloc.cl" },
207 { "minmaxloc", "minmaxloc.cl" },
208 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
209 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
210 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
211 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
212 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
213 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
214 { "non_max_suppression", "nonmax.cl" },
215 { "normalization_layer_cross_map", "normalization_layer.cl" },
216 { "normalization_layer_in_map_1D", "normalization_layer.cl" },
217 { "batchnormalization_layer", "batchnormalization_layer.cl" },
218 { "NV12_to_IYUV_bt709", "color_convert.cl" },
219 { "NV12_to_RGB888_bt709", "color_convert.cl" },
220 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
221 { "NV12_to_YUV444_bt709", "color_convert.cl" },
222 { "NV21_to_IYUV_bt709", "color_convert.cl" },
223 { "NV21_to_RGB888_bt709", "color_convert.cl" },
224 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
225 { "NV21_to_YUV444_bt709", "color_convert.cl" },
226 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
227 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
228 { "pooling_layer_2", "pooling_layer.cl" },
229 { "pooling_layer_3", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100230 { "pooling_layer_7", "pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 { "remap_nearest_neighbour", "remap.cl" },
232 { "remap_bilinear", "remap.cl" },
233 { "reshape_to_columns", "convolution_layer.cl" },
234 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
235 { "RGB888_to_NV12_bt709", "color_convert.cl" },
236 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
237 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
238 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
239 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
240 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
241 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
242 { "scale_nearest_neighbour", "scale.cl" },
243 { "scale_bilinear", "scale.cl" },
244 { "scharr3x3", "scharr_filter.cl" },
245 { "sobel3x3", "sobel_filter.cl" },
246 { "sobel_separable5x1", "sobel_filter.cl" },
247 { "sobel_separable1x5", "sobel_filter.cl" },
248 { "sobel_separable7x1", "sobel_filter.cl" },
249 { "sobel_separable1x7", "sobel_filter.cl" },
250 { "softmax_layer_max", "softmax_layer.cl" },
251 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
252 { "softmax_layer_norm", "softmax_layer.cl" },
253 { "suppress_non_maximum", "canny.cl" },
254 { "tablelookup_U8", "tablelookup.cl" },
255 { "tablelookup_S16", "tablelookup.cl" },
256 { "threshold_binary", "threshold.cl" },
257 { "threshold_range", "threshold.cl" },
258 { "transpose", "transpose.cl" },
259 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
260 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
261 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
262 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
263 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
264 { "warp_affine_bilinear", "warp_affine.cl" },
265 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
266 { "warp_perspective_bilinear", "warp_perspective.cl" },
267 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
268 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
269 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
270 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
271};
272
273const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
274{
275#ifdef EMBEDDED_KERNELS
276 {
277 "absdiff.cl",
278#include "./cl_kernels/absdiff.clembed"
279 },
280 {
281 "accumulate.cl",
282#include "./cl_kernels/accumulate.clembed"
283 },
284 {
285 "activation_layer.cl",
286#include "./cl_kernels/activation_layer.clembed"
287 },
288 {
289 "arithmetic_op.cl",
290#include "./cl_kernels/arithmetic_op.clembed"
291 },
292 {
293 "bitwise_op.cl",
294#include "./cl_kernels/bitwise_op.clembed"
295 },
296 {
297 "canny.cl",
298#include "./cl_kernels/canny.clembed"
299 },
300 {
301 "channel_combine.cl",
302#include "./cl_kernels/channel_combine.clembed"
303 },
304 {
305 "channel_extract.cl",
306#include "./cl_kernels/channel_extract.clembed"
307 },
308 {
309 "concatenate.cl",
310#include "./cl_kernels/concatenate.clembed"
311 },
312 {
313 "color_convert.cl",
314#include "./cl_kernels/color_convert.clembed"
315 },
316 {
317 "convolution3x3.cl",
318#include "./cl_kernels/convolution3x3.clembed"
319 },
320 {
321 "convolution5x5.cl",
322#include "./cl_kernels/convolution5x5.clembed"
323 },
324 {
325 "convolution7x7.cl",
326#include "./cl_kernels/convolution7x7.clembed"
327 },
328 {
329 "convolution9x9.cl",
330#include "./cl_kernels/convolution9x9.clembed"
331 },
332 {
333 "convolution_layer.cl",
334#include "./cl_kernels/convolution_layer.clembed"
335 },
336 {
337 "convolution_rectangle.cl",
338#include "./cl_kernels/convolution_rectangle.clembed"
339 },
340 {
341 "depth_convert.cl",
342#include "./cl_kernels/depth_convert.clembed"
343 },
344 {
345 "derivative.cl",
346#include "./cl_kernels/derivative.clembed"
347 },
348 {
349 "dilate.cl",
350#include "./cl_kernels/dilate.clembed"
351 },
352 {
steniu0127b386c2017-07-18 17:37:43 +0100353 "direct_convolution.cl",
354#include "./cl_kernels/direct_convolution.clembed"
355 },
356 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100357 "erode.cl",
358#include "./cl_kernels/erode.clembed"
359 },
360 {
361 "fast_corners.cl",
362#include "./cl_kernels/fast_corners.clembed"
363 },
364 {
365 "fill_border.cl",
366#include "./cl_kernels/fill_border.clembed"
367 },
368 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100369 "fixed_point.h",
370#include "./cl_kernels/fixed_point.hembed"
371 },
372 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373 "gaussian_pyramid.cl",
374#include "./cl_kernels/gaussian_pyramid.clembed"
375 },
376 {
377 "gemm.cl",
378#include "./cl_kernels/gemm.clembed"
379 },
380 {
381 "harris_corners.cl",
382#include "./cl_kernels/harris_corners.clembed"
383 },
384 {
385 "helpers.h",
386#include "./cl_kernels/helpers.hembed"
387 },
388 {
389 "histogram.cl",
390#include "./cl_kernels/histogram.clembed"
391 },
392 {
393 "hog.cl",
394#include "./cl_kernels/hog.clembed"
395 },
396 {
397 "integral_image.cl",
398#include "./cl_kernels/integral_image.clembed"
399 },
400 {
401 "magnitude_phase.cl",
402#include "./cl_kernels/magnitude_phase.clembed"
403 },
404 {
405 "mean_stddev.cl",
406#include "./cl_kernels/mean_stddev.clembed"
407 },
408 {
409 "minmaxloc.cl",
410#include "./cl_kernels/minmaxloc.clembed"
411 },
412 {
413 "non_linear_filter3x3.cl",
414#include "./cl_kernels/non_linear_filter3x3.clembed"
415 },
416 {
417 "non_linear_filter5x5.cl",
418#include "./cl_kernels/non_linear_filter5x5.clembed"
419 },
420 {
421 "non_linear_filter_helpers.h",
422#include "./cl_kernels/non_linear_filter_helpers.hembed"
423 },
424 {
425 "nonmax.cl",
426#include "./cl_kernels/nonmax.clembed"
427 },
428 {
429 "normalization_layer.cl",
430#include "./cl_kernels/normalization_layer.clembed"
431 },
432 {
433 "batchnormalization_layer.cl",
434#include "./cl_kernels/batchnormalization_layer.clembed"
435 },
436 {
437 "optical_flow_pyramid_lk.cl",
438#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
439 },
440 {
441 "pixelwise_mul_float.cl",
442#include "./cl_kernels/pixelwise_mul_float.clembed"
443 },
444 {
445 "pixelwise_mul_int.cl",
446#include "./cl_kernels/pixelwise_mul_int.clembed"
447 },
448 {
449 "pooling_layer.cl",
450#include "./cl_kernels/pooling_layer.clembed"
451 },
452 {
453 "remap.cl",
454#include "./cl_kernels/remap.clembed"
455 },
456 {
457 "scale.cl",
458#include "./cl_kernels/scale.clembed"
459 },
460 {
461 "scharr_filter.cl",
462#include "./cl_kernels/scharr_filter.clembed"
463 },
464 {
465 "sobel_filter.cl",
466#include "./cl_kernels/sobel_filter.clembed"
467 },
468 {
469 "softmax_layer.cl",
470#include "./cl_kernels/softmax_layer.clembed"
471 },
472 {
473 "tablelookup.cl",
474#include "./cl_kernels/tablelookup.clembed"
475 },
476 {
477 "threshold.cl",
478#include "./cl_kernels/threshold.clembed"
479 },
480 {
481 "transpose.cl",
482#include "./cl_kernels/transpose.clembed"
483 },
484 {
485 "types.h",
486#include "./cl_kernels/types.hembed"
487 },
488 {
489 "warp_affine.cl",
490#include "./cl_kernels/warp_affine.clembed"
491 },
492 {
493 "warp_helpers.h",
494#include "./cl_kernels/warp_helpers.hembed"
495 },
496 {
497 "warp_perspective.cl",
498#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100499 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100500#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501};
502
503CLKernelLibrary::CLKernelLibrary()
504 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
505{
506}
507
508CLKernelLibrary &CLKernelLibrary::get()
509{
510 static CLKernelLibrary _kernel_library;
511 return _kernel_library;
512}
513
514Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
515{
516 // Find which program contains the kernel
517 auto kernel_program_it = _kernel_program_map.find(kernel_name);
518
519 if(_kernel_program_map.end() == kernel_program_it)
520 {
521 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
522 }
523
steniu0134702472017-07-11 09:22:58 +0100524 std::string concat_str;
525
526 if(non_uniform_workgroup_support(_device))
527 {
528 concat_str += " -cl-arm-non-uniform-work-group-size ";
529 }
530 else if(get_cl_version(_device) == CLVersion::CL20)
531 {
532 concat_str += " -cl-std=CL2.0 ";
533 }
534 else
535 {
536 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
537 }
538
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100539 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100540 const std::string program_name = kernel_program_it->second;
541 const std::string build_options = stringify_set(build_options_set) + concat_str;
542
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100543 const std::string built_program_name = program_name + "_" + build_options;
544 auto built_program_it = _built_programs_map.find(built_program_name);
545
546 cl::Program cl_program;
547
548 if(_built_programs_map.end() != built_program_it)
549 {
550 // If program has been built, retrieve to create kernel from it
551 cl_program = built_program_it->second;
552 }
553 else
554 {
555 // Get program
556 Program program = load_program(program_name);
557
558 // Build program
559 cl_program = program.build(build_options);
560
561 // Add built program to internal map
562 _built_programs_map.emplace(built_program_name, cl_program);
563 }
564
565 // Create and return kernel
566 return Kernel(kernel_name, cl_program);
567}
568
569const Program &CLKernelLibrary::load_program(const std::string &program_name) const
570{
571 const auto program_it = _programs_map.find(program_name);
572
573 if(program_it != _programs_map.end())
574 {
575 return program_it->second;
576 }
577
578 Program program;
579
580#ifdef EMBEDDED_KERNELS
581 const auto program_source_it = _program_source_map.find(program_name);
582
583 if(_program_source_map.end() == program_source_it)
584 {
585 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
586 }
587
588 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100589#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100590 // Check for binary
591 std::string source_name = _kernel_path + program_name;
592 std::string binary_name = source_name + "bin";
593
594 if(std::ifstream(binary_name).is_open())
595 {
596 const std::string program_binary = read_file(binary_name, true);
597 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
598 }
599 else if(std::ifstream(source_name).is_open())
600 {
601 program = Program(_context, program_name, read_file(source_name, false));
602 }
603 else
604 {
605 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
606 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100607#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100608
609 // Insert program to program map
610 const auto new_program = _programs_map.emplace(program_name, std::move(program));
611
612 return new_program.first->second;
613}
614
615std::string CLKernelLibrary::stringify_set(const StringSet &s) const
616{
steniu0134702472017-07-11 09:22:58 +0100617 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100618
619#ifndef EMBEDDED_KERNELS
620 concat_set += "-I" + _kernel_path + " ";
621#endif /* EMBEDDED_KERNELS */
622
623 // Concatenate set
624 for(const auto &el : s)
625 {
626 concat_set += " " + el;
627 }
628
629 return concat_set;
630}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100631
632std::string CLKernelLibrary::get_program_source(const std::string &program_name)
633{
634 const auto program_source_it = _program_source_map.find(program_name);
635
636 if(program_source_it == _program_source_map.end())
637 {
638 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
639 }
640
641 return program_source_it->second;
642}