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