ViennaCL - The Vienna Computing Library  1.2.0
error.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_OCL_ERROR_HPP_
2 #define VIENNACL_OCL_ERROR_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 //error levels:
25 //#define VIENNACL_DEBUG_ALL //print all of the following
26 //#define VIENNACL_DEBUG_KERNEL //debug any modifications on viennacl::ocl::kernel objects
27 //#define VIENNACL_DEBUG_COPY //print infos related to setting up/modifying memory objects
28 //#define VIENNACL_DEBUG_OPENCL //display debug info for the OpenCL layer (platform/context/queue creation,
29 //#define VIENNACL_DEBUG_DEVICE //Show device info upon allocation
30 //#define VIENNACL_DEBUG_CONTEXT //Debug queries to context
31 //#define VIENNACL_DEBUG_BUILD //Show debug info from OpenCL compiler
32 
33 
34 //backwards compatibility:
35 #ifdef VIENNACL_BUILD_INFO
36  #define VIENNACL_DEBUG_ALL
37 #endif
38 
39 
40 #ifdef __APPLE__
41 #include <OpenCL/cl.h>
42 #else
43 #include <CL/cl.h>
44 #endif
45 
46 #include <string>
47 #include <iostream>
48 #include <exception>
49 
50 #define VIENNACL_BUG_REPORT_STRING \
51  "\nIf you think that this is a bug in ViennaCL, please report it at viennacl-support@lists.sourceforge.net and supply at least the following information:\n"\
52  " * Operating System\n"\
53  " * Which OpenCL implementation (AMD, NVIDIA, etc.)\n"\
54  " * ViennaCL version\n"\
55  "Many thanks in advance!";\
56 
57 namespace viennacl
58 {
59  namespace ocl
60  {
61  //Wrapper for OpenCL exceptions:
62  class device_not_found : public std::exception
63  {
64  virtual const char* what() const throw()
65  {
66  return "ViennaCL: FATAL ERROR: CL_DEVICE_NOT_FOUND \n ViennaCL could not find a suitable device. Please check whether an OpenCL implementation is properly installed and a suitable device available."
68  }
69  };
70 
71  class device_not_available : public std::exception
72  {
73  virtual const char* what() const throw()
74  {
75  return "ViennaCL: FATAL ERROR: CL_DEVICE_NOT_AVAILABLE \n ViennaCL could not use the compute device because it is not available."
77  }
78  };
79 
80  class compiler_not_available : public std::exception
81  {
82  virtual const char* what() const throw()
83  {
84  return "ViennaCL: FATAL ERROR: CL_COMPILER_NOT_AVAILABLE \n Your OpenCL framework does not provide an OpenCL compiler. Unfortunately, ViennaCL cannot be used without such a compiler."
86  }
87  };
88 
89  class mem_object_allocation_failure : public std::exception
90  {
91  virtual const char* what() const throw()
92  {
93  return "ViennaCL: FATAL ERROR: CL_MEM_OBJECT_ALLOCATION_FAILURE \n ViennaCL could not allocate memory on the device. Most likely the device simply ran out of memory."
95  }
96  };
97 
98  class out_of_resources : public std::exception
99  {
100  virtual const char* what() const throw()
101  {
102  return "ViennaCL: FATAL ERROR: CL_OUT_OF_RESOURCES \n ViennaCL tried to launch a compute kernel, but the device does not provide enough resources. Try changing the global and local work item sizes."
104  }
105  };
106 
107  class out_of_host_memory : public std::exception
108  {
109  virtual const char* what() const throw()
110  {
111  return "ViennaCL: FATAL ERROR: CL_OUT_OF_HOST_MEMORY \n The host ran out of memory (usually CPU RAM). Please try again on smaller problems."
113  }
114  };
115 
116  class profiling_info_not_available : public std::exception
117  {
118  virtual const char* what() const throw()
119  {
120  return "ViennaCL: FATAL ERROR: CL_PROFILING_INFO_NOT_AVAILABLE."
122  }
123  };
124 
125  class mem_copy_overlap : public std::exception
126  {
127  virtual const char* what() const throw()
128  {
129  return "ViennaCL: FATAL ERROR: CL_MEM_COPY_OVERLAP."
131  }
132  };
133 
134  class image_format_mismatch : public std::exception
135  {
136  virtual const char* what() const throw()
137  {
138  return "ViennaCL: FATAL ERROR: CL_IMAGE_FORMAT_MISMATCH."
140  }
141  };
142 
143  class image_format_not_supported : public std::exception
144  {
145  virtual const char* what() const throw()
146  {
147  return "ViennaCL: FATAL ERROR: CL_IMAGE_FORMAT_NOT_SUPPORTED."
149  }
150  };
151 
152  class build_program_failure : public std::exception
153  {
154  virtual const char* what() const throw()
155  {
156  return "ViennaCL: FATAL ERROR: CL_BUILD_PROGRAM_FAILURE \n The OpenCL compiler encountered an error during the compilation of ViennaCL sources. This is most likely a bug in ViennaCL."
158  }
159  };
160 
161  class map_failure : public std::exception
162  {
163  virtual const char* what() const throw()
164  {
165  return "ViennaCL: FATAL ERROR: CL_MAP_FAILURE."
167  }
168  };
169 
170  class invalid_value : public std::exception
171  {
172  virtual const char* what() const throw()
173  {
174  return "ViennaCL: FATAL ERROR: CL_INVALID_VALUE."
176  }
177  };
178 
179  class invalid_device_type : public std::exception
180  {
181  virtual const char* what() const throw()
182  {
183  return "ViennaCL: FATAL ERROR: CL_INVALID_DEVICE_TYPE."
185  }
186  };
187 
188  class invalid_platform : public std::exception
189  {
190  virtual const char* what() const throw()
191  {
192  return "ViennaCL: FATAL ERROR: CL_INVALID_PLATFORM."
194  }
195  };
196 
197  class invalid_device : public std::exception
198  {
199  virtual const char* what() const throw()
200  {
201  return "ViennaCL: FATAL ERROR: CL_INVALID_DEVICE."
203  }
204  };
205 
206  class invalid_context : public std::exception
207  {
208  virtual const char* what() const throw()
209  {
210  return "ViennaCL: FATAL ERROR: CL_INVALID_CONTEXT."
212  }
213  };
214 
215  class invalid_queue_properties : public std::exception
216  {
217  virtual const char* what() const throw()
218  {
219  return "ViennaCL: FATAL ERROR: CL_INVALID_QUEUE_PROPERTIES."
221  }
222  };
223 
224  class invalid_command_queue : public std::exception
225  {
226  virtual const char* what() const throw()
227  {
228  return "ViennaCL: FATAL ERROR: CL_INVALID_COMMAND_QUEUE."
230  }
231  };
232 
233  class invalid_host_ptr : public std::exception
234  {
235  virtual const char* what() const throw()
236  {
237  return "ViennaCL: FATAL ERROR: CL_INVALID_HOST_PTR."
239  }
240  };
241 
242  class invalid_mem_object : public std::exception
243  {
244  virtual const char* what() const throw()
245  {
246  return "ViennaCL: FATAL ERROR: CL_INVALID_MEM_OBJECT."
248  }
249  };
250 
251  class invalid_image_format_descriptor : public std::exception
252  {
253  virtual const char* what() const throw()
254  {
255  return "ViennaCL: FATAL ERROR: CL_INVALID_IMAGE_FORMAT_DESCRIPTOR."
257  }
258  };
259 
260  class invalid_image_size : public std::exception
261  {
262  virtual const char* what() const throw()
263  {
264  return "ViennaCL: FATAL ERROR: CL_INVALID_IMAGE_SIZE."
266  }
267  };
268 
269  class invalid_sampler : public std::exception
270  {
271  virtual const char* what() const throw()
272  {
273  return "ViennaCL: FATAL ERROR: CL_INVALID_SAMPLER."
275  }
276  };
277 
278  class invalid_binary : public std::exception
279  {
280  virtual const char* what() const throw()
281  {
282  return "ViennaCL: FATAL ERROR: CL_INVALID_BINARY."
284  }
285  };
286 
287  class invalid_build_options : public std::exception
288  {
289  virtual const char* what() const throw()
290  {
291  return "ViennaCL: FATAL ERROR: CL_INVALID_BUILD_OPTIONS."
293  }
294  };
295 
296  class invalid_program : public std::exception
297  {
298  virtual const char* what() const throw()
299  {
300  return "ViennaCL: FATAL ERROR: CL_INVALID_PROGRAM."
302  }
303  };
304 
305  class invalid_program_executable : public std::exception
306  {
307  virtual const char* what() const throw()
308  {
309  return "ViennaCL: FATAL ERROR: CL_INVALID_PROGRAM_EXECUTABLE."
311  }
312  };
313 
314  class invalid_kernel_name : public std::exception
315  {
316  virtual const char* what() const throw()
317  {
318  return "ViennaCL: FATAL ERROR: CL_INVALID_KERNEL_NAME \n The supplied kernel name is invalid. If you have written your own OpenCL kernel, please check that the correct kernel name is used in the initalization of the kernel object."
320  }
321  };
322 
323  class invalid_kernel_definition : public std::exception
324  {
325  virtual const char* what() const throw()
326  {
327  return "ViennaCL: FATAL ERROR: CL_INVALID_KERNEL_DEFINITION."
329  }
330  };
331 
332  class invalid_kernel : public std::exception
333  {
334  virtual const char* what() const throw()
335  {
336  return "ViennaCL: FATAL ERROR: CL_INVALID_KERNEL \n The supplied kernel argument is invalid."
338  }
339  };
340 
341  class invalid_arg_index : public std::exception
342  {
343  virtual const char* what() const throw()
344  {
345  return "ViennaCL: FATAL ERROR: CL_INVALID_ARG_INDEX."
347  }
348  };
349 
350  class invalid_arg_value : public std::exception
351  {
352  virtual const char* what() const throw()
353  {
354  return "ViennaCL: FATAL ERROR: CL_INVALID_ARG_VALUE."
356  }
357  };
358 
359  class invalid_arg_size : public std::exception
360  {
361  virtual const char* what() const throw()
362  {
363  return "ViennaCL: FATAL ERROR: CL_INVALID_ARG_SIZE."
365  }
366  };
367 
368  class invalid_kernel_args : public std::exception
369  {
370  virtual const char* what() const throw()
371  {
372  return "ViennaCL: FATAL ERROR: CL_INVALID_KERNEL_ARGS \n The supplied kernel arguments do not fit the kernel parameter list. If you have written your own OpenCL kernel, please check that the correct kernel arguments are set in the appropriate order."
374  }
375  };
376 
377  class invalid_work_dimension : public std::exception
378  {
379  virtual const char* what() const throw()
380  {
381  return "ViennaCL: FATAL ERROR: CL_INVALID_WORK_DIMENSION"
383  }
384  };
385 
386  class invalid_work_group_size : public std::exception
387  {
388  virtual const char* what() const throw()
389  {
390  return "ViennaCL: FATAL ERROR: CL_INVALID_WORK_GROUP_SIZE \n The supplied work group size is invalid. If you have set this value manually, please reconsider your choice."
392  }
393  };
394 
395  class invalid_work_item_size : public std::exception
396  {
397  virtual const char* what() const throw()
398  {
399  return "ViennaCL: FATAL ERROR: CL_INVALID_WORK_ITEM_SIZE \n The work item size is invalid. If you have set this value manually, please reconsider your choice."
401  }
402  };
403 
404  class invalid_global_offset : public std::exception
405  {
406  virtual const char* what() const throw()
407  {
408  return "ViennaCL: FATAL ERROR: CL_INVALID_GLOBAL_OFFSET."
410  }
411  };
412 
413  class invalid_event_wait_list : public std::exception
414  {
415  virtual const char* what() const throw()
416  {
417  return "ViennaCL: FATAL ERROR: CL_INVALID_EVENT_WAIT_LIST."
419  }
420  };
421 
422  class invalid_event : public std::exception
423  {
424  virtual const char* what() const throw()
425  {
426  return "ViennaCL: FATAL ERROR: CL_INVALID_EVENT."
428  }
429  };
430 
431  class invalid_operation : public std::exception
432  {
433  virtual const char* what() const throw()
434  {
435  return "ViennaCL: FATAL ERROR: CL_INVALID_OPERATION."
437  }
438  };
439 
440  class invalid_gl_object : public std::exception
441  {
442  virtual const char* what() const throw()
443  {
444  return "ViennaCL: FATAL ERROR: CL_INVALID_GL_OBJECT."
446  }
447  };
448 
449  class invalid_buffer_size : public std::exception
450  {
451  virtual const char* what() const throw()
452  {
453  return "ViennaCL: FATAL ERROR: CL_INVALID_BUFFER_SIZE."
455  }
456  };
457 
458  class invalid_mip_level : public std::exception
459  {
460  virtual const char* what() const throw()
461  {
462  return "ViennaCL: FATAL ERROR: CL_INVALID_MIP_LEVEL."
464  }
465  };
466 
467  class invalid_global_work_size : public std::exception
468  {
469  virtual const char* what() const throw()
470  {
471  return "ViennaCL: FATAL ERROR: CL_INVALID_GLOBAL_WORK_SIZE."
473  }
474  };
475 
476  class invalid_property : public std::exception
477  {
478  virtual const char* what() const throw()
479  {
480  return "ViennaCL: FATAL ERROR: CL_INVALID_PROPERTY."
482  }
483  };
484 
485  class unknown_error : public std::exception
486  {
487  virtual const char* what() const throw()
488  {
489  return "ViennaCL: FATAL ERROR: ViennaCL encountered an unknown OpenCL error. In some cases, this might be due to an invalid global work size, but it can also be due to several compilation errors."
491  }
492  };
493 
494 
495  class double_precision_not_provided_error : public std::exception
496  {
497  virtual const char* what() const throw()
498  {
499  return "ViennaCL: FATAL ERROR: You requested to create a ViennaCL type using double precision. However, double precision is not supported by your device."
501  }
502  };
503 
504 
510  template <typename T>
512  {
513 
515  static void raise_exception(cl_int err)
516  {
517  switch (err)
518  {
519  case CL_DEVICE_NOT_FOUND: throw device_not_found(); break;
520  case CL_DEVICE_NOT_AVAILABLE: throw device_not_available(); break;
521  case CL_COMPILER_NOT_AVAILABLE: throw compiler_not_available(); break;
522  case CL_MEM_OBJECT_ALLOCATION_FAILURE: throw mem_object_allocation_failure(); break;
523  case CL_OUT_OF_RESOURCES: throw out_of_resources(); break;
524  case CL_OUT_OF_HOST_MEMORY: throw out_of_host_memory(); break;
525  case CL_PROFILING_INFO_NOT_AVAILABLE: throw profiling_info_not_available(); break;
526  case CL_MEM_COPY_OVERLAP: throw mem_copy_overlap(); break;
527  case CL_IMAGE_FORMAT_MISMATCH: throw image_format_mismatch(); break;
528  case CL_IMAGE_FORMAT_NOT_SUPPORTED: throw image_format_not_supported(); break;
529  case CL_BUILD_PROGRAM_FAILURE: throw build_program_failure(); break;
530  case CL_MAP_FAILURE: throw map_failure(); break;
531 
532  case CL_INVALID_VALUE: throw invalid_value(); break;
533  case CL_INVALID_DEVICE_TYPE: throw invalid_device_type(); break;
534  case CL_INVALID_PLATFORM: throw invalid_platform(); break;
535  case CL_INVALID_DEVICE: throw invalid_device(); break;
536  case CL_INVALID_CONTEXT: throw invalid_context(); break;
537  case CL_INVALID_QUEUE_PROPERTIES: throw invalid_queue_properties(); break;
538  case CL_INVALID_COMMAND_QUEUE: throw invalid_command_queue(); break;
539  case CL_INVALID_HOST_PTR: throw invalid_host_ptr(); break;
540  case CL_INVALID_MEM_OBJECT: throw invalid_mem_object(); break;
541  case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: throw invalid_image_format_descriptor(); break;
542  case CL_INVALID_IMAGE_SIZE: throw invalid_image_size(); break;
543  case CL_INVALID_SAMPLER: throw invalid_sampler(); break;
544  case CL_INVALID_BINARY: throw invalid_binary(); break;
545  case CL_INVALID_BUILD_OPTIONS: throw invalid_build_options(); break;
546  case CL_INVALID_PROGRAM: throw invalid_program(); break;
547  case CL_INVALID_PROGRAM_EXECUTABLE: throw invalid_program_executable(); break;
548  case CL_INVALID_KERNEL_NAME: throw invalid_kernel_name(); break;
549  case CL_INVALID_KERNEL_DEFINITION: throw invalid_kernel_definition(); break;
550  case CL_INVALID_KERNEL: throw invalid_kernel(); break;
551  case CL_INVALID_ARG_INDEX: throw invalid_arg_index(); break;
552  case CL_INVALID_ARG_VALUE: throw invalid_arg_value(); break;
553  case CL_INVALID_ARG_SIZE: throw invalid_arg_size(); break;
554  case CL_INVALID_KERNEL_ARGS: throw invalid_kernel_args(); break;
555  case CL_INVALID_WORK_DIMENSION: throw invalid_work_dimension(); break;
556  case CL_INVALID_WORK_GROUP_SIZE: throw invalid_work_group_size(); break;
557  case CL_INVALID_WORK_ITEM_SIZE: throw invalid_work_item_size(); break;
558  case CL_INVALID_GLOBAL_OFFSET: throw invalid_global_offset(); break;
559  case CL_INVALID_EVENT_WAIT_LIST: throw invalid_event_wait_list(); break;
560  case CL_INVALID_EVENT: throw invalid_event(); break;
561  case CL_INVALID_OPERATION: throw invalid_operation(); break;
562  case CL_INVALID_GL_OBJECT: throw invalid_gl_object(); break;
563  case CL_INVALID_BUFFER_SIZE: throw invalid_buffer_size(); break;
564  case CL_INVALID_MIP_LEVEL: throw invalid_mip_level(); break;
565  case CL_INVALID_GLOBAL_WORK_SIZE: throw invalid_global_work_size(); break;
566  #ifdef CL_INVALID_PROPERTY
567  case CL_INVALID_PROPERTY: throw invalid_property(); break;
568  #endif
569  // return "CL_INVALID_GLOBAL_WORK_SIZE";
570 
571  default: throw unknown_error();
572  }
573 
574  } //getErrorString
575 
580  static void checkError(cl_int err, const std::string & file, const std::string & func, int line)
581  {
582  if (err != CL_SUCCESS)
583  {
584  #ifdef VIENNACL_DEBUG_ALL
585  std::cerr << "ViennaCL: Error " << err << " in function " << func << " ( "<< file << ":" << line << " ) " << std::endl;
586  #endif
587  raise_exception(err);
588  }
589  } //checkError()
590 
591  }; //struct
592 
593  #define VIENNACL_ERR_CHECK(err) viennacl::ocl::error_checker<void>::checkError(err, __FILE__, __FUNCTION__, __LINE__);
594 
595  } //namespace ocl
596 } //namespace viennacl
597 
598 #endif
599