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