xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/core/CL/OpenCL.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2016-2022 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_OPENCL_H
25 #define ARM_COMPUTE_OPENCL_H
26 
27 #include <string>
28 #include <utility>
29 
30 /* Configure the Khronos C++ wrapper to target OpenCL 1.2: */
31 #ifndef ARM_COMPUTE_NO_EXCEPTIONS
32 #define CL_HPP_ENABLE_EXCEPTIONS
33 #endif // ARM_COMPUTE_NO_EXCEPTIONS
34 #define CL_TARGET_OPENCL_VERSION 300
35 #define CL_HPP_TARGET_OPENCL_VERSION 110
36 #define CL_HPP_MINIMUM_OPENCL_VERSION 110
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Weffc++"
39 #pragma GCC diagnostic ignored "-Wignored-qualifiers"
40 #pragma GCC diagnostic ignored "-Wunused-parameter"
41 #if defined(__GNUG__) && __GNUG__ >= 8
42 #pragma GCC diagnostic ignored "-Wcatch-value"
43 #endif // defined(__GNUG__) && __GNUG__ >= 8
44 #include <CL/opencl.hpp> // include new hpp header instead of cl2.hpp
45 #pragma GCC diagnostic pop
46 
47 namespace cl
48 {
49 static const NDRange Range_128_1 = NDRange(128, 1);
50 } // namespace cl
51 
52 namespace arm_compute
53 {
54 /** Check if OpenCL is available.
55  *
56  * @return True if OpenCL is available.
57  */
58 bool opencl_is_available();
59 
60 /** Class for loading OpenCL symbols. */
61 class CLSymbols final
62 {
63 public:
64     /** Default Constructor */
65     CLSymbols() noexcept(false);
66     /** Load OpenCL symbols from handle
67      *
68      * @param[in] handle Handle to load symbols from
69      */
70     void load_symbols(void *handle);
71     /** Get the static instance of CLSymbols.
72      *
73      * @return The static instance of CLSymbols.
74      */
75     static CLSymbols &get();
76     /** Load symbols from the given OpenCL library path.
77      *
78      * @param[in] library    Path to the OpenCL library.
79      * @param[in] use_loader Use symbol loader function loadOpenCLPointer.
80      *
81      * @return True if loading the library is successful.
82      */
83     bool load(const std::string &library, bool use_loader = false);
84     /** Load symbols from any of the default OpenCL library names.
85      *
86      * @return True if loading any library is successful.
87      */
88     bool load_default();
89 
90 #define DECLARE_FUNCTION_PTR(func_name) \
91     std::function<decltype(func_name)> func_name##_ptr = nullptr
92 
93     DECLARE_FUNCTION_PTR(clCreateContext);
94     DECLARE_FUNCTION_PTR(clCreateContextFromType);
95     DECLARE_FUNCTION_PTR(clCreateCommandQueue);
96     DECLARE_FUNCTION_PTR(clCreateCommandQueueWithProperties);
97     DECLARE_FUNCTION_PTR(clGetContextInfo);
98     DECLARE_FUNCTION_PTR(clBuildProgram);
99     DECLARE_FUNCTION_PTR(clEnqueueNDRangeKernel);
100     DECLARE_FUNCTION_PTR(clSetKernelArg);
101     DECLARE_FUNCTION_PTR(clReleaseKernel);
102     DECLARE_FUNCTION_PTR(clCreateProgramWithSource);
103     DECLARE_FUNCTION_PTR(clCreateBuffer);
104     DECLARE_FUNCTION_PTR(clRetainKernel);
105     DECLARE_FUNCTION_PTR(clCreateKernel);
106     DECLARE_FUNCTION_PTR(clGetProgramInfo);
107     DECLARE_FUNCTION_PTR(clFlush);
108     DECLARE_FUNCTION_PTR(clFinish);
109     DECLARE_FUNCTION_PTR(clReleaseProgram);
110     DECLARE_FUNCTION_PTR(clRetainContext);
111     DECLARE_FUNCTION_PTR(clCreateProgramWithBinary);
112     DECLARE_FUNCTION_PTR(clReleaseCommandQueue);
113     DECLARE_FUNCTION_PTR(clEnqueueMapBuffer);
114     DECLARE_FUNCTION_PTR(clRetainProgram);
115     DECLARE_FUNCTION_PTR(clGetProgramBuildInfo);
116     DECLARE_FUNCTION_PTR(clEnqueueReadBuffer);
117     DECLARE_FUNCTION_PTR(clEnqueueWriteBuffer);
118     DECLARE_FUNCTION_PTR(clReleaseEvent);
119     DECLARE_FUNCTION_PTR(clReleaseContext);
120     DECLARE_FUNCTION_PTR(clRetainCommandQueue);
121     DECLARE_FUNCTION_PTR(clEnqueueUnmapMemObject);
122     DECLARE_FUNCTION_PTR(clRetainMemObject);
123     DECLARE_FUNCTION_PTR(clReleaseMemObject);
124     DECLARE_FUNCTION_PTR(clGetDeviceInfo);
125     DECLARE_FUNCTION_PTR(clGetDeviceIDs);
126     DECLARE_FUNCTION_PTR(clGetMemObjectInfo);
127     DECLARE_FUNCTION_PTR(clRetainEvent);
128     DECLARE_FUNCTION_PTR(clGetPlatformInfo);
129     DECLARE_FUNCTION_PTR(clGetPlatformIDs);
130     DECLARE_FUNCTION_PTR(clGetKernelWorkGroupInfo);
131     DECLARE_FUNCTION_PTR(clGetCommandQueueInfo);
132     DECLARE_FUNCTION_PTR(clGetKernelInfo);
133     DECLARE_FUNCTION_PTR(clGetEventProfilingInfo);
134     DECLARE_FUNCTION_PTR(clSVMAlloc);
135     DECLARE_FUNCTION_PTR(clSVMFree);
136     DECLARE_FUNCTION_PTR(clEnqueueSVMMap);
137     DECLARE_FUNCTION_PTR(clEnqueueSVMUnmap);
138     DECLARE_FUNCTION_PTR(clEnqueueMarker);
139     DECLARE_FUNCTION_PTR(clWaitForEvents);
140     DECLARE_FUNCTION_PTR(clCreateImage);
141     DECLARE_FUNCTION_PTR(clSetKernelExecInfo);
142 
143     // Third-party extensions
144     DECLARE_FUNCTION_PTR(clImportMemoryARM);
145 
146 #undef DECLARE_FUNCTION_PTR
147 
148 private:
149     std::pair<bool, bool> _loaded;
150 };
151 } // namespace arm_compute
152 #endif /* ARM_COMPUTE_OPENCL_H */
153