]> 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 https://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_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;
213
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;
217
218 typedef CL_API_ENTRY cl_int
219   (CL_API_CALL *MAGICKpfn_clReleaseEvent)(cl_event event)
220     CL_API_SUFFIX__VERSION_1_0;
221
222 typedef CL_API_ENTRY cl_int
223   (CL_API_CALL *MAGICKpfn_clRetainEvent)(cl_event event)
224     CL_API_SUFFIX__VERSION_1_0;
225
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;
230
231
232 /* Profiling APIs */
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;
237
238 typedef struct MagickLibraryRec MagickLibrary;
239
240 struct MagickLibraryRec
241 {
242   void *library;
243
244   MAGICKpfn_clGetPlatformIDs          clGetPlatformIDs;
245   MAGICKpfn_clGetPlatformInfo         clGetPlatformInfo;
246
247   MAGICKpfn_clGetDeviceIDs            clGetDeviceIDs;
248   MAGICKpfn_clGetDeviceInfo           clGetDeviceInfo;
249
250   MAGICKpfn_clCreateContext           clCreateContext;
251   MAGICKpfn_clReleaseContext          clReleaseContext;
252
253   MAGICKpfn_clCreateCommandQueue      clCreateCommandQueue;
254   MAGICKpfn_clReleaseCommandQueue     clReleaseCommandQueue;
255   MAGICKpfn_clFlush                   clFlush;
256   MAGICKpfn_clFinish                  clFinish;
257
258   MAGICKpfn_clCreateBuffer            clCreateBuffer;
259   MAGICKpfn_clReleaseMemObject        clReleaseMemObject;
260
261   MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
262   MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
263   MAGICKpfn_clReleaseProgram          clReleaseProgram;
264   MAGICKpfn_clBuildProgram            clBuildProgram;
265   MAGICKpfn_clGetProgramBuildInfo     clGetProgramBuildInfo;
266   MAGICKpfn_clGetProgramInfo          clGetProgramInfo;
267
268   MAGICKpfn_clCreateKernel            clCreateKernel;
269   MAGICKpfn_clReleaseKernel           clReleaseKernel;
270   MAGICKpfn_clSetKernelArg            clSetKernelArg;
271   MAGICKpfn_clGetKernelInfo           clGetKernelInfo;
272
273   MAGICKpfn_clEnqueueReadBuffer       clEnqueueReadBuffer;
274   MAGICKpfn_clEnqueueMapBuffer        clEnqueueMapBuffer;
275   MAGICKpfn_clEnqueueUnmapMemObject   clEnqueueUnmapMemObject;
276   MAGICKpfn_clEnqueueNDRangeKernel    clEnqueueNDRangeKernel;
277
278   MAGICKpfn_clGetEventInfo            clGetEventInfo;
279   MAGICKpfn_clWaitForEvents           clWaitForEvents;
280   MAGICKpfn_clReleaseEvent            clReleaseEvent;
281   MAGICKpfn_clRetainEvent             clRetainEvent;
282   MAGICKpfn_clSetEventCallback        clSetEventCallback;
283
284   MAGICKpfn_clGetEventProfilingInfo   clGetEventProfilingInfo;
285 };
286
287 struct _MagickCLDevice
288 {
289   char
290     *name,
291     *platform_name,
292     *version;
293
294   cl_command_queue
295     command_queues[MAGICKCORE_OPENCL_COMMAND_QUEUES];
296
297   cl_context
298     context;
299
300   cl_device_id
301     deviceID;
302
303   cl_device_type
304     type;
305
306   cl_program
307     program;
308
309   cl_uint
310     max_clock_frequency,
311     max_compute_units;
312
313   cl_ulong
314     local_memory_size;
315
316   double
317     score;
318
319   KernelProfileRecord
320     *profile_records;
321
322   MagickBooleanType
323     enabled,
324     profile_kernels;
325
326   SemaphoreInfo
327     *lock;
328
329   size_t
330     requested;
331
332   ssize_t
333     command_queues_index;
334
335   char
336     *vendor_name;
337 };
338
339 typedef struct _MagickCLEnv
340 {
341   cl_context
342     *contexts;
343
344   double
345     cpu_score;
346
347   MagickBooleanType
348     enabled,
349     initialized;
350
351   MagickCLDevice
352     *devices;
353
354   MagickLibrary
355     *library;
356
357   MagickThreadType
358     benchmark_thread_id;
359
360   SemaphoreInfo
361     *lock;
362
363   size_t
364     number_contexts,
365     number_devices;
366 } *MagickCLEnv;
367
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
408 #endif
409
410 extern MagickPrivate cl_command_queue
411   AcquireOpenCLCommandQueue(MagickCLDevice);
412
413 extern MagickPrivate cl_int
414   SetOpenCLKernelArg(cl_kernel,size_t,size_t,const void *);
415
416 extern MagickPrivate cl_kernel
417   AcquireOpenCLKernel(MagickCLDevice,const char *);
418
419 extern MagickPrivate cl_mem
420   CreateOpenCLBuffer(MagickCLDevice,cl_mem_flags,size_t,void *);
421
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 *,
429     const char *,...),
430   RecordProfileData(MagickCLDevice,cl_kernel,cl_event);
431
432 extern MagickPrivate MagickCLCacheInfo
433   AcquireMagickCLCacheInfo(MagickCLDevice,Quantum *,const MagickSizeType),
434   CopyMagickCLCacheInfo(MagickCLCacheInfo),
435   RelinquishMagickCLCacheInfo(MagickCLCacheInfo,const MagickBooleanType);
436
437 extern MagickPrivate MagickCLDevice
438   RequestOpenCLDevice(MagickCLEnv);
439
440 extern MagickPrivate MagickCLEnv
441   GetCurrentOpenCLEnv(void);
442
443 extern MagickPrivate unsigned long
444   GetOpenCLDeviceLocalMemorySize(const MagickCLDevice);
445
446 extern MagickPrivate void
447   DumpOpenCLProfileData(),
448   OpenCLTerminus(),
449   ReleaseOpenCLCommandQueue(MagickCLDevice,cl_command_queue),
450   ReleaseOpenCLDevice(MagickCLDevice),
451   ReleaseOpenCLKernel(cl_kernel),
452   ReleaseOpenCLMemObject(cl_mem),
453   RetainOpenCLEvent(cl_event);
454
455 #endif
456
457 #if defined(__cplusplus) || defined(c_plusplus)
458 }
459 #endif
460
461 #endif