• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OPENCL_WRAPPER_H_
17 #define OPENCL_WRAPPER_H_
18 
19 #include <memory>
20 #include <string>
21 #define CL_TARGET_OPENCL_VERSION 300
22 #include <CL/cl.h>
23 
24 #ifdef USE_OPENCL_WRAPPER
25 #define MS_ASSERT(f) ((void)0)
26 
27 namespace OHOS {
28 // This is a opencl function wrapper.
29 bool LoadOpenCLLibrary(void **handle_ptr);
30 bool UnLoadOpenCLLibrary(void *handle);
31 bool InitOpenCL();
32 
33 bool InitOpenCLExtern(void **clSoHandle);
34 bool UnLoadCLExtern(void *clSoHandle);
35 
36 // get platform id
37 using clGetPlatformIDsFunc = cl_int (*)(cl_uint, cl_platform_id *, cl_uint *);
38 // get platform info
39 using clGetPlatformInfoFunc = cl_int (*)(cl_platform_id, cl_platform_info, size_t, void *, size_t *);
40 // build program
41 using clBuildProgramFunc = cl_int (*)(cl_program, cl_uint, const cl_device_id *, const char *,
42                                       void (*pfn_notify)(cl_program, void *), void *);
43 // enqueue run kernel
44 using clEnqueueNDRangeKernelFunc = cl_int (*)(cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *,
45                                               const size_t *, cl_uint, const cl_event *, cl_event *);
46 // set kernel parameter
47 using clSetKernelArgFunc = cl_int (*)(cl_kernel, cl_uint, size_t, const void *);
48 using clRetainMemObjectFunc = cl_int (*)(cl_mem);
49 using clReleaseMemObjectFunc = cl_int (*)(cl_mem);
50 using clEnqueueUnmapMemObjectFunc = cl_int (*)(cl_command_queue, cl_mem, void *, cl_uint, const cl_event *, cl_event *);
51 using clRetainCommandQueueFunc = cl_int (*)(cl_command_queue command_queue);
52 // create context
53 using clCreateContextFunc = cl_context (*)(const cl_context_properties *, cl_uint, const cl_device_id *,
54                                            void(CL_CALLBACK *)(  // NOLINT(readability/casting)
55                                              const char *, const void *, size_t, void *),
56                                            void *, cl_int *);
57 using clEnqueueCopyImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *,
58                                           const size_t *, cl_uint, const cl_event *, cl_event *);
59 
60 using clCreateContextFromTypeFunc = cl_context (*)(const cl_context_properties *, cl_device_type,
61                                                    void(CL_CALLBACK *)(  // NOLINT(readability/casting)
62                                                      const char *, const void *, size_t, void *),
63                                                    void *, cl_int *);
64 using clReleaseContextFunc = cl_int (*)(cl_context);
65 using clWaitForEventsFunc = cl_int (*)(cl_uint, const cl_event *);
66 using clReleaseEventFunc = cl_int (*)(cl_event);
67 using clEnqueueWriteBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, const void *, cl_uint,
68                                             const cl_event *, cl_event *);
69 using clEnqueueWriteImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t,
70                                            size_t, const void *, cl_uint, const cl_event *, cl_event *);
71 using clEnqueueReadImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t,
72                                           size_t, void *, cl_uint, const cl_event *, cl_event *);
73 using clEnqueueReadBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint,
74                                            const cl_event *, cl_event *);
75 using clEnqueueReadBufferRectFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *,
76                                                const size_t *, size_t, size_t, size_t, size_t, void *, cl_uint,
77                                                const cl_event *, cl_event *);
78 using clGetProgramBuildInfoFunc = cl_int (*)(cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *);
79 using clRetainProgramFunc = cl_int (*)(cl_program program);
80 using clEnqueueMapBufferFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, size_t, size_t, cl_uint,
81                                          const cl_event *, cl_event *, cl_int *);
82 using clEnqueueMapImageFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, const size_t *, const size_t *,
83                                         size_t *, size_t *, cl_uint, const cl_event *, cl_event *, cl_int *);
84 using clCreateCommandQueueFunc = cl_command_queue (*)(cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
85 using clGetCommandQueueInfoFunc = cl_int (*)(cl_command_queue, cl_command_queue_info, size_t, void *, size_t *);
86 using clReleaseCommandQueueFunc = cl_int (*)(cl_command_queue);
87 using clCreateProgramWithBinaryFunc = cl_program (*)(cl_context, cl_uint, const cl_device_id *, const size_t *,
88                                                      const unsigned char **, cl_int *, cl_int *);
89 using clRetainContextFunc = cl_int (*)(cl_context context);
90 using clGetContextInfoFunc = cl_int (*)(cl_context, cl_context_info, size_t, void *, size_t *);
91 using clReleaseProgramFunc = cl_int (*)(cl_program program);
92 using clFlushFunc = cl_int (*)(cl_command_queue command_queue);
93 using clFinishFunc = cl_int (*)(cl_command_queue command_queue);
94 using clGetProgramInfoFunc = cl_int (*)(cl_program, cl_program_info, size_t, void *, size_t *);
95 using clCreateKernelFunc = cl_kernel (*)(cl_program, const char *, cl_int *);
96 using clRetainKernelFunc = cl_int (*)(cl_kernel kernel);
97 using clCreateBufferFunc = cl_mem (*)(cl_context, cl_mem_flags, size_t, void *, cl_int *);
98 using clCreateImage2DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t,
99                                        void *, cl_int *);
100 using clImportMemoryARMFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, void *, ssize_t, cl_int *);
101 using clCreateImage3DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t,
102                                        size_t, size_t, void *, cl_int *);
103 using clCreateProgramWithSourceFunc = cl_program (*)(cl_context, cl_uint, const char **, const size_t *, cl_int *);
104 using clReleaseKernelFunc = cl_int (*)(cl_kernel kernel);
105 using clGetDeviceInfoFunc = cl_int (*)(cl_device_id, cl_device_info, size_t, void *, size_t *);
106 using clGetDeviceIDsFunc = cl_int (*)(cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
107 using clRetainEventFunc = cl_int (*)(cl_event);
108 using clGetKernelWorkGroupInfoFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_work_group_info, size_t, void *,
109                                                 size_t *);
110 using clGetEventInfoFunc = cl_int (*)(cl_event event, cl_event_info param_name, size_t param_value_size,
111                                       void *param_value, size_t *param_value_size_ret);
112 using clGetEventProfilingInfoFunc = cl_int (*)(cl_event event, cl_profiling_info param_name, size_t param_value_size,
113                                                void *param_value, size_t *param_value_size_ret);
114 using clGetImageInfoFunc = cl_int (*)(cl_mem, cl_image_info, size_t, void *, size_t *);
115 using clEnqueueCopyBufferToImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, size_t, const size_t *,
116                                                   const size_t *, cl_uint, const cl_event *, cl_event *);
117 using clEnqueueCopyImageToBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *,
118                                                   size_t, cl_uint, const cl_event *, cl_event *);
119 #if CL_TARGET_OPENCL_VERSION >= 120
120 using clRetainDeviceFunc = cl_int (*)(cl_device_id);
121 using clReleaseDeviceFunc = cl_int (*)(cl_device_id);
122 using clCreateImageFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, const cl_image_desc *, void *,
123                                      cl_int *);
124 using clEnqueueFillImageFunc = cl_int (*)(cl_command_queue, cl_mem, const void *, const size_t *, const size_t *,
125                                           cl_uint, const cl_event *, cl_event *);
126 #endif
127 #if CL_TARGET_OPENCL_VERSION >= 200
128 using clCreateProgramWithILFunc = cl_program (*)(cl_context, const void *, size_t, cl_int *);
129 using clSVMAllocFunc = void *(*)(cl_context, cl_mem_flags, size_t size, cl_uint);
130 using clSVMFreeFunc = void (*)(cl_context, void *);
131 using clEnqueueSVMMapFunc = cl_int (*)(cl_command_queue, cl_bool, cl_map_flags, void *, size_t, cl_uint,
132                                        const cl_event *, cl_event *);
133 using clEnqueueSVMUnmapFunc = cl_int (*)(cl_command_queue, void *, cl_uint, const cl_event *, cl_event *);
134 using clSetKernelArgSVMPointerFunc = cl_int (*)(cl_kernel, cl_uint, const void *);
135 // opencl 2.0 can get sub group info and wave size.
136 using clGetKernelSubGroupInfoKHRFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_sub_group_info, size_t,
137                                                   const void *, size_t, void *, size_t *);
138 using clCreateCommandQueueWithPropertiesFunc = cl_command_queue (*)(cl_context, cl_device_id,
139                                                                     const cl_queue_properties *, cl_int *);
140 using clGetExtensionFunctionAddressFunc = void *(*)(const char *);
141 #endif
142 
143 #define CL_DECLARE_FUNC_PTR(func) extern func##Func func
144 
145 CL_DECLARE_FUNC_PTR(clGetPlatformIDs);
146 CL_DECLARE_FUNC_PTR(clGetPlatformInfo);
147 CL_DECLARE_FUNC_PTR(clBuildProgram);
148 CL_DECLARE_FUNC_PTR(clEnqueueNDRangeKernel);
149 CL_DECLARE_FUNC_PTR(clSetKernelArg);
150 CL_DECLARE_FUNC_PTR(clReleaseKernel);
151 CL_DECLARE_FUNC_PTR(clCreateProgramWithSource);
152 CL_DECLARE_FUNC_PTR(clCreateBuffer);
153 CL_DECLARE_FUNC_PTR(clCreateImage2D);
154 CL_DECLARE_FUNC_PTR(clImportMemoryARM);
155 CL_DECLARE_FUNC_PTR(clCreateImage3D);
156 CL_DECLARE_FUNC_PTR(clRetainKernel);
157 CL_DECLARE_FUNC_PTR(clCreateKernel);
158 CL_DECLARE_FUNC_PTR(clGetProgramInfo);
159 CL_DECLARE_FUNC_PTR(clFlush);
160 CL_DECLARE_FUNC_PTR(clFinish);
161 CL_DECLARE_FUNC_PTR(clReleaseProgram);
162 CL_DECLARE_FUNC_PTR(clRetainContext);
163 CL_DECLARE_FUNC_PTR(clGetContextInfo);
164 CL_DECLARE_FUNC_PTR(clCreateProgramWithBinary);
165 CL_DECLARE_FUNC_PTR(clCreateCommandQueue);
166 CL_DECLARE_FUNC_PTR(clGetCommandQueueInfo);
167 CL_DECLARE_FUNC_PTR(clReleaseCommandQueue);
168 CL_DECLARE_FUNC_PTR(clEnqueueMapBuffer);
169 CL_DECLARE_FUNC_PTR(clEnqueueMapImage);
170 CL_DECLARE_FUNC_PTR(clEnqueueCopyImage);
171 CL_DECLARE_FUNC_PTR(clRetainProgram);
172 CL_DECLARE_FUNC_PTR(clGetProgramBuildInfo);
173 CL_DECLARE_FUNC_PTR(clEnqueueReadBuffer);
174 CL_DECLARE_FUNC_PTR(clEnqueueReadBufferRect);
175 CL_DECLARE_FUNC_PTR(clEnqueueWriteBuffer);
176 CL_DECLARE_FUNC_PTR(clEnqueueWriteImage);
177 CL_DECLARE_FUNC_PTR(clEnqueueReadImage);
178 CL_DECLARE_FUNC_PTR(clWaitForEvents);
179 CL_DECLARE_FUNC_PTR(clReleaseEvent);
180 CL_DECLARE_FUNC_PTR(clCreateContext);
181 CL_DECLARE_FUNC_PTR(clCreateContextFromType);
182 CL_DECLARE_FUNC_PTR(clReleaseContext);
183 CL_DECLARE_FUNC_PTR(clRetainCommandQueue);
184 CL_DECLARE_FUNC_PTR(clEnqueueUnmapMemObject);
185 CL_DECLARE_FUNC_PTR(clRetainMemObject);
186 CL_DECLARE_FUNC_PTR(clReleaseMemObject);
187 CL_DECLARE_FUNC_PTR(clGetDeviceInfo);
188 CL_DECLARE_FUNC_PTR(clGetDeviceIDs);
189 CL_DECLARE_FUNC_PTR(clRetainEvent);
190 CL_DECLARE_FUNC_PTR(clGetKernelWorkGroupInfo);
191 CL_DECLARE_FUNC_PTR(clGetEventInfo);
192 CL_DECLARE_FUNC_PTR(clGetEventProfilingInfo);
193 CL_DECLARE_FUNC_PTR(clGetImageInfo);
194 CL_DECLARE_FUNC_PTR(clEnqueueCopyBufferToImage);
195 CL_DECLARE_FUNC_PTR(clEnqueueCopyImageToBuffer);
196 #if CL_TARGET_OPENCL_VERSION >= 120
197 CL_DECLARE_FUNC_PTR(clRetainDevice);
198 CL_DECLARE_FUNC_PTR(clReleaseDevice);
199 CL_DECLARE_FUNC_PTR(clCreateImage);
200 CL_DECLARE_FUNC_PTR(clEnqueueFillImage);
201 #endif
202 #if CL_TARGET_OPENCL_VERSION >= 200
203 CL_DECLARE_FUNC_PTR(clGetKernelSubGroupInfoKHR);
204 CL_DECLARE_FUNC_PTR(clCreateCommandQueueWithProperties);
205 CL_DECLARE_FUNC_PTR(clGetExtensionFunctionAddress);
206 CL_DECLARE_FUNC_PTR(clCreateProgramWithIL);
207 CL_DECLARE_FUNC_PTR(clSVMAlloc);
208 CL_DECLARE_FUNC_PTR(clSVMFree);
209 CL_DECLARE_FUNC_PTR(clEnqueueSVMMap);
210 CL_DECLARE_FUNC_PTR(clEnqueueSVMUnmap);
211 CL_DECLARE_FUNC_PTR(clSetKernelArgSVMPointer);
212 #endif
213 
214 #undef CL_DECLARE_FUNC_PTR
215 }  // namespace OHOS
216 #endif  // USE_OPENCL_WRAPPER
217 #endif  // OPENCL_WRAPPER_H_
218