]> granicus.if.org Git - imagemagick/blob - MagickCore/opencl-private.h
...
[imagemagick] / MagickCore / opencl-private.h
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 "MagickCore/studio.h"
25 #include "MagickCore/opencl.h"
26 #include "MagickCore/thread_.h"
27
28 #if defined(__cplusplus) || defined(c_plusplus)
29 extern "C" {
30 #endif
31
32 #if !defined(MAGICKCORE_OPENCL_SUPPORT)
33 typedef void* MagickCLCacheInfo;
34 #else
35 typedef struct _MagickCLCacheInfo
36 {
37   cl_event
38     *events;
39
40   cl_mem
41     buffer;
42
43   cl_uint
44     event_count;
45
46   MagickCLDevice
47     device;
48
49   MagickSizeType
50     length;
51
52   Quantum
53     *pixels;
54 }* MagickCLCacheInfo;
55
56 /*
57   Define declarations.
58 */
59 #define MAGICKCORE_OPENCL_UNDEFINED_SCORE -1.0
60 #define MAGICKCORE_OPENCL_COMMAND_QUEUES 16
61
62 /* Platform APIs */
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;
66
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;
71
72
73 /* Device APIs */
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;
78
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;
83
84
85 /* Context APIs */
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;
92
93 typedef CL_API_ENTRY cl_int
94   (CL_API_CALL *MAGICKpfn_clReleaseContext)(cl_context context)
95     CL_API_SUFFIX__VERSION_1_0;
96
97
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;
103
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;
107
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;
111
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;
115
116
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;
122
123 typedef CL_API_ENTRY cl_int
124   (CL_API_CALL *MAGICKpfn_clReleaseMemObject)(cl_mem memobj)
125     CL_API_SUFFIX__VERSION_1_0;
126
127
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;
133
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;
139
140 typedef CL_API_ENTRY cl_int
141   (CL_API_CALL *MAGICKpfn_clReleaseProgram)(cl_program program)
142     CL_API_SUFFIX__VERSION_1_0;
143
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;
149
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;
154
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;
159
160
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;
165
166 typedef CL_API_ENTRY cl_int
167   (CL_API_CALL *MAGICKpfn_clReleaseKernel)(cl_kernel kernel)
168     CL_API_SUFFIX__VERSION_1_0;
169
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;
173
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;
178
179
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;
186
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;
192
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;
198
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;
206
207
208 /* Events APIs */
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;
212
213 typedef CL_API_ENTRY cl_int
214   (CL_API_CALL *MAGICKpfn_clReleaseEvent)(cl_event event)
215     CL_API_SUFFIX__VERSION_1_0;
216
217 typedef CL_API_ENTRY cl_int
218   (CL_API_CALL *MAGICKpfn_clRetainEvent)(cl_event event)
219     CL_API_SUFFIX__VERSION_1_0;
220
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;
225
226
227 /* Profiling APIs */
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;
232
233 typedef struct MagickLibraryRec MagickLibrary;
234
235 struct MagickLibraryRec
236 {
237   void *library;
238
239   MAGICKpfn_clGetPlatformIDs          clGetPlatformIDs;
240   MAGICKpfn_clGetPlatformInfo         clGetPlatformInfo;
241
242   MAGICKpfn_clGetDeviceIDs            clGetDeviceIDs;
243   MAGICKpfn_clGetDeviceInfo           clGetDeviceInfo;
244
245   MAGICKpfn_clCreateContext           clCreateContext;
246   MAGICKpfn_clReleaseContext          clReleaseContext;
247
248   MAGICKpfn_clCreateCommandQueue      clCreateCommandQueue;
249   MAGICKpfn_clReleaseCommandQueue     clReleaseCommandQueue;
250   MAGICKpfn_clFlush                   clFlush;
251   MAGICKpfn_clFinish                  clFinish;
252
253   MAGICKpfn_clCreateBuffer            clCreateBuffer;
254   MAGICKpfn_clReleaseMemObject        clReleaseMemObject;
255
256   MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
257   MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
258   MAGICKpfn_clReleaseProgram          clReleaseProgram;
259   MAGICKpfn_clBuildProgram            clBuildProgram;
260   MAGICKpfn_clGetProgramBuildInfo     clGetProgramBuildInfo;
261   MAGICKpfn_clGetProgramInfo          clGetProgramInfo;
262
263   MAGICKpfn_clCreateKernel            clCreateKernel;
264   MAGICKpfn_clReleaseKernel           clReleaseKernel;
265   MAGICKpfn_clSetKernelArg            clSetKernelArg;
266   MAGICKpfn_clGetKernelInfo           clGetKernelInfo;
267
268   MAGICKpfn_clEnqueueReadBuffer       clEnqueueReadBuffer;
269   MAGICKpfn_clEnqueueMapBuffer        clEnqueueMapBuffer;
270   MAGICKpfn_clEnqueueUnmapMemObject   clEnqueueUnmapMemObject;
271   MAGICKpfn_clEnqueueNDRangeKernel    clEnqueueNDRangeKernel;
272
273   MAGICKpfn_clWaitForEvents           clWaitForEvents;
274   MAGICKpfn_clReleaseEvent            clReleaseEvent;
275   MAGICKpfn_clRetainEvent             clRetainEvent;
276   MAGICKpfn_clSetEventCallback        clSetEventCallback;
277
278   MAGICKpfn_clGetEventProfilingInfo   clGetEventProfilingInfo;
279 };
280
281 struct _MagickCLDevice
282 {
283   char
284     *name,
285     *platform_name,
286     *version;
287
288   cl_command_queue
289     command_queues[MAGICKCORE_OPENCL_COMMAND_QUEUES];
290
291   cl_context
292     context;
293
294   cl_device_id
295     deviceID;
296
297   cl_device_type
298     type;
299
300   cl_program
301     program;
302
303   cl_uint
304     max_clock_frequency,
305     max_compute_units;
306
307   cl_ulong
308     local_memory_size;
309
310   double
311     score;
312
313   KernelProfileRecord
314     *profile_records;
315
316   MagickBooleanType
317     enabled,
318     profile_kernels;
319
320   SemaphoreInfo
321     *lock;
322
323   size_t
324     requested;
325
326   ssize_t
327     command_queues_index;
328
329   char
330     *vendor_name;
331 };
332
333 typedef struct _MagickCLEnv
334 {
335   cl_context
336     *contexts;
337
338   double
339     cpu_score;
340
341   MagickBooleanType
342     enabled,
343     initialized;
344
345   MagickCLDevice
346     *devices;
347
348   MagickLibrary
349     *library;
350
351   MagickThreadType
352     benchmark_thread_id;
353
354   SemaphoreInfo
355     *lock;
356
357   size_t
358     number_contexts,
359     number_devices;
360 } *MagickCLEnv;
361
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
402 #endif
403
404 extern MagickPrivate cl_command_queue
405   AcquireOpenCLCommandQueue(MagickCLDevice);
406
407 extern MagickPrivate cl_int
408   SetOpenCLKernelArg(cl_kernel,size_t,size_t,const void *);
409
410 extern MagickPrivate cl_kernel
411   AcquireOpenCLKernel(MagickCLDevice,const char *);
412
413 extern MagickPrivate cl_mem
414   CreateOpenCLBuffer(MagickCLDevice,cl_mem_flags,size_t,void *);
415
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 *,
423     const char *,...),
424   RecordProfileData(MagickCLDevice,cl_kernel,cl_event);
425
426 extern MagickPrivate MagickCLCacheInfo
427   AcquireMagickCLCacheInfo(MagickCLDevice,Quantum *,const MagickSizeType),
428   CopyMagickCLCacheInfo(MagickCLCacheInfo),
429   RelinquishMagickCLCacheInfo(MagickCLCacheInfo,const MagickBooleanType);
430
431 extern MagickPrivate MagickCLDevice
432   RequestOpenCLDevice(MagickCLEnv);
433
434 extern MagickPrivate MagickCLEnv
435   GetCurrentOpenCLEnv(void);
436
437 extern MagickPrivate unsigned long
438   GetOpenCLDeviceLocalMemorySize(const MagickCLDevice);
439
440 extern MagickPrivate void
441   DumpOpenCLProfileData(),
442   OpenCLTerminus(),
443   ReleaseOpenCLCommandQueue(MagickCLDevice,cl_command_queue),
444   ReleaseOpenCLDevice(MagickCLDevice),
445   ReleaseOpenCLKernel(cl_kernel),
446   ReleaseOpenCLMemObject(cl_mem),
447   RetainOpenCLEvent(cl_event);
448
449 #endif
450
451 #if defined(__cplusplus) || defined(c_plusplus)
452 }
453 #endif
454
455 #endif