LAMA
/home/brandes/workspace/LAMA/src/lama/cuda/CUDAError.hpp
Go to the documentation of this file.
00001 
00033 #ifndef LAMA_CUDA_ERROR_HPP_
00034 #define LAMA_CUDA_ERROR_HPP_
00035 
00036 #include <lama/exception/Exception.hpp>
00037 #include <lama/exception/LAMAAssert.hpp>
00038 
00039 #include <cuda.h>
00040 #include <cublas.h>
00041 
00042 namespace lama 
00043 {
00046 const char* cudaDriverErrorString( CUresult res );
00047 
00050 const char* cublasErrorString( cublasStatus res );
00051 
00052 }
00053 
00056 #define LAMA_CUDA_DRV_CALL(call, msg)                                               \
00057 {                                                                                   \
00058     CUresult res = call;                                                            \
00059     if ( CUDA_SUCCESS != res )                                                      \
00060     {                                                                               \
00061         std::ostringstream errorStr;                                                \
00062         errorStr << "CUDA driver error in line " << __LINE__;                       \
00063         errorStr << " of file " << __FILE__ << std::endl;                           \
00064         errorStr << "  Msg  : " << msg << std::endl;                                \
00065         errorStr << "  Call : " #call;                                              \
00066         errorStr << "  Error: ";                                                    \
00067         errorStr << lama::cudaDriverErrorString( res );                             \
00068         errorStr << ", CUresult = " << res << "\n";                                 \
00069         lama::Exception::addCallStack( errorStr );                                  \
00070         fprintf(stderr, "%s\n", errorStr.str().c_str() );                           \
00071         throw lama::Exception( errorStr.str() );                                    \
00072     }                                                                               \
00073 }
00074 
00075 #define LAMA_CUDA_RT_CALL(call, msg)                                                \
00076 {                                                                                   \
00077     cudaError_t res = call;                                                         \
00078     if ( cudaSuccess != res )                                                       \
00079     {                                                                               \
00080         std::ostringstream errorStr;                                                \
00081         errorStr << "CUDA runtime error in line " << __LINE__;                      \
00082         errorStr << " of file " << __FILE__ << std::endl;                           \
00083         errorStr << "  Call : " #call;                                              \
00084         errorStr << "  Msg  : " << msg << std::endl;                                \
00085         errorStr << "  Error: ";                                                    \
00086         errorStr << cudaGetErrorString( res );                                      \
00087         errorStr << ", cudaError_t = " << res << "\n";                              \
00088         lama::Exception::addCallStack( errorStr );                                  \
00089         fprintf(stderr, "%s\n", errorStr.str().c_str() );                           \
00090         throw lama::Exception( errorStr.str() );                                    \
00091     }                                                                               \
00092 }
00093 
00094 #define LAMA_CUBLAS_CALL( call, msg )                                               \
00095 {                                                                                   \
00096     cublasStatus res = call;                                                        \
00097     if ( CUBLAS_STATUS_SUCCESS != res )                                             \
00098     {                                                                               \
00099         std::ostringstream errorStr;                                                \
00100         errorStr << "CUBLAS error in line " << __LINE__;                            \
00101         errorStr << " of file " << __FILE__ << std::endl;                           \
00102         errorStr << "  Call : " #call;                                              \
00103         errorStr << "  Msg  : " << msg << std::endl;                                \
00104         errorStr << "  Error: ";                                                    \
00105         errorStr << cublasErrorString( res );                                       \
00106         errorStr << ", cublasStatus = " << res << "\n";                             \
00107         lama::Exception::addCallStack( errorStr );                                  \
00108         fprintf(stderr, "%s\n", errorStr.str().c_str() );                           \
00109         throw lama::Exception( errorStr.str() );                                    \
00110     }                                                                               \
00111 }
00112 
00113 #define LAMA_CUSPARSE_CALL( call, msg )                                             \
00114 {                                                                                   \
00115     cusparseStatus_t res = call;                                                    \
00116     if ( CUSPARSE_STATUS_SUCCESS != res )                                           \
00117     {                                                                               \
00118         std::ostringstream errorStr;                                                \
00119         errorStr << "CUSparse error in line " << __LINE__;                          \
00120         errorStr << " of file " << __FILE__ << std::endl;                           \
00121         errorStr << "  Call : " #call;                                              \
00122         errorStr << "  Msg  : " << msg << std::endl;                                \
00123         errorStr << "  Error: ";                                                    \
00124         errorStr << "cusparseStatus = " << res << "\n";                             \
00125         lama::Exception::addCallStack( errorStr );                                  \
00126         fprintf(stderr, "%s\n", errorStr.str().c_str() );                           \
00127         throw lama::Exception( errorStr.str() );                                    \
00128     }                                                                               \
00129 }
00130 
00131 #define LAMA_CHECK_CUDA_ACCESS                                                        \
00132 {                                                                                     \
00133     CUcontext pctx;                                                                   \
00134     const int cudaErrorValue = cuCtxGetCurrent( &pctx );                              \
00135     LAMA_ASSERT_EQUAL_ERROR( cudaErrorValue, cudaSuccess )                            \
00136     LAMA_ASSERT_ERROR( pctx, "No current context, forgotten LAMA_CONTEXT_ACCESS ?" ); \
00137 }
00138 
00139 #define LAMA_CHECK_CUDA_ERROR                                                     \
00140 {                                                                                 \
00141     LAMA_CUDA_RT_CALL( cudaGetLastError(), "last error CUBLAS" )                  \
00142 }
00143 
00144 #define LAMA_CHECK_CUBLAS_ERROR                                                   \
00145 {                                                                                 \
00146     LAMA_CUBLAS_CALL( cublasGetError(), "last error CUBLAS" )                     \
00147 }
00148 
00149 
00150 //    #define LAMA_CHECK_CUDA_ERROR 
00151 
00152 #endif //  LAMA_CUDA_ERROR_HPP_