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