xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/ClKernelLibrary.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2021 Arm Limited.
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 #ifndef ARM_COMPUTE_CL_KERNEL_LIBRARY_H
25 #define ARM_COMPUTE_CL_KERNEL_LIBRARY_H
26 
27 #include <map>
28 #include <string>
29 #include <tuple>
30 
31 namespace arm_compute
32 {
33 namespace opencl
34 {
35 /** ClKernelLibrary contains all the OpenCL kernels that are used throughout the library
36  *
37  * @note Kernel library is a singleton to reduce memory requirements
38  * @note Sole responsibility is just to provide access to the kernel string,
39  *       does not perform any compilation and relevant tasks
40  */
41 class ClKernelLibrary final
42 {
43 private:
44     /** Default Constructor */
45     ClKernelLibrary() = default;
46     /** Prevent instances of this class from being copied */
47     ClKernelLibrary(const ClKernelLibrary &) = delete;
48     /** Prevent instances of this class from being copied */
49     const ClKernelLibrary &operator=(const ClKernelLibrary &) = delete;
50 
51 public:
52     /** Structure to encapsulte program related information */
53     struct ClProgramInfo
54     {
55         std::string program{};          /**< Program raw string */
56         bool        is_binary{ false }; /**< Flag that indicates if is in binary format */
57     };
58 
59 public:
60     /** Access the KernelLibrary singleton
61      *
62      * @return The KernelLibrary instance
63      */
64     static ClKernelLibrary &get();
65     /** Sets the path that the kernels reside in
66      *
67      * @param[in] kernel_path Path of the kernel
68      */
69     void set_kernel_path(std::string kernel_path);
70     /** Gets the path that the kernels reside in
71      */
72     const std::string &kernel_path() const;
73     /** Gets the source of the selected program
74      *
75      * @param[in] program_name Program name
76      *
77      * @return A pair with the source (false) or the binary (true), of the selected program
78      */
79     ClProgramInfo program(const std::string &program_name) const;
80     /** Returns the program name given a kernel name
81      *
82      * @return Program name
83      */
84     std::string program_name(const std::string &kernel_name) const;
85 
86 private:
87     std::string _kernel_path{};                                                 /**< Path to the kernels folder. */
88     mutable std::map<std::string, std::string>      _decompressed_source_map{}; /**< Map holding the decompressed files when compression is used */
89     static const std::map<std::string, std::string> _kernel_program_map;        /**< Map that associates kernel names with programs. */
90     static const std::map<std::string, std::string> _program_source_map;        /**< Contains sources for all programs.
91                                                                                      Used for compile-time kernel inclusion. >*/
92 };
93 } // namespace opencl
94 } // namespace arm_compute
95 #endif /* ARM_COMPUTE_CL_KERNEL_LIBRARY_H */
96