1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef __OPENCL_HELPER__
23 #define __OPENCL_HELPER__
24 
25 #ifdef OPENCL_SUPPORT
26 
27 #    include <opencl/legacy/cl.h>
28 
29 #    include <QLibrary>
30 #    include <QString>
31 
32 #    include <U2Core/global.h>
33 
34 #    define OPENCL_DRIVER_LIB "OpenCL"
35 
36 namespace U2 {
37 
38 class U2ALGORITHM_EXPORT OpenCLHelper {
39 public:
40     typedef CL_API_ENTRY cl_int(CL_API_CALL *clGetPlatformIDs_f)(
41         cl_uint num_entries,
42         cl_platform_id *platforms,
43         cl_uint *num_platforms) CL_API_SUFFIX__VERSION_1_1;
44 
45     typedef CL_API_ENTRY cl_int(CL_API_CALL *clGetPlatformInfo_f)(
46         cl_platform_id platform,
47         cl_platform_info param_name,
48         size_t param_value_size,
49         void *param_value,
50         size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_1;
51 
52     typedef CL_API_ENTRY cl_int(CL_API_CALL *clGetDeviceIDs_f)(
53         cl_platform_id platform,
54         cl_device_type device_type,
55         cl_uint num_entries,
56         cl_device_id *devices,
57         cl_uint *num_devices) CL_API_SUFFIX__VERSION_1_1;
58 
59     typedef CL_API_ENTRY cl_int(CL_API_CALL *clGetDeviceInfo_f)(
60         cl_device_id device,
61         cl_device_info param_name,
62         size_t param_value_size,
63         void *param_value,
64         size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_1;
65 
66     typedef CL_API_ENTRY cl_context(CL_API_CALL *clCreateContext_f)(
67         const cl_context_properties *properties,
68         cl_uint num_devices,
69         const cl_device_id *devices,
70         void(CL_CALLBACK *pfn_notify)(const char *errinfo,
71                                       const void *private_info,
72                                       size_t cb,
73                                       void *user_data),
74         void *user_data,
75         cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_1;
76 
77     typedef CL_API_ENTRY cl_mem(CL_API_CALL *clCreateBuffer_f)(
78         cl_context context,
79         cl_mem_flags flags,
80         size_t size,
81         void *host_ptr,
82         cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_1;
83 
84     typedef CL_API_ENTRY cl_program(CL_API_CALL *clCreateProgramWithSource_f)(
85         cl_context context,
86         cl_uint count,
87         const char **strings,
88         const size_t *lengths,
89         cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_1;
90 
91     typedef CL_API_ENTRY cl_int(CL_API_CALL *clGetProgramBuildInfo_f)(
92         cl_program program,
93         cl_device_id device,
94         cl_program_build_info param_name,
95         size_t param_value_size,
96         void *param_value,
97         size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_1;
98 
99     typedef CL_API_ENTRY cl_kernel(CL_API_CALL *clCreateKernel_f)(
100         cl_program program,
101         const char *kernel_name,
102         cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_1;
103 
104     typedef CL_API_ENTRY cl_int(CL_API_CALL *clSetKernelArg_f)(
105         cl_kernel kernel,
106         cl_uint arg_index,
107         size_t arg_size,
108         const void *arg_value) CL_API_SUFFIX__VERSION_1_1;
109 
110     typedef CL_API_ENTRY cl_command_queue(CL_API_CALL *clCreateCommandQueue_f)(
111         cl_context context,
112         cl_device_id device,
113         cl_command_queue_properties properties,
114         cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_1;
115 
116     typedef CL_API_ENTRY cl_int(CL_API_CALL *clEnqueueNDRangeKernel_f)(
117         cl_command_queue command_queue,
118         cl_kernel kernel,
119         cl_uint work_dim,
120         const size_t *global_work_offset,
121         const size_t *global_work_size,
122         const size_t *local_work_size,
123         cl_uint num_events_in_wait_list,
124         const cl_event *event_wait_list,
125         cl_event *event) CL_API_SUFFIX__VERSION_1_1;
126 
127     typedef CL_API_ENTRY cl_int(CL_API_CALL *clWaitForEvents_f)(
128         cl_uint num_events,
129         const cl_event *event_list) CL_API_SUFFIX__VERSION_1_1;
130 
131     typedef CL_API_ENTRY cl_int(CL_API_CALL *clEnqueueReadBuffer_f)(cl_command_queue command_queue,
132                                                                     cl_mem buffer,
133                                                                     cl_bool blocking_read,
134                                                                     size_t offset,
135                                                                     size_t cb,
136                                                                     void *ptr,
137                                                                     cl_uint num_events_in_wait_list,
138                                                                     const cl_event *event_wait_list,
139                                                                     cl_event *event) CL_API_SUFFIX__VERSION_1_1;
140 
141     typedef CL_API_ENTRY cl_int(CL_API_CALL *clFlush_f)(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_1;
142 
143     typedef CL_API_ENTRY cl_int(CL_API_CALL *clFinish_f)(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_1;
144 
145     typedef CL_API_ENTRY cl_int(CL_API_CALL *clReleaseEvent_f)(cl_event event) CL_API_SUFFIX__VERSION_1_1;
146 
147     typedef CL_API_ENTRY cl_int(CL_API_CALL *clReleaseKernel_f)(cl_kernel kernel) CL_API_SUFFIX__VERSION_1_1;
148 
149     typedef CL_API_ENTRY cl_int(CL_API_CALL *clReleaseProgram_f)(cl_program program) CL_API_SUFFIX__VERSION_1_1;
150 
151     typedef CL_API_ENTRY cl_int(CL_API_CALL *clReleaseCommandQueue_f)(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_1;
152 
153     typedef CL_API_ENTRY cl_int(CL_API_CALL *clReleaseContext_f)(cl_context context) CL_API_SUFFIX__VERSION_1_1;
154 
155     typedef CL_API_ENTRY cl_int(CL_API_CALL *clReleaseMemObject_f)(cl_mem memobj) CL_API_SUFFIX__VERSION_1_1;
156 
157     typedef CL_API_ENTRY cl_int(CL_API_CALL *clBuildProgram_f)(cl_program program,
158                                                                cl_uint num_devices,
159                                                                const cl_device_id *device_list,
160                                                                const char *options,
161                                                                void(CL_CALLBACK *pfn_notify)(cl_program program,
162                                                                                              void *user_data),
163                                                                void *user_data) CL_API_SUFFIX__VERSION_1_1;
164 
165     typedef CL_API_ENTRY cl_int(CL_API_CALL *clGetKernelWorkGroupInfo_f)(
166         cl_kernel kernel,
167         cl_device_id device,
168         cl_kernel_work_group_info param_name,
169         size_t param_value_size,
170         void *param_value,
171         size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_1;
172 
173     typedef CL_API_ENTRY cl_int(CL_API_CALL *clGetEventProfilingInfo_f)(
174         cl_event event,
175         cl_profiling_info param_name,
176         size_t param_value_size,
177         void *param_value,
178         size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_1;
179 
180     clGetPlatformIDs_f clGetPlatformIDs_p;
181     clGetPlatformInfo_f clGetPlatformInfo_p;
182     clGetDeviceIDs_f clGetDeviceIDs_p;
183     clGetDeviceInfo_f clGetDeviceInfo_p;
184 
185     clCreateContext_f clCreateContext_p;
186     clCreateBuffer_f clCreateBuffer_p;
187     clCreateProgramWithSource_f clCreateProgramWithSource_p;
188     clGetProgramBuildInfo_f clGetProgramBuildInfo_p;
189     clCreateKernel_f clCreateKernel_p;
190     clSetKernelArg_f clSetKernelArg_p;
191     clCreateCommandQueue_f clCreateCommandQueue_p;
192     clEnqueueNDRangeKernel_f clEnqueueNDRangeKernel_p;
193     clWaitForEvents_f clWaitForEvents_p;
194     clEnqueueReadBuffer_f clEnqueueReadBuffer_p;
195     clFlush_f clFlush_p;
196     clFinish_f clFinish_p;
197     clBuildProgram_f clBuildProgram_p;
198     clReleaseEvent_f clReleaseEvent_p;
199 
200     clReleaseKernel_f clReleaseKernel_p;
201     clReleaseProgram_f clReleaseProgram_p;
202     clReleaseCommandQueue_f clReleaseCommandQueue_p;
203     clReleaseContext_f clReleaseContext_p;
204     clReleaseMemObject_f clReleaseMemObject_p;
205 
206     clGetKernelWorkGroupInfo_f clGetKernelWorkGroupInfo_p;
207     clGetEventProfilingInfo_f clGetEventProfilingInfo_p;
208 
209     enum OpenCLHelperError {
210         Error_NoError,
211         Error_NoDriverLib,
212         Error_BadDriverLib,
213     };
214 
isLoaded()215     bool isLoaded() const {
216         return (status == Error_NoError);
217     }
218 
219     QString getErrorString() const;
220 
221     OpenCLHelper();
222     ~OpenCLHelper();
223 
224 private:
225     OpenCLHelper &operator=(const OpenCLHelper &s);
226     OpenCLHelper(const OpenCLHelper &);
227 
228 private:
229     OpenCLHelperError status;
230     QLibrary openclLib;
231 };
232 
233 }  // namespace U2
234 
235 #endif /* OPENCL_SUPPORT */
236 
237 #endif  //__OPENCL_HELPER__
238