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