blob: 769d50992da3a44b2ee97af0ef8a2940052cd418 [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" },
148 { "erode", "erode.cl" },
149 { "fast_corners", "fast_corners.cl" },
150 { "fill_image_borders_constant", "fill_border.cl" },
151 { "fill_image_borders_replicate", "fill_border.cl" },
152 { "finalize", "optical_flow_pyramid_lk.cl" },
153 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
154 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100155 { "gemm_accumulate_biases", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 { "gemm_interleave4x4_8bit", "gemm.cl" },
157 { "gemm_interleave4x4_16bit", "gemm.cl" },
158 { "gemm_interleave4x4_32bit", "gemm.cl" },
159 { "gemm_ma_f16", "gemm.cl" },
160 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100161 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100162 { "gemm_ma_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 { "gemm_mm_u8", "gemm.cl" },
164 { "gemm_mm_f16", "gemm.cl" },
165 { "gemm_mm_f32_midgard", "gemm.cl" },
166 { "gemm_mm_f32_bifrost", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100167 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100168 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 { "gemm_vm_f16", "gemm.cl" },
170 { "gemm_vm_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100171 { "gemm_vm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100172 { "gemm_vm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100174 { "gemm_transpose1x16", "gemm.cl" },
175 { "gemm_transpose1x8", "gemm.cl" },
176 { "gemm_transpose1x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 { "harris_score_3x3", "harris_corners.cl" },
178 { "harris_score_5x5", "harris_corners.cl" },
179 { "harris_score_7x7", "harris_corners.cl" },
180 { "hist_border_kernel", "histogram.cl" },
181 { "hist_border_kernel_fixed", "histogram.cl" },
182 { "hist_local_kernel", "histogram.cl" },
183 { "hist_local_kernel_fixed", "histogram.cl" },
184 { "hog_block_normalization", "hog.cl" },
185 { "hog_detector", "hog.cl" },
186 { "hog_orientation_binning", "hog.cl" },
187 { "hysteresis", "canny.cl" },
188 { "im2col_generic", "convolution_layer.cl" },
189 { "im2col_reduced", "convolution_layer.cl" },
190 { "init_level", "optical_flow_pyramid_lk.cl" },
191 { "init_level_max", "optical_flow_pyramid_lk.cl" },
192 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
193 { "integral_horizontal", "integral_image.cl" },
194 { "integral_vertical", "integral_image.cl" },
195 { "IYUV_to_NV12_bt709", "color_convert.cl" },
196 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
197 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
198 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
199 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
200 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
201 { "magnitude_phase", "magnitude_phase.cl" },
202 { "mean_stddev_accumulate", "mean_stddev.cl" },
203 { "minmax", "minmaxloc.cl" },
204 { "minmax_border", "minmaxloc.cl" },
205 { "minmaxloc", "minmaxloc.cl" },
206 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
207 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
208 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
209 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
210 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
211 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
212 { "non_max_suppression", "nonmax.cl" },
213 { "normalization_layer_cross_map", "normalization_layer.cl" },
214 { "normalization_layer_in_map_1D", "normalization_layer.cl" },
215 { "batchnormalization_layer", "batchnormalization_layer.cl" },
216 { "NV12_to_IYUV_bt709", "color_convert.cl" },
217 { "NV12_to_RGB888_bt709", "color_convert.cl" },
218 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
219 { "NV12_to_YUV444_bt709", "color_convert.cl" },
220 { "NV21_to_IYUV_bt709", "color_convert.cl" },
221 { "NV21_to_RGB888_bt709", "color_convert.cl" },
222 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
223 { "NV21_to_YUV444_bt709", "color_convert.cl" },
224 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
225 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
226 { "pooling_layer_2", "pooling_layer.cl" },
227 { "pooling_layer_3", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100228 { "pooling_layer_7", "pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 { "remap_nearest_neighbour", "remap.cl" },
230 { "remap_bilinear", "remap.cl" },
231 { "reshape_to_columns", "convolution_layer.cl" },
232 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
233 { "RGB888_to_NV12_bt709", "color_convert.cl" },
234 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
235 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
236 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
237 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
238 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
239 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
240 { "scale_nearest_neighbour", "scale.cl" },
241 { "scale_bilinear", "scale.cl" },
242 { "scharr3x3", "scharr_filter.cl" },
243 { "sobel3x3", "sobel_filter.cl" },
244 { "sobel_separable5x1", "sobel_filter.cl" },
245 { "sobel_separable1x5", "sobel_filter.cl" },
246 { "sobel_separable7x1", "sobel_filter.cl" },
247 { "sobel_separable1x7", "sobel_filter.cl" },
248 { "softmax_layer_max", "softmax_layer.cl" },
249 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
250 { "softmax_layer_norm", "softmax_layer.cl" },
251 { "suppress_non_maximum", "canny.cl" },
252 { "tablelookup_U8", "tablelookup.cl" },
253 { "tablelookup_S16", "tablelookup.cl" },
254 { "threshold_binary", "threshold.cl" },
255 { "threshold_range", "threshold.cl" },
256 { "transpose", "transpose.cl" },
257 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
258 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
259 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
260 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
261 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
262 { "warp_affine_bilinear", "warp_affine.cl" },
263 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
264 { "warp_perspective_bilinear", "warp_perspective.cl" },
265 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
266 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
267 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
268 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
269};
270
271const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
272{
273#ifdef EMBEDDED_KERNELS
274 {
275 "absdiff.cl",
276#include "./cl_kernels/absdiff.clembed"
277 },
278 {
279 "accumulate.cl",
280#include "./cl_kernels/accumulate.clembed"
281 },
282 {
283 "activation_layer.cl",
284#include "./cl_kernels/activation_layer.clembed"
285 },
286 {
287 "arithmetic_op.cl",
288#include "./cl_kernels/arithmetic_op.clembed"
289 },
290 {
291 "bitwise_op.cl",
292#include "./cl_kernels/bitwise_op.clembed"
293 },
294 {
295 "canny.cl",
296#include "./cl_kernels/canny.clembed"
297 },
298 {
299 "channel_combine.cl",
300#include "./cl_kernels/channel_combine.clembed"
301 },
302 {
303 "channel_extract.cl",
304#include "./cl_kernels/channel_extract.clembed"
305 },
306 {
307 "concatenate.cl",
308#include "./cl_kernels/concatenate.clembed"
309 },
310 {
311 "color_convert.cl",
312#include "./cl_kernels/color_convert.clembed"
313 },
314 {
315 "convolution3x3.cl",
316#include "./cl_kernels/convolution3x3.clembed"
317 },
318 {
319 "convolution5x5.cl",
320#include "./cl_kernels/convolution5x5.clembed"
321 },
322 {
323 "convolution7x7.cl",
324#include "./cl_kernels/convolution7x7.clembed"
325 },
326 {
327 "convolution9x9.cl",
328#include "./cl_kernels/convolution9x9.clembed"
329 },
330 {
331 "convolution_layer.cl",
332#include "./cl_kernels/convolution_layer.clembed"
333 },
334 {
335 "convolution_rectangle.cl",
336#include "./cl_kernels/convolution_rectangle.clembed"
337 },
338 {
339 "depth_convert.cl",
340#include "./cl_kernels/depth_convert.clembed"
341 },
342 {
343 "derivative.cl",
344#include "./cl_kernels/derivative.clembed"
345 },
346 {
347 "dilate.cl",
348#include "./cl_kernels/dilate.clembed"
349 },
350 {
351 "erode.cl",
352#include "./cl_kernels/erode.clembed"
353 },
354 {
355 "fast_corners.cl",
356#include "./cl_kernels/fast_corners.clembed"
357 },
358 {
359 "fill_border.cl",
360#include "./cl_kernels/fill_border.clembed"
361 },
362 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100363 "fixed_point.h",
364#include "./cl_kernels/fixed_point.hembed"
365 },
366 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367 "gaussian_pyramid.cl",
368#include "./cl_kernels/gaussian_pyramid.clembed"
369 },
370 {
371 "gemm.cl",
372#include "./cl_kernels/gemm.clembed"
373 },
374 {
375 "harris_corners.cl",
376#include "./cl_kernels/harris_corners.clembed"
377 },
378 {
379 "helpers.h",
380#include "./cl_kernels/helpers.hembed"
381 },
382 {
383 "histogram.cl",
384#include "./cl_kernels/histogram.clembed"
385 },
386 {
387 "hog.cl",
388#include "./cl_kernels/hog.clembed"
389 },
390 {
391 "integral_image.cl",
392#include "./cl_kernels/integral_image.clembed"
393 },
394 {
395 "magnitude_phase.cl",
396#include "./cl_kernels/magnitude_phase.clembed"
397 },
398 {
399 "mean_stddev.cl",
400#include "./cl_kernels/mean_stddev.clembed"
401 },
402 {
403 "minmaxloc.cl",
404#include "./cl_kernels/minmaxloc.clembed"
405 },
406 {
407 "non_linear_filter3x3.cl",
408#include "./cl_kernels/non_linear_filter3x3.clembed"
409 },
410 {
411 "non_linear_filter5x5.cl",
412#include "./cl_kernels/non_linear_filter5x5.clembed"
413 },
414 {
415 "non_linear_filter_helpers.h",
416#include "./cl_kernels/non_linear_filter_helpers.hembed"
417 },
418 {
419 "nonmax.cl",
420#include "./cl_kernels/nonmax.clembed"
421 },
422 {
423 "normalization_layer.cl",
424#include "./cl_kernels/normalization_layer.clembed"
425 },
426 {
427 "batchnormalization_layer.cl",
428#include "./cl_kernels/batchnormalization_layer.clembed"
429 },
430 {
431 "optical_flow_pyramid_lk.cl",
432#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
433 },
434 {
435 "pixelwise_mul_float.cl",
436#include "./cl_kernels/pixelwise_mul_float.clembed"
437 },
438 {
439 "pixelwise_mul_int.cl",
440#include "./cl_kernels/pixelwise_mul_int.clembed"
441 },
442 {
443 "pooling_layer.cl",
444#include "./cl_kernels/pooling_layer.clembed"
445 },
446 {
447 "remap.cl",
448#include "./cl_kernels/remap.clembed"
449 },
450 {
451 "scale.cl",
452#include "./cl_kernels/scale.clembed"
453 },
454 {
455 "scharr_filter.cl",
456#include "./cl_kernels/scharr_filter.clembed"
457 },
458 {
459 "sobel_filter.cl",
460#include "./cl_kernels/sobel_filter.clembed"
461 },
462 {
463 "softmax_layer.cl",
464#include "./cl_kernels/softmax_layer.clembed"
465 },
466 {
467 "tablelookup.cl",
468#include "./cl_kernels/tablelookup.clembed"
469 },
470 {
471 "threshold.cl",
472#include "./cl_kernels/threshold.clembed"
473 },
474 {
475 "transpose.cl",
476#include "./cl_kernels/transpose.clembed"
477 },
478 {
479 "types.h",
480#include "./cl_kernels/types.hembed"
481 },
482 {
483 "warp_affine.cl",
484#include "./cl_kernels/warp_affine.clembed"
485 },
486 {
487 "warp_helpers.h",
488#include "./cl_kernels/warp_helpers.hembed"
489 },
490 {
491 "warp_perspective.cl",
492#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100493 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100494#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100495};
496
497CLKernelLibrary::CLKernelLibrary()
498 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
499{
500}
501
502CLKernelLibrary &CLKernelLibrary::get()
503{
504 static CLKernelLibrary _kernel_library;
505 return _kernel_library;
506}
507
508Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
509{
510 // Find which program contains the kernel
511 auto kernel_program_it = _kernel_program_map.find(kernel_name);
512
513 if(_kernel_program_map.end() == kernel_program_it)
514 {
515 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
516 }
517
steniu0134702472017-07-11 09:22:58 +0100518 std::string concat_str;
519
520 if(non_uniform_workgroup_support(_device))
521 {
522 concat_str += " -cl-arm-non-uniform-work-group-size ";
523 }
524 else if(get_cl_version(_device) == CLVersion::CL20)
525 {
526 concat_str += " -cl-std=CL2.0 ";
527 }
528 else
529 {
530 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
531 }
532
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100533 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100534 const std::string program_name = kernel_program_it->second;
535 const std::string build_options = stringify_set(build_options_set) + concat_str;
536
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100537 const std::string built_program_name = program_name + "_" + build_options;
538 auto built_program_it = _built_programs_map.find(built_program_name);
539
540 cl::Program cl_program;
541
542 if(_built_programs_map.end() != built_program_it)
543 {
544 // If program has been built, retrieve to create kernel from it
545 cl_program = built_program_it->second;
546 }
547 else
548 {
549 // Get program
550 Program program = load_program(program_name);
551
552 // Build program
553 cl_program = program.build(build_options);
554
555 // Add built program to internal map
556 _built_programs_map.emplace(built_program_name, cl_program);
557 }
558
559 // Create and return kernel
560 return Kernel(kernel_name, cl_program);
561}
562
563const Program &CLKernelLibrary::load_program(const std::string &program_name) const
564{
565 const auto program_it = _programs_map.find(program_name);
566
567 if(program_it != _programs_map.end())
568 {
569 return program_it->second;
570 }
571
572 Program program;
573
574#ifdef EMBEDDED_KERNELS
575 const auto program_source_it = _program_source_map.find(program_name);
576
577 if(_program_source_map.end() == program_source_it)
578 {
579 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
580 }
581
582 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100583#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100584 // Check for binary
585 std::string source_name = _kernel_path + program_name;
586 std::string binary_name = source_name + "bin";
587
588 if(std::ifstream(binary_name).is_open())
589 {
590 const std::string program_binary = read_file(binary_name, true);
591 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
592 }
593 else if(std::ifstream(source_name).is_open())
594 {
595 program = Program(_context, program_name, read_file(source_name, false));
596 }
597 else
598 {
599 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
600 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100601#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100602
603 // Insert program to program map
604 const auto new_program = _programs_map.emplace(program_name, std::move(program));
605
606 return new_program.first->second;
607}
608
609std::string CLKernelLibrary::stringify_set(const StringSet &s) const
610{
steniu0134702472017-07-11 09:22:58 +0100611 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100612
613#ifndef EMBEDDED_KERNELS
614 concat_set += "-I" + _kernel_path + " ";
615#endif /* EMBEDDED_KERNELS */
616
617 // Concatenate set
618 for(const auto &el : s)
619 {
620 concat_set += " " + el;
621 }
622
623 return concat_set;
624}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100625
626std::string CLKernelLibrary::get_program_source(const std::string &program_name)
627{
628 const auto program_source_it = _program_source_map.find(program_name);
629
630 if(program_source_it == _program_source_map.end())
631 {
632 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
633 }
634
635 return program_source_it->second;
636}