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_RUNTIME_KERNEL_GPU_OPENCL_OPENCL_WRAPPER_H_ 18 #define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_GPU_OPENCL_OPENCL_WRAPPER_H_ 19 20 #include <memory> 21 #include <string> 22 #include <algorithm> 23 #include "CL/cl2.hpp" 24 #ifdef USE_OPENCL_WRAPPER 25 26 namespace mindspore::lite::opencl { 27 // This is a opencl function wrapper. 28 bool LoadOpenCLLibrary(void **handle_ptr); 29 bool UnLoadOpenCLLibrary(void *handle); 30 31 // get platform id 32 using clGetPlatformIDsFunc = cl_int (*)(cl_uint, cl_platform_id *, cl_uint *); 33 // get platform info 34 using clGetPlatformInfoFunc = cl_int (*)(cl_platform_id, cl_platform_info, size_t, void *, size_t *); 35 // build program 36 using clBuildProgramFunc = cl_int (*)(cl_program, cl_uint, const cl_device_id *, const char *, 37 void (*pfn_notify)(cl_program, void *), void *); 38 // enqueue run kernel 39 using clEnqueueNDRangeKernelFunc = cl_int (*)(cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *, 40 const size_t *, cl_uint, const cl_event *, cl_event *); 41 // set kernel parameter 42 using clSetKernelArgFunc = cl_int (*)(cl_kernel, cl_uint, size_t, const void *); 43 using clRetainMemObjectFunc = cl_int (*)(cl_mem); 44 using clReleaseMemObjectFunc = cl_int (*)(cl_mem); 45 using clEnqueueUnmapMemObjectFunc = cl_int (*)(cl_command_queue, cl_mem, void *, cl_uint, const cl_event *, cl_event *); 46 using clRetainCommandQueueFunc = cl_int (*)(cl_command_queue command_queue); 47 // create context 48 using clCreateContextFunc = cl_context (*)(const cl_context_properties *, cl_uint, const cl_device_id *, 49 void(CL_CALLBACK *)( // NOLINT(readability/casting) 50 const char *, const void *, size_t, void *), 51 void *, cl_int *); 52 using clEnqueueCopyImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *, 53 const size_t *, cl_uint, const cl_event *, cl_event *); 54 55 using clCreateContextFromTypeFunc = cl_context (*)(const cl_context_properties *, cl_device_type, 56 void(CL_CALLBACK *)( // NOLINT(readability/casting) 57 const char *, const void *, size_t, void *), 58 void *, cl_int *); 59 using clReleaseContextFunc = cl_int (*)(cl_context); 60 using clWaitForEventsFunc = cl_int (*)(cl_uint, const cl_event *); 61 using clReleaseEventFunc = cl_int (*)(cl_event); 62 using clEnqueueWriteBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, const void *, cl_uint, 63 const cl_event *, cl_event *); 64 using clEnqueueWriteImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t, 65 size_t, const void *, cl_uint, const cl_event *, cl_event *); 66 using clEnqueueReadImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t, 67 size_t, void *, cl_uint, const cl_event *, cl_event *); 68 using clEnqueueReadBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, 69 const cl_event *, cl_event *); 70 using clGetProgramBuildInfoFunc = cl_int (*)(cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *); 71 using clRetainProgramFunc = cl_int (*)(cl_program program); 72 using clEnqueueMapBufferFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, size_t, size_t, cl_uint, 73 const cl_event *, cl_event *, cl_int *); 74 using clEnqueueMapImageFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, const size_t *, const size_t *, 75 size_t *, size_t *, cl_uint, const cl_event *, cl_event *, cl_int *); 76 using clCreateCommandQueueFunc = cl_command_queue (*)(cl_context, cl_device_id, cl_command_queue_properties, cl_int *); 77 using clGetCommandQueueInfoFunc = cl_int (*)(cl_command_queue, cl_command_queue_info, size_t, void *, size_t *); 78 using clReleaseCommandQueueFunc = cl_int (*)(cl_command_queue); 79 using clCreateProgramWithBinaryFunc = cl_program (*)(cl_context, cl_uint, const cl_device_id *, const size_t *, 80 const unsigned char **, cl_int *, cl_int *); 81 using clRetainContextFunc = cl_int (*)(cl_context context); 82 using clGetContextInfoFunc = cl_int (*)(cl_context, cl_context_info, size_t, void *, size_t *); 83 using clReleaseProgramFunc = cl_int (*)(cl_program program); 84 using clFlushFunc = cl_int (*)(cl_command_queue command_queue); 85 using clFinishFunc = cl_int (*)(cl_command_queue command_queue); 86 using clGetProgramInfoFunc = cl_int (*)(cl_program, cl_program_info, size_t, void *, size_t *); 87 using clCreateKernelFunc = cl_kernel (*)(cl_program, const char *, cl_int *); 88 using clRetainKernelFunc = cl_int (*)(cl_kernel kernel); 89 using clCreateBufferFunc = cl_mem (*)(cl_context, cl_mem_flags, size_t, void *, cl_int *); 90 using clCreateImage2DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t, 91 void *, cl_int *); 92 using clImportMemoryARMFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, void *, ssize_t, cl_int *); 93 using clCreateImage3DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t, 94 size_t, size_t, void *, cl_int *); 95 using clCreateProgramWithSourceFunc = cl_program (*)(cl_context, cl_uint, const char **, const size_t *, cl_int *); 96 using clReleaseKernelFunc = cl_int (*)(cl_kernel kernel); 97 using clGetDeviceInfoFunc = cl_int (*)(cl_device_id, cl_device_info, size_t, void *, size_t *); 98 using clGetDeviceIDsFunc = cl_int (*)(cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *); 99 using clRetainEventFunc = cl_int (*)(cl_event); 100 using clGetKernelWorkGroupInfoFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_work_group_info, size_t, void *, 101 size_t *); 102 using clGetEventInfoFunc = cl_int (*)(cl_event event, cl_event_info param_name, size_t param_value_size, 103 void *param_value, size_t *param_value_size_ret); 104 using clGetEventProfilingInfoFunc = cl_int (*)(cl_event event, cl_profiling_info param_name, size_t param_value_size, 105 void *param_value, size_t *param_value_size_ret); 106 using clGetImageInfoFunc = cl_int (*)(cl_mem, cl_image_info, size_t, void *, size_t *); 107 using clEnqueueCopyBufferToImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, size_t, const size_t *, 108 const size_t *, cl_uint, const cl_event *, cl_event *); 109 using clEnqueueCopyImageToBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *, 110 size_t, cl_uint, const cl_event *, cl_event *); 111 using clGetGLContextInfoKHRFunc = cl_int (*)(const cl_context_properties *, cl_gl_context_info, size_t, void *, 112 size_t *); 113 114 #if CL_TARGET_OPENCL_VERSION >= 120 115 using clRetainDeviceFunc = cl_int (*)(cl_device_id); 116 using clReleaseDeviceFunc = cl_int (*)(cl_device_id); 117 using clCreateImageFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, const cl_image_desc *, void *, 118 cl_int *); 119 using clEnqueueFillImageFunc = cl_int (*)(cl_command_queue, cl_mem, const void *, const size_t *, const size_t *, 120 cl_uint, const cl_event *, cl_event *); 121 using clCreateFromGLTextureFunc = cl_mem (*)(cl_context context, cl_mem_flags flags, cl_GLenum target, 122 cl_GLint miplevel, cl_GLuint texture, cl_int *errcode_ret); 123 #endif 124 #if CL_TARGET_OPENCL_VERSION >= 200 125 using clCreateProgramWithILFunc = cl_program (*)(cl_context, const void *, size_t, cl_int *); 126 using clSVMAllocFunc = void *(*)(cl_context, cl_mem_flags, size_t size, cl_uint); 127 using clSVMFreeFunc = void (*)(cl_context, void *); 128 using clEnqueueSVMMapFunc = cl_int (*)(cl_command_queue, cl_bool, cl_map_flags, void *, size_t, cl_uint, 129 const cl_event *, cl_event *); 130 using clEnqueueSVMUnmapFunc = cl_int (*)(cl_command_queue, void *, cl_uint, const cl_event *, cl_event *); 131 using clSetKernelArgSVMPointerFunc = cl_int (*)(cl_kernel, cl_uint, const void *); 132 // opencl 2.0 can get sub group info and wave size. 133 using clGetKernelSubGroupInfoKHRFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_sub_group_info, size_t, 134 const void *, size_t, void *, size_t *); 135 using clCreateCommandQueueWithPropertiesFunc = cl_command_queue (*)(cl_context, cl_device_id, 136 const cl_queue_properties *, cl_int *); 137 using clGetExtensionFunctionAddressFunc = void *(*)(const char *); 138 #endif 139 140 #define CL_DECLARE_FUNC_PTR(func) extern func##Func func 141 142 CL_DECLARE_FUNC_PTR(clGetPlatformIDs); 143 CL_DECLARE_FUNC_PTR(clGetPlatformInfo); 144 CL_DECLARE_FUNC_PTR(clBuildProgram); 145 CL_DECLARE_FUNC_PTR(clEnqueueNDRangeKernel); 146 CL_DECLARE_FUNC_PTR(clSetKernelArg); 147 CL_DECLARE_FUNC_PTR(clReleaseKernel); 148 CL_DECLARE_FUNC_PTR(clCreateProgramWithSource); 149 CL_DECLARE_FUNC_PTR(clCreateBuffer); 150 CL_DECLARE_FUNC_PTR(clCreateImage2D); 151 CL_DECLARE_FUNC_PTR(clImportMemoryARM); 152 CL_DECLARE_FUNC_PTR(clCreateImage3D); 153 CL_DECLARE_FUNC_PTR(clRetainKernel); 154 CL_DECLARE_FUNC_PTR(clCreateKernel); 155 CL_DECLARE_FUNC_PTR(clGetProgramInfo); 156 CL_DECLARE_FUNC_PTR(clFlush); 157 CL_DECLARE_FUNC_PTR(clFinish); 158 CL_DECLARE_FUNC_PTR(clReleaseProgram); 159 CL_DECLARE_FUNC_PTR(clRetainContext); 160 CL_DECLARE_FUNC_PTR(clGetContextInfo); 161 CL_DECLARE_FUNC_PTR(clCreateProgramWithBinary); 162 CL_DECLARE_FUNC_PTR(clCreateCommandQueue); 163 CL_DECLARE_FUNC_PTR(clGetCommandQueueInfo); 164 CL_DECLARE_FUNC_PTR(clReleaseCommandQueue); 165 CL_DECLARE_FUNC_PTR(clEnqueueMapBuffer); 166 CL_DECLARE_FUNC_PTR(clEnqueueMapImage); 167 CL_DECLARE_FUNC_PTR(clEnqueueCopyImage); 168 CL_DECLARE_FUNC_PTR(clRetainProgram); 169 CL_DECLARE_FUNC_PTR(clGetProgramBuildInfo); 170 CL_DECLARE_FUNC_PTR(clEnqueueReadBuffer); 171 CL_DECLARE_FUNC_PTR(clEnqueueWriteBuffer); 172 CL_DECLARE_FUNC_PTR(clEnqueueWriteImage); 173 CL_DECLARE_FUNC_PTR(clEnqueueReadImage); 174 CL_DECLARE_FUNC_PTR(clWaitForEvents); 175 CL_DECLARE_FUNC_PTR(clReleaseEvent); 176 CL_DECLARE_FUNC_PTR(clCreateContext); 177 CL_DECLARE_FUNC_PTR(clCreateContextFromType); 178 CL_DECLARE_FUNC_PTR(clReleaseContext); 179 CL_DECLARE_FUNC_PTR(clRetainCommandQueue); 180 CL_DECLARE_FUNC_PTR(clEnqueueUnmapMemObject); 181 CL_DECLARE_FUNC_PTR(clRetainMemObject); 182 CL_DECLARE_FUNC_PTR(clReleaseMemObject); 183 CL_DECLARE_FUNC_PTR(clGetDeviceInfo); 184 CL_DECLARE_FUNC_PTR(clGetDeviceIDs); 185 CL_DECLARE_FUNC_PTR(clRetainEvent); 186 CL_DECLARE_FUNC_PTR(clGetKernelWorkGroupInfo); 187 CL_DECLARE_FUNC_PTR(clGetEventInfo); 188 CL_DECLARE_FUNC_PTR(clGetEventProfilingInfo); 189 CL_DECLARE_FUNC_PTR(clGetImageInfo); 190 CL_DECLARE_FUNC_PTR(clEnqueueCopyBufferToImage); 191 CL_DECLARE_FUNC_PTR(clEnqueueCopyImageToBuffer); 192 CL_DECLARE_FUNC_PTR(clGetGLContextInfoKHR); 193 #if CL_TARGET_OPENCL_VERSION >= 120 194 CL_DECLARE_FUNC_PTR(clRetainDevice); 195 CL_DECLARE_FUNC_PTR(clReleaseDevice); 196 CL_DECLARE_FUNC_PTR(clCreateImage); 197 CL_DECLARE_FUNC_PTR(clEnqueueFillImage); 198 CL_DECLARE_FUNC_PTR(clCreateFromGLTexture); 199 #endif 200 #if CL_TARGET_OPENCL_VERSION >= 200 201 CL_DECLARE_FUNC_PTR(clGetKernelSubGroupInfoKHR); 202 CL_DECLARE_FUNC_PTR(clCreateCommandQueueWithProperties); 203 CL_DECLARE_FUNC_PTR(clGetExtensionFunctionAddress); 204 CL_DECLARE_FUNC_PTR(clCreateProgramWithIL); 205 CL_DECLARE_FUNC_PTR(clSVMAlloc); 206 CL_DECLARE_FUNC_PTR(clSVMFree); 207 CL_DECLARE_FUNC_PTR(clEnqueueSVMMap); 208 CL_DECLARE_FUNC_PTR(clEnqueueSVMUnmap); 209 CL_DECLARE_FUNC_PTR(clSetKernelArgSVMPointer); 210 #endif 211 212 #undef CL_DECLARE_FUNC_PTR 213 } // namespace mindspore::lite::opencl 214 #endif // USE_OPENCL_WRAPPER 215 #endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_GPU_OPENCL_OPENCL_WRAPPER_H_ 216