2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * Gabriele Gristina <matrix@hashcat.net>
8 #include <ext_OpenCL.h>
10 const char *val2cstr_cl (cl_int CL_err
)
12 #define CLERR(a) case a: return #a
16 CLERR (CL_BUILD_PROGRAM_FAILURE
);
17 CLERR (CL_COMPILER_NOT_AVAILABLE
);
18 CLERR (CL_DEVICE_NOT_FOUND
);
19 CLERR (CL_INVALID_ARG_INDEX
);
20 CLERR (CL_INVALID_ARG_SIZE
);
21 CLERR (CL_INVALID_ARG_VALUE
);
22 CLERR (CL_INVALID_BINARY
);
23 CLERR (CL_INVALID_BUFFER_SIZE
);
24 CLERR (CL_INVALID_BUILD_OPTIONS
);
25 CLERR (CL_INVALID_COMMAND_QUEUE
);
26 CLERR (CL_INVALID_CONTEXT
);
27 CLERR (CL_INVALID_DEVICE
);
28 CLERR (CL_INVALID_DEVICE_TYPE
);
29 CLERR (CL_INVALID_EVENT
);
30 CLERR (CL_INVALID_EVENT_WAIT_LIST
);
31 CLERR (CL_INVALID_GLOBAL_OFFSET
);
32 CLERR (CL_INVALID_HOST_PTR
);
33 CLERR (CL_INVALID_KERNEL
);
34 CLERR (CL_INVALID_KERNEL_ARGS
);
35 CLERR (CL_INVALID_KERNEL_DEFINITION
);
36 CLERR (CL_INVALID_KERNEL_NAME
);
37 CLERR (CL_INVALID_MEM_OBJECT
);
38 CLERR (CL_INVALID_OPERATION
);
39 CLERR (CL_INVALID_PLATFORM
);
40 CLERR (CL_INVALID_PROGRAM
);
41 CLERR (CL_INVALID_PROGRAM_EXECUTABLE
);
42 CLERR (CL_INVALID_QUEUE_PROPERTIES
);
43 CLERR (CL_INVALID_SAMPLER
);
44 CLERR (CL_INVALID_VALUE
);
45 CLERR (CL_INVALID_WORK_DIMENSION
);
46 CLERR (CL_INVALID_WORK_GROUP_SIZE
);
47 CLERR (CL_INVALID_WORK_ITEM_SIZE
);
48 CLERR (CL_MISALIGNED_SUB_BUFFER_OFFSET
);
49 CLERR (CL_MAP_FAILURE
);
50 CLERR (CL_MEM_COPY_OVERLAP
);
51 CLERR (CL_MEM_OBJECT_ALLOCATION_FAILURE
);
52 CLERR (CL_OUT_OF_HOST_MEMORY
);
53 CLERR (CL_OUT_OF_RESOURCES
);
56 return "CL_UNKNOWN_ERROR";
59 #define LOAD_FUNC(ptr,name,type) \
60 ptr->name = (type) hc_dlsym (ptr->lib, #name); \
62 log_error ("ERROR: #name is missing from opencl shared library"); \
66 void ocl_init (OCL_PTR
*ocl
)
68 memset (ocl
, 0, sizeof (hc_opencl_lib_t
));
71 ocl
->lib
= hc_dlopen ("OpenCL");
73 ocl
->lib
= hc_dlopen ("/System/Library/Frameworks/OpenCL.framework/OpenCL", RTLD_NOW
);
75 ocl
->lib
= hc_dlopen ("libOpenCL.so", RTLD_NOW
);
80 log_error ("ERROR: cannot load opencl library");
85 LOAD_FUNC(ocl
, clBuildProgram
, OCL_CLBUILDPROGRAM
)
86 LOAD_FUNC(ocl
, clCreateBuffer
, OCL_CLCREATEBUFFER
)
87 LOAD_FUNC(ocl
, clCreateCommandQueue
, OCL_CLCREATECOMMANDQUEUE
)
88 LOAD_FUNC(ocl
, clCreateContext
, OCL_CLCREATECONTEXT
)
89 LOAD_FUNC(ocl
, clCreateKernel
, OCL_CLCREATEKERNEL
)
90 LOAD_FUNC(ocl
, clCreateProgramWithBinary
, OCL_CLCREATEPROGRAMWITHBINARY
)
91 LOAD_FUNC(ocl
, clCreateProgramWithSource
, OCL_CLCREATEPROGRAMWITHSOURCE
)
92 LOAD_FUNC(ocl
, clEnqueueCopyBuffer
, OCL_CLENQUEUECOPYBUFFER
)
93 LOAD_FUNC(ocl
, clEnqueueFillBuffer
, OCL_CLENQUEUEFILLBUFFER
)
94 LOAD_FUNC(ocl
, clEnqueueMapBuffer
, OCL_CLENQUEUEMAPBUFFER
)
95 LOAD_FUNC(ocl
, clEnqueueNDRangeKernel
, OCL_CLENQUEUENDRANGEKERNEL
)
96 LOAD_FUNC(ocl
, clEnqueueReadBuffer
, OCL_CLENQUEUEREADBUFFER
)
97 LOAD_FUNC(ocl
, clEnqueueUnmapMemObject
, OCL_CLENQUEUEUNMAPMEMOBJECT
)
98 LOAD_FUNC(ocl
, clEnqueueWriteBuffer
, OCL_CLENQUEUEWRITEBUFFER
)
99 LOAD_FUNC(ocl
, clFinish
, OCL_CLFINISH
)
100 LOAD_FUNC(ocl
, clFlush
, OCL_CLFLUSH
)
101 LOAD_FUNC(ocl
, clGetDeviceIDs
, OCL_CLGETDEVICEIDS
)
102 LOAD_FUNC(ocl
, clGetDeviceInfo
, OCL_CLGETDEVICEINFO
)
103 LOAD_FUNC(ocl
, clGetEventInfo
, OCL_CLGETEVENTINFO
)
104 LOAD_FUNC(ocl
, clGetKernelWorkGroupInfo
, OCL_CLGETKERNELWORKGROUPINFO
)
105 LOAD_FUNC(ocl
, clGetPlatformIDs
, OCL_CLGETPLATFORMIDS
)
106 LOAD_FUNC(ocl
, clGetPlatformInfo
, OCL_CLGETPLATFORMINFO
)
107 LOAD_FUNC(ocl
, clGetProgramBuildInfo
, OCL_CLGETPROGRAMBUILDINFO
)
108 LOAD_FUNC(ocl
, clGetProgramInfo
, OCL_CLGETPROGRAMINFO
)
109 LOAD_FUNC(ocl
, clReleaseCommandQueue
, OCL_CLRELEASECOMMANDQUEUE
)
110 LOAD_FUNC(ocl
, clReleaseContext
, OCL_CLRELEASECONTEXT
)
111 LOAD_FUNC(ocl
, clReleaseKernel
, OCL_CLRELEASEKERNEL
)
112 LOAD_FUNC(ocl
, clReleaseMemObject
, OCL_CLRELEASEMEMOBJECT
)
113 LOAD_FUNC(ocl
, clReleaseProgram
, OCL_CLRELEASEPROGRAM
)
114 LOAD_FUNC(ocl
, clSetKernelArg
, OCL_CLSETKERNELARG
)
117 void ocl_close (OCL_PTR
*ocl
)
122 hc_dlclose (ocl
->lib
);
128 void hc_clEnqueueNDRangeKernel (OCL_PTR
*ocl
, cl_command_queue command_queue
, cl_kernel kernel
, cl_uint work_dim
, const size_t *global_work_offset
, const size_t *global_work_size
, const size_t *local_work_size
, cl_uint num_events_in_wait_list
, const cl_event
*event_wait_list
, cl_event
*event
)
130 cl_int CL_err
= ocl
->clEnqueueNDRangeKernel (command_queue
, kernel
, work_dim
, global_work_offset
, global_work_size
, local_work_size
, num_events_in_wait_list
, event_wait_list
, event
);
132 if (CL_err
!= CL_SUCCESS
)
134 log_error ("ERROR: %s : %d : %s\n", "clEnqueueNDRangeKernel()", CL_err
, val2cstr_cl (CL_err
));
140 void hc_clGetEventInfo (OCL_PTR
*ocl
, cl_event event
, cl_event_info param_name
, size_t param_value_size
, void *param_value
, size_t *param_value_size_ret
)
142 cl_int CL_err
= ocl
->clGetEventInfo (event
, param_name
, param_value_size
, param_value
, param_value_size_ret
);
144 if (CL_err
!= CL_SUCCESS
)
146 log_error ("ERROR: %s : %d : %s\n", "clGetEventInfo()", CL_err
, val2cstr_cl (CL_err
));
152 void hc_clFlush (OCL_PTR
*ocl
, cl_command_queue command_queue
)
154 cl_int CL_err
= ocl
->clFlush (command_queue
);
156 if (CL_err
!= CL_SUCCESS
)
158 log_error ("ERROR: %s : %d : %s\n", "clFlush()", CL_err
, val2cstr_cl (CL_err
));
164 void hc_clFinish (OCL_PTR
*ocl
, cl_command_queue command_queue
)
166 cl_int CL_err
= ocl
->clFinish (command_queue
);
168 if (CL_err
!= CL_SUCCESS
)
170 log_error ("ERROR: %s : %d : %s\n", "clFinish()", CL_err
, val2cstr_cl (CL_err
));
176 void hc_clSetKernelArg (OCL_PTR
*ocl
, cl_kernel kernel
, cl_uint arg_index
, size_t arg_size
, const void *arg_value
)
178 cl_int CL_err
= ocl
->clSetKernelArg (kernel
, arg_index
, arg_size
, arg_value
);
180 if (CL_err
!= CL_SUCCESS
)
182 log_error ("ERROR: %s : %d : %s\n", "clSetKernelArg()", CL_err
, val2cstr_cl (CL_err
));
188 void hc_clEnqueueWriteBuffer (OCL_PTR
*ocl
, cl_command_queue command_queue
, cl_mem buffer
, cl_bool blocking_write
, size_t offset
, size_t cb
, const void *ptr
, cl_uint num_events_in_wait_list
, const cl_event
*event_wait_list
, cl_event
*event
)
190 cl_int CL_err
= ocl
->clEnqueueWriteBuffer (command_queue
, buffer
, blocking_write
, offset
, cb
, ptr
, num_events_in_wait_list
, event_wait_list
, event
);
192 if (CL_err
!= CL_SUCCESS
)
194 log_error ("ERROR: %s : %d : %s\n", "clEnqueueWriteBuffer()", CL_err
, val2cstr_cl (CL_err
));
200 void hc_clEnqueueCopyBuffer (OCL_PTR
*ocl
, cl_command_queue command_queue
, cl_mem src_buffer
, cl_mem dst_buffer
, size_t src_offset
, size_t dst_offset
, size_t cb
, cl_uint num_events_in_wait_list
, const cl_event
*event_wait_list
, cl_event
*event
)
202 cl_int CL_err
= ocl
->clEnqueueCopyBuffer (command_queue
, src_buffer
, dst_buffer
, src_offset
, dst_offset
, cb
, num_events_in_wait_list
, event_wait_list
, event
);
204 if (CL_err
!= CL_SUCCESS
)
206 log_error ("ERROR: %s : %d : %s\n", "clEnqueueCopyBuffer()", CL_err
, val2cstr_cl (CL_err
));
212 void hc_clEnqueueReadBuffer (OCL_PTR
*ocl
, cl_command_queue command_queue
, cl_mem buffer
, cl_bool blocking_read
, size_t offset
, size_t cb
, void *ptr
, cl_uint num_events_in_wait_list
, const cl_event
*event_wait_list
, cl_event
*event
)
214 cl_int CL_err
= ocl
->clEnqueueReadBuffer (command_queue
, buffer
, blocking_read
, offset
, cb
, ptr
, num_events_in_wait_list
, event_wait_list
, event
);
216 if (CL_err
!= CL_SUCCESS
)
218 log_error ("ERROR: %s : %d : %s\n", "clEnqueueReadBuffer()", CL_err
, val2cstr_cl (CL_err
));
224 void hc_clGetPlatformIDs (OCL_PTR
*ocl
, cl_uint num_entries
, cl_platform_id
*platforms
, cl_uint
*num_platforms
)
226 cl_int CL_err
= ocl
->clGetPlatformIDs (num_entries
, platforms
, num_platforms
);
228 if (CL_err
!= CL_SUCCESS
)
230 log_error ("ERROR: %s : %d : %s\n", "clGetPlatformIDs()", CL_err
, val2cstr_cl (CL_err
));
236 void hc_clGetPlatformInfo (OCL_PTR
*ocl
, cl_platform_id platform
, cl_platform_info param_name
, size_t param_value_size
, void *param_value
, size_t *param_value_size_ret
)
238 cl_int CL_err
= ocl
->clGetPlatformInfo (platform
, param_name
, param_value_size
, param_value
, param_value_size_ret
);
240 if (CL_err
!= CL_SUCCESS
)
242 log_error ("ERROR: %s : %d : %s\n", "clGetPlatformInfo()", CL_err
, val2cstr_cl (CL_err
));
248 void hc_clGetDeviceIDs (OCL_PTR
*ocl
, cl_platform_id platform
, cl_device_type device_type
, cl_uint num_entries
, cl_device_id
*devices
, cl_uint
*num_devices
)
250 cl_int CL_err
= ocl
->clGetDeviceIDs (platform
, device_type
, num_entries
, devices
, num_devices
);
252 if (CL_err
!= CL_SUCCESS
)
254 log_error ("ERROR: %s : %d : %s\n", "clGetDeviceIDs()", CL_err
, val2cstr_cl (CL_err
));
260 void hc_clGetDeviceInfo (OCL_PTR
*ocl
, cl_device_id device
, cl_device_info param_name
, size_t param_value_size
, void *param_value
, size_t *param_value_size_ret
)
262 cl_int CL_err
= ocl
->clGetDeviceInfo (device
, param_name
, param_value_size
, param_value
, param_value_size_ret
);
264 if (CL_err
!= CL_SUCCESS
)
266 log_error ("ERROR: %s : %d : %s\n", "clGetDeviceInfo()", CL_err
, val2cstr_cl (CL_err
));
272 cl_context
hc_clCreateContext (OCL_PTR
*ocl
, cl_context_properties
*properties
, cl_uint num_devices
, const cl_device_id
*devices
, void (CL_CALLBACK
*pfn_notify
) (const char *, const void *, size_t, void *), void *user_data
)
276 cl_context context
= ocl
->clCreateContext (properties
, num_devices
, devices
, pfn_notify
, user_data
, &CL_err
);
278 if (CL_err
!= CL_SUCCESS
)
280 log_error ("ERROR: %s : %d : %s\n", "clCreateContext()", CL_err
, val2cstr_cl (CL_err
));
288 cl_command_queue
hc_clCreateCommandQueue (OCL_PTR
*ocl
, cl_context context
, cl_device_id device
, cl_command_queue_properties properties
)
292 cl_command_queue command_queue
= ocl
->clCreateCommandQueue (context
, device
, properties
, &CL_err
);
294 if (CL_err
!= CL_SUCCESS
)
296 log_error ("ERROR: %s : %d : %s\n", "clCreateCommandQueue()", CL_err
, val2cstr_cl (CL_err
));
301 return (command_queue
);
305 cl_command_queue hc_clCreateCommandQueueWithProperties (cl_context context, cl_device_id device, const cl_queue_properties *properties)
309 cl_command_queue command_queue = clCreateCommandQueueWithProperties (context, device, properties, &CL_err);
311 if (CL_err != CL_SUCCESS)
313 log_error ("ERROR: %s : %d : %s\n", "clCreateCommandQueueWithProperties()", CL_err, val2cstr_cl (CL_err));
318 return (command_queue);
322 cl_mem
hc_clCreateBuffer (OCL_PTR
*ocl
, cl_context context
, cl_mem_flags flags
, size_t size
, void *host_ptr
)
326 cl_mem mem
= ocl
->clCreateBuffer (context
, flags
, size
, host_ptr
, &CL_err
);
328 if (CL_err
!= CL_SUCCESS
)
330 log_error ("ERROR: %s : %d : %s\n", "clCreateBuffer()", CL_err
, val2cstr_cl (CL_err
));
338 cl_program
hc_clCreateProgramWithSource (OCL_PTR
*ocl
, cl_context context
, cl_uint count
, const char **strings
, const size_t *lengths
)
342 cl_program program
= ocl
->clCreateProgramWithSource (context
, count
, strings
, lengths
, &CL_err
);
344 if (CL_err
!= CL_SUCCESS
)
346 log_error ("ERROR: %s : %d : %s\n", "clCreateProgramWithSource()", CL_err
, val2cstr_cl (CL_err
));
354 cl_program
hc_clCreateProgramWithBinary (OCL_PTR
*ocl
, cl_context context
, cl_uint num_devices
, const cl_device_id
*device_list
, const size_t *lengths
, const unsigned char **binaries
, cl_int
*binary_status
)
358 cl_program program
= ocl
->clCreateProgramWithBinary (context
, num_devices
, device_list
, lengths
, binaries
, binary_status
, &CL_err
);
360 if (CL_err
!= CL_SUCCESS
)
362 log_error ("ERROR: %s : %d : %s\n", "clCreateProgramWithBinary()", CL_err
, val2cstr_cl (CL_err
));
370 void hc_clBuildProgram (OCL_PTR
*ocl
, cl_program program
, cl_uint num_devices
, const cl_device_id
*device_list
, const char *options
, void (CL_CALLBACK
*pfn_notify
) (cl_program program
, void *user_data
), void *user_data
)
372 cl_int CL_err
= ocl
->clBuildProgram (program
, num_devices
, device_list
, options
, pfn_notify
, user_data
);
374 if (CL_err
!= CL_SUCCESS
)
376 log_error ("ERROR: %s : %d : %s\n", "clBuildProgram()", CL_err
, val2cstr_cl (CL_err
));
378 // If we exit here we can't see the error message
383 cl_kernel
hc_clCreateKernel (OCL_PTR
*ocl
, cl_program program
, const char *kernel_name
)
387 cl_kernel kernel
= ocl
->clCreateKernel (program
, kernel_name
, &CL_err
);
389 if (CL_err
!= CL_SUCCESS
)
391 log_error ("ERROR: %s %d - %s\n", "clCreateKernel()", CL_err
, kernel_name
);
399 void hc_clReleaseMemObject (OCL_PTR
*ocl
, cl_mem mem
)
401 cl_int CL_err
= ocl
->clReleaseMemObject (mem
);
403 if (CL_err
!= CL_SUCCESS
)
405 log_error ("ERROR: %s : %d : %s\n", "clReleaseMemObject()", CL_err
, val2cstr_cl (CL_err
));
411 void hc_clReleaseKernel (OCL_PTR
*ocl
, cl_kernel kernel
)
413 cl_int CL_err
= ocl
->clReleaseKernel (kernel
);
415 if (CL_err
!= CL_SUCCESS
)
417 log_error ("ERROR: %s : %d : %s\n", "clReleaseProgram()", CL_err
, val2cstr_cl (CL_err
));
423 void hc_clReleaseProgram (OCL_PTR
*ocl
, cl_program program
)
425 cl_int CL_err
= ocl
->clReleaseProgram (program
);
427 if (CL_err
!= CL_SUCCESS
)
429 log_error ("ERROR: %s : %d : %s\n", "clReleaseProgram()", CL_err
, val2cstr_cl (CL_err
));
435 void hc_clReleaseCommandQueue (OCL_PTR
*ocl
, cl_command_queue command_queue
)
437 cl_int CL_err
= ocl
->clReleaseCommandQueue (command_queue
);
439 if (CL_err
!= CL_SUCCESS
)
441 log_error ("ERROR: %s : %d : %s\n", "clReleaseCommandQueue()", CL_err
, val2cstr_cl (CL_err
));
447 void hc_clReleaseContext (OCL_PTR
*ocl
, cl_context context
)
449 cl_int CL_err
= ocl
->clReleaseContext (context
);
451 if (CL_err
!= CL_SUCCESS
)
453 log_error ("ERROR: %s : %d : %s\n", "clReleaseContext()", CL_err
, val2cstr_cl (CL_err
));
459 void *hc_clEnqueueMapBuffer (OCL_PTR
*ocl
, cl_command_queue command_queue
, cl_mem buffer
, cl_bool blocking_read
, cl_map_flags map_flags
, size_t offset
, size_t cb
, cl_uint num_events_in_wait_list
, const cl_event
*event_wait_list
, cl_event
*event
)
463 void *buf
= ocl
->clEnqueueMapBuffer (command_queue
, buffer
, blocking_read
, map_flags
, offset
, cb
, num_events_in_wait_list
, event_wait_list
, event
, &CL_err
);
465 if (CL_err
!= CL_SUCCESS
)
467 log_error ("ERROR: %s : %d : %s\n", "clEnqueueMapBuffer()", CL_err
, val2cstr_cl (CL_err
));
475 void hc_clEnqueueUnmapMemObject (OCL_PTR
*ocl
, cl_command_queue command_queue
, cl_mem memobj
, void *mapped_ptr
, cl_uint num_events_in_wait_list
, const cl_event
*event_wait_list
, cl_event
*event
)
477 cl_int CL_err
= ocl
->clEnqueueUnmapMemObject (command_queue
, memobj
, mapped_ptr
, num_events_in_wait_list
, event_wait_list
, event
);
479 if (CL_err
!= CL_SUCCESS
)
481 log_error ("ERROR: %s : %d : %s\n", "clEnqueueUnmapMemObject()", CL_err
, val2cstr_cl (CL_err
));
487 void hc_clEnqueueFillBuffer (OCL_PTR
*ocl
, cl_command_queue command_queue
, cl_mem buffer
, const void *pattern
, size_t pattern_size
, size_t offset
, size_t size
, cl_uint num_events_in_wait_list
, const cl_event
*event_wait_list
, cl_event
*event
)
489 cl_int CL_err
= ocl
->clEnqueueFillBuffer (command_queue
, buffer
, pattern
, pattern_size
, offset
, size
, num_events_in_wait_list
, event_wait_list
, event
);
491 if (CL_err
!= CL_SUCCESS
)
493 log_error ("ERROR: %s : %d : %s\n", "clEnqueueFillBuffer()", CL_err
, val2cstr_cl (CL_err
));
499 void hc_clGetKernelWorkGroupInfo (OCL_PTR
*ocl
, cl_kernel kernel
, cl_device_id device
, cl_kernel_work_group_info param_name
, size_t param_value_size
, void *param_value
, size_t *param_value_size_ret
)
501 cl_int CL_err
= ocl
->clGetKernelWorkGroupInfo (kernel
, device
, param_name
, param_value_size
, param_value
, param_value_size_ret
);
503 if (CL_err
!= CL_SUCCESS
)
505 log_error ("ERROR: %s : %d : %s\n", "clGetKernelWorkGroupInfo()", CL_err
, val2cstr_cl (CL_err
));
511 void hc_clGetProgramBuildInfo (OCL_PTR
*ocl
, cl_program program
, cl_device_id device
, cl_program_build_info param_name
, size_t param_value_size
, void *param_value
, size_t *param_value_size_ret
)
513 cl_int CL_err
= ocl
->clGetProgramBuildInfo (program
, device
, param_name
, param_value_size
, param_value
, param_value_size_ret
);
515 if (CL_err
!= CL_SUCCESS
)
517 log_error ("ERROR: %s : %d : %s\n", "clGetProgramBuildInfo()", CL_err
, val2cstr_cl (CL_err
));
523 void hc_clGetProgramInfo (OCL_PTR
*ocl
, cl_program program
, cl_program_info param_name
, size_t param_value_size
, void *param_value
, size_t * param_value_size_ret
)
525 cl_int CL_err
= ocl
->clGetProgramInfo (program
, param_name
, param_value_size
, param_value
, param_value_size_ret
);
527 if (CL_err
!= CL_SUCCESS
)
529 log_error ("ERROR: %s : %d : %s\n", "clGetProgramInfo()", CL_err
, val2cstr_cl (CL_err
));