blob: bbd4009389a473b6e043359f12ecb259c1fb8d0f [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"
25
26#include "arm_compute/core/Error.h"
Georgios Pinitas908f6162021-05-04 10:11:09 +010027#include "src/core/gpu/cl/ClKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
steniu015f910722017-08-23 10:15:22 +010029#include <algorithm>
Georgios Pinitasea857272021-01-22 05:47:37 +000030#include <array>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <fstream>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <utility>
33#include <vector>
34
Georgios Pinitas908f6162021-05-04 10:11:09 +010035namespace arm_compute
Georgios Pinitasea857272021-01-22 05:47:37 +000036{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037CLKernelLibrary::CLKernelLibrary()
Georgios Pinitas908f6162021-05-04 10:11:09 +010038 : _compile_context()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
Anthony Barbierecb1c622018-04-17 11:45:10 +010040 opencl_is_available(); // Make sure the OpenCL symbols are initialised *before* the CLKernelLibrary is built
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041}
42
43CLKernelLibrary &CLKernelLibrary::get()
44{
45 static CLKernelLibrary _kernel_library;
46 return _kernel_library;
47}
48
Michalis Spyrou11d49182020-03-26 10:31:32 +000049Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const std::set<std::string> &build_options_set) const
50{
Georgios Pinitas908f6162021-05-04 10:11:09 +010051 const opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get();
Michalis Spyrou11d49182020-03-26 10:31:32 +000052
Georgios Pinitas908f6162021-05-04 10:11:09 +010053 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 Spyrou11d49182020-03-26 10:31:32 +000058}
59
60std::string CLKernelLibrary::get_program_name(const std::string &kernel_name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061{
Georgios Pinitas908f6162021-05-04 10:11:09 +010062 return opencl::ClKernelLibrary::get().program_name(kernel_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063}
64
Pablo Tellodb8485a2019-09-24 11:03:47 +010065void CLKernelLibrary::init(std::string kernel_path, cl::Context context, cl::Device device)
66{
Michalis Spyrou11d49182020-03-26 10:31:32 +000067 _compile_context = CLCompileContext(context, device);
Georgios Pinitas908f6162021-05-04 10:11:09 +010068 opencl::ClKernelLibrary::get().set_kernel_path(kernel_path);
Pablo Tellodb8485a2019-09-24 11:03:47 +010069}
70
71void CLKernelLibrary::set_kernel_path(const std::string &kernel_path)
72{
Georgios Pinitas908f6162021-05-04 10:11:09 +010073 opencl::ClKernelLibrary::get().set_kernel_path(kernel_path);
Pablo Tellodb8485a2019-09-24 11:03:47 +010074}
75
76cl::Context &CLKernelLibrary::context()
77{
Michalis Spyrou11d49182020-03-26 10:31:32 +000078 return _compile_context.context();
Pablo Tellodb8485a2019-09-24 11:03:47 +010079}
80
Michalis Spyrou11d49182020-03-26 10:31:32 +000081const cl::Device &CLKernelLibrary::get_device()
Pablo Tellodb8485a2019-09-24 11:03:47 +010082{
Michalis Spyrou11d49182020-03-26 10:31:32 +000083 return _compile_context.get_device();
Pablo Tellodb8485a2019-09-24 11:03:47 +010084}
85
86void CLKernelLibrary::set_device(cl::Device device)
87{
Michalis Spyrou11d49182020-03-26 10:31:32 +000088 _compile_context.set_device(device);
89}
90
91void CLKernelLibrary::set_context(cl::Context context)
92{
93 _compile_context.set_context(context);
Pablo Tellodb8485a2019-09-24 11:03:47 +010094}
95
96std::string CLKernelLibrary::get_kernel_path()
97{
Georgios Pinitas908f6162021-05-04 10:11:09 +010098 return opencl::ClKernelLibrary::get().kernel_path();
Pablo Tellodb8485a2019-09-24 11:03:47 +010099}
100
101void CLKernelLibrary::clear_programs_cache()
102{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000103 _compile_context.clear_programs_cache();
Pablo Tellodb8485a2019-09-24 11:03:47 +0100104}
105
106const std::map<std::string, cl::Program> &CLKernelLibrary::get_built_programs() const
107{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000108 return _compile_context.get_built_programs();
Pablo Tellodb8485a2019-09-24 11:03:47 +0100109}
110
giuros0146a49a02019-04-01 13:50:22 +0100111void CLKernelLibrary::add_built_program(const std::string &built_program_name, const cl::Program &program)
Anthony Barbier7da55aa2018-04-13 16:58:43 +0100112{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000113 _compile_context.add_built_program(built_program_name, program);
Anthony Barbier7da55aa2018-04-13 16:58:43 +0100114}
115
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100116bool CLKernelLibrary::fp16_supported() const
117{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000118 return _compile_context.fp16_supported();
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100119}
120
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +0100121bool CLKernelLibrary::int64_base_atomics_supported() const
122{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000123 return _compile_context.int64_base_atomics_supported();
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +0100124}
125
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000126bool CLKernelLibrary::is_wbsm_supported()
127{
128 return _compile_context.is_wbsm_supported();
129}
130
Michalis Spyrou11d49182020-03-26 10:31:32 +0000131std::pair<std::string, bool> CLKernelLibrary::get_program(const std::string &program_name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132{
Georgios Pinitas908f6162021-05-04 10:11:09 +0100133 auto program_info = opencl::ClKernelLibrary::get().program(program_name);
134 return std::make_pair(std::move(program_info.program), program_info.is_binary);
Michalis Spyroud7e82812017-06-20 15:00:14 +0100135}
steniu015f910722017-08-23 10:15:22 +0100136
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100137size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100138{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000139 return _compile_context.max_local_workgroup_size(kernel);
steniu015f910722017-08-23 10:15:22 +0100140}
141
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100142cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100143{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000144 return _compile_context.default_ndrange();
steniu015f910722017-08-23 10:15:22 +0100145}
Anthony Barbier847864d2018-03-07 11:35:53 +0000146
147std::string CLKernelLibrary::get_device_version()
148{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000149 return _compile_context.get_device_version();
Anthony Barbier847864d2018-03-07 11:35:53 +0000150}
Giorgio Arena5d42b462019-07-26 15:54:20 +0100151
152cl_uint CLKernelLibrary::get_num_compute_units()
153{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000154 return _compile_context.get_num_compute_units();
155}
156
157CLCompileContext &CLKernelLibrary::get_compile_context()
158{
159 return _compile_context;
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100160}
Georgios Pinitas908f6162021-05-04 10:11:09 +0100161} // namespace arm_compute