Cytnx v0.9.5
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
69 #if defined(UNI_CUTENSOR)
70 #include <cutensor.h>
71 #endif
72
73 #ifdef __DRIVER_TYPES_H__
74static const char *_cudaGetErrorEnum(cudaError_t error) {
75 switch (error) {
76 case cudaSuccess:
77 return "cudaSuccess";
78
79 case cudaErrorMissingConfiguration:
80 return "cudaErrorMissingConfiguration";
81
82 case cudaErrorMemoryAllocation:
83 return "cudaErrorMemoryAllocation";
84
85 case cudaErrorInitializationError:
86 return "cudaErrorInitializationError";
87
88 case cudaErrorLaunchFailure:
89 return "cudaErrorLaunchFailure";
90
91 case cudaErrorPriorLaunchFailure:
92 return "cudaErrorPriorLaunchFailure";
93
94 case cudaErrorLaunchTimeout:
95 return "cudaErrorLaunchTimeout";
96
97 case cudaErrorLaunchOutOfResources:
98 return "cudaErrorLaunchOutOfResources";
99
100 case cudaErrorInvalidDeviceFunction:
101 return "cudaErrorInvalidDeviceFunction";
102
103 case cudaErrorInvalidConfiguration:
104 return "cudaErrorInvalidConfiguration";
105
106 case cudaErrorInvalidDevice:
107 return "cudaErrorInvalidDevice";
108
109 case cudaErrorInvalidValue:
110 return "cudaErrorInvalidValue";
111
112 case cudaErrorInvalidPitchValue:
113 return "cudaErrorInvalidPitchValue";
114
115 case cudaErrorInvalidSymbol:
116 return "cudaErrorInvalidSymbol";
117
118 case cudaErrorMapBufferObjectFailed:
119 return "cudaErrorMapBufferObjectFailed";
120
121 case cudaErrorUnmapBufferObjectFailed:
122 return "cudaErrorUnmapBufferObjectFailed";
123
124 case cudaErrorInvalidHostPointer:
125 return "cudaErrorInvalidHostPointer";
126
127 case cudaErrorInvalidDevicePointer:
128 return "cudaErrorInvalidDevicePointer";
129
130 case cudaErrorInvalidTexture:
131 return "cudaErrorInvalidTexture";
132
133 case cudaErrorInvalidTextureBinding:
134 return "cudaErrorInvalidTextureBinding";
135
136 case cudaErrorInvalidChannelDescriptor:
137 return "cudaErrorInvalidChannelDescriptor";
138
139 case cudaErrorInvalidMemcpyDirection:
140 return "cudaErrorInvalidMemcpyDirection";
141
142 case cudaErrorAddressOfConstant:
143 return "cudaErrorAddressOfConstant";
144
145 case cudaErrorTextureFetchFailed:
146 return "cudaErrorTextureFetchFailed";
147
148 case cudaErrorTextureNotBound:
149 return "cudaErrorTextureNotBound";
150
151 case cudaErrorSynchronizationError:
152 return "cudaErrorSynchronizationError";
153
154 case cudaErrorInvalidFilterSetting:
155 return "cudaErrorInvalidFilterSetting";
156
157 case cudaErrorInvalidNormSetting:
158 return "cudaErrorInvalidNormSetting";
159
160 case cudaErrorMixedDeviceExecution:
161 return "cudaErrorMixedDeviceExecution";
162
163 case cudaErrorCudartUnloading:
164 return "cudaErrorCudartUnloading";
165
166 case cudaErrorUnknown:
167 return "cudaErrorUnknown";
168
169 case cudaErrorNotYetImplemented:
170 return "cudaErrorNotYetImplemented";
171
172 case cudaErrorMemoryValueTooLarge:
173 return "cudaErrorMemoryValueTooLarge";
174
175 case cudaErrorInvalidResourceHandle:
176 return "cudaErrorInvalidResourceHandle";
177
178 case cudaErrorNotReady:
179 return "cudaErrorNotReady";
180
181 case cudaErrorInsufficientDriver:
182 return "cudaErrorInsufficientDriver";
183
184 case cudaErrorSetOnActiveProcess:
185 return "cudaErrorSetOnActiveProcess";
186
187 case cudaErrorInvalidSurface:
188 return "cudaErrorInvalidSurface";
189
190 case cudaErrorNoDevice:
191 return "cudaErrorNoDevice";
192
193 case cudaErrorECCUncorrectable:
194 return "cudaErrorECCUncorrectable";
195
196 case cudaErrorSharedObjectSymbolNotFound:
197 return "cudaErrorSharedObjectSymbolNotFound";
198
199 case cudaErrorSharedObjectInitFailed:
200 return "cudaErrorSharedObjectInitFailed";
201
202 case cudaErrorUnsupportedLimit:
203 return "cudaErrorUnsupportedLimit";
204
205 case cudaErrorDuplicateVariableName:
206 return "cudaErrorDuplicateVariableName";
207
208 case cudaErrorDuplicateTextureName:
209 return "cudaErrorDuplicateTextureName";
210
211 case cudaErrorDuplicateSurfaceName:
212 return "cudaErrorDuplicateSurfaceName";
213
214 case cudaErrorDevicesUnavailable:
215 return "cudaErrorDevicesUnavailable";
216
217 case cudaErrorInvalidKernelImage:
218 return "cudaErrorInvalidKernelImage";
219
220 case cudaErrorNoKernelImageForDevice:
221 return "cudaErrorNoKernelImageForDevice";
222
223 case cudaErrorIncompatibleDriverContext:
224 return "cudaErrorIncompatibleDriverContext";
225
226 case cudaErrorPeerAccessAlreadyEnabled:
227 return "cudaErrorPeerAccessAlreadyEnabled";
228
229 case cudaErrorPeerAccessNotEnabled:
230 return "cudaErrorPeerAccessNotEnabled";
231
232 case cudaErrorDeviceAlreadyInUse:
233 return "cudaErrorDeviceAlreadyInUse";
234
235 case cudaErrorProfilerDisabled:
236 return "cudaErrorProfilerDisabled";
237
238 case cudaErrorProfilerNotInitialized:
239 return "cudaErrorProfilerNotInitialized";
240
241 case cudaErrorProfilerAlreadyStarted:
242 return "cudaErrorProfilerAlreadyStarted";
243
244 case cudaErrorProfilerAlreadyStopped:
245 return "cudaErrorProfilerAlreadyStopped";
246
247 /* Since CUDA 4.0*/
248 case cudaErrorAssert:
249 return "cudaErrorAssert";
250
251 case cudaErrorTooManyPeers:
252 return "cudaErrorTooManyPeers";
253
254 case cudaErrorHostMemoryAlreadyRegistered:
255 return "cudaErrorHostMemoryAlreadyRegistered";
256
257 case cudaErrorHostMemoryNotRegistered:
258 return "cudaErrorHostMemoryNotRegistered";
259
260 /* Since CUDA 5.0 */
261 case cudaErrorOperatingSystem:
262 return "cudaErrorOperatingSystem";
263
264 case cudaErrorPeerAccessUnsupported:
265 return "cudaErrorPeerAccessUnsupported";
266
267 case cudaErrorLaunchMaxDepthExceeded:
268 return "cudaErrorLaunchMaxDepthExceeded";
269
270 case cudaErrorLaunchFileScopedTex:
271 return "cudaErrorLaunchFileScopedTex";
272
273 case cudaErrorLaunchFileScopedSurf:
274 return "cudaErrorLaunchFileScopedSurf";
275
276 case cudaErrorSyncDepthExceeded:
277 return "cudaErrorSyncDepthExceeded";
278
279 case cudaErrorLaunchPendingCountExceeded:
280 return "cudaErrorLaunchPendingCountExceeded";
281
282 case cudaErrorNotPermitted:
283 return "cudaErrorNotPermitted";
284
285 case cudaErrorNotSupported:
286 return "cudaErrorNotSupported";
287
288 /* Since CUDA 6.0 */
289 case cudaErrorHardwareStackError:
290 return "cudaErrorHardwareStackError";
291
292 case cudaErrorIllegalInstruction:
293 return "cudaErrorIllegalInstruction";
294
295 case cudaErrorMisalignedAddress:
296 return "cudaErrorMisalignedAddress";
297
298 case cudaErrorInvalidAddressSpace:
299 return "cudaErrorInvalidAddressSpace";
300
301 case cudaErrorInvalidPc:
302 return "cudaErrorInvalidPc";
303
304 case cudaErrorIllegalAddress:
305 return "cudaErrorIllegalAddress";
306
307 /* Since CUDA 6.5*/
308 case cudaErrorInvalidPtx:
309 return "cudaErrorInvalidPtx";
310
311 case cudaErrorInvalidGraphicsContext:
312 return "cudaErrorInvalidGraphicsContext";
313
314 case cudaErrorStartupFailure:
315 return "cudaErrorStartupFailure";
316
317 case cudaErrorApiFailureBase:
318 return "cudaErrorApiFailureBase";
319
320 /* Since CUDA 8.0*/
321 case cudaErrorNvlinkUncorrectable:
322 return "cudaErrorNvlinkUncorrectable";
323 default:
324 break;
325 }
326
327 return "<unknown>";
328}
329 #endif
330
331 #ifdef CUBLAS_API_H_
332// cuBLAS API errors
333static const char *_cudaGetErrorEnum(cublasStatus_t error) {
334 switch (error) {
335 case CUBLAS_STATUS_SUCCESS:
336 return "CUBLAS_STATUS_SUCCESS";
337
338 case CUBLAS_STATUS_NOT_INITIALIZED:
339 return "CUBLAS_STATUS_NOT_INITIALIZED";
340
341 case CUBLAS_STATUS_ALLOC_FAILED:
342 return "CUBLAS_STATUS_ALLOC_FAILED";
343
344 case CUBLAS_STATUS_INVALID_VALUE:
345 return "CUBLAS_STATUS_INVALID_VALUE";
346
347 case CUBLAS_STATUS_ARCH_MISMATCH:
348 return "CUBLAS_STATUS_ARCH_MISMATCH";
349
350 case CUBLAS_STATUS_MAPPING_ERROR:
351 return "CUBLAS_STATUS_MAPPING_ERROR";
352
353 case CUBLAS_STATUS_EXECUTION_FAILED:
354 return "CUBLAS_STATUS_EXECUTION_FAILED";
355
356 case CUBLAS_STATUS_INTERNAL_ERROR:
357 return "CUBLAS_STATUS_INTERNAL_ERROR";
358
359 case CUBLAS_STATUS_NOT_SUPPORTED:
360 return "CUBLAS_STATUS_NOT_SUPPORTED";
361
362 case CUBLAS_STATUS_LICENSE_ERROR:
363 return "CUBLAS_STATUS_LICENSE_ERROR";
364 }
365
366 return "<unknown>";
367}
368 #endif
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 #ifdef __DRIVER_TYPES_H__
447 #ifndef DEVICE_RESET
448 #define DEVICE_RESET cudaDeviceReset();
449 #endif
450 #else
451 #ifndef DEVICE_RESET
452 #define DEVICE_RESET
453 #endif
454 #endif
455
456template <typename T>
457void check(T result, char const *const func, const char *const file, int const line) {
458 if (result) {
459 fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", file, line,
460 static_cast<unsigned int>(result), _cudaGetErrorEnum(result), func);
461 DEVICE_RESET
462 // Make sure we call CUDA Device Reset before exiting
463 exit(EXIT_FAILURE);
464 }
465}
466
467 #ifdef __DRIVER_TYPES_H__
468 // This will output the proper CUDA error strings in the event that a CUDA host call returns an
469 // error
470 #define checkCudaErrors(val) check((val), #val, __FILE__, __LINE__)
471
472 #endif
473
474#endif // End of #if defined(UNI_GPU)
475
476#endif