Merge pull request #134 from yhfudev/add-cl-error-messages
[hashcat.git] / src / ext_OpenCL.c
1 /**
2 * Author......: Jens Steube <jens.steube@gmail.com>
3 * License.....: MIT
4 */
5
6 #include <ext_OpenCL.h>
7
8 const char *
9 val2cstr_cl (cl_int CL_err)
10 {
11 #define CLERR(a) case a: return #a
12 switch (CL_err) {
13 CLERR(CL_BUILD_PROGRAM_FAILURE);
14 CLERR(CL_COMPILER_NOT_AVAILABLE);
15 CLERR(CL_DEVICE_NOT_FOUND);
16 CLERR(CL_INVALID_ARG_INDEX);
17 CLERR(CL_INVALID_ARG_SIZE);
18 CLERR(CL_INVALID_ARG_VALUE);
19 CLERR(CL_INVALID_BINARY);
20 CLERR(CL_INVALID_BUFFER_SIZE);
21 CLERR(CL_INVALID_BUILD_OPTIONS);
22 CLERR(CL_INVALID_COMMAND_QUEUE);
23 CLERR(CL_INVALID_CONTEXT);
24 CLERR(CL_INVALID_DEVICE);
25 CLERR(CL_INVALID_DEVICE_TYPE);
26 CLERR(CL_INVALID_EVENT);
27 CLERR(CL_INVALID_EVENT_WAIT_LIST);
28 CLERR(CL_INVALID_GLOBAL_OFFSET);
29 CLERR(CL_INVALID_HOST_PTR);
30 CLERR(CL_INVALID_KERNEL);
31 CLERR(CL_INVALID_KERNEL_ARGS);
32 CLERR(CL_INVALID_KERNEL_DEFINITION);
33 CLERR(CL_INVALID_KERNEL_NAME);
34 CLERR(CL_INVALID_MEM_OBJECT);
35 CLERR(CL_INVALID_OPERATION);
36 CLERR(CL_INVALID_PLATFORM);
37 CLERR(CL_INVALID_PROGRAM);
38 CLERR(CL_INVALID_PROGRAM_EXECUTABLE);
39 CLERR(CL_INVALID_QUEUE_PROPERTIES);
40 CLERR(CL_INVALID_SAMPLER);
41 CLERR(CL_INVALID_VALUE);
42 CLERR(CL_INVALID_WORK_DIMENSION);
43 CLERR(CL_INVALID_WORK_GROUP_SIZE);
44 CLERR(CL_INVALID_WORK_ITEM_SIZE);
45 CLERR(CL_MISALIGNED_SUB_BUFFER_OFFSET);
46 CLERR(CL_MAP_FAILURE);
47 CLERR(CL_MEM_COPY_OVERLAP);
48 CLERR(CL_MEM_OBJECT_ALLOCATION_FAILURE);
49 CLERR(CL_OUT_OF_HOST_MEMORY);
50 CLERR(CL_OUT_OF_RESOURCES);
51
52 }
53 return "(unknown CL error)";
54 }
55
56 void hc_clEnqueueNDRangeKernel (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)
57 {
58 cl_int CL_err = clEnqueueNDRangeKernel (command_queue, kernel, work_dim, global_work_offset, global_work_size, local_work_size, num_events_in_wait_list, event_wait_list, event);
59
60 if (CL_err != CL_SUCCESS)
61 {
62 log_error ("ERROR: %s (%d)%s\n", "clEnqueueNDRangeKernel()", CL_err, val2cstr_cl(CL_err));
63
64 exit (-1);
65 }
66 }
67
68 void hc_clGetEventInfo (cl_event event, cl_event_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
69 {
70 cl_int CL_err = clGetEventInfo (event, param_name, param_value_size, param_value, param_value_size_ret);
71
72 if (CL_err != CL_SUCCESS)
73 {
74 log_error ("ERROR: %s (%d)%s\n", "clGetEventInfo()", CL_err, val2cstr_cl(CL_err));
75
76 exit (-1);
77 }
78 }
79
80 void hc_clFlush (cl_command_queue command_queue)
81 {
82 cl_int CL_err = clFlush (command_queue);
83
84 if (CL_err != CL_SUCCESS)
85 {
86 log_error ("ERROR: %s (%d)%s\n", "clFlush()", CL_err, val2cstr_cl(CL_err));
87
88 exit (-1);
89 }
90 }
91
92 void hc_clFinish (cl_command_queue command_queue)
93 {
94 cl_int CL_err = clFinish (command_queue);
95
96 if (CL_err != CL_SUCCESS)
97 {
98 log_error ("ERROR: %s (%d)%s\n", "clFinish()", CL_err, val2cstr_cl(CL_err));
99
100 exit (-1);
101 }
102 }
103
104 void hc_clSetKernelArg (cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value)
105 {
106 cl_int CL_err = clSetKernelArg (kernel, arg_index, arg_size, arg_value);
107
108 if (CL_err != CL_SUCCESS)
109 {
110 log_error ("ERROR: %s (%d)%s\n", "clSetKernelArg()", CL_err, val2cstr_cl(CL_err));
111
112 exit (-1);
113 }
114 }
115
116 void hc_clEnqueueWriteBuffer (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)
117 {
118 cl_int CL_err = clEnqueueWriteBuffer (command_queue, buffer, blocking_write, offset, cb, ptr, num_events_in_wait_list, event_wait_list, event);
119
120 if (CL_err != CL_SUCCESS)
121 {
122 log_error ("ERROR: %s (%d)%s\n", "clEnqueueWriteBuffer()", CL_err, val2cstr_cl(CL_err));
123
124 exit (-1);
125 }
126 }
127
128 void hc_clEnqueueCopyBuffer (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)
129 {
130 cl_int CL_err = clEnqueueCopyBuffer (command_queue, src_buffer, dst_buffer, src_offset, dst_offset, cb, num_events_in_wait_list, event_wait_list, event);
131
132 if (CL_err != CL_SUCCESS)
133 {
134 log_error ("ERROR: %s (%d)%s\n", "clEnqueueCopyBuffer()", CL_err, val2cstr_cl(CL_err));
135
136 exit (-1);
137 }
138 }
139
140 void hc_clEnqueueReadBuffer (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)
141 {
142 cl_int CL_err = clEnqueueReadBuffer (command_queue, buffer, blocking_read, offset, cb, ptr, num_events_in_wait_list, event_wait_list, event);
143
144 if (CL_err != CL_SUCCESS)
145 {
146 log_error ("ERROR: %s (%d)%s\n", "clEnqueueReadBuffer()", CL_err, val2cstr_cl(CL_err));
147
148 exit (-1);
149 }
150 }
151
152 void hc_clGetPlatformIDs (cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms)
153 {
154 cl_int CL_err = clGetPlatformIDs (num_entries, platforms, num_platforms);
155
156 if (CL_err != CL_SUCCESS)
157 {
158 log_error ("ERROR: %s (%d)%s\n", "clGetPlatformIDs()", CL_err, val2cstr_cl(CL_err));
159
160 exit (-1);
161 }
162 }
163
164 void hc_clGetPlatformInfo (cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
165 {
166 cl_int CL_err = clGetPlatformInfo (platform, param_name, param_value_size, param_value, param_value_size_ret);
167
168 if (CL_err != CL_SUCCESS)
169 {
170 log_error ("ERROR: %s (%d)%s\n", "clGetPlatformInfo()", CL_err, val2cstr_cl(CL_err));
171
172 exit (-1);
173 }
174 }
175
176 void hc_clGetDeviceIDs (cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id *devices, cl_uint *num_devices)
177 {
178 cl_int CL_err = clGetDeviceIDs (platform, device_type, num_entries, devices, num_devices);
179
180 if (CL_err != CL_SUCCESS)
181 {
182 log_error ("ERROR: %s (%d)%s\n", "clGetDeviceIDs()", CL_err, val2cstr_cl(CL_err));
183
184 exit (-1);
185 }
186 }
187
188 void hc_clGetDeviceInfo (cl_device_id device, cl_device_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
189 {
190 cl_int CL_err = clGetDeviceInfo (device, param_name, param_value_size, param_value, param_value_size_ret);
191
192 if (CL_err != CL_SUCCESS)
193 {
194 log_error ("ERROR: %s (%d)%s\n", "clGetDeviceInfo()", CL_err, val2cstr_cl(CL_err));
195
196 exit (-1);
197 }
198 }
199
200 cl_context hc_clCreateContext (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)
201 {
202 cl_int CL_err;
203
204 cl_context context = clCreateContext (properties, num_devices, devices, pfn_notify, user_data, &CL_err);
205
206 if (CL_err != CL_SUCCESS)
207 {
208 log_error ("ERROR: %s (%d)%s\n", "clCreateContext()", CL_err, val2cstr_cl(CL_err));
209
210 exit (-1);
211 }
212
213 return (context);
214 }
215
216
217 cl_command_queue hc_clCreateCommandQueue (cl_context context, cl_device_id device, cl_command_queue_properties properties)
218 {
219 cl_int CL_err;
220
221 cl_command_queue command_queue = clCreateCommandQueue (context, device, properties, &CL_err);
222
223 if (CL_err != CL_SUCCESS)
224 {
225 log_error ("ERROR: %s (%d)%s\n", "clCreateCommandQueue()", CL_err, val2cstr_cl(CL_err));
226
227 exit (-1);
228 }
229
230 return (command_queue);
231 }
232
233 /*
234 cl_command_queue hc_clCreateCommandQueueWithProperties (cl_context context, cl_device_id device, const cl_queue_properties *properties)
235 {
236 cl_int CL_err;
237
238 cl_command_queue command_queue = clCreateCommandQueueWithProperties (context, device, properties, &CL_err);
239
240 if (CL_err != CL_SUCCESS)
241 {
242 log_error ("ERROR: %s (%d)%s\n", "clCreateCommandQueueWithProperties()", CL_err, val2cstr_cl(CL_err));
243
244 exit (-1);
245 }
246
247 return (command_queue);
248 }
249 */
250
251 cl_mem hc_clCreateBuffer (cl_context context, cl_mem_flags flags, size_t size, void *host_ptr)
252 {
253 cl_int CL_err;
254
255 cl_mem mem = clCreateBuffer (context, flags, size, host_ptr, &CL_err);
256
257 if (CL_err != CL_SUCCESS)
258 {
259 log_error ("ERROR: %s (%d)%s\n", "clCreateBuffer()", CL_err, val2cstr_cl(CL_err));
260
261 exit (-1);
262 }
263
264 return (mem);
265 }
266
267 cl_program hc_clCreateProgramWithSource (cl_context context, cl_uint count, const char **strings, const size_t *lengths)
268 {
269 cl_int CL_err;
270
271 cl_program program = clCreateProgramWithSource (context, count, strings, lengths, &CL_err);
272
273 if (CL_err != CL_SUCCESS)
274 {
275 log_error ("ERROR: %s (%d)%s\n", "clCreateProgramWithSource()", CL_err, val2cstr_cl(CL_err));
276
277 exit (-1);
278 }
279
280 return (program);
281 }
282
283 cl_program hc_clCreateProgramWithBinary (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)
284 {
285 cl_int CL_err;
286
287 cl_program program = clCreateProgramWithBinary (context, num_devices, device_list, lengths, binaries, binary_status, &CL_err);
288
289 if (CL_err != CL_SUCCESS)
290 {
291 log_error ("ERROR: %s (%d)%s\n", "clCreateProgramWithBinary()", CL_err, val2cstr_cl(CL_err));
292
293 exit (-1);
294 }
295
296 return (program);
297 }
298
299 void hc_clBuildProgram (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)
300 {
301 cl_int CL_err = clBuildProgram (program, num_devices, device_list, options, pfn_notify, user_data);
302
303 if (CL_err != CL_SUCCESS)
304 {
305 log_error ("ERROR: %s (%d)%s\n", "clBuildProgram()", CL_err, val2cstr_cl(CL_err));
306
307 // If we exit here we can't see the error message
308 // exit (-1);
309 }
310 }
311
312 cl_kernel hc_clCreateKernel (cl_program program, const char *kernel_name)
313 {
314 cl_int CL_err;
315
316 cl_kernel kernel = clCreateKernel (program, kernel_name, &CL_err);
317
318 if (CL_err != CL_SUCCESS)
319 {
320 log_error ("ERROR: %s %d - %s\n", "clCreateKernel()", CL_err, kernel_name);
321
322 exit (-1);
323 }
324
325 return (kernel);
326 }
327
328 void hc_clReleaseMemObject (cl_mem mem)
329 {
330 cl_int CL_err = clReleaseMemObject (mem);
331
332 if (CL_err != CL_SUCCESS)
333 {
334 log_error ("ERROR: %s (%d)%s\n", "clReleaseMemObject()", CL_err, val2cstr_cl(CL_err));
335
336 exit (-1);
337 }
338 }
339
340 void hc_clReleaseKernel (cl_kernel kernel)
341 {
342 cl_int CL_err = clReleaseKernel (kernel);
343
344 if (CL_err != CL_SUCCESS)
345 {
346 log_error ("ERROR: %s (%d)%s\n", "clReleaseProgram()", CL_err, val2cstr_cl(CL_err));
347
348 exit (-1);
349 }
350 }
351
352 void hc_clReleaseProgram (cl_program program)
353 {
354 cl_int CL_err = clReleaseProgram (program);
355
356 if (CL_err != CL_SUCCESS)
357 {
358 log_error ("ERROR: %s (%d)%s\n", "clReleaseProgram()", CL_err, val2cstr_cl(CL_err));
359
360 exit (-1);
361 }
362 }
363
364 void hc_clReleaseCommandQueue (cl_command_queue command_queue)
365 {
366 cl_int CL_err = clReleaseCommandQueue (command_queue);
367
368 if (CL_err != CL_SUCCESS)
369 {
370 log_error ("ERROR: %s (%d)%s\n", "clReleaseCommandQueue()", CL_err, val2cstr_cl(CL_err));
371
372 exit (-1);
373 }
374 }
375
376 void hc_clReleaseContext (cl_context context)
377 {
378 cl_int CL_err = clReleaseContext (context);
379
380 if (CL_err != CL_SUCCESS)
381 {
382 log_error ("ERROR: %s (%d)%s\n", "clReleaseContext()", CL_err, val2cstr_cl(CL_err));
383
384 exit (-1);
385 }
386 }
387
388 void *hc_clEnqueueMapBuffer (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)
389 {
390 cl_int CL_err;
391
392 void *buf = clEnqueueMapBuffer (command_queue, buffer, blocking_read, map_flags, offset, cb, num_events_in_wait_list, event_wait_list, event, &CL_err);
393
394 if (CL_err != CL_SUCCESS)
395 {
396 log_error ("ERROR: %s (%d)%s\n", "clEnqueueMapBuffer()", CL_err, val2cstr_cl(CL_err));
397
398 exit (-1);
399 }
400
401 return buf;
402 }
403
404 void hc_clEnqueueUnmapMemObject (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)
405 {
406 cl_int CL_err = clEnqueueUnmapMemObject (command_queue, memobj, mapped_ptr, num_events_in_wait_list, event_wait_list, event);
407
408 if (CL_err != CL_SUCCESS)
409 {
410 log_error ("ERROR: %s (%d)%s\n", "clEnqueueUnmapMemObject()", CL_err, val2cstr_cl(CL_err));
411
412 exit (-1);
413 }
414 }
415
416 void hc_clEnqueueFillBuffer (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)
417 {
418 cl_int CL_err = clEnqueueFillBuffer (command_queue, buffer, pattern, pattern_size, offset, size, num_events_in_wait_list, event_wait_list, event);
419
420 if (CL_err != CL_SUCCESS)
421 {
422 log_error ("ERROR: %s (%d)%s\n", "clEnqueueFillBuffer()", CL_err, val2cstr_cl(CL_err));
423
424 exit (-1);
425 }
426 }
427
428 void hc_clGetKernelWorkGroupInfo (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)
429 {
430 cl_int CL_err = clGetKernelWorkGroupInfo (kernel, device, param_name, param_value_size, param_value, param_value_size_ret);
431
432 if (CL_err != CL_SUCCESS)
433 {
434 log_error ("ERROR: %s (%d)%s\n", "clGetKernelWorkGroupInfo()", CL_err, val2cstr_cl(CL_err));
435
436 exit (-1);
437 }
438 }