Cytnx v0.7.4
Loading...
Searching...
No Matches
cytnx_error.hpp
Go to the documentation of this file.
1#ifndef _H_cytnx_error_
2#define _H_cytnx_error_
3
4 #include <cstdio>
5 #include <cstdlib>
6 #include <cstring>
7 #include <stdarg.h>
8
9 #include <iostream>
10 #include <stdexcept>
11
12 #ifdef _MSC_VER
13 #define __PRETTY_FUNCTION__ __FUNCTION__
14 #endif
15
16
17
18 #define cytnx_error_msg(is_true, format, ...) {error_msg ( __PRETTY_FUNCTION__, __FILE__, __LINE__, (is_true), (format), __VA_ARGS__);}
19 static inline void error_msg( char const *const func, const char *const file, int const line, bool is_true, char const* format, ...){
20 try{
21 if (is_true)
22 {
23 va_list args;
24 char output_str[1024];
25 char msg[512];
26 va_start(args, format);
27 vsprintf(msg, format, args);
28 sprintf(output_str, "\n# Cytnx error occur at %s\n# error: %s\n# file : %s (%d)", func, msg, file, line) ;
29 va_end(args);
30 throw std::logic_error(output_str);
31 }
32 }catch(const char *output_msg){
33 std::cerr << output_msg << std::endl;
34 }
35
36 }
37 #define cytnx_warning_msg(is_true, format, ...) {warning_msg ( __PRETTY_FUNCTION__, __FILE__, __LINE__, (is_true), (format), __VA_ARGS__);}
38 static inline void warning_msg( char const *const func, const char *const file, int const line, bool is_true, char const* format, ...){
39 if (is_true)
40 {
41 va_list args;
42 char output_str[1024];
43 char msg[512];
44 va_start(args, format);
45 vsprintf(msg, format, args);
46 sprintf(output_str, "\n# Cytnx warning occur at %s\n# warning: %s\n# file : %s (%d)", func, msg, file, line) ;
47 va_end(args);
48 std::cerr << output_str << std::endl;
49 }
50 }
51
52
53 #if defined(UNI_GPU)
54
55 #include <cuda.h>
56 #include <cuda_runtime.h>
57 #include <cublas_v2.h>
58 #include <cusolverDn.h>
59 #include <cuComplex.h>
60 #include <curand.h>
61
62 #ifdef __DRIVER_TYPES_H__
63 static const char *_cudaGetErrorEnum(cudaError_t error)
64 {
65 switch (error)
66 {
67 case cudaSuccess:
68 return "cudaSuccess";
69
70 case cudaErrorMissingConfiguration:
71 return "cudaErrorMissingConfiguration";
72
73 case cudaErrorMemoryAllocation:
74 return "cudaErrorMemoryAllocation";
75
76 case cudaErrorInitializationError:
77 return "cudaErrorInitializationError";
78
79 case cudaErrorLaunchFailure:
80 return "cudaErrorLaunchFailure";
81
82 case cudaErrorPriorLaunchFailure:
83 return "cudaErrorPriorLaunchFailure";
84
85 case cudaErrorLaunchTimeout:
86 return "cudaErrorLaunchTimeout";
87
88 case cudaErrorLaunchOutOfResources:
89 return "cudaErrorLaunchOutOfResources";
90
91 case cudaErrorInvalidDeviceFunction:
92 return "cudaErrorInvalidDeviceFunction";
93
94 case cudaErrorInvalidConfiguration:
95 return "cudaErrorInvalidConfiguration";
96
97 case cudaErrorInvalidDevice:
98 return "cudaErrorInvalidDevice";
99
100 case cudaErrorInvalidValue:
101 return "cudaErrorInvalidValue";
102
103 case cudaErrorInvalidPitchValue:
104 return "cudaErrorInvalidPitchValue";
105
106 case cudaErrorInvalidSymbol:
107 return "cudaErrorInvalidSymbol";
108
109 case cudaErrorMapBufferObjectFailed:
110 return "cudaErrorMapBufferObjectFailed";
111
112 case cudaErrorUnmapBufferObjectFailed:
113 return "cudaErrorUnmapBufferObjectFailed";
114
115 case cudaErrorInvalidHostPointer:
116 return "cudaErrorInvalidHostPointer";
117
118 case cudaErrorInvalidDevicePointer:
119 return "cudaErrorInvalidDevicePointer";
120
121 case cudaErrorInvalidTexture:
122 return "cudaErrorInvalidTexture";
123
124 case cudaErrorInvalidTextureBinding:
125 return "cudaErrorInvalidTextureBinding";
126
127 case cudaErrorInvalidChannelDescriptor:
128 return "cudaErrorInvalidChannelDescriptor";
129
130 case cudaErrorInvalidMemcpyDirection:
131 return "cudaErrorInvalidMemcpyDirection";
132
133 case cudaErrorAddressOfConstant:
134 return "cudaErrorAddressOfConstant";
135
136 case cudaErrorTextureFetchFailed:
137 return "cudaErrorTextureFetchFailed";
138
139 case cudaErrorTextureNotBound:
140 return "cudaErrorTextureNotBound";
141
142 case cudaErrorSynchronizationError:
143 return "cudaErrorSynchronizationError";
144
145 case cudaErrorInvalidFilterSetting:
146 return "cudaErrorInvalidFilterSetting";
147
148 case cudaErrorInvalidNormSetting:
149 return "cudaErrorInvalidNormSetting";
150
151 case cudaErrorMixedDeviceExecution:
152 return "cudaErrorMixedDeviceExecution";
153
154 case cudaErrorCudartUnloading:
155 return "cudaErrorCudartUnloading";
156
157 case cudaErrorUnknown:
158 return "cudaErrorUnknown";
159
160 case cudaErrorNotYetImplemented:
161 return "cudaErrorNotYetImplemented";
162
163 case cudaErrorMemoryValueTooLarge:
164 return "cudaErrorMemoryValueTooLarge";
165
166 case cudaErrorInvalidResourceHandle:
167 return "cudaErrorInvalidResourceHandle";
168
169 case cudaErrorNotReady:
170 return "cudaErrorNotReady";
171
172 case cudaErrorInsufficientDriver:
173 return "cudaErrorInsufficientDriver";
174
175 case cudaErrorSetOnActiveProcess:
176 return "cudaErrorSetOnActiveProcess";
177
178 case cudaErrorInvalidSurface:
179 return "cudaErrorInvalidSurface";
180
181 case cudaErrorNoDevice:
182 return "cudaErrorNoDevice";
183
184 case cudaErrorECCUncorrectable:
185 return "cudaErrorECCUncorrectable";
186
187 case cudaErrorSharedObjectSymbolNotFound:
188 return "cudaErrorSharedObjectSymbolNotFound";
189
190 case cudaErrorSharedObjectInitFailed:
191 return "cudaErrorSharedObjectInitFailed";
192
193 case cudaErrorUnsupportedLimit:
194 return "cudaErrorUnsupportedLimit";
195
196 case cudaErrorDuplicateVariableName:
197 return "cudaErrorDuplicateVariableName";
198
199 case cudaErrorDuplicateTextureName:
200 return "cudaErrorDuplicateTextureName";
201
202 case cudaErrorDuplicateSurfaceName:
203 return "cudaErrorDuplicateSurfaceName";
204
205 case cudaErrorDevicesUnavailable:
206 return "cudaErrorDevicesUnavailable";
207
208 case cudaErrorInvalidKernelImage:
209 return "cudaErrorInvalidKernelImage";
210
211 case cudaErrorNoKernelImageForDevice:
212 return "cudaErrorNoKernelImageForDevice";
213
214 case cudaErrorIncompatibleDriverContext:
215 return "cudaErrorIncompatibleDriverContext";
216
217 case cudaErrorPeerAccessAlreadyEnabled:
218 return "cudaErrorPeerAccessAlreadyEnabled";
219
220 case cudaErrorPeerAccessNotEnabled:
221 return "cudaErrorPeerAccessNotEnabled";
222
223 case cudaErrorDeviceAlreadyInUse:
224 return "cudaErrorDeviceAlreadyInUse";
225
226 case cudaErrorProfilerDisabled:
227 return "cudaErrorProfilerDisabled";
228
229 case cudaErrorProfilerNotInitialized:
230 return "cudaErrorProfilerNotInitialized";
231
232 case cudaErrorProfilerAlreadyStarted:
233 return "cudaErrorProfilerAlreadyStarted";
234
235 case cudaErrorProfilerAlreadyStopped:
236 return "cudaErrorProfilerAlreadyStopped";
237
238 /* Since CUDA 4.0*/
239 case cudaErrorAssert:
240 return "cudaErrorAssert";
241
242 case cudaErrorTooManyPeers:
243 return "cudaErrorTooManyPeers";
244
245 case cudaErrorHostMemoryAlreadyRegistered:
246 return "cudaErrorHostMemoryAlreadyRegistered";
247
248 case cudaErrorHostMemoryNotRegistered:
249 return "cudaErrorHostMemoryNotRegistered";
250
251 /* Since CUDA 5.0 */
252 case cudaErrorOperatingSystem:
253 return "cudaErrorOperatingSystem";
254
255 case cudaErrorPeerAccessUnsupported:
256 return "cudaErrorPeerAccessUnsupported";
257
258 case cudaErrorLaunchMaxDepthExceeded:
259 return "cudaErrorLaunchMaxDepthExceeded";
260
261 case cudaErrorLaunchFileScopedTex:
262 return "cudaErrorLaunchFileScopedTex";
263
264 case cudaErrorLaunchFileScopedSurf:
265 return "cudaErrorLaunchFileScopedSurf";
266
267 case cudaErrorSyncDepthExceeded:
268 return "cudaErrorSyncDepthExceeded";
269
270 case cudaErrorLaunchPendingCountExceeded:
271 return "cudaErrorLaunchPendingCountExceeded";
272
273 case cudaErrorNotPermitted:
274 return "cudaErrorNotPermitted";
275
276 case cudaErrorNotSupported:
277 return "cudaErrorNotSupported";
278
279 /* Since CUDA 6.0 */
280 case cudaErrorHardwareStackError:
281 return "cudaErrorHardwareStackError";
282
283 case cudaErrorIllegalInstruction:
284 return "cudaErrorIllegalInstruction";
285
286 case cudaErrorMisalignedAddress:
287 return "cudaErrorMisalignedAddress";
288
289 case cudaErrorInvalidAddressSpace:
290 return "cudaErrorInvalidAddressSpace";
291
292 case cudaErrorInvalidPc:
293 return "cudaErrorInvalidPc";
294
295 case cudaErrorIllegalAddress:
296 return "cudaErrorIllegalAddress";
297
298 /* Since CUDA 6.5*/
299 case cudaErrorInvalidPtx:
300 return "cudaErrorInvalidPtx";
301
302 case cudaErrorInvalidGraphicsContext:
303 return "cudaErrorInvalidGraphicsContext";
304
305 case cudaErrorStartupFailure:
306 return "cudaErrorStartupFailure";
307
308 case cudaErrorApiFailureBase:
309 return "cudaErrorApiFailureBase";
310
311 /* Since CUDA 8.0*/
312 case cudaErrorNvlinkUncorrectable :
313 return "cudaErrorNvlinkUncorrectable";
314 default:
315 break;
316 }
317
318 return "<unknown>";
319 }
320 #endif
321
322 #ifdef CUBLAS_API_H_
323 // cuBLAS API errors
324 static const char *_cudaGetErrorEnum(cublasStatus_t error)
325 {
326 switch (error)
327 {
328 case CUBLAS_STATUS_SUCCESS:
329 return "CUBLAS_STATUS_SUCCESS";
330
331 case CUBLAS_STATUS_NOT_INITIALIZED:
332 return "CUBLAS_STATUS_NOT_INITIALIZED";
333
334 case CUBLAS_STATUS_ALLOC_FAILED:
335 return "CUBLAS_STATUS_ALLOC_FAILED";
336
337 case CUBLAS_STATUS_INVALID_VALUE:
338 return "CUBLAS_STATUS_INVALID_VALUE";
339
340 case CUBLAS_STATUS_ARCH_MISMATCH:
341 return "CUBLAS_STATUS_ARCH_MISMATCH";
342
343 case CUBLAS_STATUS_MAPPING_ERROR:
344 return "CUBLAS_STATUS_MAPPING_ERROR";
345
346 case CUBLAS_STATUS_EXECUTION_FAILED:
347 return "CUBLAS_STATUS_EXECUTION_FAILED";
348
349 case CUBLAS_STATUS_INTERNAL_ERROR:
350 return "CUBLAS_STATUS_INTERNAL_ERROR";
351
352 case CUBLAS_STATUS_NOT_SUPPORTED:
353 return "CUBLAS_STATUS_NOT_SUPPORTED";
354
355 case CUBLAS_STATUS_LICENSE_ERROR:
356 return "CUBLAS_STATUS_LICENSE_ERROR";
357 }
358
359 return "<unknown>";
360 }
361 #endif
362
363 #ifdef CUSOLVER_COMMON_H_
364 //cuSOLVER API errors
365 static const char *_cudaGetErrorEnum(cusolverStatus_t error)
366 {
367 switch(error)
368 {
369 case CUSOLVER_STATUS_SUCCESS:
370 return "CUSOLVER_STATUS_SUCCESS";
371 case CUSOLVER_STATUS_NOT_INITIALIZED:
372 return "CUSOLVER_STATUS_NOT_INITIALIZED";
373 case CUSOLVER_STATUS_ALLOC_FAILED:
374 return "CUSOLVER_STATUS_ALLOC_FAILED";
375 case CUSOLVER_STATUS_INVALID_VALUE:
376 return "CUSOLVER_STATUS_INVALID_VALUE";
377 case CUSOLVER_STATUS_ARCH_MISMATCH:
378 return "CUSOLVER_STATUS_ARCH_MISMATCH";
379 case CUSOLVER_STATUS_MAPPING_ERROR:
380 return "CUSOLVER_STATUS_MAPPING_ERROR";
381 case CUSOLVER_STATUS_EXECUTION_FAILED:
382 return "CUSOLVER_STATUS_EXECUTION_FAILED";
383 case CUSOLVER_STATUS_INTERNAL_ERROR:
384 return "CUSOLVER_STATUS_INTERNAL_ERROR";
385 case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
386 return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
387 case CUSOLVER_STATUS_NOT_SUPPORTED :
388 return "CUSOLVER_STATUS_NOT_SUPPORTED ";
389 case CUSOLVER_STATUS_ZERO_PIVOT:
390 return "CUSOLVER_STATUS_ZERO_PIVOT";
391 case CUSOLVER_STATUS_INVALID_LICENSE:
392 return "CUSOLVER_STATUS_INVALID_LICENSE";
393 }
394
395 return "<unknown>";
396
397 }
398 #endif
399
400 #ifdef CURAND_H_
401 //cuRAND API errors
402 static const char *_cudaGetErrorEnum(curandStatus_t error)
403 {
404 switch(error)
405 {
406 case CURAND_STATUS_SUCCESS: return "CURAND_STATUS_SUCCESS";
407 case CURAND_STATUS_VERSION_MISMATCH: return "CURAND_STATUS_VERSION_MISMATCH";
408 case CURAND_STATUS_NOT_INITIALIZED: return "CURAND_STATUS_NOT_INITIALIZED";
409 case CURAND_STATUS_ALLOCATION_FAILED: return "CURAND_STATUS_ALLOCATION_FAILED";
410 case CURAND_STATUS_TYPE_ERROR: return "CURAND_STATUS_TYPE_ERROR";
411 case CURAND_STATUS_OUT_OF_RANGE: return "CURAND_STATUS_OUT_OF_RANGE";
412 case CURAND_STATUS_LENGTH_NOT_MULTIPLE: return "CURAND_STATUS_LENGTH_NOT_MULTIPLE";
413 case CURAND_STATUS_DOUBLE_PRECISION_REQUIRED: return "CURAND_STATUS_DOUBLE_PRECISION_REQUIRED";
414 case CURAND_STATUS_LAUNCH_FAILURE: return "CURAND_STATUS_LAUNCH_FAILURE";
415 case CURAND_STATUS_PREEXISTING_FAILURE: return "CURAND_STATUS_PREEXISTING_FAILURE";
416 case CURAND_STATUS_INITIALIZATION_FAILED: return "CURAND_STATUS_INITIALIZATION_FAILED";
417 case CURAND_STATUS_ARCH_MISMATCH: return "CURAND_STATUS_ARCH_MISMATCH";
418 case CURAND_STATUS_INTERNAL_ERROR: return "CURAND_STATUS_INTERNAL_ERROR";
419 }
420
421 return "<unknown>";
422
423 }
424 #endif
425
426
427
428
429
430 #ifdef __DRIVER_TYPES_H__
431 #ifndef DEVICE_RESET
432 #define DEVICE_RESET cudaDeviceReset();
433 #endif
434 #else
435 #ifndef DEVICE_RESET
436 #define DEVICE_RESET
437 #endif
438 #endif
439
440 template< typename T >
441 void check(T result, char const *const func, const char *const file, int const line)
442 {
443 if (result)
444 {
445 fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n",
446 file, line, static_cast<unsigned int>(result), _cudaGetErrorEnum(result), func);
447 DEVICE_RESET
448 // Make sure we call CUDA Device Reset before exiting
449 exit(EXIT_FAILURE);
450 }
451 }
452
453 #ifdef __DRIVER_TYPES_H__
454 // This will output the proper CUDA error strings in the event that a CUDA host call returns an error
455 #define checkCudaErrors(val) check ( (val), #val, __FILE__, __LINE__ )
456
457 #endif
458
459 #endif // End of #if defined(UNI_GPU)
460
461#endif