MagickCore  6.9.7
opencl-private.h
Go to the documentation of this file.
1 /*
2 Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4 
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
7 
8 http://www.imagemagick.org/script/license.php
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 MagickCore OpenCL private methods.
17 */
18 #ifndef MAGICKCORE_OPENCL_PRIVATE_H
19 #define MAGICKCORE_OPENCL_PRIVATE_H
20 
21 /*
22 Include declarations.
23 */
24 #include "magick/studio.h"
25 #include "magick/opencl.h"
26 
27 #if defined(__cplusplus) || defined(c_plusplus)
28 extern "C" {
29 #endif
30 
31 #if !defined(MAGICKCORE_OPENCL_SUPPORT)
32  typedef void* cl_event;
33  typedef void* cl_mem;
34  typedef void* cl_uint;
35 #else
36 
37 #define MAX_COMMAND_QUEUES 16
38 
39 /*
40  *
41  * function pointer typedefs
42  *
43  */
44 
45 /* Platform APIs */
46 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clGetPlatformIDs)(
47  cl_uint num_entries,
48  cl_platform_id * platforms,
49  cl_uint * num_platforms) CL_API_SUFFIX__VERSION_1_0;
50 
51 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clGetPlatformInfo)(
52  cl_platform_id platform,
53  cl_platform_info param_name,
54  size_t param_value_size,
55  void * param_value,
56  size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
57 
58 /* Device APIs */
59 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clGetDeviceIDs)(
60  cl_platform_id platform,
61  cl_device_type device_type,
62  cl_uint num_entries,
63  cl_device_id * devices,
64  cl_uint * num_devices) CL_API_SUFFIX__VERSION_1_0;
65 
66 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clGetDeviceInfo)(
67  cl_device_id device,
68  cl_device_info param_name,
69  size_t param_value_size,
70  void * param_value,
71  size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
72 
73 /* Context APIs */
74 typedef CL_API_ENTRY cl_context (CL_API_CALL *MAGICKpfn_clCreateContext)(
75  const cl_context_properties * properties,
76  cl_uint num_devices,
77  const cl_device_id * devices,
78  void (CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *),
79  void * user_data,
80  cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0;
81 
82 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clReleaseContext)(
83  cl_context context) CL_API_SUFFIX__VERSION_1_0;
84 
85 /* Command Queue APIs */
86 typedef CL_API_ENTRY cl_command_queue (CL_API_CALL *MAGICKpfn_clCreateCommandQueue)(
87  cl_context context,
88  cl_device_id device,
89  cl_command_queue_properties properties,
90  cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0;
91 
92 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clReleaseCommandQueue)(
93  cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0;
94 
95 /* Memory Object APIs */
96 typedef CL_API_ENTRY cl_mem (CL_API_CALL *MAGICKpfn_clCreateBuffer)(
97  cl_context context,
98  cl_mem_flags flags,
99  size_t size,
100  void * host_ptr,
101  cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0;
102 
103 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clReleaseMemObject)(cl_mem memobj) CL_API_SUFFIX__VERSION_1_0;
104 
105 /* Program Object APIs */
106 typedef CL_API_ENTRY cl_program (CL_API_CALL *MAGICKpfn_clCreateProgramWithSource)(
107  cl_context context,
108  cl_uint count,
109  const char ** strings,
110  const size_t * lengths,
111  cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0;
112 
113 typedef CL_API_ENTRY cl_program (CL_API_CALL *MAGICKpfn_clCreateProgramWithBinary)(
114  cl_context context,
115  cl_uint num_devices,
116  const cl_device_id * device_list,
117  const size_t * lengths,
118  const unsigned char ** binaries,
119  cl_int * binary_status,
120  cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0;
121 
122 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clReleaseProgram)(cl_program program) CL_API_SUFFIX__VERSION_1_0;
123 
124 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clBuildProgram)(
125  cl_program program,
126  cl_uint num_devices,
127  const cl_device_id * device_list,
128  const char * options,
129  void (CL_CALLBACK *pfn_notify)(cl_program program, void * user_data),
130  void * user_data) CL_API_SUFFIX__VERSION_1_0;
131 
132 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clGetProgramInfo)(
133  cl_program program,
134  cl_program_info param_name,
135  size_t param_value_size,
136  void * param_value,
137  size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
138 
139 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clGetProgramBuildInfo)(
140  cl_program program,
141  cl_device_id device,
142  cl_program_build_info param_name,
143  size_t param_value_size,
144  void * param_value,
145  size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
146 
147 /* Kernel Object APIs */
148 typedef CL_API_ENTRY cl_kernel (CL_API_CALL *MAGICKpfn_clCreateKernel)(
149  cl_program program,
150  const char * kernel_name,
151  cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0;
152 
153 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clReleaseKernel)(cl_kernel kernel) CL_API_SUFFIX__VERSION_1_0;
154 
155 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clSetKernelArg)(
156  cl_kernel kernel,
157  cl_uint arg_index,
158  size_t arg_size,
159  const void * arg_value) CL_API_SUFFIX__VERSION_1_0;
160 
161 typedef CL_API_ENTRY cl_int
162  (CL_API_CALL *MAGICKpfn_clFlush)(cl_command_queue command_queue)
163  CL_API_SUFFIX__VERSION_1_0;
164 
165 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clFinish)(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0;
166 
167 /* Enqueued Commands APIs */
168 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clEnqueueReadBuffer)(
169  cl_command_queue command_queue,
170  cl_mem buffer,
171  cl_bool blocking_read,
172  size_t offset,
173  size_t cb,
174  void * ptr,
175  cl_uint num_events_in_wait_list,
176  const cl_event * event_wait_list,
177  cl_event * event) CL_API_SUFFIX__VERSION_1_0;
178 
179 typedef CL_API_ENTRY void * (CL_API_CALL *MAGICKpfn_clEnqueueMapBuffer)(
180  cl_command_queue command_queue,
181  cl_mem buffer,
182  cl_bool blocking_map,
183  cl_map_flags map_flags,
184  size_t offset,
185  size_t cb,
186  cl_uint num_events_in_wait_list,
187  const cl_event * event_wait_list,
188  cl_event * event,
189  cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0;
190 
191 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clEnqueueUnmapMemObject)(
192  cl_command_queue command_queue,
193  cl_mem memobj,
194  void * mapped_ptr,
195  cl_uint num_events_in_wait_list,
196  const cl_event * event_wait_list,
197  cl_event * event) CL_API_SUFFIX__VERSION_1_0;
198 
199 typedef CL_API_ENTRY cl_int (CL_API_CALL *MAGICKpfn_clEnqueueNDRangeKernel)(
200  cl_command_queue command_queue,
201  cl_kernel kernel,
202  cl_uint work_dim,
203  const size_t * global_work_offset,
204  const size_t * global_work_size,
205  const size_t * local_work_size,
206  cl_uint num_events_in_wait_list,
207  const cl_event * event_wait_list,
208  cl_event * event) CL_API_SUFFIX__VERSION_1_0;
209 
210 typedef CL_API_ENTRY cl_int(CL_API_CALL *MAGICKpfn_clGetEventProfilingInfo)(
211  cl_event event,
212  cl_profiling_info param_name,
213  size_t param_value_size,
214  void *param_value,
215  size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
216 
217 typedef CL_API_ENTRY cl_int(CL_API_CALL *MAGICKpfn_clWaitForEvents)(
218  cl_uint num_events,
219  const cl_event *event_list) CL_API_SUFFIX__VERSION_1_0;
220 
221 typedef CL_API_ENTRY cl_int(CL_API_CALL *MAGICKpfn_clReleaseEvent)(
222  cl_event event) CL_API_SUFFIX__VERSION_1_0;
223 
224 typedef CL_API_ENTRY cl_int(CL_API_CALL *MAGICKpfn_clRetainEvent)(
225  cl_event event) CL_API_SUFFIX__VERSION_1_0;
226 
227 typedef CL_API_ENTRY cl_int(CL_API_CALL *MAGICKpfn_clSetEventCallback)(
228  cl_event event,cl_int command_exec_callback_type,
229  void (CL_CALLBACK *MAGICKpfn_notify)(cl_event,cl_int,void *),
230  void *user_data) CL_API_SUFFIX__VERSION_1_1;
231 
232 /*
233  *
234  * vendor dispatch table structure
235  *
236  * note that the types in the structure KHRicdVendorDispatch mirror the function
237  * names listed in the string table khrIcdVendorDispatchFunctionNames
238  *
239  */
240 
241 typedef struct MagickLibraryRec MagickLibrary;
242 
243 struct MagickLibraryRec
244 {
245  void * base;
246 
247  MAGICKpfn_clGetPlatformIDs clGetPlatformIDs;
248  MAGICKpfn_clGetPlatformInfo clGetPlatformInfo;
249 
250  MAGICKpfn_clGetDeviceIDs clGetDeviceIDs;
251  MAGICKpfn_clGetDeviceInfo clGetDeviceInfo;
252 
253  MAGICKpfn_clCreateContext clCreateContext;
254  MAGICKpfn_clReleaseContext clReleaseContext;
255 
256  MAGICKpfn_clCreateCommandQueue clCreateCommandQueue;
257  MAGICKpfn_clReleaseCommandQueue clReleaseCommandQueue;
258  MAGICKpfn_clFlush clFlush;
259  MAGICKpfn_clFinish clFinish;
260 
261  MAGICKpfn_clCreateBuffer clCreateBuffer;
262  MAGICKpfn_clReleaseMemObject clReleaseMemObject;
263  MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
264  MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
265  MAGICKpfn_clReleaseProgram clReleaseProgram;
266  MAGICKpfn_clBuildProgram clBuildProgram;
267  MAGICKpfn_clGetProgramInfo clGetProgramInfo;
268  MAGICKpfn_clGetProgramBuildInfo clGetProgramBuildInfo;
269 
270  MAGICKpfn_clCreateKernel clCreateKernel;
271  MAGICKpfn_clReleaseKernel clReleaseKernel;
272  MAGICKpfn_clSetKernelArg clSetKernelArg;
273 
274  MAGICKpfn_clEnqueueReadBuffer clEnqueueReadBuffer;
275  MAGICKpfn_clEnqueueMapBuffer clEnqueueMapBuffer;
276  MAGICKpfn_clEnqueueUnmapMemObject clEnqueueUnmapMemObject;
277  MAGICKpfn_clEnqueueNDRangeKernel clEnqueueNDRangeKernel;
278 
279  MAGICKpfn_clGetEventProfilingInfo clGetEventProfilingInfo;
280 
281  MAGICKpfn_clWaitForEvents clWaitForEvents;
282  MAGICKpfn_clReleaseEvent clReleaseEvent;
283  MAGICKpfn_clRetainEvent clRetainEvent;
284  MAGICKpfn_clSetEventCallback clSetEventCallback;
285 };
286 
287 struct _MagickCLEnv {
288  MagickBooleanType OpenCLInitialized; /* whether OpenCL environment is initialized. */
289  MagickBooleanType OpenCLDisabled; /* whether if OpenCL has been explicitely disabled. */
290 
291  MagickLibrary * library;
292 
293  /*OpenCL objects */
294  cl_platform_id platform;
295  cl_device_type deviceType;
296  cl_device_id device;
297  cl_context context;
298 
299  MagickBooleanType disableProgramCache; /* disable the OpenCL program cache */
300  cl_program programs[MAGICK_OPENCL_NUM_PROGRAMS]; /* one program object maps one kernel source file */
301 
302  MagickBooleanType regenerateProfile; /* re-run the microbenchmark in auto device selection mode */
303 
304  SemaphoreInfo* lock;
305 
306  cl_command_queue commandQueues[MAX_COMMAND_QUEUES];
307  ssize_t commandQueuesPos;
308  SemaphoreInfo* commandQueuesLock;
309 };
310 
311 
312 #if defined(MAGICKCORE_HDRI_SUPPORT)
313 #define CLOptions "-cl-single-precision-constant -cl-mad-enable -DMAGICKCORE_HDRI_SUPPORT=1 "\
314  "-DCLQuantum=float -DCLSignedQuantum=float -DCLPixelType=float4 -DQuantumRange=%f " \
315  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
316  " -DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
317 #define CLPixelPacket cl_float4
318 #define CLCharQuantumScale 1.0f
319 #elif (MAGICKCORE_QUANTUM_DEPTH == 8)
320 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
321  "-DCLQuantum=uchar -DCLSignedQuantum=char -DCLPixelType=uchar4 -DQuantumRange=%ff " \
322  "-DQuantumScale=%ff -DCharQuantumScale=%ff -DMagickEpsilon=%ff -DMagickPI=%ff "\
323  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
324 #define CLPixelPacket cl_uchar4
325 #define CLCharQuantumScale 1.0f
326 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
327 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
328  "-DCLQuantum=ushort -DCLSignedQuantum=short -DCLPixelType=ushort4 -DQuantumRange=%f "\
329  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
330  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
331 #define CLPixelPacket cl_ushort4
332 #define CLCharQuantumScale 257.0f
333 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
334 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
335  "-DCLQuantum=uint -DCLSignedQuantum=int -DCLPixelType=uint4 -DQuantumRange=%f "\
336  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
337  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
338 #define CLPixelPacket cl_uint4
339 #define CLCharQuantumScale 16843009.0f
340 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
341 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
342  "-DCLQuantum=ulong -DCLSignedQuantum=long -DCLPixelType=ulong4 -DQuantumRange=%f "\
343  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
344  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
345 #define CLPixelPacket cl_ulong4
346 #define CLCharQuantumScale 72340172838076673.0f
347 #endif
348 
349 typedef enum {
350  AddNoiseKernel,
351  BlurRowKernel,
352  BlurColumnKernel,
353  CompositeKernel,
354  ComputeFunctionKernel,
355  ContrastKernel,
356  ContrastStretchKernel,
357  ConvolveKernel,
358  EqualizeKernel,
359  GrayScaleKernel,
360  HistogramKernel,
361  HullPass1Kernel,
362  HullPass2Kernel,
363  LocalContrastBlurRowKernel,
364  LocalContrastBlurApplyColumnKernel,
365  ModulateKernel,
366  MotionBlurKernel,
367  RadialBlurKernel,
368  RandomNumberGeneratorKernel,
369  ResizeHorizontalKernel,
370  ResizeVerticalKernel,
371  UnsharpMaskBlurColumnKernel,
372  UnsharpMaskKernel,
373  WaveletDenoiseKernel,
374  KERNEL_COUNT
375 } ProfiledKernels;
376 
377 extern MagickPrivate cl_context
378  GetOpenCLContext(MagickCLEnv);
379 
380 extern MagickPrivate cl_kernel
381  AcquireOpenCLKernel(MagickCLEnv, MagickOpenCLProgram, const char*);
382 
383 extern MagickPrivate cl_command_queue
384  AcquireOpenCLCommandQueue(MagickCLEnv);
385 
387  OpenCLThrowMagickException(ExceptionInfo *,
388  const char *,const char *,const size_t,
389  const ExceptionType,const char *,const char *,...),
390  RecordProfileData(MagickCLEnv,ProfiledKernels,cl_event),
391  RelinquishMagickOpenCLEnv(MagickCLEnv),
392  RelinquishOpenCLCommandQueue(MagickCLEnv, cl_command_queue),
393  RelinquishOpenCLKernel(MagickCLEnv, cl_kernel);
394 
396  AcquireMagickOpenCLEnv(),
397  SetDefaultOpenCLEnv(MagickCLEnv);
398 
399 extern MagickPrivate unsigned long
400  GetOpenCLDeviceLocalMemorySize(MagickCLEnv),
401  GetOpenCLDeviceMaxMemAllocSize(MagickCLEnv);
402 
403 extern MagickPrivate const char*
404  GetOpenCLCachedFilesDirectory();
405 
406 extern MagickPrivate void
407  OpenCLLog(const char*);
408 
409 extern MagickPrivate void
410  OpenCLTerminus();
411 
412 /* #define OPENCLLOG_ENABLED 1 */
413 static inline void OpenCLLogException(const char* function,
414  const unsigned int line,
415  ExceptionInfo* exception) {
416 #ifdef OPENCLLOG_ENABLED
417  if (exception->severity!=0) {
418  char message[MaxTextExtent];
419  /* dump the source into a file */
420  (void) FormatLocaleString(message,MaxTextExtent,"%s:%d Exception(%d):%s "
421  ,function,line,exception->severity,exception->reason);
422  OpenCLLog(message);
423  }
424 #else
425  magick_unreferenced(function);
426  magick_unreferenced(line);
427  magick_unreferenced(exception);
428 #endif
429 }
430 #endif
431 
432 #if defined(__cplusplus) || defined(c_plusplus)
433 }
434 #endif
435 
436 #endif
void * cl_event
Definition: opencl-private.h:32
Definition: opencl.h:29
Definition: exception.h:102
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:470
void(MagickDLLCall *delete_instance)(gs_main_instance *)
ExceptionType
Definition: exception.h:28
MagickBooleanType
Definition: magick-type.h:215
char * reason
Definition: exception.h:111
void * cl_uint
Definition: opencl-private.h:34
#define MaxTextExtent
Definition: method-attribute.h:106
MagickOpenCLProgram
Definition: opencl.h:27
void * cl_mem
Definition: opencl-private.h:33
#define magick_unreferenced(x)
Definition: method-attribute.h:123
#define MagickPrivate
Definition: method-attribute.h:99
Definition: semaphore.c:58
Definition: opencl.c:3024
ExceptionType severity
Definition: exception.h:105