opencl_error.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_EXCEPTION_OPENCL_ERROR_HPP
  11. #define BOOST_COMPUTE_EXCEPTION_OPENCL_ERROR_HPP
  12. #include <exception>
  13. #include <string>
  14. #include <sstream>
  15. #include <boost/compute/cl.hpp>
  16. namespace boost {
  17. namespace compute {
  18. /// \class opencl_error
  19. /// \brief A run-time OpenCL error.
  20. ///
  21. /// The opencl_error class represents an error returned from an OpenCL
  22. /// function.
  23. ///
  24. /// \see context_error
  25. class opencl_error : public std::exception
  26. {
  27. public:
  28. /// Creates a new opencl_error exception object for \p error.
  29. explicit opencl_error(cl_int error) throw()
  30. : m_error(error),
  31. m_error_string(to_string(error))
  32. {
  33. }
  34. /// Destroys the opencl_error object.
  35. ~opencl_error() throw()
  36. {
  37. }
  38. /// Returns the numeric error code.
  39. cl_int error_code() const throw()
  40. {
  41. return m_error;
  42. }
  43. /// Returns a string description of the error.
  44. std::string error_string() const throw()
  45. {
  46. return m_error_string;
  47. }
  48. /// Returns a C-string description of the error.
  49. const char* what() const throw()
  50. {
  51. return m_error_string.c_str();
  52. }
  53. /// Static function which converts the numeric OpenCL error code \p error
  54. /// to a human-readable string.
  55. ///
  56. /// For example:
  57. /// \code
  58. /// std::cout << opencl_error::to_string(CL_INVALID_KERNEL_ARGS) << std::endl;
  59. /// \endcode
  60. ///
  61. /// Will print "Invalid Kernel Arguments".
  62. ///
  63. /// If the error code is unknown (e.g. not a valid OpenCL error), a string
  64. /// containing "Unknown OpenCL Error" along with the error number will be
  65. /// returned.
  66. static std::string to_string(cl_int error)
  67. {
  68. switch(error){
  69. case CL_SUCCESS: return "Success";
  70. case CL_DEVICE_NOT_FOUND: return "Device Not Found";
  71. case CL_DEVICE_NOT_AVAILABLE: return "Device Not Available";
  72. case CL_COMPILER_NOT_AVAILABLE: return "Compiler Not Available";
  73. case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "Memory Object Allocation Failure";
  74. case CL_OUT_OF_RESOURCES: return "Out of Resources";
  75. case CL_OUT_OF_HOST_MEMORY: return "Out of Host Memory";
  76. case CL_PROFILING_INFO_NOT_AVAILABLE: return "Profiling Information Not Available";
  77. case CL_MEM_COPY_OVERLAP: return "Memory Copy Overlap";
  78. case CL_IMAGE_FORMAT_MISMATCH: return "Image Format Mismatch";
  79. case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "Image Format Not Supported";
  80. case CL_BUILD_PROGRAM_FAILURE: return "Build Program Failure";
  81. case CL_MAP_FAILURE: return "Map Failure";
  82. case CL_INVALID_VALUE: return "Invalid Value";
  83. case CL_INVALID_DEVICE_TYPE: return "Invalid Device Type";
  84. case CL_INVALID_PLATFORM: return "Invalid Platform";
  85. case CL_INVALID_DEVICE: return "Invalid Device";
  86. case CL_INVALID_CONTEXT: return "Invalid Context";
  87. case CL_INVALID_QUEUE_PROPERTIES: return "Invalid Queue Properties";
  88. case CL_INVALID_COMMAND_QUEUE: return "Invalid Command Queue";
  89. case CL_INVALID_HOST_PTR: return "Invalid Host Pointer";
  90. case CL_INVALID_MEM_OBJECT: return "Invalid Memory Object";
  91. case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "Invalid Image Format Descriptor";
  92. case CL_INVALID_IMAGE_SIZE: return "Invalid Image Size";
  93. case CL_INVALID_SAMPLER: return "Invalid Sampler";
  94. case CL_INVALID_BINARY: return "Invalid Binary";
  95. case CL_INVALID_BUILD_OPTIONS: return "Invalid Build Options";
  96. case CL_INVALID_PROGRAM: return "Invalid Program";
  97. case CL_INVALID_PROGRAM_EXECUTABLE: return "Invalid Program Executable";
  98. case CL_INVALID_KERNEL_NAME: return "Invalid Kernel Name";
  99. case CL_INVALID_KERNEL_DEFINITION: return "Invalid Kernel Definition";
  100. case CL_INVALID_KERNEL: return "Invalid Kernel";
  101. case CL_INVALID_ARG_INDEX: return "Invalid Argument Index";
  102. case CL_INVALID_ARG_VALUE: return "Invalid Argument Value";
  103. case CL_INVALID_ARG_SIZE: return "Invalid Argument Size";
  104. case CL_INVALID_KERNEL_ARGS: return "Invalid Kernel Arguments";
  105. case CL_INVALID_WORK_DIMENSION: return "Invalid Work Dimension";
  106. case CL_INVALID_WORK_GROUP_SIZE: return "Invalid Work Group Size";
  107. case CL_INVALID_WORK_ITEM_SIZE: return "Invalid Work Item Size";
  108. case CL_INVALID_GLOBAL_OFFSET: return "Invalid Global Offset";
  109. case CL_INVALID_EVENT_WAIT_LIST: return "Invalid Event Wait List";
  110. case CL_INVALID_EVENT: return "Invalid Event";
  111. case CL_INVALID_OPERATION: return "Invalid Operation";
  112. case CL_INVALID_GL_OBJECT: return "Invalid GL Object";
  113. case CL_INVALID_BUFFER_SIZE: return "Invalid Buffer Size";
  114. case CL_INVALID_MIP_LEVEL: return "Invalid MIP Level";
  115. case CL_INVALID_GLOBAL_WORK_SIZE: return "Invalid Global Work Size";
  116. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  117. case CL_COMPILE_PROGRAM_FAILURE: return "Compile Program Failure";
  118. case CL_LINKER_NOT_AVAILABLE: return "Linker Not Available";
  119. case CL_LINK_PROGRAM_FAILURE: return "Link Program Failure";
  120. case CL_DEVICE_PARTITION_FAILED: return "Device Partition Failed";
  121. case CL_KERNEL_ARG_INFO_NOT_AVAILABLE: return "Kernel Argument Info Not Available";
  122. case CL_INVALID_PROPERTY: return "Invalid Property";
  123. case CL_INVALID_IMAGE_DESCRIPTOR: return "Invalid Image Descriptor";
  124. case CL_INVALID_COMPILER_OPTIONS: return "Invalid Compiler Options";
  125. case CL_INVALID_LINKER_OPTIONS: return "Invalid Linker Options";
  126. case CL_INVALID_DEVICE_PARTITION_COUNT: return "Invalid Device Partition Count";
  127. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  128. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  129. case CL_INVALID_PIPE_SIZE: return "Invalid Pipe Size";
  130. case CL_INVALID_DEVICE_QUEUE: return "Invalid Device Queue";
  131. #endif
  132. default: {
  133. std::stringstream s;
  134. s << "Unknown OpenCL Error (" << error << ")";
  135. return s.str();
  136. }
  137. }
  138. }
  139. private:
  140. cl_int m_error;
  141. std::string m_error_string;
  142. };
  143. } // end compute namespace
  144. } // end boost namespace
  145. #endif // BOOST_COMPUTE_EXCEPTION_OPENCL_ERROR_HPP