opencl-private.h

Go to the documentation of this file.
00001 /*
00002 Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization
00003 dedicated to making software imaging solutions freely available.
00004 
00005 You may not use this file except in compliance with the License.  You may
00006 obtain a copy of the License at
00007 
00008 https://imagemagick.org/script/license.php
00009 
00010 Unless required by applicable law or agreed to in writing, software
00011 distributed under the License is distributed on an "AS IS" BASIS,
00012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 See the License for the specific language governing permissions and
00014 limitations under the License.
00015 
00016 MagickCore OpenCL private methods.
00017 */
00018 #ifndef MAGICKCORE_OPENCL_PRIVATE_H
00019 #define MAGICKCORE_OPENCL_PRIVATE_H
00020 
00021 /*
00022 Include declarations.
00023 */
00024 #include "MagickCore/studio.h"
00025 #include "MagickCore/opencl.h"
00026 #include "MagickCore/thread_.h"
00027 
00028 #if defined(__cplusplus) || defined(c_plusplus)
00029 extern "C" {
00030 #endif
00031 
00032 #if !defined(MAGICKCORE_OPENCL_SUPPORT)
00033 typedef void* MagickCLCacheInfo;
00034 #else
00035 typedef struct _MagickCLCacheInfo
00036 {
00037   cl_event
00038     *events;
00039 
00040   cl_mem
00041     buffer;
00042 
00043   cl_uint
00044     event_count;
00045 
00046   MagickCLDevice
00047     device;
00048 
00049   MagickSizeType
00050     length;
00051 
00052   Quantum
00053     *pixels;
00054 
00055   SemaphoreInfo
00056     *events_semaphore;
00057 }* MagickCLCacheInfo;
00058 
00059 /*
00060   Define declarations.
00061 */
00062 #define MAGICKCORE_OPENCL_UNDEFINED_SCORE -1.0
00063 #define MAGICKCORE_OPENCL_COMMAND_QUEUES 16
00064 
00065 /* Platform APIs */
00066 typedef CL_API_ENTRY cl_int
00067   (CL_API_CALL *MAGICKpfn_clGetPlatformIDs)(cl_uint num_entries,
00068     cl_platform_id *platforms,cl_uint *num_platforms) CL_API_SUFFIX__VERSION_1_0;
00069 
00070 typedef CL_API_ENTRY cl_int
00071   (CL_API_CALL *MAGICKpfn_clGetPlatformInfo)(cl_platform_id platform,
00072     cl_platform_info param_name,size_t param_value_size,void *param_value,
00073     size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
00074 
00075 
00076 /* Device APIs */
00077 typedef CL_API_ENTRY cl_int
00078   (CL_API_CALL *MAGICKpfn_clGetDeviceIDs)(cl_platform_id platform,
00079     cl_device_type device_type,cl_uint num_entries,cl_device_id *devices,
00080     cl_uint *num_devices) CL_API_SUFFIX__VERSION_1_0;
00081 
00082 typedef CL_API_ENTRY cl_int
00083   (CL_API_CALL *MAGICKpfn_clGetDeviceInfo)(cl_device_id device,
00084     cl_device_info param_name,size_t param_value_size,void *param_value,
00085     size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
00086 
00087 
00088 /* Context APIs */
00089 typedef CL_API_ENTRY cl_context
00090   (CL_API_CALL *MAGICKpfn_clCreateContext)(
00091     const cl_context_properties *properties,cl_uint num_devices,
00092     const cl_device_id *devices,void (CL_CALLBACK *pfn_notify)(const char *,
00093     const void *,size_t,void *),void *user_data,cl_int *errcode_ret)
00094     CL_API_SUFFIX__VERSION_1_0;
00095 
00096 typedef CL_API_ENTRY cl_int
00097   (CL_API_CALL *MAGICKpfn_clReleaseContext)(cl_context context)
00098     CL_API_SUFFIX__VERSION_1_0;
00099 
00100 
00101 /* Command Queue APIs */
00102 typedef CL_API_ENTRY cl_command_queue
00103   (CL_API_CALL *MAGICKpfn_clCreateCommandQueue)(cl_context context,
00104     cl_device_id device,cl_command_queue_properties properties,
00105     cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
00106 
00107 typedef CL_API_ENTRY cl_int
00108   (CL_API_CALL *MAGICKpfn_clReleaseCommandQueue)(
00109     cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0;
00110 
00111 typedef CL_API_ENTRY cl_int
00112   (CL_API_CALL *MAGICKpfn_clFlush)(cl_command_queue command_queue)
00113     CL_API_SUFFIX__VERSION_1_0;
00114 
00115 typedef CL_API_ENTRY cl_int
00116   (CL_API_CALL *MAGICKpfn_clFinish)(cl_command_queue command_queue)
00117     CL_API_SUFFIX__VERSION_1_0;
00118 
00119 
00120 /* Memory Object APIs */
00121 typedef CL_API_ENTRY cl_mem
00122   (CL_API_CALL *MAGICKpfn_clCreateBuffer)(cl_context context,
00123     cl_mem_flags flags,size_t size,void *host_ptr,cl_int *errcode_ret)
00124     CL_API_SUFFIX__VERSION_1_0;
00125 
00126 typedef CL_API_ENTRY cl_int
00127   (CL_API_CALL *MAGICKpfn_clRetainMemObject)(cl_mem memobj)
00128     CL_API_SUFFIX__VERSION_1_0;
00129 
00130 typedef CL_API_ENTRY cl_int
00131   (CL_API_CALL *MAGICKpfn_clReleaseMemObject)(cl_mem memobj)
00132     CL_API_SUFFIX__VERSION_1_0;
00133 
00134 
00135 /* Program Object APIs */
00136 typedef CL_API_ENTRY cl_program
00137   (CL_API_CALL *MAGICKpfn_clCreateProgramWithSource)(cl_context context,
00138     cl_uint count,const char **strings,const size_t *lengths,
00139     cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
00140 
00141 typedef CL_API_ENTRY cl_program
00142   (CL_API_CALL *MAGICKpfn_clCreateProgramWithBinary)(cl_context context,
00143     cl_uint num_devices,const cl_device_id *device_list,const size_t *lengths,
00144     const unsigned char **binaries,cl_int *binary_status,cl_int *errcode_ret)
00145     CL_API_SUFFIX__VERSION_1_0;
00146 
00147 typedef CL_API_ENTRY cl_int
00148   (CL_API_CALL *MAGICKpfn_clReleaseProgram)(cl_program program)
00149     CL_API_SUFFIX__VERSION_1_0;
00150 
00151 typedef CL_API_ENTRY cl_int
00152   (CL_API_CALL *MAGICKpfn_clBuildProgram)(cl_program program,
00153     cl_uint num_devices,const cl_device_id *device_list,const char *options,
00154     void (CL_CALLBACK *pfn_notify)(cl_program program,void * user_data),
00155     void *user_data) CL_API_SUFFIX__VERSION_1_0;
00156 
00157 typedef CL_API_ENTRY cl_int
00158   (CL_API_CALL *MAGICKpfn_clGetProgramBuildInfo)(cl_program program,
00159     cl_device_id device,cl_program_build_info param_name,size_t param_value_size,
00160     void *param_value,size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
00161 
00162 typedef CL_API_ENTRY cl_int
00163   (CL_API_CALL *MAGICKpfn_clGetProgramInfo)(cl_program program,
00164     cl_program_info param_name,size_t param_value_size,void *param_value,
00165     size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
00166 
00167 
00168 /* Kernel Object APIs */
00169 typedef CL_API_ENTRY cl_kernel
00170   (CL_API_CALL *MAGICKpfn_clCreateKernel)(cl_program program,
00171     const char *kernel_name,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
00172 
00173 typedef CL_API_ENTRY cl_int
00174   (CL_API_CALL *MAGICKpfn_clReleaseKernel)(cl_kernel kernel)
00175     CL_API_SUFFIX__VERSION_1_0;
00176 
00177 typedef CL_API_ENTRY cl_int
00178   (CL_API_CALL *MAGICKpfn_clSetKernelArg)(cl_kernel kernel,cl_uint arg_index,
00179   size_t arg_size,const void * arg_value) CL_API_SUFFIX__VERSION_1_0;
00180 
00181 typedef CL_API_ENTRY cl_int
00182   (CL_API_CALL *MAGICKpfn_clGetKernelInfo)(cl_kernel kernel,
00183     cl_kernel_info param_name,size_t param_value_size,void *param_value,
00184     size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
00185 
00186 
00187 /* Enqueued Commands APIs */
00188 typedef CL_API_ENTRY cl_int
00189   (CL_API_CALL *MAGICKpfn_clEnqueueReadBuffer)(cl_command_queue command_queue,
00190     cl_mem buffer,cl_bool blocking_read,size_t offset,size_t cb,void *ptr,
00191     cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
00192     cl_event *event) CL_API_SUFFIX__VERSION_1_0;
00193 
00194 typedef CL_API_ENTRY void
00195   *(CL_API_CALL *MAGICKpfn_clEnqueueMapBuffer)(cl_command_queue command_queue,
00196     cl_mem buffer,cl_bool blocking_map,cl_map_flags map_flags,size_t offset,
00197     size_t cb,cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
00198     cl_event *event,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
00199 
00200 typedef CL_API_ENTRY cl_int
00201   (CL_API_CALL *MAGICKpfn_clEnqueueUnmapMemObject)(
00202     cl_command_queue command_queue,cl_mem memobj,void *mapped_ptr,
00203     cl_uint num_events_in_wait_list,const cl_event *event_wait_list,
00204     cl_event *event) CL_API_SUFFIX__VERSION_1_0;
00205 
00206 typedef CL_API_ENTRY cl_int
00207   (CL_API_CALL *MAGICKpfn_clEnqueueNDRangeKernel)(
00208     cl_command_queue command_queue,cl_kernel kernel,cl_uint work_dim,
00209     const size_t *global_work_offset,const size_t *global_work_size,
00210     const size_t *local_work_size,cl_uint num_events_in_wait_list,
00211     const cl_event * event_wait_list,cl_event *event)
00212     CL_API_SUFFIX__VERSION_1_0;
00213 
00214 
00215 /* Events APIs */
00216 typedef CL_API_ENTRY cl_int
00217   (CL_API_CALL *MAGICKpfn_clGetEventInfo)(cl_event event,
00218     cl_profiling_info param_name,size_t param_value_size,void *param_value,
00219     size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
00220 
00221 typedef CL_API_ENTRY cl_int
00222   (CL_API_CALL *MAGICKpfn_clWaitForEvents)(cl_uint num_events,
00223     const cl_event *event_list) CL_API_SUFFIX__VERSION_1_0;
00224 
00225 typedef CL_API_ENTRY cl_int
00226   (CL_API_CALL *MAGICKpfn_clReleaseEvent)(cl_event event)
00227     CL_API_SUFFIX__VERSION_1_0;
00228 
00229 typedef CL_API_ENTRY cl_int
00230   (CL_API_CALL *MAGICKpfn_clRetainEvent)(cl_event event)
00231     CL_API_SUFFIX__VERSION_1_0;
00232 
00233 typedef CL_API_ENTRY cl_int
00234   (CL_API_CALL *MAGICKpfn_clSetEventCallback)(cl_event event,
00235     cl_int command_exec_callback_type,void (CL_CALLBACK *MAGICKpfn_notify)(
00236       cl_event,cl_int,void *),void *user_data) CL_API_SUFFIX__VERSION_1_1;
00237 
00238 
00239 /* Profiling APIs */
00240 typedef CL_API_ENTRY cl_int
00241   (CL_API_CALL *MAGICKpfn_clGetEventProfilingInfo)(cl_event event,
00242     cl_profiling_info param_name,size_t param_value_size,void *param_value,
00243     size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
00244 
00245 typedef struct MagickLibraryRec MagickLibrary;
00246 
00247 struct MagickLibraryRec
00248 {
00249   void *library;
00250 
00251   MAGICKpfn_clGetPlatformIDs          clGetPlatformIDs;
00252   MAGICKpfn_clGetPlatformInfo         clGetPlatformInfo;
00253 
00254   MAGICKpfn_clGetDeviceIDs            clGetDeviceIDs;
00255   MAGICKpfn_clGetDeviceInfo           clGetDeviceInfo;
00256 
00257   MAGICKpfn_clCreateContext           clCreateContext;
00258   MAGICKpfn_clReleaseContext          clReleaseContext;
00259 
00260   MAGICKpfn_clCreateCommandQueue      clCreateCommandQueue;
00261   MAGICKpfn_clReleaseCommandQueue     clReleaseCommandQueue;
00262   MAGICKpfn_clFlush                   clFlush;
00263   MAGICKpfn_clFinish                  clFinish;
00264 
00265   MAGICKpfn_clCreateBuffer            clCreateBuffer;
00266   MAGICKpfn_clRetainMemObject         clRetainMemObject;
00267   MAGICKpfn_clReleaseMemObject        clReleaseMemObject;
00268 
00269   MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
00270   MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
00271   MAGICKpfn_clReleaseProgram          clReleaseProgram;
00272   MAGICKpfn_clBuildProgram            clBuildProgram;
00273   MAGICKpfn_clGetProgramBuildInfo     clGetProgramBuildInfo;
00274   MAGICKpfn_clGetProgramInfo          clGetProgramInfo;
00275 
00276   MAGICKpfn_clCreateKernel            clCreateKernel;
00277   MAGICKpfn_clReleaseKernel           clReleaseKernel;
00278   MAGICKpfn_clSetKernelArg            clSetKernelArg;
00279   MAGICKpfn_clGetKernelInfo           clGetKernelInfo;
00280 
00281   MAGICKpfn_clEnqueueReadBuffer       clEnqueueReadBuffer;
00282   MAGICKpfn_clEnqueueMapBuffer        clEnqueueMapBuffer;
00283   MAGICKpfn_clEnqueueUnmapMemObject   clEnqueueUnmapMemObject;
00284   MAGICKpfn_clEnqueueNDRangeKernel    clEnqueueNDRangeKernel;
00285 
00286   MAGICKpfn_clGetEventInfo            clGetEventInfo;
00287   MAGICKpfn_clWaitForEvents           clWaitForEvents;
00288   MAGICKpfn_clReleaseEvent            clReleaseEvent;
00289   MAGICKpfn_clRetainEvent             clRetainEvent;
00290   MAGICKpfn_clSetEventCallback        clSetEventCallback;
00291 
00292   MAGICKpfn_clGetEventProfilingInfo   clGetEventProfilingInfo;
00293 };
00294 
00295 struct _MagickCLDevice
00296 {
00297   char
00298     *name,
00299     *platform_name,
00300     *version;
00301 
00302   cl_command_queue
00303     command_queues[MAGICKCORE_OPENCL_COMMAND_QUEUES];
00304 
00305   cl_context
00306     context;
00307 
00308   cl_device_id
00309     deviceID;
00310 
00311   cl_device_type
00312     type;
00313 
00314   cl_program
00315     program;
00316 
00317   cl_uint
00318     max_clock_frequency,
00319     max_compute_units;
00320 
00321   cl_ulong
00322     local_memory_size;
00323 
00324   double
00325     score;
00326 
00327   KernelProfileRecord
00328     *profile_records;
00329 
00330   MagickBooleanType
00331     enabled,
00332     profile_kernels;
00333 
00334   SemaphoreInfo
00335     *lock;
00336 
00337   size_t
00338     requested;
00339 
00340   ssize_t
00341     command_queues_index;
00342 
00343   char
00344     *vendor_name;
00345 };
00346 
00347 typedef struct _MagickCLEnv
00348 {
00349   cl_context
00350     *contexts;
00351 
00352   double
00353     cpu_score;
00354 
00355   MagickBooleanType
00356     enabled,
00357     initialized;
00358 
00359   MagickCLDevice
00360     *devices;
00361 
00362   MagickLibrary
00363     *library;
00364 
00365   MagickThreadType
00366     benchmark_thread_id;
00367 
00368   SemaphoreInfo
00369     *lock;
00370 
00371   size_t
00372     number_contexts,
00373     number_devices;
00374 } *MagickCLEnv;
00375 
00376 #if defined(MAGICKCORE_HDRI_SUPPORT)
00377 #define CLOptions "-cl-single-precision-constant -cl-mad-enable -DMAGICKCORE_HDRI_SUPPORT=1 "\
00378   "-DCLQuantum=float -DCLSignedQuantum=float -DCLPixelType=float4 -DQuantumRange=%ff " \
00379   "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
00380   "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
00381 #define CLQuantum  cl_float
00382 #define CLPixelPacket  cl_float4
00383 #define CLCharQuantumScale 1.0f
00384 #elif (MAGICKCORE_QUANTUM_DEPTH == 8)
00385 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
00386   "-DCLQuantum=uchar -DCLSignedQuantum=char -DCLPixelType=uchar4 -DQuantumRange=%ff " \
00387   "-DQuantumScale=%ff -DCharQuantumScale=%ff -DMagickEpsilon=%ff -DMagickPI=%ff "\
00388   "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
00389 #define CLQuantum  cl_uchar
00390 #define CLPixelPacket  cl_uchar4
00391 #define CLCharQuantumScale 1.0f
00392 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
00393 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
00394   "-DCLQuantum=ushort -DCLSignedQuantum=short -DCLPixelType=ushort4 -DQuantumRange=%ff "\
00395   "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
00396   "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
00397 #define CLQuantum  cl_ushort
00398 #define CLPixelPacket  cl_ushort4
00399 #define CLCharQuantumScale 257.0f
00400 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
00401 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
00402   "-DCLQuantum=uint -DCLSignedQuantum=int -DCLPixelType=uint4 -DQuantumRange=%ff "\
00403   "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
00404   "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
00405 #define CLQuantum  cl_uint
00406 #define CLPixelPacket  cl_uint4
00407 #define CLCharQuantumScale 16843009.0f
00408 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
00409 #define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
00410   "-DCLQuantum=ulong -DCLSignedQuantum=long -DCLPixelType=ulong4 -DQuantumRange=%ff "\
00411   "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
00412   "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
00413 #define CLQuantum  cl_ulong
00414 #define CLPixelPacket  cl_ulong4
00415 #define CLCharQuantumScale 72340172838076673.0f
00416 #endif
00417 
00418 extern MagickPrivate cl_command_queue
00419   AcquireOpenCLCommandQueue(MagickCLDevice);
00420 
00421 extern MagickPrivate cl_int
00422   SetOpenCLKernelArg(cl_kernel,size_t,size_t,const void *);
00423 
00424 extern MagickPrivate cl_kernel
00425   AcquireOpenCLKernel(MagickCLDevice,const char *);
00426 
00427 extern MagickPrivate cl_mem
00428   CreateOpenCLBuffer(MagickCLDevice,cl_mem_flags,size_t,void *);
00429 
00430 extern MagickPrivate MagickBooleanType
00431   EnqueueOpenCLKernel(cl_command_queue,cl_kernel,cl_uint,const size_t *,
00432     const size_t *,const size_t *,const Image *,const Image *,
00433     MagickBooleanType,ExceptionInfo *),
00434   InitializeOpenCL(MagickCLEnv,ExceptionInfo *),
00435   OpenCLThrowMagickException(MagickCLDevice,ExceptionInfo *,
00436     const char *,const char *,const size_t,const ExceptionType,const char *,
00437     const char *,...),
00438   RecordProfileData(MagickCLDevice,cl_kernel,cl_event);
00439 
00440 extern MagickPrivate MagickCLCacheInfo
00441   AcquireMagickCLCacheInfo(MagickCLDevice,Quantum *,const MagickSizeType),
00442   CopyMagickCLCacheInfo(MagickCLCacheInfo),
00443   RelinquishMagickCLCacheInfo(MagickCLCacheInfo,const MagickBooleanType);
00444 
00445 extern MagickPrivate MagickCLDevice
00446   RequestOpenCLDevice(MagickCLEnv);
00447 
00448 extern MagickPrivate MagickCLEnv
00449   GetCurrentOpenCLEnv(void);
00450 
00451 extern MagickPrivate unsigned long
00452   GetOpenCLDeviceLocalMemorySize(const MagickCLDevice);
00453 
00454 extern MagickPrivate void
00455   DumpOpenCLProfileData(),
00456   OpenCLTerminus(),
00457   ReleaseOpenCLCommandQueue(MagickCLDevice,cl_command_queue),
00458   ReleaseOpenCLDevice(MagickCLDevice),
00459   ReleaseOpenCLKernel(cl_kernel),
00460   ReleaseOpenCLMemObject(cl_mem),
00461   RetainOpenCLEvent(cl_event),
00462   RetainOpenCLMemObject(cl_mem);
00463 
00464 #endif
00465 
00466 #if defined(__cplusplus) || defined(c_plusplus)
00467 }
00468 #endif
00469 
00470 #endif

Generated on 27 Apr 2020 for MagickCore by  doxygen 1.6.1