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