2 Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
8 https://www.imagemagick.org/script/license.php
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.
16 MagickCore OpenCL private methods.
18 #ifndef MAGICKCORE_OPENCL_PRIVATE_H
19 #define MAGICKCORE_OPENCL_PRIVATE_H
24 #include "MagickCore/studio.h"
25 #include "MagickCore/opencl.h"
26 #include "MagickCore/thread_.h"
28 #if defined(__cplusplus) || defined(c_plusplus)
32 #if !defined(MAGICKCORE_OPENCL_SUPPORT)
33 typedef void* MagickCLCacheInfo;
35 typedef struct _MagickCLCacheInfo
59 #define MAGICKCORE_OPENCL_UNDEFINED_SCORE -1.0
60 #define MAGICKCORE_OPENCL_COMMAND_QUEUES 16
63 typedef CL_API_ENTRY cl_int
64 (CL_API_CALL *MAGICKpfn_clGetPlatformIDs)(cl_uint num_entries,
65 cl_platform_id *platforms,cl_uint *num_platforms) CL_API_SUFFIX__VERSION_1_0;
67 typedef CL_API_ENTRY cl_int
68 (CL_API_CALL *MAGICKpfn_clGetPlatformInfo)(cl_platform_id platform,
69 cl_platform_info param_name,size_t param_value_size,void *param_value,
70 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
74 typedef CL_API_ENTRY cl_int
75 (CL_API_CALL *MAGICKpfn_clGetDeviceIDs)(cl_platform_id platform,
76 cl_device_type device_type,cl_uint num_entries,cl_device_id *devices,
77 cl_uint *num_devices) CL_API_SUFFIX__VERSION_1_0;
79 typedef CL_API_ENTRY cl_int
80 (CL_API_CALL *MAGICKpfn_clGetDeviceInfo)(cl_device_id device,
81 cl_device_info param_name,size_t param_value_size,void *param_value,
82 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
86 typedef CL_API_ENTRY cl_context
87 (CL_API_CALL *MAGICKpfn_clCreateContext)(
88 const cl_context_properties *properties,cl_uint num_devices,
89 const cl_device_id *devices,void (CL_CALLBACK *pfn_notify)(const char *,
90 const void *,size_t,void *),void *user_data,cl_int *errcode_ret)
91 CL_API_SUFFIX__VERSION_1_0;
93 typedef CL_API_ENTRY cl_int
94 (CL_API_CALL *MAGICKpfn_clReleaseContext)(cl_context context)
95 CL_API_SUFFIX__VERSION_1_0;
98 /* Command Queue APIs */
99 typedef CL_API_ENTRY cl_command_queue
100 (CL_API_CALL *MAGICKpfn_clCreateCommandQueue)(cl_context context,
101 cl_device_id device,cl_command_queue_properties properties,
102 cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
104 typedef CL_API_ENTRY cl_int
105 (CL_API_CALL *MAGICKpfn_clReleaseCommandQueue)(
106 cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0;
108 typedef CL_API_ENTRY cl_int
109 (CL_API_CALL *MAGICKpfn_clFlush)(cl_command_queue command_queue)
110 CL_API_SUFFIX__VERSION_1_0;
112 typedef CL_API_ENTRY cl_int
113 (CL_API_CALL *MAGICKpfn_clFinish)(cl_command_queue command_queue)
114 CL_API_SUFFIX__VERSION_1_0;
117 /* Memory Object APIs */
118 typedef CL_API_ENTRY cl_mem
119 (CL_API_CALL *MAGICKpfn_clCreateBuffer)(cl_context context,
120 cl_mem_flags flags,size_t size,void *host_ptr,cl_int *errcode_ret)
121 CL_API_SUFFIX__VERSION_1_0;
123 typedef CL_API_ENTRY cl_int
124 (CL_API_CALL *MAGICKpfn_clReleaseMemObject)(cl_mem memobj)
125 CL_API_SUFFIX__VERSION_1_0;
128 /* Program Object APIs */
129 typedef CL_API_ENTRY cl_program
130 (CL_API_CALL *MAGICKpfn_clCreateProgramWithSource)(cl_context context,
131 cl_uint count,const char **strings,const size_t *lengths,
132 cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
134 typedef CL_API_ENTRY cl_program
135 (CL_API_CALL *MAGICKpfn_clCreateProgramWithBinary)(cl_context context,
136 cl_uint num_devices,const cl_device_id *device_list,const size_t *lengths,
137 const unsigned char **binaries,cl_int *binary_status,cl_int *errcode_ret)
138 CL_API_SUFFIX__VERSION_1_0;
140 typedef CL_API_ENTRY cl_int
141 (CL_API_CALL *MAGICKpfn_clReleaseProgram)(cl_program program)
142 CL_API_SUFFIX__VERSION_1_0;
144 typedef CL_API_ENTRY cl_int
145 (CL_API_CALL *MAGICKpfn_clBuildProgram)(cl_program program,
146 cl_uint num_devices,const cl_device_id *device_list,const char *options,
147 void (CL_CALLBACK *pfn_notify)(cl_program program,void * user_data),
148 void *user_data) CL_API_SUFFIX__VERSION_1_0;
150 typedef CL_API_ENTRY cl_int
151 (CL_API_CALL *MAGICKpfn_clGetProgramBuildInfo)(cl_program program,
152 cl_device_id device,cl_program_build_info param_name,size_t param_value_size,
153 void *param_value,size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
155 typedef CL_API_ENTRY cl_int
156 (CL_API_CALL *MAGICKpfn_clGetProgramInfo)(cl_program program,
157 cl_program_info param_name,size_t param_value_size,void *param_value,
158 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
161 /* Kernel Object APIs */
162 typedef CL_API_ENTRY cl_kernel
163 (CL_API_CALL *MAGICKpfn_clCreateKernel)(cl_program program,
164 const char *kernel_name,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
166 typedef CL_API_ENTRY cl_int
167 (CL_API_CALL *MAGICKpfn_clReleaseKernel)(cl_kernel kernel)
168 CL_API_SUFFIX__VERSION_1_0;
170 typedef CL_API_ENTRY cl_int
171 (CL_API_CALL *MAGICKpfn_clSetKernelArg)(cl_kernel kernel,cl_uint arg_index,
172 size_t arg_size,const void * arg_value) CL_API_SUFFIX__VERSION_1_0;
174 typedef CL_API_ENTRY cl_int
175 (CL_API_CALL *MAGICKpfn_clGetKernelInfo)(cl_kernel kernel,
176 cl_kernel_info param_name,size_t param_value_size,void *param_value,
177 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
180 /* Enqueued Commands APIs */
181 typedef CL_API_ENTRY cl_int
182 (CL_API_CALL *MAGICKpfn_clEnqueueReadBuffer)(cl_command_queue command_queue,
183 cl_mem buffer,cl_bool blocking_read,size_t offset,size_t cb,void *ptr,
184 cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
185 cl_event *event) CL_API_SUFFIX__VERSION_1_0;
187 typedef CL_API_ENTRY void
188 *(CL_API_CALL *MAGICKpfn_clEnqueueMapBuffer)(cl_command_queue command_queue,
189 cl_mem buffer,cl_bool blocking_map,cl_map_flags map_flags,size_t offset,
190 size_t cb,cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
191 cl_event *event,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
193 typedef CL_API_ENTRY cl_int
194 (CL_API_CALL *MAGICKpfn_clEnqueueUnmapMemObject)(
195 cl_command_queue command_queue,cl_mem memobj,void *mapped_ptr,
196 cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
197 cl_event *event) CL_API_SUFFIX__VERSION_1_0;
199 typedef CL_API_ENTRY cl_int
200 (CL_API_CALL *MAGICKpfn_clEnqueueNDRangeKernel)(
201 cl_command_queue command_queue,cl_kernel kernel,cl_uint work_dim,
202 const size_t *global_work_offset,const size_t *global_work_size,
203 const size_t *local_work_size,cl_uint num_events_in_wait_list,
204 const cl_event * event_wait_list,cl_event *event)
205 CL_API_SUFFIX__VERSION_1_0;
209 typedef CL_API_ENTRY cl_int
210 (CL_API_CALL *MAGICKpfn_clGetEventInfo)(cl_event event,
211 cl_profiling_info param_name,size_t param_value_size,void *param_value,
212 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
214 typedef CL_API_ENTRY cl_int
215 (CL_API_CALL *MAGICKpfn_clWaitForEvents)(cl_uint num_events,
216 const cl_event *event_list) CL_API_SUFFIX__VERSION_1_0;
218 typedef CL_API_ENTRY cl_int
219 (CL_API_CALL *MAGICKpfn_clReleaseEvent)(cl_event event)
220 CL_API_SUFFIX__VERSION_1_0;
222 typedef CL_API_ENTRY cl_int
223 (CL_API_CALL *MAGICKpfn_clRetainEvent)(cl_event event)
224 CL_API_SUFFIX__VERSION_1_0;
226 typedef CL_API_ENTRY cl_int
227 (CL_API_CALL *MAGICKpfn_clSetEventCallback)(cl_event event,
228 cl_int command_exec_callback_type,void (CL_CALLBACK *MAGICKpfn_notify)(
229 cl_event,cl_int,void *),void *user_data) CL_API_SUFFIX__VERSION_1_1;
233 typedef CL_API_ENTRY cl_int
234 (CL_API_CALL *MAGICKpfn_clGetEventProfilingInfo)(cl_event event,
235 cl_profiling_info param_name,size_t param_value_size,void *param_value,
236 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
238 typedef struct MagickLibraryRec MagickLibrary;
240 struct MagickLibraryRec
244 MAGICKpfn_clGetPlatformIDs clGetPlatformIDs;
245 MAGICKpfn_clGetPlatformInfo clGetPlatformInfo;
247 MAGICKpfn_clGetDeviceIDs clGetDeviceIDs;
248 MAGICKpfn_clGetDeviceInfo clGetDeviceInfo;
250 MAGICKpfn_clCreateContext clCreateContext;
251 MAGICKpfn_clReleaseContext clReleaseContext;
253 MAGICKpfn_clCreateCommandQueue clCreateCommandQueue;
254 MAGICKpfn_clReleaseCommandQueue clReleaseCommandQueue;
255 MAGICKpfn_clFlush clFlush;
256 MAGICKpfn_clFinish clFinish;
258 MAGICKpfn_clCreateBuffer clCreateBuffer;
259 MAGICKpfn_clReleaseMemObject clReleaseMemObject;
261 MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
262 MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
263 MAGICKpfn_clReleaseProgram clReleaseProgram;
264 MAGICKpfn_clBuildProgram clBuildProgram;
265 MAGICKpfn_clGetProgramBuildInfo clGetProgramBuildInfo;
266 MAGICKpfn_clGetProgramInfo clGetProgramInfo;
268 MAGICKpfn_clCreateKernel clCreateKernel;
269 MAGICKpfn_clReleaseKernel clReleaseKernel;
270 MAGICKpfn_clSetKernelArg clSetKernelArg;
271 MAGICKpfn_clGetKernelInfo clGetKernelInfo;
273 MAGICKpfn_clEnqueueReadBuffer clEnqueueReadBuffer;
274 MAGICKpfn_clEnqueueMapBuffer clEnqueueMapBuffer;
275 MAGICKpfn_clEnqueueUnmapMemObject clEnqueueUnmapMemObject;
276 MAGICKpfn_clEnqueueNDRangeKernel clEnqueueNDRangeKernel;
278 MAGICKpfn_clGetEventInfo clGetEventInfo;
279 MAGICKpfn_clWaitForEvents clWaitForEvents;
280 MAGICKpfn_clReleaseEvent clReleaseEvent;
281 MAGICKpfn_clRetainEvent clRetainEvent;
282 MAGICKpfn_clSetEventCallback clSetEventCallback;
284 MAGICKpfn_clGetEventProfilingInfo clGetEventProfilingInfo;
287 struct _MagickCLDevice
295 command_queues[MAGICKCORE_OPENCL_COMMAND_QUEUES];
333 command_queues_index;
339 typedef struct _MagickCLEnv
368 #if defined(MAGICKCORE_HDRI_SUPPORT)
369 #define CLOptions "-cl-single-precision-constant -cl-mad-enable -DMAGICKCORE_HDRI_SUPPORT=1 "\
370 "-DCLQuantum=float -DCLSignedQuantum=float -DCLPixelType=float4 -DQuantumRange=%ff " \
371 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
372 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
373 #define CLQuantum cl_float
374 #define CLPixelPacket cl_float4
375 #define CLCharQuantumScale 1.0f
376 #elif (MAGICKCORE_QUANTUM_DEPTH == 8)
377 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
378 "-DCLQuantum=uchar -DCLSignedQuantum=char -DCLPixelType=uchar4 -DQuantumRange=%ff " \
379 "-DQuantumScale=%ff -DCharQuantumScale=%ff -DMagickEpsilon=%ff -DMagickPI=%ff "\
380 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
381 #define CLQuantum cl_uchar
382 #define CLPixelPacket cl_uchar4
383 #define CLCharQuantumScale 1.0f
384 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
385 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
386 "-DCLQuantum=ushort -DCLSignedQuantum=short -DCLPixelType=ushort4 -DQuantumRange=%ff "\
387 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
388 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
389 #define CLQuantum cl_ushort
390 #define CLPixelPacket cl_ushort4
391 #define CLCharQuantumScale 257.0f
392 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
393 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
394 "-DCLQuantum=uint -DCLSignedQuantum=int -DCLPixelType=uint4 -DQuantumRange=%ff "\
395 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
396 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
397 #define CLQuantum cl_uint
398 #define CLPixelPacket cl_uint4
399 #define CLCharQuantumScale 16843009.0f
400 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
401 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
402 "-DCLQuantum=ulong -DCLSignedQuantum=long -DCLPixelType=ulong4 -DQuantumRange=%ff "\
403 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
404 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
405 #define CLQuantum cl_ulong
406 #define CLPixelPacket cl_ulong4
407 #define CLCharQuantumScale 72340172838076673.0f
410 extern MagickPrivate cl_command_queue
411 AcquireOpenCLCommandQueue(MagickCLDevice);
413 extern MagickPrivate cl_int
414 SetOpenCLKernelArg(cl_kernel,size_t,size_t,const void *);
416 extern MagickPrivate cl_kernel
417 AcquireOpenCLKernel(MagickCLDevice,const char *);
419 extern MagickPrivate cl_mem
420 CreateOpenCLBuffer(MagickCLDevice,cl_mem_flags,size_t,void *);
422 extern MagickPrivate MagickBooleanType
423 EnqueueOpenCLKernel(cl_command_queue,cl_kernel,cl_uint,const size_t *,
424 const size_t *,const size_t *,const Image *,const Image *,
425 MagickBooleanType,ExceptionInfo *),
426 InitializeOpenCL(MagickCLEnv,ExceptionInfo *),
427 OpenCLThrowMagickException(MagickCLDevice,ExceptionInfo *,
428 const char *,const char *,const size_t,const ExceptionType,const char *,
430 RecordProfileData(MagickCLDevice,cl_kernel,cl_event);
432 extern MagickPrivate MagickCLCacheInfo
433 AcquireMagickCLCacheInfo(MagickCLDevice,Quantum *,const MagickSizeType),
434 CopyMagickCLCacheInfo(MagickCLCacheInfo),
435 RelinquishMagickCLCacheInfo(MagickCLCacheInfo,const MagickBooleanType);
437 extern MagickPrivate MagickCLDevice
438 RequestOpenCLDevice(MagickCLEnv);
440 extern MagickPrivate MagickCLEnv
441 GetCurrentOpenCLEnv(void);
443 extern MagickPrivate unsigned long
444 GetOpenCLDeviceLocalMemorySize(const MagickCLDevice);
446 extern MagickPrivate void
447 DumpOpenCLProfileData(),
449 ReleaseOpenCLCommandQueue(MagickCLDevice,cl_command_queue),
450 ReleaseOpenCLDevice(MagickCLDevice),
451 ReleaseOpenCLKernel(cl_kernel),
452 ReleaseOpenCLMemObject(cl_mem),
453 RetainOpenCLEvent(cl_event);
457 #if defined(__cplusplus) || defined(c_plusplus)