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