blob: 527733ccf1b07aff25fada0316bd4332e0a31ef4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasea857272021-01-22 05:47:37 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLKERNELLIBRARY_H
25#define ARM_COMPUTE_CLKERNELLIBRARY_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Michalis Spyrou11d49182020-03-26 10:31:32 +000027#include "arm_compute/core/CL/CLCompileContext.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/OpenCL.h"
29
30#include <map>
31#include <set>
32#include <string>
33#include <utility>
34
35namespace arm_compute
36{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037/** CLKernelLibrary class */
Pablo Tellodb8485a2019-09-24 11:03:47 +010038class CLKernelLibrary final
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
Michalis Spyrou11d49182020-03-26 10:31:32 +000040private:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 /** Default Constructor. */
42 CLKernelLibrary();
Alex Gildayc357c472018-03-21 13:54:09 +000043 /** Prevent instances of this class from being copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 CLKernelLibrary(const CLKernelLibrary &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000045 /** Prevent instances of this class from being copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 const CLKernelLibrary &operator=(const CLKernelLibrary &) = delete;
Michalis Spyrou11d49182020-03-26 10:31:32 +000047
48public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 /** Access the KernelLibrary singleton.
Giorgio Arena5b50f422021-02-17 11:43:05 +000050 * This method has been deprecated and will be removed in future releases
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 * @return The KernelLibrary instance.
52 */
53 static CLKernelLibrary &get();
54 /** Initialises the kernel library.
55 *
Anthony Barbierb6eb3532018-08-08 13:20:04 +010056 * @param[in] kernel_path Path of the directory from which kernel sources are loaded.
57 * @param[in] context CL context used to create programs.
58 * @param[in] device CL device for which the programs are created.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010060 void init(std::string kernel_path, cl::Context context, cl::Device device);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 /** Sets the path that the kernels reside in.
62 *
63 * @param[in] kernel_path Path of the kernel.
64 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010065 void set_kernel_path(const std::string &kernel_path);
Michalis Spyroud7e82812017-06-20 15:00:14 +010066 /** Gets the path that the kernels reside in.
67 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010068 std::string get_kernel_path();
Alex Gildayc357c472018-03-21 13:54:09 +000069 /** Gets the source of the selected program.
Anthony Barbierf202e502017-11-23 18:02:04 +000070 *
71 * @param[in] program_name Program name.
Alex Gildayc357c472018-03-21 13:54:09 +000072 *
Michalis Spyrou11d49182020-03-26 10:31:32 +000073 * @return A pair with the source (false) or the binary (true), of the selected program.
Michalis Spyroud7e82812017-06-20 15:00:14 +010074 */
Michalis Spyrou11d49182020-03-26 10:31:32 +000075 std::pair<std::string, bool> get_program(const std::string &program_name) const;
Anthony Barbier4dcb5832018-05-08 11:29:05 +010076
77 /** Accessor for the associated CL context.
78 *
79 * @return A CL context.
80 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010081 cl::Context &context();
Anthony Barbier4dcb5832018-05-08 11:29:05 +010082
Giorgio Arena6200fa42018-07-06 17:06:36 +010083 /** Gets the CL device for which the programs are created. */
Michalis Spyrou11d49182020-03-26 10:31:32 +000084 const cl::Device &get_device();
Giorgio Arena6200fa42018-07-06 17:06:36 +010085
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 /** Sets the CL device for which the programs are created.
87 *
88 * @param[in] device A CL device.
89 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010090 void set_device(cl::Device device);
Anthony Barbier847864d2018-03-07 11:35:53 +000091
92 /** Return the device version
93 *
94 * @return The content of CL_DEVICE_VERSION
95 */
96 std::string get_device_version();
Giorgio Arena5d42b462019-07-26 15:54:20 +010097 /** Return the maximum number of compute units in the device
98 *
99 * @return The content of CL_DEVICE_MAX_COMPUTE_UNITS
100 */
101 cl_uint get_num_compute_units();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 /** Creates a kernel from the kernel library.
103 *
104 * @param[in] kernel_name Kernel name.
105 * @param[in] build_options_set Kernel build options as a set.
106 *
107 * @return The created kernel.
108 */
Michalis Spyrou11d49182020-03-26 10:31:32 +0000109 Kernel create_kernel(const std::string &kernel_name, const std::set<std::string> &build_options_set = {}) const;
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100110 /** Find the maximum number of local work items in a workgroup can be supported for the kernel.
steniu015f910722017-08-23 10:15:22 +0100111 *
112 */
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100113 size_t max_local_workgroup_size(const cl::Kernel &kernel) const;
114 /** Return the default NDRange for the device.
steniu015f910722017-08-23 10:15:22 +0100115 *
116 */
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100117 cl::NDRange default_ndrange() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
Anthony Barbier200b6e32018-01-16 17:21:45 +0000119 /** Clear the library's cache of binary programs
120 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100121 void clear_programs_cache();
Anthony Barbier200b6e32018-01-16 17:21:45 +0000122
Anthony Barbier48c19f12018-04-20 11:31:52 +0100123 /** Access the cache of built OpenCL programs */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100124 const std::map<std::string, cl::Program> &get_built_programs() const;
Anthony Barbier7da55aa2018-04-13 16:58:43 +0100125
126 /** Add a new built program to the cache
127 *
128 * @param[in] built_program_name Name of the program
129 * @param[in] program Built program to add to the cache
130 */
giuros0146a49a02019-04-01 13:50:22 +0100131 void add_built_program(const std::string &built_program_name, const cl::Program &program);
Anthony Barbier7da55aa2018-04-13 16:58:43 +0100132
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100133 /** Returns true if FP16 is supported by the CL device
134 *
135 * @return true if the CL device supports FP16
136 */
137 bool fp16_supported() const;
138
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +0100139 /** Returns true if int64_base_atomics extension is supported by the CL device
140 *
141 * @return true if the CL device supports int64_base_atomics extension
142 */
143 bool int64_base_atomics_supported() const;
144
Michalis Spyrou11d49182020-03-26 10:31:32 +0000145 /** Returns the program name given a kernel name
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 *
Michalis Spyrou11d49182020-03-26 10:31:32 +0000147 * @return Program name
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 */
Michalis Spyrou11d49182020-03-26 10:31:32 +0000149 std::string get_program_name(const std::string &kernel_name) const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000151 /* Returns true if the workgroup batch size modifier parameter is supported on the cl device
152 *
153 * @return true if the workgroup batch size modifier parameter is supported, false otherwise
154 */
155 bool is_wbsm_supported();
156
Michalis Spyrou11d49182020-03-26 10:31:32 +0000157 /** Sets the CL context used to create programs.
158 *
159 * @note Setting the context also resets the device to the
160 * first one available in the new context.
161 *
162 * @param[in] context A CL context.
163 */
164 void set_context(cl::Context context);
165
166 /** Gets the compile context used
167 *
168 * @return The used compile context
169 */
170 CLCompileContext &get_compile_context();
171
172private:
Georgios Pinitas908f6162021-05-04 10:11:09 +0100173 CLCompileContext _compile_context; /**< Compile Context. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174};
Georgios Pinitas7ae80a92019-10-25 18:25:17 +0100175} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000176#endif /* ARM_COMPUTE_CLKERNELLIBRARY_H */