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