]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/cuda_error_check.hpp
README: Describe the main differences from the original implementation
[hercules2020/kcf.git] / src / cuda_error_check.hpp
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/
5 #include <iostream>
6
7 #define CudaSafeCall( err ) __cudaSafeCall( err, #err, __FILE__, __LINE__ )
8 #define CudaCheckError()    __cudaCheckError( __FILE__, __LINE__ )
9
10 static inline void __cudaSafeCall( cudaError err, const char *text, const char *file, const int line )
11 {
12     if ( cudaSuccess != err )
13     {
14         fprintf( stderr, "%s:%i: %s: %s\n",
15                  file, line, text, cudaGetErrorString( err ) );
16         exit( -1 );
17     }
18
19     return;
20 }
21
22 static inline void __cudaCheckError( const char *file, const int line )
23 {
24     cudaError err = cudaGetLastError();
25     if ( cudaSuccess != err )
26     {
27         fprintf( stderr, "cudaCheckError() failed at %s:%i : %s\n",
28                  file, line, cudaGetErrorString( err ) );
29         exit( -1 );
30     }
31
32 #ifdef CUDA_DEBUG
33     // More careful checking. However, this will affect performance.
34     err = cudaDeviceSynchronize();
35     if( cudaSuccess != err )
36     {
37         fprintf( stderr, "cudaCheckError() with sync failed at %s:%i : %s\n",
38                  file, line, cudaGetErrorString( err ) );
39         exit( -1 );
40     }
41 #endif
42
43     return;
44 }
45 //##############################################################################
46 #ifdef _CUFFT_H_
47 // cuFFT API errors
48 static inline const char *_cudaGetErrorEnum(cufftResult error)
49 {
50     switch (error)
51     {
52         case CUFFT_SUCCESS:
53             return "CUFFT_SUCCESS";
54
55         case CUFFT_INVALID_PLAN:
56             return "CUFFT_INVALID_PLAN";
57
58         case CUFFT_ALLOC_FAILED:
59             return "CUFFT_ALLOC_FAILED";
60
61         case CUFFT_INVALID_TYPE:
62             return "CUFFT_INVALID_TYPE";
63
64         case CUFFT_INVALID_VALUE:
65             return "CUFFT_INVALID_VALUE";
66
67         case CUFFT_INTERNAL_ERROR:
68             return "CUFFT_INTERNAL_ERROR";
69
70         case CUFFT_EXEC_FAILED:
71             return "CUFFT_EXEC_FAILED";
72
73         case CUFFT_SETUP_FAILED:
74             return "CUFFT_SETUP_FAILED";
75
76         case CUFFT_INVALID_SIZE:
77             return "CUFFT_INVALID_SIZE";
78
79         case CUFFT_UNALIGNED_DATA:
80             return "CUFFT_UNALIGNED_DATA";
81
82         case CUFFT_INVALID_DEVICE:
83             return "CUFFT_INVALID_DEVICE";
84
85         case CUFFT_PARSE_ERROR:
86             return "CUFFT_PARSE_ERROR";
87
88         case CUFFT_NO_WORKSPACE:
89             return "CUFFT_NO_WORKSPACE";
90
91         case CUFFT_NOT_IMPLEMENTED:
92             return "CUFFT_NOT_IMPLEMENTED";
93
94         case CUFFT_LICENSE_ERROR:
95             return "CUFFT_LICENSE_ERROR";
96
97         case CUFFT_NOT_SUPPORTED:
98             return "CUFFT_NOT_SUPPORTED";
99
100         case CUFFT_INCOMPLETE_PARAMETER_LIST:
101             return "CUFFT_INCOMPLETE_PARAMETER_LIST";
102     }
103
104     return "<unknown>";
105 }
106
107 // This uses C++ overload resolution to print correct error message based on call type
108 #define cudaErrorCheck(call) __cudaErrorCheck(call, __FILE__, __LINE__ )
109
110 static inline void __cudaErrorCheck(cufftResult_t call, const char *file, const int line )
111 {
112     if (call != CUFFT_SUCCESS) {
113         fprintf(stderr, "cuFFT error %d:%s at %s:%d\n", call, _cudaGetErrorEnum(call), file, line);
114         exit(-1);
115     }
116
117     return;
118 }
119
120 static inline void __cudaErrorCheck(cublasStatus_t call, const char *file, const int line )
121 {
122     if (call != CUBLAS_STATUS_SUCCESS) {
123         const char *status_str;
124         switch (call) {
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);
136         default:
137             status_str = "???";
138 #undef CUBLAS_STATUS_STR
139         }
140         fprintf(stderr, "cuBLAS error %d (%s) at %s:%d\n", call, status_str, file, line);
141         exit(-1);
142     }
143
144     return;
145 }
146
147 #endif
148
149 #endif