ViennaCL - The Vienna Computing Library  1.4.2
viennacl/ocl/error.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_OCL_ERROR_HPP_
00002 #define VIENNACL_OCL_ERROR_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2013, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015                
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00025 //error levels:
00026 //#define VIENNACL_DEBUG_ALL           //print all of the following
00027 //#define VIENNACL_DEBUG_KERNEL        //debug any modifications on viennacl::ocl::kernel objects
00028 //#define VIENNACL_DEBUG_COPY          //print infos related to setting up/modifying memory objects
00029 //#define VIENNACL_DEBUG_OPENCL        //display debug info for the OpenCL layer (platform/context/queue creation,
00030 //#define VIENNACL_DEBUG_DEVICE        //Show device info upon allocation
00031 //#define VIENNACL_DEBUG_CONTEXT       //Debug queries to context
00032 //#define VIENNACL_DEBUG_BUILD         //Show debug info from OpenCL compiler
00033 
00034 
00035 //backwards compatibility:
00036 #ifdef VIENNACL_BUILD_INFO
00037   #define VIENNACL_DEBUG_ALL
00038 #endif
00039 
00040 
00041 #ifdef __APPLE__
00042 #include <OpenCL/cl.h>
00043 #else
00044 #include <CL/cl.h>
00045 #endif
00046 
00047 #include <string>
00048 #include <iostream>
00049 #include <exception>
00050 
00051 #define VIENNACL_BUG_REPORT_STRING  \
00052                "\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"\
00053                " * Operating System\n"\
00054                " * Which OpenCL implementation (AMD, NVIDIA, etc.)\n"\
00055                " * ViennaCL version\n"\
00056                "Many thanks in advance!";\
00057 
00058 namespace viennacl
00059 {
00060   namespace ocl
00061   {
00062     //Wrapper for OpenCL exceptions:
00063     class device_not_found : public std::exception
00064     {
00065       virtual const char* what() const throw()
00066       {
00067         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."
00068                VIENNACL_BUG_REPORT_STRING;
00069       }
00070     };
00071     
00072     class device_not_available : public std::exception
00073     {
00074       virtual const char* what() const throw()
00075       {
00076         return "ViennaCL: FATAL ERROR: CL_DEVICE_NOT_AVAILABLE \n ViennaCL could not use the compute device because it is not available."
00077                VIENNACL_BUG_REPORT_STRING;
00078       }
00079     };
00080 
00081     class compiler_not_available : public std::exception
00082     {
00083       virtual const char* what() const throw()
00084       {
00085         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."
00086                VIENNACL_BUG_REPORT_STRING;
00087       }
00088     };
00089     
00090     class mem_object_allocation_failure : public std::exception
00091     {
00092       virtual const char* what() const throw()
00093       {
00094         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."
00095                VIENNACL_BUG_REPORT_STRING;
00096       }
00097     };
00098     
00099     class out_of_resources : public std::exception
00100     {
00101       virtual const char* what() const throw()
00102       {
00103         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."
00104                VIENNACL_BUG_REPORT_STRING;
00105       }
00106     };
00107 
00108     class out_of_host_memory : public std::exception
00109     {
00110       virtual const char* what() const throw()
00111       {
00112         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."
00113                VIENNACL_BUG_REPORT_STRING;
00114       }
00115     };
00116     
00117     class profiling_info_not_available : public std::exception
00118     {
00119       virtual const char* what() const throw()
00120       {
00121         return "ViennaCL: FATAL ERROR: CL_PROFILING_INFO_NOT_AVAILABLE."
00122                VIENNACL_BUG_REPORT_STRING;
00123       }
00124     };
00125     
00126     class mem_copy_overlap : public std::exception
00127     {
00128       virtual const char* what() const throw()
00129       {
00130         return "ViennaCL: FATAL ERROR: CL_MEM_COPY_OVERLAP."
00131                VIENNACL_BUG_REPORT_STRING;
00132       }
00133     };
00134     
00135     class image_format_mismatch : public std::exception
00136     {
00137       virtual const char* what() const throw()
00138       {
00139         return "ViennaCL: FATAL ERROR: CL_IMAGE_FORMAT_MISMATCH."
00140                VIENNACL_BUG_REPORT_STRING;
00141       }
00142     };
00143     
00144     class image_format_not_supported : public std::exception
00145     {
00146       virtual const char* what() const throw()
00147       {
00148         return "ViennaCL: FATAL ERROR: CL_IMAGE_FORMAT_NOT_SUPPORTED."
00149                VIENNACL_BUG_REPORT_STRING;
00150       }
00151     };
00152 
00153     class build_program_failure : public std::exception
00154     {
00155       virtual const char* what() const throw()
00156       {
00157         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."
00158                VIENNACL_BUG_REPORT_STRING;
00159       }
00160     };
00161     
00162     class map_failure : public std::exception
00163     {
00164       virtual const char* what() const throw()
00165       {
00166         return "ViennaCL: FATAL ERROR: CL_MAP_FAILURE."
00167                VIENNACL_BUG_REPORT_STRING;
00168       }
00169     };
00170     
00171     class invalid_value : public std::exception
00172     {
00173       virtual const char* what() const throw()
00174       {
00175         return "ViennaCL: FATAL ERROR: CL_INVALID_VALUE."
00176                VIENNACL_BUG_REPORT_STRING;
00177       }
00178     };
00179     
00180     class invalid_device_type : public std::exception
00181     {
00182       virtual const char* what() const throw()
00183       {
00184         return "ViennaCL: FATAL ERROR: CL_INVALID_DEVICE_TYPE."
00185                VIENNACL_BUG_REPORT_STRING;
00186       }
00187     };
00188     
00189     class invalid_platform : public std::exception
00190     {
00191       virtual const char* what() const throw()
00192       {
00193         return "ViennaCL: FATAL ERROR: CL_INVALID_PLATFORM."
00194                VIENNACL_BUG_REPORT_STRING;
00195       }
00196     };
00197     
00198     class invalid_device : public std::exception
00199     {
00200       virtual const char* what() const throw()
00201       {
00202         return "ViennaCL: FATAL ERROR: CL_INVALID_DEVICE."
00203                VIENNACL_BUG_REPORT_STRING;
00204       }
00205     };
00206     
00207     class invalid_context : public std::exception
00208     {
00209       virtual const char* what() const throw()
00210       {
00211         return "ViennaCL: FATAL ERROR: CL_INVALID_CONTEXT."
00212                VIENNACL_BUG_REPORT_STRING;
00213       }
00214     };
00215     
00216     class invalid_queue_properties : public std::exception
00217     {
00218       virtual const char* what() const throw()
00219       {
00220         return "ViennaCL: FATAL ERROR: CL_INVALID_QUEUE_PROPERTIES."
00221                VIENNACL_BUG_REPORT_STRING;
00222       }
00223     };
00224     
00225     class invalid_command_queue : public std::exception
00226     {
00227       virtual const char* what() const throw()
00228       {
00229         return "ViennaCL: FATAL ERROR: CL_INVALID_COMMAND_QUEUE."
00230                VIENNACL_BUG_REPORT_STRING;
00231       }
00232     };
00233     
00234     class invalid_host_ptr : public std::exception
00235     {
00236       virtual const char* what() const throw()
00237       {
00238         return "ViennaCL: FATAL ERROR: CL_INVALID_HOST_PTR."
00239                VIENNACL_BUG_REPORT_STRING;
00240       }
00241     };
00242     
00243     class invalid_mem_object : public std::exception
00244     {
00245       virtual const char* what() const throw()
00246       {
00247         return "ViennaCL: FATAL ERROR: CL_INVALID_MEM_OBJECT."
00248                VIENNACL_BUG_REPORT_STRING;
00249       }
00250     };
00251     
00252     class invalid_image_format_descriptor : public std::exception
00253     {
00254       virtual const char* what() const throw()
00255       {
00256         return "ViennaCL: FATAL ERROR: CL_INVALID_IMAGE_FORMAT_DESCRIPTOR."
00257                VIENNACL_BUG_REPORT_STRING;
00258       }
00259     };
00260     
00261     class invalid_image_size : public std::exception
00262     {
00263       virtual const char* what() const throw()
00264       {
00265         return "ViennaCL: FATAL ERROR: CL_INVALID_IMAGE_SIZE."
00266                VIENNACL_BUG_REPORT_STRING;
00267       }
00268     };
00269     
00270     class invalid_sampler : public std::exception
00271     {
00272       virtual const char* what() const throw()
00273       {
00274         return "ViennaCL: FATAL ERROR: CL_INVALID_SAMPLER."
00275                VIENNACL_BUG_REPORT_STRING;
00276       }
00277     };
00278     
00279     class invalid_binary : public std::exception
00280     {
00281       virtual const char* what() const throw()
00282       {
00283         return "ViennaCL: FATAL ERROR: CL_INVALID_BINARY."
00284                VIENNACL_BUG_REPORT_STRING;
00285       }
00286     };
00287     
00288     class invalid_build_options : public std::exception
00289     {
00290       virtual const char* what() const throw()
00291       {
00292         return "ViennaCL: FATAL ERROR: CL_INVALID_BUILD_OPTIONS."
00293                VIENNACL_BUG_REPORT_STRING;
00294       }
00295     };
00296     
00297     class invalid_program : public std::exception
00298     {
00299       virtual const char* what() const throw()
00300       {
00301         return "ViennaCL: FATAL ERROR: CL_INVALID_PROGRAM."
00302                VIENNACL_BUG_REPORT_STRING;
00303       }
00304     };
00305     
00306     class invalid_program_executable : public std::exception
00307     {
00308       virtual const char* what() const throw()
00309       {
00310         return "ViennaCL: FATAL ERROR: CL_INVALID_PROGRAM_EXECUTABLE."
00311                VIENNACL_BUG_REPORT_STRING;
00312       }
00313     };
00314     
00315     class invalid_kernel_name : public std::exception
00316     {
00317       virtual const char* what() const throw()
00318       {
00319         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."
00320                VIENNACL_BUG_REPORT_STRING;
00321       }
00322     };
00323 
00324     class invalid_kernel_definition : public std::exception
00325     {
00326       virtual const char* what() const throw()
00327       {
00328         return "ViennaCL: FATAL ERROR: CL_INVALID_KERNEL_DEFINITION."
00329                VIENNACL_BUG_REPORT_STRING;
00330       }
00331     };
00332 
00333     class invalid_kernel : public std::exception
00334     {
00335       virtual const char* what() const throw()
00336       {
00337         return "ViennaCL: FATAL ERROR: CL_INVALID_KERNEL \n The supplied kernel argument is invalid."
00338                VIENNACL_BUG_REPORT_STRING;
00339       }
00340     };
00341     
00342     class invalid_arg_index : public std::exception
00343     {
00344       virtual const char* what() const throw()
00345       {
00346         return "ViennaCL: FATAL ERROR: CL_INVALID_ARG_INDEX."
00347                VIENNACL_BUG_REPORT_STRING;
00348       }
00349     };
00350     
00351     class invalid_arg_value : public std::exception
00352     {
00353       virtual const char* what() const throw()
00354       {
00355         return "ViennaCL: FATAL ERROR: CL_INVALID_ARG_VALUE."
00356                VIENNACL_BUG_REPORT_STRING;
00357       }
00358     };
00359 
00360     class invalid_arg_size : public std::exception
00361     {
00362       virtual const char* what() const throw()
00363       {
00364         return "ViennaCL: FATAL ERROR: CL_INVALID_ARG_SIZE."
00365                VIENNACL_BUG_REPORT_STRING;
00366       }
00367     };
00368     
00369     class invalid_kernel_args : public std::exception
00370     {
00371       virtual const char* what() const throw()
00372       {
00373         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."
00374                VIENNACL_BUG_REPORT_STRING;
00375       }
00376     };
00377     
00378     class invalid_work_dimension : public std::exception
00379     {
00380       virtual const char* what() const throw()
00381       {
00382         return "ViennaCL: FATAL ERROR: CL_INVALID_WORK_DIMENSION"
00383                VIENNACL_BUG_REPORT_STRING;
00384       }
00385     };
00386     
00387     class invalid_work_group_size : public std::exception
00388     {
00389       virtual const char* what() const throw()
00390       {
00391         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."
00392                VIENNACL_BUG_REPORT_STRING;
00393       }
00394     };
00395 
00396     class invalid_work_item_size : public std::exception
00397     {
00398       virtual const char* what() const throw()
00399       {
00400         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."
00401                VIENNACL_BUG_REPORT_STRING;
00402       }
00403     };
00404     
00405     class invalid_global_offset : public std::exception
00406     {
00407       virtual const char* what() const throw()
00408       {
00409         return "ViennaCL: FATAL ERROR: CL_INVALID_GLOBAL_OFFSET."
00410                VIENNACL_BUG_REPORT_STRING;
00411       }
00412     };
00413     
00414     class invalid_event_wait_list : public std::exception
00415     {
00416       virtual const char* what() const throw()
00417       {
00418         return "ViennaCL: FATAL ERROR: CL_INVALID_EVENT_WAIT_LIST."
00419                VIENNACL_BUG_REPORT_STRING;
00420       }
00421     };
00422     
00423     class invalid_event : public std::exception
00424     {
00425       virtual const char* what() const throw()
00426       {
00427         return "ViennaCL: FATAL ERROR: CL_INVALID_EVENT."
00428                VIENNACL_BUG_REPORT_STRING;
00429       }
00430     };
00431     
00432     class invalid_operation : public std::exception
00433     {
00434       virtual const char* what() const throw()
00435       {
00436         return "ViennaCL: FATAL ERROR: CL_INVALID_OPERATION."
00437                VIENNACL_BUG_REPORT_STRING;
00438       }
00439     };
00440     
00441     class invalid_gl_object : public std::exception
00442     {
00443       virtual const char* what() const throw()
00444       {
00445         return "ViennaCL: FATAL ERROR: CL_INVALID_GL_OBJECT."
00446                VIENNACL_BUG_REPORT_STRING;
00447       }
00448     };
00449     
00450     class invalid_buffer_size : public std::exception
00451     {
00452       virtual const char* what() const throw()
00453       {
00454         return "ViennaCL: FATAL ERROR: CL_INVALID_BUFFER_SIZE."
00455                VIENNACL_BUG_REPORT_STRING;
00456       }
00457     };
00458     
00459     class invalid_mip_level : public std::exception
00460     {
00461       virtual const char* what() const throw()
00462       {
00463         return "ViennaCL: FATAL ERROR: CL_INVALID_MIP_LEVEL."
00464                VIENNACL_BUG_REPORT_STRING;
00465       }
00466     };
00467     
00468     class invalid_global_work_size : public std::exception
00469     {
00470       virtual const char* what() const throw()
00471       {
00472         return "ViennaCL: FATAL ERROR: CL_INVALID_GLOBAL_WORK_SIZE."
00473                VIENNACL_BUG_REPORT_STRING;
00474       }
00475     };
00476 
00477     class invalid_property : public std::exception
00478     {
00479       virtual const char* what() const throw()
00480       {
00481         return "ViennaCL: FATAL ERROR: CL_INVALID_PROPERTY."
00482                VIENNACL_BUG_REPORT_STRING;
00483       }
00484     };
00485 
00486     class unknown_error : public std::exception
00487     {
00488       virtual const char* what() const throw()
00489       {
00490         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."
00491                VIENNACL_BUG_REPORT_STRING;
00492       }
00493     };
00494 
00495     
00496     class double_precision_not_provided_error : public std::exception
00497     {
00498       virtual const char* what() const throw()
00499       {
00500         return "ViennaCL: FATAL ERROR: You requested to create a ViennaCL type using double precision. However, double precision is not supported by your device."
00501                VIENNACL_BUG_REPORT_STRING;
00502       }
00503     };
00504     
00505     
00511     template <typename T>
00512     struct error_checker
00513     {
00514       
00516       static void raise_exception(cl_int err)
00517       {
00518         switch (err)
00519         {
00520           case CL_DEVICE_NOT_FOUND:               throw device_not_found();
00521           case CL_DEVICE_NOT_AVAILABLE:           throw device_not_available();
00522           case CL_COMPILER_NOT_AVAILABLE:         throw compiler_not_available();
00523           case CL_MEM_OBJECT_ALLOCATION_FAILURE:  throw mem_object_allocation_failure();
00524           case CL_OUT_OF_RESOURCES:               throw out_of_resources();
00525           case CL_OUT_OF_HOST_MEMORY:             throw out_of_host_memory();
00526           case CL_PROFILING_INFO_NOT_AVAILABLE:   throw profiling_info_not_available();
00527           case CL_MEM_COPY_OVERLAP:               throw mem_copy_overlap();
00528           case CL_IMAGE_FORMAT_MISMATCH:          throw image_format_mismatch();
00529           case CL_IMAGE_FORMAT_NOT_SUPPORTED:     throw image_format_not_supported();
00530           case CL_BUILD_PROGRAM_FAILURE:          throw build_program_failure();
00531           case CL_MAP_FAILURE:                    throw map_failure();
00532 
00533           case CL_INVALID_VALUE:                  throw invalid_value();
00534           case CL_INVALID_DEVICE_TYPE:            throw invalid_device_type();
00535           case CL_INVALID_PLATFORM:               throw invalid_platform();
00536           case CL_INVALID_DEVICE:                 throw invalid_device();
00537           case CL_INVALID_CONTEXT:                throw invalid_context();
00538           case CL_INVALID_QUEUE_PROPERTIES:       throw invalid_queue_properties();
00539           case CL_INVALID_COMMAND_QUEUE:          throw invalid_command_queue();
00540           case CL_INVALID_HOST_PTR:               throw invalid_host_ptr();
00541           case CL_INVALID_MEM_OBJECT:             throw invalid_mem_object();
00542           case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: throw invalid_image_format_descriptor();
00543           case CL_INVALID_IMAGE_SIZE:             throw invalid_image_size();
00544           case CL_INVALID_SAMPLER:                throw invalid_sampler();
00545           case CL_INVALID_BINARY:                 throw invalid_binary();
00546           case CL_INVALID_BUILD_OPTIONS:          throw invalid_build_options();
00547           case CL_INVALID_PROGRAM:                throw invalid_program();
00548           case CL_INVALID_PROGRAM_EXECUTABLE:     throw invalid_program_executable();
00549           case CL_INVALID_KERNEL_NAME:            throw invalid_kernel_name();
00550           case CL_INVALID_KERNEL_DEFINITION:      throw invalid_kernel_definition();
00551           case CL_INVALID_KERNEL:                 throw invalid_kernel();
00552           case CL_INVALID_ARG_INDEX:              throw invalid_arg_index();
00553           case CL_INVALID_ARG_VALUE:              throw invalid_arg_value();
00554           case CL_INVALID_ARG_SIZE:               throw invalid_arg_size();
00555           case CL_INVALID_KERNEL_ARGS:            throw invalid_kernel_args();
00556           case CL_INVALID_WORK_DIMENSION:         throw invalid_work_dimension();
00557           case CL_INVALID_WORK_GROUP_SIZE:        throw invalid_work_group_size();
00558           case CL_INVALID_WORK_ITEM_SIZE:         throw invalid_work_item_size();
00559           case CL_INVALID_GLOBAL_OFFSET:          throw invalid_global_offset();
00560           case CL_INVALID_EVENT_WAIT_LIST:        throw invalid_event_wait_list();
00561           case CL_INVALID_EVENT:                  throw invalid_event();
00562           case CL_INVALID_OPERATION:              throw invalid_operation();
00563           case CL_INVALID_GL_OBJECT:              throw invalid_gl_object();
00564           case CL_INVALID_BUFFER_SIZE:            throw invalid_buffer_size();
00565           case CL_INVALID_MIP_LEVEL:              throw invalid_mip_level();
00566           case CL_INVALID_GLOBAL_WORK_SIZE:       throw invalid_global_work_size();
00567       #ifdef CL_INVALID_PROPERTY
00568           case CL_INVALID_PROPERTY:               throw invalid_property();
00569       #endif
00570           //  return "CL_INVALID_GLOBAL_WORK_SIZE";
00571             
00572           default: throw unknown_error();
00573         }
00574 
00575       } //getErrorString
00576     
00581       static void checkError(cl_int err,
00582           #ifdef VIENNACL_DEBUG_ALL
00583                              const std::string & file,
00584                              const std::string & func,
00585                              int line)
00586           #else
00587                              const std::string &,
00588                              const std::string &,
00589                              int)
00590           #endif
00591       {
00592         if (err != CL_SUCCESS)
00593         {
00594           #ifdef VIENNACL_DEBUG_ALL
00595           std::cerr << "ViennaCL: Error " << err  << " in function " << func << " ( "<< file << ":" << line << " ) " << std::endl;
00596           #endif
00597           raise_exception(err);
00598         }
00599       } //checkError()
00600       
00601     }; //struct 
00602     
00603     #define VIENNACL_ERR_CHECK(err) viennacl::ocl::error_checker<void>::checkError(err, __FILE__, __FUNCTION__, __LINE__);
00604     
00605   } //namespace ocl
00606 } //namespace viennacl
00607 
00608 #endif
00609