MagickCore  7.1.1-43
Convert, Edit, Or Compose Bitmap Images
opencl-private.h
1 /*
2  Copyright @ 1999 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 "MagickCore/studio.h"
25 #include "MagickCore/opencl.h"
26 #include "MagickCore/thread_.h"
27 
28 #if defined(MAGICKCORE_HAVE_CL_CL_H)
29 # include <CL/cl.h>
30 #endif
31 #if defined(MAGICKCORE_HAVE_OPENCL_CL_H)
32 # include <OpenCL/cl.h>
33 #endif
34 
35 #if defined(__cplusplus) || defined(c_plusplus)
36 extern "C" {
37 #endif
38 
39 #if !defined(MAGICKCORE_OPENCL_SUPPORT)
40 typedef void* MagickCLCacheInfo;
41 #else
42 typedef struct _MagickCLCacheInfo
43 {
44  cl_event
45  *events;
46 
47  cl_mem
48  buffer;
49 
50  cl_uint
51  event_count;
52 
53  MagickCLDevice
54  device;
55 
56  MagickSizeType
57  length;
58 
59  Quantum
60  *pixels;
61 
63  *events_semaphore;
64 }* MagickCLCacheInfo;
65 
66 /*
67  Define declarations.
68 */
69 #define MAGICKCORE_OPENCL_UNDEFINED_SCORE -1.0
70 #define MAGICKCORE_OPENCL_COMMAND_QUEUES 16
71 
72 /* Platform APIs */
73 typedef CL_API_ENTRY cl_int
74  (CL_API_CALL *MAGICKpfn_clGetPlatformIDs)(cl_uint num_entries,
75  cl_platform_id *platforms,cl_uint *num_platforms) CL_API_SUFFIX__VERSION_1_0;
76 
77 typedef CL_API_ENTRY cl_int
78  (CL_API_CALL *MAGICKpfn_clGetPlatformInfo)(cl_platform_id platform,
79  cl_platform_info param_name,size_t param_value_size,void *param_value,
80  size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
81 
82 
83 /* Device APIs */
84 typedef CL_API_ENTRY cl_int
85  (CL_API_CALL *MAGICKpfn_clGetDeviceIDs)(cl_platform_id platform,
86  cl_device_type device_type,cl_uint num_entries,cl_device_id *devices,
87  cl_uint *num_devices) CL_API_SUFFIX__VERSION_1_0;
88 
89 typedef CL_API_ENTRY cl_int
90  (CL_API_CALL *MAGICKpfn_clGetDeviceInfo)(cl_device_id device,
91  cl_device_info param_name,size_t param_value_size,void *param_value,
92  size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
93 
94 
95 /* Context APIs */
96 typedef CL_API_ENTRY cl_context
97  (CL_API_CALL *MAGICKpfn_clCreateContext)(
98  const cl_context_properties *properties,cl_uint num_devices,
99  const cl_device_id *devices,void (CL_CALLBACK *pfn_notify)(const char *,
100  const void *,size_t,void *),void *user_data,cl_int *errcode_ret)
101  CL_API_SUFFIX__VERSION_1_0;
102 
103 typedef CL_API_ENTRY cl_int
104  (CL_API_CALL *MAGICKpfn_clReleaseContext)(cl_context context)
105  CL_API_SUFFIX__VERSION_1_0;
106 
107 
108 /* Command Queue APIs */
109 typedef CL_API_ENTRY cl_command_queue
110  (CL_API_CALL *MAGICKpfn_clCreateCommandQueue)(cl_context context,
111  cl_device_id device,cl_command_queue_properties properties,
112  cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
113 
114 typedef CL_API_ENTRY cl_int
115  (CL_API_CALL *MAGICKpfn_clReleaseCommandQueue)(
116  cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0;
117 
118 typedef CL_API_ENTRY cl_int
119  (CL_API_CALL *MAGICKpfn_clFlush)(cl_command_queue command_queue)
120  CL_API_SUFFIX__VERSION_1_0;
121 
122 typedef CL_API_ENTRY cl_int
123  (CL_API_CALL *MAGICKpfn_clFinish)(cl_command_queue command_queue)
124  CL_API_SUFFIX__VERSION_1_0;
125 
126 
127 /* Memory Object APIs */
128 typedef CL_API_ENTRY cl_mem
129  (CL_API_CALL *MAGICKpfn_clCreateBuffer)(cl_context context,
130  cl_mem_flags flags,size_t size,void *host_ptr,cl_int *errcode_ret)
131  CL_API_SUFFIX__VERSION_1_0;
132 
133 typedef CL_API_ENTRY cl_int
134  (CL_API_CALL *MAGICKpfn_clRetainMemObject)(cl_mem memobj)
135  CL_API_SUFFIX__VERSION_1_0;
136 
137 typedef CL_API_ENTRY cl_int
138  (CL_API_CALL *MAGICKpfn_clReleaseMemObject)(cl_mem memobj)
139  CL_API_SUFFIX__VERSION_1_0;
140 
141 
142 /* Program Object APIs */
143 typedef CL_API_ENTRY cl_program
144  (CL_API_CALL *MAGICKpfn_clCreateProgramWithSource)(cl_context context,
145  cl_uint count,const char **strings,const size_t *lengths,
146  cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
147 
148 typedef CL_API_ENTRY cl_program
149  (CL_API_CALL *MAGICKpfn_clCreateProgramWithBinary)(cl_context context,
150  cl_uint num_devices,const cl_device_id *device_list,const size_t *lengths,
151  const unsigned char **binaries,cl_int *binary_status,cl_int *errcode_ret)
152  CL_API_SUFFIX__VERSION_1_0;
153 
154 typedef CL_API_ENTRY cl_int
155  (CL_API_CALL *MAGICKpfn_clReleaseProgram)(cl_program program)
156  CL_API_SUFFIX__VERSION_1_0;
157 
158 typedef CL_API_ENTRY cl_int
159  (CL_API_CALL *MAGICKpfn_clBuildProgram)(cl_program program,
160  cl_uint num_devices,const cl_device_id *device_list,const char *options,
161  void (CL_CALLBACK *pfn_notify)(cl_program program,void * user_data),
162  void *user_data) CL_API_SUFFIX__VERSION_1_0;
163 
164 typedef CL_API_ENTRY cl_int
165  (CL_API_CALL *MAGICKpfn_clGetProgramBuildInfo)(cl_program program,
166  cl_device_id device,cl_program_build_info param_name,size_t param_value_size,
167  void *param_value,size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
168 
169 typedef CL_API_ENTRY cl_int
170  (CL_API_CALL *MAGICKpfn_clGetProgramInfo)(cl_program program,
171  cl_program_info param_name,size_t param_value_size,void *param_value,
172  size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
173 
174 
175 /* Kernel Object APIs */
176 typedef CL_API_ENTRY cl_kernel
177  (CL_API_CALL *MAGICKpfn_clCreateKernel)(cl_program program,
178  const char *kernel_name,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
179 
180 typedef CL_API_ENTRY cl_int
181  (CL_API_CALL *MAGICKpfn_clReleaseKernel)(cl_kernel kernel)
182  CL_API_SUFFIX__VERSION_1_0;
183 
184 typedef CL_API_ENTRY cl_int
185  (CL_API_CALL *MAGICKpfn_clSetKernelArg)(cl_kernel kernel,cl_uint arg_index,
186  size_t arg_size,const void * arg_value) CL_API_SUFFIX__VERSION_1_0;
187 
188 typedef CL_API_ENTRY cl_int
189  (CL_API_CALL *MAGICKpfn_clGetKernelInfo)(cl_kernel kernel,
190  cl_kernel_info param_name,size_t param_value_size,void *param_value,
191  size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
192 
193 
194 /* Enqueued Commands APIs */
195 typedef CL_API_ENTRY cl_int
196  (CL_API_CALL *MAGICKpfn_clEnqueueReadBuffer)(cl_command_queue command_queue,
197  cl_mem buffer,cl_bool blocking_read,size_t offset,size_t cb,void *ptr,
198  cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
199  cl_event *event) CL_API_SUFFIX__VERSION_1_0;
200 
201 typedef CL_API_ENTRY void
202  *(CL_API_CALL *MAGICKpfn_clEnqueueMapBuffer)(cl_command_queue command_queue,
203  cl_mem buffer,cl_bool blocking_map,cl_map_flags map_flags,size_t offset,
204  size_t cb,cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
205  cl_event *event,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
206 
207 typedef CL_API_ENTRY cl_int
208  (CL_API_CALL *MAGICKpfn_clEnqueueUnmapMemObject)(
209  cl_command_queue command_queue,cl_mem memobj,void *mapped_ptr,
210  cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
211  cl_event *event) CL_API_SUFFIX__VERSION_1_0;
212 
213 typedef CL_API_ENTRY cl_int
214  (CL_API_CALL *MAGICKpfn_clEnqueueNDRangeKernel)(
215  cl_command_queue command_queue,cl_kernel kernel,cl_uint work_dim,
216  const size_t *global_work_offset,const size_t *global_work_size,
217  const size_t *local_work_size,cl_uint num_events_in_wait_list,
218  const cl_event * event_wait_list,cl_event *event)
219  CL_API_SUFFIX__VERSION_1_0;
220 
221 
222 /* Events APIs */
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
229  (CL_API_CALL *MAGICKpfn_clWaitForEvents)(cl_uint num_events,
230  const cl_event *event_list) CL_API_SUFFIX__VERSION_1_0;
231 
232 typedef CL_API_ENTRY cl_int
233  (CL_API_CALL *MAGICKpfn_clReleaseEvent)(cl_event event)
234  CL_API_SUFFIX__VERSION_1_0;
235 
236 typedef CL_API_ENTRY cl_int
237  (CL_API_CALL *MAGICKpfn_clRetainEvent)(cl_event event)
238  CL_API_SUFFIX__VERSION_1_0;
239 
240 typedef CL_API_ENTRY cl_int
241  (CL_API_CALL *MAGICKpfn_clSetEventCallback)(cl_event event,
242  cl_int command_exec_callback_type,void (CL_CALLBACK *MAGICKpfn_notify)(
243  cl_event,cl_int,void *),void *user_data) CL_API_SUFFIX__VERSION_1_1;
244 
245 
246 /* Profiling APIs */
247 typedef CL_API_ENTRY cl_int
248  (CL_API_CALL *MAGICKpfn_clGetEventProfilingInfo)(cl_event event,
249  cl_profiling_info param_name,size_t param_value_size,void *param_value,
250  size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
251 
252 typedef struct MagickLibraryRec MagickLibrary;
253 
254 struct MagickLibraryRec
255 {
256  void *library;
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 
276  MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
277  MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
278  MAGICKpfn_clReleaseProgram clReleaseProgram;
279  MAGICKpfn_clBuildProgram clBuildProgram;
280  MAGICKpfn_clGetProgramBuildInfo clGetProgramBuildInfo;
281  MAGICKpfn_clGetProgramInfo clGetProgramInfo;
282 
283  MAGICKpfn_clCreateKernel clCreateKernel;
284  MAGICKpfn_clReleaseKernel clReleaseKernel;
285  MAGICKpfn_clSetKernelArg clSetKernelArg;
286  MAGICKpfn_clGetKernelInfo clGetKernelInfo;
287 
288  MAGICKpfn_clEnqueueReadBuffer clEnqueueReadBuffer;
289  MAGICKpfn_clEnqueueMapBuffer clEnqueueMapBuffer;
290  MAGICKpfn_clEnqueueUnmapMemObject clEnqueueUnmapMemObject;
291  MAGICKpfn_clEnqueueNDRangeKernel clEnqueueNDRangeKernel;
292 
293  MAGICKpfn_clGetEventInfo clGetEventInfo;
294  MAGICKpfn_clWaitForEvents clWaitForEvents;
295  MAGICKpfn_clReleaseEvent clReleaseEvent;
296  MAGICKpfn_clRetainEvent clRetainEvent;
297  MAGICKpfn_clSetEventCallback clSetEventCallback;
298 
299  MAGICKpfn_clGetEventProfilingInfo clGetEventProfilingInfo;
300 };
301 
302 struct _MagickCLDevice
303 {
304  char
305  *name,
306  *platform_name,
307  *version;
308 
309  cl_command_queue
310  command_queues[MAGICKCORE_OPENCL_COMMAND_QUEUES];
311 
312  cl_context
313  context;
314 
315  cl_device_id
316  deviceID;
317 
318  cl_device_type
319  type;
320 
321  cl_program
322  program;
323 
324  cl_uint
325  max_clock_frequency,
326  max_compute_units;
327 
328  cl_ulong
329  local_memory_size;
330 
331  double
332  score;
333 
335  *profile_records;
336 
337  MagickBooleanType
338  enabled,
339  profile_kernels;
340 
342  *lock;
343 
344  size_t
345  requested;
346 
347  ssize_t
348  command_queues_index;
349 
350  char
351  *vendor_name;
352 };
353 
354 typedef struct _MagickCLEnv
355 {
356  cl_context
357  *contexts;
358 
359  double
360  cpu_score;
361 
362  MagickBooleanType
363  enabled,
364  initialized;
365 
366  MagickCLDevice
367  *devices;
368 
369  MagickLibrary
370  *library;
371 
372  MagickThreadType
373  benchmark_thread_id;
374 
376  *lock;
377 
378  size_t
379  number_contexts,
380  number_devices;
381 } *MagickCLEnv;
382 
383 #if defined(MAGICKCORE_HDRI_SUPPORT)
384 #define CLOptions "-cl-single-precision-constant -cl-mad-enable -DMAGICKCORE_HDRI_SUPPORT=1 "\
385  "-DCLQuantum=float -DCLSignedQuantum=float -DCLPixelType=float4 -DQuantumRange=%ff " \
386  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
387  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
388 #define CLQuantum cl_float
389 #define CLPixelPacket cl_float4
390 #define CLCharQuantumScale 1.0f
391 #elif (MAGICKCORE_QUANTUM_DEPTH == 8)
392 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
393  "-DCLQuantum=uchar -DCLSignedQuantum=char -DCLPixelType=uchar4 -DQuantumRange=%ff " \
394  "-DQuantumScale=%ff -DCharQuantumScale=%ff -DMagickEpsilon=%ff -DMagickPI=%ff "\
395  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
396 #define CLQuantum cl_uchar
397 #define CLPixelPacket cl_uchar4
398 #define CLCharQuantumScale 1.0f
399 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
400 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
401  "-DCLQuantum=ushort -DCLSignedQuantum=short -DCLPixelType=ushort4 -DQuantumRange=%ff "\
402  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
403  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
404 #define CLQuantum cl_ushort
405 #define CLPixelPacket cl_ushort4
406 #define CLCharQuantumScale 257.0f
407 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
408 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
409  "-DCLQuantum=uint -DCLSignedQuantum=int -DCLPixelType=uint4 -DQuantumRange=%ff "\
410  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
411  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
412 #define CLQuantum cl_uint
413 #define CLPixelPacket cl_uint4
414 #define CLCharQuantumScale 16843009.0f
415 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
416 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
417  "-DCLQuantum=ulong -DCLSignedQuantum=long -DCLPixelType=ulong4 -DQuantumRange=%ff "\
418  "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
419  "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
420 #define CLQuantum cl_ulong
421 #define CLPixelPacket cl_ulong4
422 #define CLCharQuantumScale 72340172838076673.0f
423 #endif
424 
425 extern MagickPrivate cl_command_queue
426  AcquireOpenCLCommandQueue(MagickCLDevice);
427 
428 extern MagickPrivate cl_int
429  SetOpenCLKernelArg(cl_kernel,size_t,size_t,const void *);
430 
431 extern MagickPrivate cl_kernel
432  AcquireOpenCLKernel(MagickCLDevice,const char *);
433 
434 extern MagickPrivate cl_mem
435  CreateOpenCLBuffer(MagickCLDevice,cl_mem_flags,size_t,void *);
436 
437 extern MagickPrivate MagickBooleanType
438  EnqueueOpenCLKernel(cl_command_queue,cl_kernel,cl_uint,const size_t *,
439  const size_t *,const size_t *,const Image *,const Image *,
440  MagickBooleanType,ExceptionInfo *),
441  InitializeOpenCL(MagickCLEnv,ExceptionInfo *),
442  OpenCLThrowMagickException(MagickCLDevice,ExceptionInfo *,
443  const char *,const char *,const size_t,const ExceptionType,const char *,
444  const char *,...),
445  RecordProfileData(MagickCLDevice,cl_kernel,cl_event);
446 
447 extern MagickPrivate MagickCLCacheInfo
448  AcquireMagickCLCacheInfo(MagickCLDevice,Quantum *,const MagickSizeType),
449  CopyMagickCLCacheInfo(MagickCLCacheInfo),
450  RelinquishMagickCLCacheInfo(MagickCLCacheInfo,const MagickBooleanType);
451 
452 extern MagickPrivate MagickCLDevice
453  RequestOpenCLDevice(MagickCLEnv);
454 
455 extern MagickPrivate MagickCLEnv
456  GetCurrentOpenCLEnv(void);
457 
458 extern MagickPrivate unsigned long
459  GetOpenCLDeviceLocalMemorySize(const MagickCLDevice);
460 
461 extern MagickPrivate void
462  DumpOpenCLProfileData(),
463  OpenCLTerminus(),
464  ReleaseOpenCLCommandQueue(MagickCLDevice,cl_command_queue),
465  ReleaseOpenCLDevice(MagickCLDevice),
466  ReleaseOpenCLKernel(cl_kernel),
467  ReleaseOpenCLMemObject(cl_mem),
468  RetainOpenCLEvent(cl_event),
469  RetainOpenCLMemObject(cl_mem);
470 
471 #endif
472 
473 #if defined(__cplusplus) || defined(c_plusplus)
474 }
475 #endif
476 
477 #endif
SemaphoreInfo
Definition: semaphore.c:60
_Image
Definition: image.h:131
_ExceptionInfo
Definition: exception.h:101
_KernelProfileRecord
Definition: opencl.h:32