2 Copyright 1999-2016 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 http://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_clWaitForEvents)(cl_uint num_events,
211 const cl_event *event_list) CL_API_SUFFIX__VERSION_1_0;
213 typedef CL_API_ENTRY cl_int
214 (CL_API_CALL *MAGICKpfn_clReleaseEvent)(cl_event event)
215 CL_API_SUFFIX__VERSION_1_0;
217 typedef CL_API_ENTRY cl_int
218 (CL_API_CALL *MAGICKpfn_clRetainEvent)(cl_event event)
219 CL_API_SUFFIX__VERSION_1_0;
221 typedef CL_API_ENTRY cl_int
222 (CL_API_CALL *MAGICKpfn_clSetEventCallback)(cl_event event,
223 cl_int command_exec_callback_type,void (CL_CALLBACK *MAGICKpfn_notify)(
224 cl_event,cl_int,void *),void *user_data) CL_API_SUFFIX__VERSION_1_1;
228 typedef CL_API_ENTRY cl_int
229 (CL_API_CALL *MAGICKpfn_clGetEventProfilingInfo)(cl_event event,
230 cl_profiling_info param_name,size_t param_value_size,void *param_value,
231 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
233 typedef struct MagickLibraryRec MagickLibrary;
235 struct MagickLibraryRec
239 MAGICKpfn_clGetPlatformIDs clGetPlatformIDs;
240 MAGICKpfn_clGetPlatformInfo clGetPlatformInfo;
242 MAGICKpfn_clGetDeviceIDs clGetDeviceIDs;
243 MAGICKpfn_clGetDeviceInfo clGetDeviceInfo;
245 MAGICKpfn_clCreateContext clCreateContext;
246 MAGICKpfn_clReleaseContext clReleaseContext;
248 MAGICKpfn_clCreateCommandQueue clCreateCommandQueue;
249 MAGICKpfn_clReleaseCommandQueue clReleaseCommandQueue;
250 MAGICKpfn_clFlush clFlush;
251 MAGICKpfn_clFinish clFinish;
253 MAGICKpfn_clCreateBuffer clCreateBuffer;
254 MAGICKpfn_clReleaseMemObject clReleaseMemObject;
256 MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
257 MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
258 MAGICKpfn_clReleaseProgram clReleaseProgram;
259 MAGICKpfn_clBuildProgram clBuildProgram;
260 MAGICKpfn_clGetProgramBuildInfo clGetProgramBuildInfo;
261 MAGICKpfn_clGetProgramInfo clGetProgramInfo;
263 MAGICKpfn_clCreateKernel clCreateKernel;
264 MAGICKpfn_clReleaseKernel clReleaseKernel;
265 MAGICKpfn_clSetKernelArg clSetKernelArg;
266 MAGICKpfn_clGetKernelInfo clGetKernelInfo;
268 MAGICKpfn_clEnqueueReadBuffer clEnqueueReadBuffer;
269 MAGICKpfn_clEnqueueMapBuffer clEnqueueMapBuffer;
270 MAGICKpfn_clEnqueueUnmapMemObject clEnqueueUnmapMemObject;
271 MAGICKpfn_clEnqueueNDRangeKernel clEnqueueNDRangeKernel;
273 MAGICKpfn_clWaitForEvents clWaitForEvents;
274 MAGICKpfn_clReleaseEvent clReleaseEvent;
275 MAGICKpfn_clRetainEvent clRetainEvent;
276 MAGICKpfn_clSetEventCallback clSetEventCallback;
278 MAGICKpfn_clGetEventProfilingInfo clGetEventProfilingInfo;
281 struct _MagickCLDevice
289 command_queues[MAGICKCORE_OPENCL_COMMAND_QUEUES];
327 command_queues_index;
333 typedef struct _MagickCLEnv
362 #if defined(MAGICKCORE_HDRI_SUPPORT)
363 #define CLOptions "-cl-single-precision-constant -cl-mad-enable -DMAGICKCORE_HDRI_SUPPORT=1 "\
364 "-DCLQuantum=float -DCLSignedQuantum=float -DCLPixelType=float4 -DQuantumRange=%ff " \
365 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
366 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
367 #define CLQuantum cl_float
368 #define CLPixelPacket cl_float4
369 #define CLCharQuantumScale 1.0f
370 #elif (MAGICKCORE_QUANTUM_DEPTH == 8)
371 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
372 "-DCLQuantum=uchar -DCLSignedQuantum=char -DCLPixelType=uchar4 -DQuantumRange=%ff " \
373 "-DQuantumScale=%ff -DCharQuantumScale=%ff -DMagickEpsilon=%ff -DMagickPI=%ff "\
374 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
375 #define CLQuantum cl_uchar
376 #define CLPixelPacket cl_uchar4
377 #define CLCharQuantumScale 1.0f
378 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
379 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
380 "-DCLQuantum=ushort -DCLSignedQuantum=short -DCLPixelType=ushort4 -DQuantumRange=%ff "\
381 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
382 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
383 #define CLQuantum cl_ushort
384 #define CLPixelPacket cl_ushort4
385 #define CLCharQuantumScale 257.0f
386 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
387 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
388 "-DCLQuantum=uint -DCLSignedQuantum=int -DCLPixelType=uint4 -DQuantumRange=%ff "\
389 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
390 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
391 #define CLQuantum cl_uint
392 #define CLPixelPacket cl_uint4
393 #define CLCharQuantumScale 16843009.0f
394 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
395 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
396 "-DCLQuantum=ulong -DCLSignedQuantum=long -DCLPixelType=ulong4 -DQuantumRange=%ff "\
397 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
398 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
399 #define CLQuantum cl_ulong
400 #define CLPixelPacket cl_ulong4
401 #define CLCharQuantumScale 72340172838076673.0f
404 extern MagickPrivate cl_command_queue
405 AcquireOpenCLCommandQueue(MagickCLDevice);
407 extern MagickPrivate cl_int
408 SetOpenCLKernelArg(cl_kernel,size_t,size_t,const void *);
410 extern MagickPrivate cl_kernel
411 AcquireOpenCLKernel(MagickCLDevice,const char *);
413 extern MagickPrivate cl_mem
414 CreateOpenCLBuffer(MagickCLDevice,cl_mem_flags,size_t,void *);
416 extern MagickPrivate MagickBooleanType
417 EnqueueOpenCLKernel(cl_command_queue,cl_kernel,cl_uint,const size_t *,
418 const size_t *,const size_t *,const Image *,const Image *,
419 MagickBooleanType,ExceptionInfo *),
420 InitializeOpenCL(MagickCLEnv,ExceptionInfo *),
421 OpenCLThrowMagickException(MagickCLDevice,ExceptionInfo *,
422 const char *,const char *,const size_t,const ExceptionType,const char *,
424 RecordProfileData(MagickCLDevice,cl_kernel,cl_event);
426 extern MagickPrivate MagickCLCacheInfo
427 AcquireMagickCLCacheInfo(MagickCLDevice,Quantum *,const MagickSizeType),
428 CopyMagickCLCacheInfo(MagickCLCacheInfo),
429 RelinquishMagickCLCacheInfo(MagickCLCacheInfo,const MagickBooleanType);
431 extern MagickPrivate MagickCLDevice
432 RequestOpenCLDevice(MagickCLEnv);
434 extern MagickPrivate MagickCLEnv
435 GetCurrentOpenCLEnv(void);
437 extern MagickPrivate unsigned long
438 GetOpenCLDeviceLocalMemorySize(const MagickCLDevice);
440 extern MagickPrivate void
441 DumpOpenCLProfileData(),
443 ReleaseOpenCLCommandQueue(MagickCLDevice,cl_command_queue),
444 ReleaseOpenCLDevice(MagickCLDevice),
445 ReleaseOpenCLKernel(cl_kernel),
446 ReleaseOpenCLMemObject(cl_mem),
447 RetainOpenCLEvent(cl_event);
451 #if defined(__cplusplus) || defined(c_plusplus)