blob: c5a0796c3aa316e147eb693c817b950eb5c98087 [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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025#include "arm_compute/core/Error.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010026#include "src/gpu/cl/ClKernelLibrary.h"
steniu015f910722017-08-23 10:15:22 +010027#include <algorithm>
Georgios Pinitasea857272021-01-22 05:47:37 +000028#include <array>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include <fstream>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include <utility>
31#include <vector>
Georgios Pinitas908f6162021-05-04 10:11:09 +010032namespace arm_compute
Georgios Pinitasea857272021-01-22 05:47:37 +000033{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034CLKernelLibrary::CLKernelLibrary()
Georgios Pinitas908f6162021-05-04 10:11:09 +010035 : _compile_context()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036{
Anthony Barbierecb1c622018-04-17 11:45:10 +010037 opencl_is_available(); // Make sure the OpenCL symbols are initialised *before* the CLKernelLibrary is built
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039CLKernelLibrary &CLKernelLibrary::get()
40{
41 static CLKernelLibrary _kernel_library;
42 return _kernel_library;
43}
Michalis Spyrou11d49182020-03-26 10:31:32 +000044Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const std::set<std::string> &build_options_set) const
45{
Gian Marco Iodice8155c022021-04-16 15:08:59 +010046 const opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get();
47 const std::string program_name = klib.program_name(kernel_name);
48 auto program = klib.program(program_name);
49 const std::string &kernel_path = CLKernelLibrary::get().get_kernel_path();
Georgios Pinitas908f6162021-05-04 10:11:09 +010050 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 +000051}
Michalis Spyrou11d49182020-03-26 10:31:32 +000052std::string CLKernelLibrary::get_program_name(const std::string &kernel_name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053{
Georgios Pinitas908f6162021-05-04 10:11:09 +010054 return opencl::ClKernelLibrary::get().program_name(kernel_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055}
Pablo Tellodb8485a2019-09-24 11:03:47 +010056void CLKernelLibrary::init(std::string kernel_path, cl::Context context, cl::Device device)
57{
Michalis Spyrou11d49182020-03-26 10:31:32 +000058 _compile_context = CLCompileContext(context, device);
Georgios Pinitas908f6162021-05-04 10:11:09 +010059 opencl::ClKernelLibrary::get().set_kernel_path(kernel_path);
Pablo Tellodb8485a2019-09-24 11:03:47 +010060}
Pablo Tellodb8485a2019-09-24 11:03:47 +010061void CLKernelLibrary::set_kernel_path(const std::string &kernel_path)
62{
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 +010065cl::Context &CLKernelLibrary::context()
66{
Michalis Spyrou11d49182020-03-26 10:31:32 +000067 return _compile_context.context();
Pablo Tellodb8485a2019-09-24 11:03:47 +010068}
Michalis Spyrou11d49182020-03-26 10:31:32 +000069const cl::Device &CLKernelLibrary::get_device()
Pablo Tellodb8485a2019-09-24 11:03:47 +010070{
Michalis Spyrou11d49182020-03-26 10:31:32 +000071 return _compile_context.get_device();
Pablo Tellodb8485a2019-09-24 11:03:47 +010072}
Pablo Tellodb8485a2019-09-24 11:03:47 +010073void CLKernelLibrary::set_device(cl::Device device)
74{
Michalis Spyrou11d49182020-03-26 10:31:32 +000075 _compile_context.set_device(device);
76}
Michalis Spyrou11d49182020-03-26 10:31:32 +000077void CLKernelLibrary::set_context(cl::Context context)
78{
79 _compile_context.set_context(context);
Pablo Tellodb8485a2019-09-24 11:03:47 +010080}
Pablo Tellodb8485a2019-09-24 11:03:47 +010081std::string CLKernelLibrary::get_kernel_path()
82{
Georgios Pinitas908f6162021-05-04 10:11:09 +010083 return opencl::ClKernelLibrary::get().kernel_path();
Pablo Tellodb8485a2019-09-24 11:03:47 +010084}
Pablo Tellodb8485a2019-09-24 11:03:47 +010085void CLKernelLibrary::clear_programs_cache()
86{
Michalis Spyrou11d49182020-03-26 10:31:32 +000087 _compile_context.clear_programs_cache();
Pablo Tellodb8485a2019-09-24 11:03:47 +010088}
Pablo Tellodb8485a2019-09-24 11:03:47 +010089const std::map<std::string, cl::Program> &CLKernelLibrary::get_built_programs() const
90{
Michalis Spyrou11d49182020-03-26 10:31:32 +000091 return _compile_context.get_built_programs();
Pablo Tellodb8485a2019-09-24 11:03:47 +010092}
giuros0146a49a02019-04-01 13:50:22 +010093void CLKernelLibrary::add_built_program(const std::string &built_program_name, const cl::Program &program)
Anthony Barbier7da55aa2018-04-13 16:58:43 +010094{
Michalis Spyrou11d49182020-03-26 10:31:32 +000095 _compile_context.add_built_program(built_program_name, program);
Anthony Barbier7da55aa2018-04-13 16:58:43 +010096}
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010097bool CLKernelLibrary::fp16_supported() const
98{
Michalis Spyrou11d49182020-03-26 10:31:32 +000099 return _compile_context.fp16_supported();
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100100}
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +0100101bool CLKernelLibrary::int64_base_atomics_supported() const
102{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000103 return _compile_context.int64_base_atomics_supported();
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +0100104}
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000105bool CLKernelLibrary::is_wbsm_supported()
106{
107 return _compile_context.is_wbsm_supported();
108}
Michalis Spyrou11d49182020-03-26 10:31:32 +0000109std::pair<std::string, bool> CLKernelLibrary::get_program(const std::string &program_name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110{
Georgios Pinitas908f6162021-05-04 10:11:09 +0100111 auto program_info = opencl::ClKernelLibrary::get().program(program_name);
112 return std::make_pair(std::move(program_info.program), program_info.is_binary);
Michalis Spyroud7e82812017-06-20 15:00:14 +0100113}
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100114size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100115{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000116 return _compile_context.max_local_workgroup_size(kernel);
steniu015f910722017-08-23 10:15:22 +0100117}
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100118cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100119{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000120 return _compile_context.default_ndrange();
steniu015f910722017-08-23 10:15:22 +0100121}
Anthony Barbier847864d2018-03-07 11:35:53 +0000122std::string CLKernelLibrary::get_device_version()
123{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000124 return _compile_context.get_device_version();
Anthony Barbier847864d2018-03-07 11:35:53 +0000125}
Giorgio Arena5d42b462019-07-26 15:54:20 +0100126cl_uint CLKernelLibrary::get_num_compute_units()
127{
Michalis Spyrou11d49182020-03-26 10:31:32 +0000128 return _compile_context.get_num_compute_units();
129}
Michalis Spyrou11d49182020-03-26 10:31:32 +0000130CLCompileContext &CLKernelLibrary::get_compile_context()
131{
132 return _compile_context;
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100133}
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100134} // namespace arm_compute