blob: e69d006750119f4c1f085942a628fd6af4b724d7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Error.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
Georgios Pinitas7891a732021-08-20 21:39:25 +010028#include "src/gpu/cl/ClKernelLibrary.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
steniu015f910722017-08-23 10:15:22 +010030#include <algorithm>
Georgios Pinitasea857272021-01-22 05:47:37 +000031#include <array>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <fstream>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include <utility>
34#include <vector>
Georgios Pinitas908f6162021-05-04 10:11:09 +010035namespace arm_compute
Georgios Pinitasea857272021-01-22 05:47:37 +000036{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037CLKernelLibrary::CLKernelLibrary() : _compile_context()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038{
Anthony Barbierecb1c622018-04-17 11:45:10 +010039 opencl_is_available(); // Make sure the OpenCL symbols are initialised *before* the CLKernelLibrary is built
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041CLKernelLibrary &CLKernelLibrary::get()
42{
43 static CLKernelLibrary _kernel_library;
44 return _kernel_library;
45}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name,
47 const std::set<std::string> &build_options_set) const
Michalis Spyrou11d49182020-03-26 10:31:32 +000048{
Gian Marco Iodice8155c022021-04-16 15:08:59 +010049 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 return _compile_context.create_kernel(kernel_name, program_name, program.program, kernel_path, build_options_set,
54 program.is_binary);
Michalis Spyrou11d49182020-03-26 10:31:32 +000055}
Michalis Spyrou11d49182020-03-26 10:31:32 +000056std::string CLKernelLibrary::get_program_name(const std::string &kernel_name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
Georgios Pinitas908f6162021-05-04 10:11:09 +010058 return opencl::ClKernelLibrary::get().program_name(kernel_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059}
Pablo Tellodb8485a2019-09-24 11:03:47 +010060void CLKernelLibrary::init(std::string kernel_path, cl::Context context, cl::Device device)
61{
Michalis Spyrou11d49182020-03-26 10:31:32 +000062 _compile_context = CLCompileContext(context, device);
Georgios Pinitas908f6162021-05-04 10:11:09 +010063 opencl::ClKernelLibrary::get().set_kernel_path(kernel_path);
Pablo Tellodb8485a2019-09-24 11:03:47 +010064}
Pablo Tellodb8485a2019-09-24 11:03:47 +010065void CLKernelLibrary::set_kernel_path(const std::string &kernel_path)
66{
Georgios Pinitas908f6162021-05-04 10:11:09 +010067 opencl::ClKernelLibrary::get().set_kernel_path(kernel_path);
Pablo Tellodb8485a2019-09-24 11:03:47 +010068}
Pablo Tellodb8485a2019-09-24 11:03:47 +010069cl::Context &CLKernelLibrary::context()
70{
Michalis Spyrou11d49182020-03-26 10:31:32 +000071 return _compile_context.context();
Pablo Tellodb8485a2019-09-24 11:03:47 +010072}
Michalis Spyrou11d49182020-03-26 10:31:32 +000073const cl::Device &CLKernelLibrary::get_device()
Pablo Tellodb8485a2019-09-24 11:03:47 +010074{
Michalis Spyrou11d49182020-03-26 10:31:32 +000075 return _compile_context.get_device();
Pablo Tellodb8485a2019-09-24 11:03:47 +010076}
Pablo Tellodb8485a2019-09-24 11:03:47 +010077void CLKernelLibrary::set_device(cl::Device device)
78{
Michalis Spyrou11d49182020-03-26 10:31:32 +000079 _compile_context.set_device(device);
80}
Michalis Spyrou11d49182020-03-26 10:31:32 +000081void CLKernelLibrary::set_context(cl::Context context)
82{
83 _compile_context.set_context(context);
Pablo Tellodb8485a2019-09-24 11:03:47 +010084}
Pablo Tellodb8485a2019-09-24 11:03:47 +010085std::string CLKernelLibrary::get_kernel_path()
86{
Georgios Pinitas908f6162021-05-04 10:11:09 +010087 return opencl::ClKernelLibrary::get().kernel_path();
Pablo Tellodb8485a2019-09-24 11:03:47 +010088}
Pablo Tellodb8485a2019-09-24 11:03:47 +010089void CLKernelLibrary::clear_programs_cache()
90{
Michalis Spyrou11d49182020-03-26 10:31:32 +000091 _compile_context.clear_programs_cache();
Pablo Tellodb8485a2019-09-24 11:03:47 +010092}
Pablo Tellodb8485a2019-09-24 11:03:47 +010093const std::map<std::string, cl::Program> &CLKernelLibrary::get_built_programs() const
94{
Michalis Spyrou11d49182020-03-26 10:31:32 +000095 return _compile_context.get_built_programs();
Pablo Tellodb8485a2019-09-24 11:03:47 +010096}
giuros0146a49a02019-04-01 13:50:22 +010097void CLKernelLibrary::add_built_program(const std::string &built_program_name, const cl::Program &program)
Anthony Barbier7da55aa2018-04-13 16:58:43 +010098{
Michalis Spyrou11d49182020-03-26 10:31:32 +000099 _compile_context.add_built_program(built_program_name, program);
Anthony Barbier7da55aa2018-04-13 16:58:43 +0100100}
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100101bool CLKernelLibrary::fp16_supported() const
102{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000103 return _compile_context.fp16_supported();
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100104}
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +0100105bool CLKernelLibrary::int64_base_atomics_supported() const
106{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000107 return _compile_context.int64_base_atomics_supported();
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +0100108}
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000109bool CLKernelLibrary::is_wbsm_supported()
110{
111 return _compile_context.is_wbsm_supported();
112}
Michalis Spyrou11d49182020-03-26 10:31:32 +0000113std::pair<std::string, bool> CLKernelLibrary::get_program(const std::string &program_name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114{
Georgios Pinitas908f6162021-05-04 10:11:09 +0100115 auto program_info = opencl::ClKernelLibrary::get().program(program_name);
116 return std::make_pair(std::move(program_info.program), program_info.is_binary);
Michalis Spyroud7e82812017-06-20 15:00:14 +0100117}
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100118size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100119{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000120 return _compile_context.max_local_workgroup_size(kernel);
steniu015f910722017-08-23 10:15:22 +0100121}
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100122cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100123{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000124 return _compile_context.default_ndrange();
steniu015f910722017-08-23 10:15:22 +0100125}
Anthony Barbier847864d2018-03-07 11:35:53 +0000126std::string CLKernelLibrary::get_device_version()
127{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000128 return _compile_context.get_device_version();
Anthony Barbier847864d2018-03-07 11:35:53 +0000129}
Giorgio Arena5d42b462019-07-26 15:54:20 +0100130cl_uint CLKernelLibrary::get_num_compute_units()
131{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000132 return _compile_context.get_num_compute_units();
133}
Michalis Spyrou11d49182020-03-26 10:31:32 +0000134CLCompileContext &CLKernelLibrary::get_compile_context()
135{
136 return _compile_context;
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100137}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138} // namespace arm_compute