1 //////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2009 Organic Vectory B.V.
3 // Written by George van Venrooij
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file doc/license/Boost.txt)
7 // Extracted from the CLCC project - http://clcc.sourceforge.net/
8 //////////////////////////////////////////////////////////////////////////
12 #include "util_opencl.h"
14 #ifndef CLCC_GENERATE_DOCUMENTATION
16 #define WIN32_LEAN_AND_MEAN
20 typedef HMODULE CLCC_DYNLIB_HANDLE;
22 #define CLCC_DYNLIB_OPEN LoadLibrary
23 #define CLCC_DYNLIB_CLOSE FreeLibrary
24 #define CLCC_DYNLIB_IMPORT GetProcAddress
28 typedef void* CLCC_DYNLIB_HANDLE;
30 #define CLCC_DYNLIB_OPEN(path) dlopen(path, RTLD_NOW | RTLD_GLOBAL)
31 #define CLCC_DYNLIB_CLOSE dlclose
32 #define CLCC_DYNLIB_IMPORT dlsym
35 //typedef implementation_defined CLCC_DYNLIB_HANDLE;
36 //#define CLCC_DYNLIB_OPEN(path) implementation_defined
37 //#define CLCC_DYNLIB_CLOSE implementation_defined
38 //#define CLCC_DYNLIB_IMPORT implementation_defined
43 //! \brief module handle
44 static CLCC_DYNLIB_HANDLE module = NULL;
46 // Variables holding function entry points
47 #ifndef CLCC_GENERATE_DOCUMENTATION
48 PFNCLGETPLATFORMIDS __clewGetPlatformIDs = NULL;
49 PFNCLGETPLATFORMINFO __clewGetPlatformInfo = NULL;
50 PFNCLGETDEVICEIDS __clewGetDeviceIDs = NULL;
51 PFNCLGETDEVICEINFO __clewGetDeviceInfo = NULL;
52 PFNCLCREATECONTEXT __clewCreateContext = NULL;
53 PFNCLCREATECONTEXTFROMTYPE __clewCreateContextFromType = NULL;
54 PFNCLRETAINCONTEXT __clewRetainContext = NULL;
55 PFNCLRELEASECONTEXT __clewReleaseContext = NULL;
56 PFNCLGETCONTEXTINFO __clewGetContextInfo = NULL;
57 PFNCLCREATECOMMANDQUEUE __clewCreateCommandQueue = NULL;
58 PFNCLRETAINCOMMANDQUEUE __clewRetainCommandQueue = NULL;
59 PFNCLRELEASECOMMANDQUEUE __clewReleaseCommandQueue = NULL;
60 PFNCLGETCOMMANDQUEUEINFO __clewGetCommandQueueInfo = NULL;
61 PFNCLSETCOMMANDQUEUEPROPERTY __clewSetCommandQueueProperty = NULL;
62 PFNCLCREATEBUFFER __clewCreateBuffer = NULL;
63 PFNCLCREATEIMAGE2D __clewCreateImage2D = NULL;
64 PFNCLCREATEIMAGE3D __clewCreateImage3D = NULL;
65 PFNCLRETAINMEMOBJECT __clewRetainMemObject = NULL;
66 PFNCLRELEASEMEMOBJECT __clewReleaseMemObject = NULL;
67 PFNCLGETSUPPORTEDIMAGEFORMATS __clewGetSupportedImageFormats = NULL;
68 PFNCLGETMEMOBJECTINFO __clewGetMemObjectInfo = NULL;
69 PFNCLGETIMAGEINFO __clewGetImageInfo = NULL;
70 PFNCLCREATESAMPLER __clewCreateSampler = NULL;
71 PFNCLRETAINSAMPLER __clewRetainSampler = NULL;
72 PFNCLRELEASESAMPLER __clewReleaseSampler = NULL;
73 PFNCLGETSAMPLERINFO __clewGetSamplerInfo = NULL;
74 PFNCLCREATEPROGRAMWITHSOURCE __clewCreateProgramWithSource = NULL;
75 PFNCLCREATEPROGRAMWITHBINARY __clewCreateProgramWithBinary = NULL;
76 PFNCLRETAINPROGRAM __clewRetainProgram = NULL;
77 PFNCLRELEASEPROGRAM __clewReleaseProgram = NULL;
78 PFNCLBUILDPROGRAM __clewBuildProgram = NULL;
79 PFNCLUNLOADCOMPILER __clewUnloadCompiler = NULL;
80 PFNCLGETPROGRAMINFO __clewGetProgramInfo = NULL;
81 PFNCLGETPROGRAMBUILDINFO __clewGetProgramBuildInfo = NULL;
82 PFNCLCREATEKERNEL __clewCreateKernel = NULL;
83 PFNCLCREATEKERNELSINPROGRAM __clewCreateKernelsInProgram = NULL;
84 PFNCLRETAINKERNEL __clewRetainKernel = NULL;
85 PFNCLRELEASEKERNEL __clewReleaseKernel = NULL;
86 PFNCLSETKERNELARG __clewSetKernelArg = NULL;
87 PFNCLGETKERNELINFO __clewGetKernelInfo = NULL;
88 PFNCLGETKERNELWORKGROUPINFO __clewGetKernelWorkGroupInfo = NULL;
89 PFNCLWAITFOREVENTS __clewWaitForEvents = NULL;
90 PFNCLGETEVENTINFO __clewGetEventInfo = NULL;
91 PFNCLRETAINEVENT __clewRetainEvent = NULL;
92 PFNCLRELEASEEVENT __clewReleaseEvent = NULL;
93 PFNCLGETEVENTPROFILINGINFO __clewGetEventProfilingInfo = NULL;
94 PFNCLFLUSH __clewFlush = NULL;
95 PFNCLFINISH __clewFinish = NULL;
96 PFNCLENQUEUEREADBUFFER __clewEnqueueReadBuffer = NULL;
97 PFNCLENQUEUEWRITEBUFFER __clewEnqueueWriteBuffer = NULL;
98 PFNCLENQUEUECOPYBUFFER __clewEnqueueCopyBuffer = NULL;
99 PFNCLENQUEUEREADIMAGE __clewEnqueueReadImage = NULL;
100 PFNCLENQUEUEWRITEIMAGE __clewEnqueueWriteImage = NULL;
101 PFNCLENQUEUECOPYIMAGE __clewEnqueueCopyImage = NULL;
102 PFNCLENQUEUECOPYIMAGETOBUFFER __clewEnqueueCopyImageToBuffer = NULL;
103 PFNCLENQUEUECOPYBUFFERTOIMAGE __clewEnqueueCopyBufferToImage = NULL;
104 PFNCLENQUEUEMAPBUFFER __clewEnqueueMapBuffer = NULL;
105 PFNCLENQUEUEMAPIMAGE __clewEnqueueMapImage = NULL;
106 PFNCLENQUEUEUNMAPMEMOBJECT __clewEnqueueUnmapMemObject = NULL;
107 PFNCLENQUEUENDRANGEKERNEL __clewEnqueueNDRangeKernel = NULL;
108 PFNCLENQUEUETASK __clewEnqueueTask = NULL;
109 PFNCLENQUEUENATIVEKERNEL __clewEnqueueNativeKernel = NULL;
110 PFNCLENQUEUEMARKER __clewEnqueueMarker = NULL;
111 PFNCLENQUEUEWAITFOREVENTS __clewEnqueueWaitForEvents = NULL;
112 PFNCLENQUEUEBARRIER __clewEnqueueBarrier = NULL;
113 PFNCLGETEXTENSIONFUNCTIONADDRESS __clewGetExtensionFunctionAddress = NULL;
114 #endif // CLCC_GENERATE_DOCUMENTATION
117 //! \brief Unloads OpenCL dynamic library, should not be called directly
118 static void clewExit(void)
123 CLCC_DYNLIB_CLOSE(module);
128 //! \param path path to dynamic library to load
129 //! \return CLEW_ERROR_OPEN_FAILED if the library could not be opened
130 //! CLEW_ERROR_ATEXIT_FAILED if atexit(clewExit) failed
131 //! CLEW_SUCCESS when the library was succesfully loaded
135 const char *path = "OpenCL.dll";
136 #elif defined(__APPLE__)
137 const char *path = "/Library/Frameworks/OpenCL.framework/OpenCL";
139 const char *path = "libOpenCL.so";
143 // Check if already initialized
150 module = CLCC_DYNLIB_OPEN(path);
159 error = atexit(clewExit);
163 // Failure queing atexit, shutdown with error
164 CLCC_DYNLIB_CLOSE(module);
170 // Determine function entry-points
171 __clewGetPlatformIDs = (PFNCLGETPLATFORMIDS )CLCC_DYNLIB_IMPORT(module, "clGetPlatformIDs");
172 __clewGetPlatformInfo = (PFNCLGETPLATFORMINFO )CLCC_DYNLIB_IMPORT(module, "clGetPlatformInfo");
173 __clewGetDeviceIDs = (PFNCLGETDEVICEIDS )CLCC_DYNLIB_IMPORT(module, "clGetDeviceIDs");
174 __clewGetDeviceInfo = (PFNCLGETDEVICEINFO )CLCC_DYNLIB_IMPORT(module, "clGetDeviceInfo");
175 __clewCreateContext = (PFNCLCREATECONTEXT )CLCC_DYNLIB_IMPORT(module, "clCreateContext");
176 __clewCreateContextFromType = (PFNCLCREATECONTEXTFROMTYPE )CLCC_DYNLIB_IMPORT(module, "clCreateContextFromType");
177 __clewRetainContext = (PFNCLRETAINCONTEXT )CLCC_DYNLIB_IMPORT(module, "clRetainContext");
178 __clewReleaseContext = (PFNCLRELEASECONTEXT )CLCC_DYNLIB_IMPORT(module, "clReleaseContext");
179 __clewGetContextInfo = (PFNCLGETCONTEXTINFO )CLCC_DYNLIB_IMPORT(module, "clGetContextInfo");
180 __clewCreateCommandQueue = (PFNCLCREATECOMMANDQUEUE )CLCC_DYNLIB_IMPORT(module, "clCreateCommandQueue");
181 __clewRetainCommandQueue = (PFNCLRETAINCOMMANDQUEUE )CLCC_DYNLIB_IMPORT(module, "clRetainCommandQueue");
182 __clewReleaseCommandQueue = (PFNCLRELEASECOMMANDQUEUE )CLCC_DYNLIB_IMPORT(module, "clReleaseCommandQueue");
183 __clewGetCommandQueueInfo = (PFNCLGETCOMMANDQUEUEINFO )CLCC_DYNLIB_IMPORT(module, "clGetCommandQueueInfo");
184 __clewSetCommandQueueProperty = (PFNCLSETCOMMANDQUEUEPROPERTY )CLCC_DYNLIB_IMPORT(module, "clSetCommandQueueProperty");
185 __clewCreateBuffer = (PFNCLCREATEBUFFER )CLCC_DYNLIB_IMPORT(module, "clCreateBuffer");
186 __clewCreateImage2D = (PFNCLCREATEIMAGE2D )CLCC_DYNLIB_IMPORT(module, "clCreateImage2D");
187 __clewCreateImage3D = (PFNCLCREATEIMAGE3D )CLCC_DYNLIB_IMPORT(module, "clCreateImage3D");
188 __clewRetainMemObject = (PFNCLRETAINMEMOBJECT )CLCC_DYNLIB_IMPORT(module, "clRetainMemObject");
189 __clewReleaseMemObject = (PFNCLRELEASEMEMOBJECT )CLCC_DYNLIB_IMPORT(module, "clReleaseMemObject");
190 __clewGetSupportedImageFormats = (PFNCLGETSUPPORTEDIMAGEFORMATS )CLCC_DYNLIB_IMPORT(module, "clGetSupportedImageFormats");
191 __clewGetMemObjectInfo = (PFNCLGETMEMOBJECTINFO )CLCC_DYNLIB_IMPORT(module, "clGetMemObjectInfo");
192 __clewGetImageInfo = (PFNCLGETIMAGEINFO )CLCC_DYNLIB_IMPORT(module, "clGetImageInfo");
193 __clewCreateSampler = (PFNCLCREATESAMPLER )CLCC_DYNLIB_IMPORT(module, "clCreateSampler");
194 __clewRetainSampler = (PFNCLRETAINSAMPLER )CLCC_DYNLIB_IMPORT(module, "clRetainSampler");
195 __clewReleaseSampler = (PFNCLRELEASESAMPLER )CLCC_DYNLIB_IMPORT(module, "clReleaseSampler");
196 __clewGetSamplerInfo = (PFNCLGETSAMPLERINFO )CLCC_DYNLIB_IMPORT(module, "clGetSamplerInfo");
197 __clewCreateProgramWithSource = (PFNCLCREATEPROGRAMWITHSOURCE )CLCC_DYNLIB_IMPORT(module, "clCreateProgramWithSource");
198 __clewCreateProgramWithBinary = (PFNCLCREATEPROGRAMWITHBINARY )CLCC_DYNLIB_IMPORT(module, "clCreateProgramWithBinary");
199 __clewRetainProgram = (PFNCLRETAINPROGRAM )CLCC_DYNLIB_IMPORT(module, "clRetainProgram");
200 __clewReleaseProgram = (PFNCLRELEASEPROGRAM )CLCC_DYNLIB_IMPORT(module, "clReleaseProgram");
201 __clewBuildProgram = (PFNCLBUILDPROGRAM )CLCC_DYNLIB_IMPORT(module, "clBuildProgram");
202 __clewUnloadCompiler = (PFNCLUNLOADCOMPILER )CLCC_DYNLIB_IMPORT(module, "clUnloadCompiler");
203 __clewGetProgramInfo = (PFNCLGETPROGRAMINFO )CLCC_DYNLIB_IMPORT(module, "clGetProgramInfo");
204 __clewGetProgramBuildInfo = (PFNCLGETPROGRAMBUILDINFO )CLCC_DYNLIB_IMPORT(module, "clGetProgramBuildInfo");
205 __clewCreateKernel = (PFNCLCREATEKERNEL )CLCC_DYNLIB_IMPORT(module, "clCreateKernel");
206 __clewCreateKernelsInProgram = (PFNCLCREATEKERNELSINPROGRAM )CLCC_DYNLIB_IMPORT(module, "clCreateKernelsInProgram");
207 __clewRetainKernel = (PFNCLRETAINKERNEL )CLCC_DYNLIB_IMPORT(module, "clRetainKernel");
208 __clewReleaseKernel = (PFNCLRELEASEKERNEL )CLCC_DYNLIB_IMPORT(module, "clReleaseKernel");
209 __clewSetKernelArg = (PFNCLSETKERNELARG )CLCC_DYNLIB_IMPORT(module, "clSetKernelArg");
210 __clewGetKernelInfo = (PFNCLGETKERNELINFO )CLCC_DYNLIB_IMPORT(module, "clGetKernelInfo");
211 __clewGetKernelWorkGroupInfo = (PFNCLGETKERNELWORKGROUPINFO )CLCC_DYNLIB_IMPORT(module, "clGetKernelWorkGroupInfo");
212 __clewWaitForEvents = (PFNCLWAITFOREVENTS )CLCC_DYNLIB_IMPORT(module, "clWaitForEvents");
213 __clewGetEventInfo = (PFNCLGETEVENTINFO )CLCC_DYNLIB_IMPORT(module, "clGetEventInfo");
214 __clewRetainEvent = (PFNCLRETAINEVENT )CLCC_DYNLIB_IMPORT(module, "clRetainEvent");
215 __clewReleaseEvent = (PFNCLRELEASEEVENT )CLCC_DYNLIB_IMPORT(module, "clReleaseEvent");
216 __clewGetEventProfilingInfo = (PFNCLGETEVENTPROFILINGINFO )CLCC_DYNLIB_IMPORT(module, "clGetEventProfilingInfo");
217 __clewFlush = (PFNCLFLUSH )CLCC_DYNLIB_IMPORT(module, "clFlush");
218 __clewFinish = (PFNCLFINISH )CLCC_DYNLIB_IMPORT(module, "clFinish");
219 __clewEnqueueReadBuffer = (PFNCLENQUEUEREADBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueReadBuffer");
220 __clewEnqueueWriteBuffer = (PFNCLENQUEUEWRITEBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueWriteBuffer");
221 __clewEnqueueCopyBuffer = (PFNCLENQUEUECOPYBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyBuffer");
222 __clewEnqueueReadImage = (PFNCLENQUEUEREADIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueReadImage");
223 __clewEnqueueWriteImage = (PFNCLENQUEUEWRITEIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueWriteImage");
224 __clewEnqueueCopyImage = (PFNCLENQUEUECOPYIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyImage");
225 __clewEnqueueCopyImageToBuffer = (PFNCLENQUEUECOPYIMAGETOBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyImageToBuffer");
226 __clewEnqueueCopyBufferToImage = (PFNCLENQUEUECOPYBUFFERTOIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyBufferToImage");
227 __clewEnqueueMapBuffer = (PFNCLENQUEUEMAPBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueMapBuffer");
228 __clewEnqueueMapImage = (PFNCLENQUEUEMAPIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueMapImage");
229 __clewEnqueueUnmapMemObject = (PFNCLENQUEUEUNMAPMEMOBJECT )CLCC_DYNLIB_IMPORT(module, "clEnqueueUnmapMemObject");
230 __clewEnqueueNDRangeKernel = (PFNCLENQUEUENDRANGEKERNEL )CLCC_DYNLIB_IMPORT(module, "clEnqueueNDRangeKernel");
231 __clewEnqueueTask = (PFNCLENQUEUETASK )CLCC_DYNLIB_IMPORT(module, "clEnqueueTask");
232 __clewEnqueueNativeKernel = (PFNCLENQUEUENATIVEKERNEL )CLCC_DYNLIB_IMPORT(module, "clEnqueueNativeKernel");
233 __clewEnqueueMarker = (PFNCLENQUEUEMARKER )CLCC_DYNLIB_IMPORT(module, "clEnqueueMarker");
234 __clewEnqueueWaitForEvents = (PFNCLENQUEUEWAITFOREVENTS )CLCC_DYNLIB_IMPORT(module, "clEnqueueWaitForEvents");
235 __clewEnqueueBarrier = (PFNCLENQUEUEBARRIER )CLCC_DYNLIB_IMPORT(module, "clEnqueueBarrier");
236 __clewGetExtensionFunctionAddress = (PFNCLGETEXTENSIONFUNCTIONADDRESS )CLCC_DYNLIB_IMPORT(module, "clGetExtensionFunctionAddress");
241 //! \param error CL error code
242 //! \return a string representation of the error code
243 const char *clErrorString(cl_int error)
245 static const char* strings[] =
249 , "CL_DEVICE_NOT_FOUND" // -1
250 , "CL_DEVICE_NOT_AVAILABLE" // -2
251 , "CL_COMPILER_NOT_AVAILABLE" // -3
252 , "CL_MEM_OBJECT_ALLOCATION_FAILURE" // -4
253 , "CL_OUT_OF_RESOURCES" // -5
254 , "CL_OUT_OF_HOST_MEMORY" // -6
255 , "CL_PROFILING_INFO_NOT_AVAILABLE" // -7
256 , "CL_MEM_COPY_OVERLAP" // -8
257 , "CL_IMAGE_FORMAT_MISMATCH" // -9
258 , "CL_IMAGE_FORMAT_NOT_SUPPORTED" // -10
259 , "CL_BUILD_PROGRAM_FAILURE" // -11
260 , "CL_MAP_FAILURE" // -12
281 , "CL_INVALID_VALUE" // -30
282 , "CL_INVALID_DEVICE_TYPE" // -31
283 , "CL_INVALID_PLATFORM" // -32
284 , "CL_INVALID_DEVICE" // -33
285 , "CL_INVALID_CONTEXT" // -34
286 , "CL_INVALID_QUEUE_PROPERTIES" // -35
287 , "CL_INVALID_COMMAND_QUEUE" // -36
288 , "CL_INVALID_HOST_PTR" // -37
289 , "CL_INVALID_MEM_OBJECT" // -38
290 , "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR" // -39
291 , "CL_INVALID_IMAGE_SIZE" // -40
292 , "CL_INVALID_SAMPLER" // -41
293 , "CL_INVALID_BINARY" // -42
294 , "CL_INVALID_BUILD_OPTIONS" // -43
295 , "CL_INVALID_PROGRAM" // -44
296 , "CL_INVALID_PROGRAM_EXECUTABLE" // -45
297 , "CL_INVALID_KERNEL_NAME" // -46
298 , "CL_INVALID_KERNEL_DEFINITION" // -47
299 , "CL_INVALID_KERNEL" // -48
300 , "CL_INVALID_ARG_INDEX" // -49
301 , "CL_INVALID_ARG_VALUE" // -50
302 , "CL_INVALID_ARG_SIZE" // -51
303 , "CL_INVALID_KERNEL_ARGS" // -52
304 , "CL_INVALID_WORK_DIMENSION" // -53
305 , "CL_INVALID_WORK_GROUP_SIZE" // -54
306 , "CL_INVALID_WORK_ITEM_SIZE" // -55
307 , "CL_INVALID_GLOBAL_OFFSET" // -56
308 , "CL_INVALID_EVENT_WAIT_LIST" // -57
309 , "CL_INVALID_EVENT" // -58
310 , "CL_INVALID_OPERATION" // -59
311 , "CL_INVALID_GL_OBJECT" // -60
312 , "CL_INVALID_BUFFER_SIZE" // -61
313 , "CL_INVALID_MIP_LEVEL" // -62
314 , "CL_INVALID_GLOBAL_WORK_SIZE" // -63
317 return strings[-error];