180d59932Sopenharmony_ci/*
280d59932Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
380d59932Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480d59932Sopenharmony_ci * you may not use this file except in compliance with the License.
580d59932Sopenharmony_ci * You may obtain a copy of the License at
680d59932Sopenharmony_ci *
780d59932Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880d59932Sopenharmony_ci *
980d59932Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080d59932Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180d59932Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280d59932Sopenharmony_ci * See the License for the specific language governing permissions and
1380d59932Sopenharmony_ci * limitations under the License.
1480d59932Sopenharmony_ci */
1580d59932Sopenharmony_ci
1680d59932Sopenharmony_ci#ifndef OPENCL_WRAPPER_H_
1780d59932Sopenharmony_ci#define OPENCL_WRAPPER_H_
1880d59932Sopenharmony_ci
1980d59932Sopenharmony_ci#include <memory>
2080d59932Sopenharmony_ci#include <string>
2180d59932Sopenharmony_ci#define CL_TARGET_OPENCL_VERSION 300
2280d59932Sopenharmony_ci#include <CL/cl.h>
2380d59932Sopenharmony_ci
2480d59932Sopenharmony_ci#ifdef USE_OPENCL_WRAPPER
2580d59932Sopenharmony_ci#define MS_ASSERT(f) ((void)0)
2680d59932Sopenharmony_ci
2780d59932Sopenharmony_cinamespace OHOS {
2880d59932Sopenharmony_ci// This is a opencl function wrapper.
2980d59932Sopenharmony_cibool LoadOpenCLLibrary(void **handle_ptr);
3080d59932Sopenharmony_cibool UnLoadOpenCLLibrary(void *handle);
3180d59932Sopenharmony_cibool InitOpenCL();
3280d59932Sopenharmony_ci
3380d59932Sopenharmony_cibool InitOpenCLExtern(void **clSoHandle);
3480d59932Sopenharmony_cibool UnLoadCLExtern(void *clSoHandle);
3580d59932Sopenharmony_ci
3680d59932Sopenharmony_ci// get platform id
3780d59932Sopenharmony_ciusing clGetPlatformIDsFunc = cl_int (*)(cl_uint, cl_platform_id *, cl_uint *);
3880d59932Sopenharmony_ci// get platform info
3980d59932Sopenharmony_ciusing clGetPlatformInfoFunc = cl_int (*)(cl_platform_id, cl_platform_info, size_t, void *, size_t *);
4080d59932Sopenharmony_ci// build program
4180d59932Sopenharmony_ciusing clBuildProgramFunc = cl_int (*)(cl_program, cl_uint, const cl_device_id *, const char *,
4280d59932Sopenharmony_ci                                      void (*pfn_notify)(cl_program, void *), void *);
4380d59932Sopenharmony_ci// enqueue run kernel
4480d59932Sopenharmony_ciusing clEnqueueNDRangeKernelFunc = cl_int (*)(cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *,
4580d59932Sopenharmony_ci                                              const size_t *, cl_uint, const cl_event *, cl_event *);
4680d59932Sopenharmony_ci// set kernel parameter
4780d59932Sopenharmony_ciusing clSetKernelArgFunc = cl_int (*)(cl_kernel, cl_uint, size_t, const void *);
4880d59932Sopenharmony_ciusing clRetainMemObjectFunc = cl_int (*)(cl_mem);
4980d59932Sopenharmony_ciusing clReleaseMemObjectFunc = cl_int (*)(cl_mem);
5080d59932Sopenharmony_ciusing clEnqueueUnmapMemObjectFunc = cl_int (*)(cl_command_queue, cl_mem, void *, cl_uint, const cl_event *, cl_event *);
5180d59932Sopenharmony_ciusing clRetainCommandQueueFunc = cl_int (*)(cl_command_queue command_queue);
5280d59932Sopenharmony_ci// create context
5380d59932Sopenharmony_ciusing clCreateContextFunc = cl_context (*)(const cl_context_properties *, cl_uint, const cl_device_id *,
5480d59932Sopenharmony_ci                                           void(CL_CALLBACK *)(  // NOLINT(readability/casting)
5580d59932Sopenharmony_ci                                             const char *, const void *, size_t, void *),
5680d59932Sopenharmony_ci                                           void *, cl_int *);
5780d59932Sopenharmony_ciusing clEnqueueCopyImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *,
5880d59932Sopenharmony_ci                                          const size_t *, cl_uint, const cl_event *, cl_event *);
5980d59932Sopenharmony_ci
6080d59932Sopenharmony_ciusing clCreateContextFromTypeFunc = cl_context (*)(const cl_context_properties *, cl_device_type,
6180d59932Sopenharmony_ci                                                   void(CL_CALLBACK *)(  // NOLINT(readability/casting)
6280d59932Sopenharmony_ci                                                     const char *, const void *, size_t, void *),
6380d59932Sopenharmony_ci                                                   void *, cl_int *);
6480d59932Sopenharmony_ciusing clReleaseContextFunc = cl_int (*)(cl_context);
6580d59932Sopenharmony_ciusing clWaitForEventsFunc = cl_int (*)(cl_uint, const cl_event *);
6680d59932Sopenharmony_ciusing clReleaseEventFunc = cl_int (*)(cl_event);
6780d59932Sopenharmony_ciusing clEnqueueWriteBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, const void *, cl_uint,
6880d59932Sopenharmony_ci                                            const cl_event *, cl_event *);
6980d59932Sopenharmony_ciusing clEnqueueWriteImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t,
7080d59932Sopenharmony_ci                                           size_t, const void *, cl_uint, const cl_event *, cl_event *);
7180d59932Sopenharmony_ciusing clEnqueueReadImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t,
7280d59932Sopenharmony_ci                                          size_t, void *, cl_uint, const cl_event *, cl_event *);
7380d59932Sopenharmony_ciusing clEnqueueReadBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint,
7480d59932Sopenharmony_ci                                           const cl_event *, cl_event *);
7580d59932Sopenharmony_ciusing clEnqueueReadBufferRectFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *,
7680d59932Sopenharmony_ci                                               const size_t *, size_t, size_t, size_t, size_t, void *, cl_uint,
7780d59932Sopenharmony_ci                                               const cl_event *, cl_event *);
7880d59932Sopenharmony_ciusing clGetProgramBuildInfoFunc = cl_int (*)(cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *);
7980d59932Sopenharmony_ciusing clRetainProgramFunc = cl_int (*)(cl_program program);
8080d59932Sopenharmony_ciusing clEnqueueMapBufferFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, size_t, size_t, cl_uint,
8180d59932Sopenharmony_ci                                         const cl_event *, cl_event *, cl_int *);
8280d59932Sopenharmony_ciusing clEnqueueMapImageFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, const size_t *, const size_t *,
8380d59932Sopenharmony_ci                                        size_t *, size_t *, cl_uint, const cl_event *, cl_event *, cl_int *);
8480d59932Sopenharmony_ciusing clCreateCommandQueueFunc = cl_command_queue (*)(cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
8580d59932Sopenharmony_ciusing clGetCommandQueueInfoFunc = cl_int (*)(cl_command_queue, cl_command_queue_info, size_t, void *, size_t *);
8680d59932Sopenharmony_ciusing clReleaseCommandQueueFunc = cl_int (*)(cl_command_queue);
8780d59932Sopenharmony_ciusing clCreateProgramWithBinaryFunc = cl_program (*)(cl_context, cl_uint, const cl_device_id *, const size_t *,
8880d59932Sopenharmony_ci                                                     const unsigned char **, cl_int *, cl_int *);
8980d59932Sopenharmony_ciusing clRetainContextFunc = cl_int (*)(cl_context context);
9080d59932Sopenharmony_ciusing clGetContextInfoFunc = cl_int (*)(cl_context, cl_context_info, size_t, void *, size_t *);
9180d59932Sopenharmony_ciusing clReleaseProgramFunc = cl_int (*)(cl_program program);
9280d59932Sopenharmony_ciusing clFlushFunc = cl_int (*)(cl_command_queue command_queue);
9380d59932Sopenharmony_ciusing clFinishFunc = cl_int (*)(cl_command_queue command_queue);
9480d59932Sopenharmony_ciusing clGetProgramInfoFunc = cl_int (*)(cl_program, cl_program_info, size_t, void *, size_t *);
9580d59932Sopenharmony_ciusing clCreateKernelFunc = cl_kernel (*)(cl_program, const char *, cl_int *);
9680d59932Sopenharmony_ciusing clRetainKernelFunc = cl_int (*)(cl_kernel kernel);
9780d59932Sopenharmony_ciusing clCreateBufferFunc = cl_mem (*)(cl_context, cl_mem_flags, size_t, void *, cl_int *);
9880d59932Sopenharmony_ciusing clCreateImage2DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t,
9980d59932Sopenharmony_ci                                       void *, cl_int *);
10080d59932Sopenharmony_ciusing clImportMemoryARMFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, void *, ssize_t, cl_int *);
10180d59932Sopenharmony_ciusing clCreateImage3DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t,
10280d59932Sopenharmony_ci                                       size_t, size_t, void *, cl_int *);
10380d59932Sopenharmony_ciusing clCreateProgramWithSourceFunc = cl_program (*)(cl_context, cl_uint, const char **, const size_t *, cl_int *);
10480d59932Sopenharmony_ciusing clReleaseKernelFunc = cl_int (*)(cl_kernel kernel);
10580d59932Sopenharmony_ciusing clGetDeviceInfoFunc = cl_int (*)(cl_device_id, cl_device_info, size_t, void *, size_t *);
10680d59932Sopenharmony_ciusing clGetDeviceIDsFunc = cl_int (*)(cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
10780d59932Sopenharmony_ciusing clRetainEventFunc = cl_int (*)(cl_event);
10880d59932Sopenharmony_ciusing clGetKernelWorkGroupInfoFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_work_group_info, size_t, void *,
10980d59932Sopenharmony_ci                                                size_t *);
11080d59932Sopenharmony_ciusing clGetEventInfoFunc = cl_int (*)(cl_event event, cl_event_info param_name, size_t param_value_size,
11180d59932Sopenharmony_ci                                      void *param_value, size_t *param_value_size_ret);
11280d59932Sopenharmony_ciusing clGetEventProfilingInfoFunc = cl_int (*)(cl_event event, cl_profiling_info param_name, size_t param_value_size,
11380d59932Sopenharmony_ci                                               void *param_value, size_t *param_value_size_ret);
11480d59932Sopenharmony_ciusing clGetImageInfoFunc = cl_int (*)(cl_mem, cl_image_info, size_t, void *, size_t *);
11580d59932Sopenharmony_ciusing clEnqueueCopyBufferToImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, size_t, const size_t *,
11680d59932Sopenharmony_ci                                                  const size_t *, cl_uint, const cl_event *, cl_event *);
11780d59932Sopenharmony_ciusing clEnqueueCopyImageToBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *,
11880d59932Sopenharmony_ci                                                  size_t, cl_uint, const cl_event *, cl_event *);
11980d59932Sopenharmony_ci#if CL_TARGET_OPENCL_VERSION >= 120
12080d59932Sopenharmony_ciusing clRetainDeviceFunc = cl_int (*)(cl_device_id);
12180d59932Sopenharmony_ciusing clReleaseDeviceFunc = cl_int (*)(cl_device_id);
12280d59932Sopenharmony_ciusing clCreateImageFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, const cl_image_desc *, void *,
12380d59932Sopenharmony_ci                                     cl_int *);
12480d59932Sopenharmony_ciusing clEnqueueFillImageFunc = cl_int (*)(cl_command_queue, cl_mem, const void *, const size_t *, const size_t *,
12580d59932Sopenharmony_ci                                          cl_uint, const cl_event *, cl_event *);
12680d59932Sopenharmony_ci#endif
12780d59932Sopenharmony_ci#if CL_TARGET_OPENCL_VERSION >= 200
12880d59932Sopenharmony_ciusing clCreateProgramWithILFunc = cl_program (*)(cl_context, const void *, size_t, cl_int *);
12980d59932Sopenharmony_ciusing clSVMAllocFunc = void *(*)(cl_context, cl_mem_flags, size_t size, cl_uint);
13080d59932Sopenharmony_ciusing clSVMFreeFunc = void (*)(cl_context, void *);
13180d59932Sopenharmony_ciusing clEnqueueSVMMapFunc = cl_int (*)(cl_command_queue, cl_bool, cl_map_flags, void *, size_t, cl_uint,
13280d59932Sopenharmony_ci                                       const cl_event *, cl_event *);
13380d59932Sopenharmony_ciusing clEnqueueSVMUnmapFunc = cl_int (*)(cl_command_queue, void *, cl_uint, const cl_event *, cl_event *);
13480d59932Sopenharmony_ciusing clSetKernelArgSVMPointerFunc = cl_int (*)(cl_kernel, cl_uint, const void *);
13580d59932Sopenharmony_ci// opencl 2.0 can get sub group info and wave size.
13680d59932Sopenharmony_ciusing clGetKernelSubGroupInfoKHRFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_sub_group_info, size_t,
13780d59932Sopenharmony_ci                                                  const void *, size_t, void *, size_t *);
13880d59932Sopenharmony_ciusing clCreateCommandQueueWithPropertiesFunc = cl_command_queue (*)(cl_context, cl_device_id,
13980d59932Sopenharmony_ci                                                                    const cl_queue_properties *, cl_int *);
14080d59932Sopenharmony_ciusing clGetExtensionFunctionAddressFunc = void *(*)(const char *);
14180d59932Sopenharmony_ci#endif
14280d59932Sopenharmony_ci
14380d59932Sopenharmony_ci#define CL_DECLARE_FUNC_PTR(func) extern func##Func func
14480d59932Sopenharmony_ci
14580d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetPlatformIDs);
14680d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetPlatformInfo);
14780d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clBuildProgram);
14880d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueNDRangeKernel);
14980d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clSetKernelArg);
15080d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clReleaseKernel);
15180d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateProgramWithSource);
15280d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateBuffer);
15380d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateImage2D);
15480d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clImportMemoryARM);
15580d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateImage3D);
15680d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clRetainKernel);
15780d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateKernel);
15880d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetProgramInfo);
15980d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clFlush);
16080d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clFinish);
16180d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clReleaseProgram);
16280d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clRetainContext);
16380d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetContextInfo);
16480d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateProgramWithBinary);
16580d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateCommandQueue);
16680d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetCommandQueueInfo);
16780d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clReleaseCommandQueue);
16880d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueMapBuffer);
16980d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueMapImage);
17080d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueCopyImage);
17180d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clRetainProgram);
17280d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetProgramBuildInfo);
17380d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueReadBuffer);
17480d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueReadBufferRect);
17580d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueWriteBuffer);
17680d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueWriteImage);
17780d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueReadImage);
17880d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clWaitForEvents);
17980d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clReleaseEvent);
18080d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateContext);
18180d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateContextFromType);
18280d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clReleaseContext);
18380d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clRetainCommandQueue);
18480d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueUnmapMemObject);
18580d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clRetainMemObject);
18680d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clReleaseMemObject);
18780d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetDeviceInfo);
18880d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetDeviceIDs);
18980d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clRetainEvent);
19080d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetKernelWorkGroupInfo);
19180d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetEventInfo);
19280d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetEventProfilingInfo);
19380d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetImageInfo);
19480d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueCopyBufferToImage);
19580d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueCopyImageToBuffer);
19680d59932Sopenharmony_ci#if CL_TARGET_OPENCL_VERSION >= 120
19780d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clRetainDevice);
19880d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clReleaseDevice);
19980d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateImage);
20080d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueFillImage);
20180d59932Sopenharmony_ci#endif
20280d59932Sopenharmony_ci#if CL_TARGET_OPENCL_VERSION >= 200
20380d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetKernelSubGroupInfoKHR);
20480d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateCommandQueueWithProperties);
20580d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clGetExtensionFunctionAddress);
20680d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clCreateProgramWithIL);
20780d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clSVMAlloc);
20880d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clSVMFree);
20980d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueSVMMap);
21080d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clEnqueueSVMUnmap);
21180d59932Sopenharmony_ciCL_DECLARE_FUNC_PTR(clSetKernelArgSVMPointer);
21280d59932Sopenharmony_ci#endif
21380d59932Sopenharmony_ci
21480d59932Sopenharmony_ci#undef CL_DECLARE_FUNC_PTR
21580d59932Sopenharmony_ci}  // namespace OHOS
21680d59932Sopenharmony_ci#endif  // USE_OPENCL_WRAPPER
21780d59932Sopenharmony_ci#endif  // OPENCL_WRAPPER_H_
218