1 #ifndef CUDA_ERROR_CHECK_H
2 #define CUDA_ERROR_CHECK_H
3 //##################################################################################
4 //Code taken from https://codeyarns.com/2011/03/02/how-to-do-error-checking-in-cuda/
7 #define CudaSafeCall( err ) __cudaSafeCall( err, #err, __FILE__, __LINE__ )
8 #define CudaCheckError() __cudaCheckError( __FILE__, __LINE__ )
10 static inline void __cudaSafeCall( cudaError err, const char *text, const char *file, const int line )
12 if ( cudaSuccess != err )
14 fprintf( stderr, "%s:%i: %s: %s\n",
15 file, line, text, cudaGetErrorString( err ) );
22 static inline void __cudaCheckError( const char *file, const int line )
24 cudaError err = cudaGetLastError();
25 if ( cudaSuccess != err )
27 fprintf( stderr, "cudaCheckError() failed at %s:%i : %s\n",
28 file, line, cudaGetErrorString( err ) );
33 // More careful checking. However, this will affect performance.
34 err = cudaDeviceSynchronize();
35 if( cudaSuccess != err )
37 fprintf( stderr, "cudaCheckError() with sync failed at %s:%i : %s\n",
38 file, line, cudaGetErrorString( err ) );
45 //##############################################################################
48 static inline const char *_cudaGetErrorEnum(cufftResult error)
53 return "CUFFT_SUCCESS";
55 case CUFFT_INVALID_PLAN:
56 return "CUFFT_INVALID_PLAN";
58 case CUFFT_ALLOC_FAILED:
59 return "CUFFT_ALLOC_FAILED";
61 case CUFFT_INVALID_TYPE:
62 return "CUFFT_INVALID_TYPE";
64 case CUFFT_INVALID_VALUE:
65 return "CUFFT_INVALID_VALUE";
67 case CUFFT_INTERNAL_ERROR:
68 return "CUFFT_INTERNAL_ERROR";
70 case CUFFT_EXEC_FAILED:
71 return "CUFFT_EXEC_FAILED";
73 case CUFFT_SETUP_FAILED:
74 return "CUFFT_SETUP_FAILED";
76 case CUFFT_INVALID_SIZE:
77 return "CUFFT_INVALID_SIZE";
79 case CUFFT_UNALIGNED_DATA:
80 return "CUFFT_UNALIGNED_DATA";
82 case CUFFT_INVALID_DEVICE:
83 return "CUFFT_INVALID_DEVICE";
85 case CUFFT_PARSE_ERROR:
86 return "CUFFT_PARSE_ERROR";
88 case CUFFT_NO_WORKSPACE:
89 return "CUFFT_NO_WORKSPACE";
91 case CUFFT_NOT_IMPLEMENTED:
92 return "CUFFT_NOT_IMPLEMENTED";
94 case CUFFT_LICENSE_ERROR:
95 return "CUFFT_LICENSE_ERROR";
97 case CUFFT_NOT_SUPPORTED:
98 return "CUFFT_NOT_SUPPORTED";
100 case CUFFT_INCOMPLETE_PARAMETER_LIST:
101 return "CUFFT_INCOMPLETE_PARAMETER_LIST";
107 // This uses C++ overload resolution to print correct error message based on call type
108 #define cudaErrorCheck(call) __cudaErrorCheck(call, __FILE__, __LINE__ )
110 static inline void __cudaErrorCheck(cufftResult_t call, const char *file, const int line )
112 if (call != CUFFT_SUCCESS) {
113 fprintf(stderr, "cuFFT error %d:%s at %s:%d\n", call, _cudaGetErrorEnum(call), file, line);
120 static inline void __cudaErrorCheck(cublasStatus_t call, const char *file, const int line )
122 if (call != CUBLAS_STATUS_SUCCESS) {
123 const char *status_str;
125 #define CUBLAS_STATUS_STR(SYM) case CUBLAS_STATUS_ ## SYM: status_str = #SYM; break
126 CUBLAS_STATUS_STR(SUCCESS);
127 CUBLAS_STATUS_STR(NOT_INITIALIZED);
128 CUBLAS_STATUS_STR(ALLOC_FAILED);
129 CUBLAS_STATUS_STR(INVALID_VALUE);
130 CUBLAS_STATUS_STR(ARCH_MISMATCH);
131 CUBLAS_STATUS_STR(MAPPING_ERROR);
132 CUBLAS_STATUS_STR(EXECUTION_FAILED);
133 CUBLAS_STATUS_STR(INTERNAL_ERROR);
134 CUBLAS_STATUS_STR(NOT_SUPPORTED);
135 CUBLAS_STATUS_STR(LICENSE_ERROR);
138 #undef CUBLAS_STATUS_STR
140 fprintf(stderr, "cuBLAS error %d (%s) at %s:%d\n", call, status_str, file, line);