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