Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 26 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 27 | #include "src/core/gpu/cl/ClKernelLibrary.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 29 | #include <algorithm> |
Georgios Pinitas | ea85727 | 2021-01-22 05:47:37 +0000 | [diff] [blame] | 30 | #include <array> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include <fstream> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include <utility> |
| 33 | #include <vector> |
| 34 | |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 35 | namespace arm_compute |
Georgios Pinitas | ea85727 | 2021-01-22 05:47:37 +0000 | [diff] [blame] | 36 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | CLKernelLibrary::CLKernelLibrary() |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 38 | : _compile_context() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | { |
Anthony Barbier | ecb1c62 | 2018-04-17 11:45:10 +0100 | [diff] [blame] | 40 | opencl_is_available(); // Make sure the OpenCL symbols are initialised *before* the CLKernelLibrary is built |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | CLKernelLibrary &CLKernelLibrary::get() |
| 44 | { |
| 45 | static CLKernelLibrary _kernel_library; |
| 46 | return _kernel_library; |
| 47 | } |
| 48 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 49 | Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const std::set<std::string> &build_options_set) const |
| 50 | { |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 51 | const opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get(); |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 52 | |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 53 | const std::string program_name = klib.program_name(kernel_name); |
| 54 | auto program = klib.program(program_name); |
| 55 | const std::string &kernel_path = CLKernelLibrary::get().get_kernel_path(); |
| 56 | |
| 57 | return _compile_context.create_kernel(kernel_name, program_name, program.program, kernel_path, build_options_set, program.is_binary); |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | std::string CLKernelLibrary::get_program_name(const std::string &kernel_name) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | { |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 62 | return opencl::ClKernelLibrary::get().program_name(kernel_name); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 65 | void CLKernelLibrary::init(std::string kernel_path, cl::Context context, cl::Device device) |
| 66 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 67 | _compile_context = CLCompileContext(context, device); |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 68 | opencl::ClKernelLibrary::get().set_kernel_path(kernel_path); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void CLKernelLibrary::set_kernel_path(const std::string &kernel_path) |
| 72 | { |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 73 | opencl::ClKernelLibrary::get().set_kernel_path(kernel_path); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | cl::Context &CLKernelLibrary::context() |
| 77 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 78 | return _compile_context.context(); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 81 | const cl::Device &CLKernelLibrary::get_device() |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 82 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 83 | return _compile_context.get_device(); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void CLKernelLibrary::set_device(cl::Device device) |
| 87 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 88 | _compile_context.set_device(device); |
| 89 | } |
| 90 | |
| 91 | void CLKernelLibrary::set_context(cl::Context context) |
| 92 | { |
| 93 | _compile_context.set_context(context); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | std::string CLKernelLibrary::get_kernel_path() |
| 97 | { |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 98 | return opencl::ClKernelLibrary::get().kernel_path(); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void CLKernelLibrary::clear_programs_cache() |
| 102 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 103 | _compile_context.clear_programs_cache(); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | const std::map<std::string, cl::Program> &CLKernelLibrary::get_built_programs() const |
| 107 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 108 | return _compile_context.get_built_programs(); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 109 | } |
| 110 | |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 111 | void CLKernelLibrary::add_built_program(const std::string &built_program_name, const cl::Program &program) |
Anthony Barbier | 7da55aa | 2018-04-13 16:58:43 +0100 | [diff] [blame] | 112 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 113 | _compile_context.add_built_program(built_program_name, program); |
Anthony Barbier | 7da55aa | 2018-04-13 16:58:43 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 116 | bool CLKernelLibrary::fp16_supported() const |
| 117 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 118 | return _compile_context.fp16_supported(); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Vidhya Sudhan Loganathan | 76c8564 | 2018-05-25 13:53:02 +0100 | [diff] [blame] | 121 | bool CLKernelLibrary::int64_base_atomics_supported() const |
| 122 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 123 | return _compile_context.int64_base_atomics_supported(); |
Vidhya Sudhan Loganathan | 76c8564 | 2018-05-25 13:53:02 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 126 | bool CLKernelLibrary::is_wbsm_supported() |
| 127 | { |
| 128 | return _compile_context.is_wbsm_supported(); |
| 129 | } |
| 130 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 131 | std::pair<std::string, bool> CLKernelLibrary::get_program(const std::string &program_name) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | { |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 133 | auto program_info = opencl::ClKernelLibrary::get().program(program_name); |
| 134 | return std::make_pair(std::move(program_info.program), program_info.is_binary); |
Michalis Spyrou | d7e8281 | 2017-06-20 15:00:14 +0100 | [diff] [blame] | 135 | } |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 136 | |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 137 | size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 138 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 139 | return _compile_context.max_local_workgroup_size(kernel); |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 142 | cl::NDRange CLKernelLibrary::default_ndrange() const |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 143 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 144 | return _compile_context.default_ndrange(); |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 145 | } |
Anthony Barbier | 847864d | 2018-03-07 11:35:53 +0000 | [diff] [blame] | 146 | |
| 147 | std::string CLKernelLibrary::get_device_version() |
| 148 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 149 | return _compile_context.get_device_version(); |
Anthony Barbier | 847864d | 2018-03-07 11:35:53 +0000 | [diff] [blame] | 150 | } |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 151 | |
| 152 | cl_uint CLKernelLibrary::get_num_compute_units() |
| 153 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 154 | return _compile_context.get_num_compute_units(); |
| 155 | } |
| 156 | |
| 157 | CLCompileContext &CLKernelLibrary::get_compile_context() |
| 158 | { |
| 159 | return _compile_context; |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 160 | } |
Georgios Pinitas | 908f616 | 2021-05-04 10:11:09 +0100 | [diff] [blame^] | 161 | } // namespace arm_compute |