ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
device_check.h
Go to the documentation of this file.
1#ifndef DEVICE_CHECK_H
2#define DEVICE_CHECK_H
3
4#include <cstdlib>
5#include <cstdio>
6
7#ifdef __CUDA
8#include "cublas_v2.h"
9#include "cufft.h"
10#include "cusolverDn.h"
11#include <cuda.h>
12
13static const char* _cublasGetErrorString(cublasStatus_t error)
14{
15 switch (error)
16 {
17 case CUBLAS_STATUS_SUCCESS:
18 return "CUBLAS_STATUS_SUCCESS";
19 case CUBLAS_STATUS_NOT_INITIALIZED:
20 return "CUBLAS_STATUS_NOT_INITIALIZED";
21 case CUBLAS_STATUS_ALLOC_FAILED:
22 return "CUBLAS_STATUS_ALLOC_FAILED";
23 case CUBLAS_STATUS_INVALID_VALUE:
24 return "CUBLAS_STATUS_INVALID_VALUE";
25 case CUBLAS_STATUS_ARCH_MISMATCH:
26 return "CUBLAS_STATUS_ARCH_MISMATCH";
27 case CUBLAS_STATUS_MAPPING_ERROR:
28 return "CUBLAS_STATUS_MAPPING_ERROR";
29 case CUBLAS_STATUS_EXECUTION_FAILED:
30 return "CUBLAS_STATUS_EXECUTION_FAILED";
31 case CUBLAS_STATUS_INTERNAL_ERROR:
32 return "CUBLAS_STATUS_INTERNAL_ERROR";
33 case CUBLAS_STATUS_NOT_SUPPORTED:
34 return "CUBLAS_STATUS_NOT_SUPPORTED";
35 case CUBLAS_STATUS_LICENSE_ERROR:
36 return "CUBLAS_STATUS_LICENSE_ERROR";
37 default:
38 return "<unknown>";
39 }
40}
41
42static const char* _cusolverGetErrorString(cusolverStatus_t error)
43{
44 switch (error)
45 {
46 case CUSOLVER_STATUS_SUCCESS:
47 return "CUSOLVER_STATUS_SUCCESS";
48 case CUSOLVER_STATUS_NOT_INITIALIZED:
49 return "CUSOLVER_STATUS_NOT_INITIALIZED";
50 case CUSOLVER_STATUS_ALLOC_FAILED:
51 return "CUSOLVER_STATUS_ALLOC_FAILED";
52 case CUSOLVER_STATUS_INVALID_VALUE:
53 return "CUSOLVER_STATUS_INVALID_VALUE";
54 case CUSOLVER_STATUS_ARCH_MISMATCH:
55 return "CUSOLVER_STATUS_ARCH_MISMATCH";
56 case CUSOLVER_STATUS_MAPPING_ERROR:
57 return "CUSOLVER_STATUS_MAPPING_ERROR";
58 case CUSOLVER_STATUS_EXECUTION_FAILED:
59 return "CUSOLVER_STATUS_EXECUTION_FAILED";
60 case CUSOLVER_STATUS_INTERNAL_ERROR:
61 return "CUSOLVER_STATUS_INTERNAL_ERROR";
62 case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
63 return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
64 case CUSOLVER_STATUS_NOT_SUPPORTED:
65 return "CUSOLVER_STATUS_NOT_SUPPORTED";
66 case CUSOLVER_STATUS_ZERO_PIVOT:
67 return "CUSOLVER_STATUS_ZERO_PIVOT";
68 case CUSOLVER_STATUS_INVALID_LICENSE:
69 return "CUSOLVER_STATUS_INVALID_LICENSE";
70#if CUDA_VERSION >= 11000
71 case CUSOLVER_STATUS_IRS_PARAMS_NOT_INITIALIZED:
72 return "CUSOLVER_STATUS_IRS_PARAMS_NOT_INITIALIZED";
73 case CUSOLVER_STATUS_IRS_PARAMS_INVALID:
74 return "CUSOLVER_STATUS_IRS_PARAMS_INVALID";
75 case CUSOLVER_STATUS_IRS_PARAMS_INVALID_PREC:
76 return "CUSOLVER_STATUS_IRS_PARAMS_INVALID_PREC";
77 case CUSOLVER_STATUS_IRS_PARAMS_INVALID_REFINE:
78 return "CUSOLVER_STATUS_IRS_PARAMS_INVALID_REFINE";
79 case CUSOLVER_STATUS_IRS_PARAMS_INVALID_MAXITER:
80 return "CUSOLVER_STATUS_IRS_PARAMS_INVALID_MAXITER";
81 case CUSOLVER_STATUS_IRS_INTERNAL_ERROR:
82 return "CUSOLVER_STATUS_IRS_INTERNAL_ERROR";
83 case CUSOLVER_STATUS_IRS_NOT_SUPPORTED:
84 return "CUSOLVER_STATUS_IRS_NOT_SUPPORTED";
85 case CUSOLVER_STATUS_IRS_OUT_OF_RANGE:
86 return "CUSOLVER_STATUS_IRS_OUT_OF_RANGE";
87 case CUSOLVER_STATUS_IRS_NRHS_NOT_SUPPORTED_FOR_REFINE_GMRES:
88 return "CUSOLVER_STATUS_IRS_NRHS_NOT_SUPPORTED_FOR_REFINE_GMRES";
89 case CUSOLVER_STATUS_IRS_INFOS_NOT_INITIALIZED:
90 return "CUSOLVER_STATUS_IRS_INFOS_NOT_INITIALIZED";
91 case CUSOLVER_STATUS_IRS_INFOS_NOT_DESTROYED:
92 return "CUSOLVER_STATUS_IRS_INFOS_NOT_DESTROYED";
93 case CUSOLVER_STATUS_IRS_MATRIX_SINGULAR:
94 return "CUSOLVER_STATUS_IRS_MATRIX_SINGULAR";
95 case CUSOLVER_STATUS_INVALID_WORKSPACE:
96 return "CUSOLVER_STATUS_INVALID_WORKSPACE";
97#endif
98 default:
99 return "<unknown>";
100 }
101}
102
103static const char* _cufftGetErrorString(cufftResult_t error)
104{
105 switch (error)
106 {
107 case CUFFT_SUCCESS:
108 return "CUFFT_SUCCESS";
109 case CUFFT_INVALID_PLAN:
110 return "CUFFT_INVALID_PLAN";
111 case CUFFT_ALLOC_FAILED:
112 return "CUFFT_ALLOC_FAILED";
113 case CUFFT_INVALID_TYPE:
114 return "CUFFT_INVALID_TYPE";
115 case CUFFT_INVALID_VALUE:
116 return "CUFFT_INVALID_VALUE";
117 case CUFFT_INTERNAL_ERROR:
118 return "CUFFT_INTERNAL_ERROR";
119 case CUFFT_EXEC_FAILED:
120 return "CUFFT_EXEC_FAILED";
121 case CUFFT_SETUP_FAILED:
122 return "CUFFT_SETUP_FAILED";
123 case CUFFT_INVALID_SIZE:
124 return "CUFFT_INVALID_SIZE";
125 case CUFFT_UNALIGNED_DATA:
126 return "CUFFT_UNALIGNED_DATA";
127 case CUFFT_INVALID_DEVICE:
128 return "CUFFT_INVALID_DEVICE";
129 case CUFFT_NO_WORKSPACE:
130 return "CUFFT_NO_WORKSPACE";
131 case CUFFT_NOT_IMPLEMENTED:
132 return "CUFFT_NOT_IMPLEMENTED";
133 case CUFFT_NOT_SUPPORTED:
134 return "CUFFT_NOT_SUPPORTED";
135#if defined(CUDA_VERSION) && CUDA_VERSION < 13000
136 case CUFFT_INCOMPLETE_PARAMETER_LIST:
137 return "CUFFT_INCOMPLETE_PARAMETER_LIST";
138 case CUFFT_PARSE_ERROR:
139 return "CUFFT_PARSE_ERROR";
140 case CUFFT_LICENSE_ERROR:
141 return "CUFFT_LICENSE_ERROR";
142#endif
143 default:
144 return "<unknown>";
145 }
146}
147
148#define CHECK_CUDA(func) \
149 do \
150 { \
151 cudaError_t status = (func); \
152 if (status != cudaSuccess) \
153 { \
154 fprintf(stderr, "In File %s : CUDA API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
155 cudaGetErrorString(status), status); \
156 exit(EXIT_FAILURE); \
157 } \
158 } while (0)
159
160#define CHECK_CUBLAS(func) \
161 do \
162 { \
163 cublasStatus_t status = (func); \
164 if (status != CUBLAS_STATUS_SUCCESS) \
165 { \
166 fprintf(stderr, "In File %s : CUBLAS API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
167 _cublasGetErrorString(status), status); \
168 exit(EXIT_FAILURE); \
169 } \
170 } while (0)
171
172#define CHECK_CUSOLVER(func) \
173 do \
174 { \
175 cusolverStatus_t status = (func); \
176 if (status != CUSOLVER_STATUS_SUCCESS) \
177 { \
178 fprintf(stderr, "In File %s : CUSOLVER API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
179 _cusolverGetErrorString(status), status); \
180 exit(EXIT_FAILURE); \
181 } \
182 } while (0)
183
184#define CHECK_CUFFT(func) \
185 do \
186 { \
187 cufftResult_t status = (func); \
188 if (status != CUFFT_SUCCESS) \
189 { \
190 fprintf(stderr, "In File %s : CUFFT API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
191 _cufftGetErrorString(status), status); \
192 exit(EXIT_FAILURE); \
193 } \
194 } while (0)
195
196#define CHECK_LAST_CUDA_ERROR(msg) \
197 do \
198 { \
199 cudaError_t status = cudaGetLastError(); \
200 if (status != cudaSuccess) \
201 { \
202 fprintf(stderr, "%s(%d) : CUDA error : %s : (%d) %s.\n", __FILE__, __LINE__, msg, \
203 static_cast<int>(status), cudaGetErrorString(status)); \
204 exit(EXIT_FAILURE); \
205 } \
206 } while (0)
207
208#ifdef __DEBUG
209#define CHECK_CUDA_SYNC() \
210 do \
211 { \
212 cudaError_t status = cudaDeviceSynchronize(); \
213 if (status != cudaSuccess) \
214 { \
215 fprintf(stderr, "In File %s : CUDA sync failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
216 cudaGetErrorString(status), status); \
217 exit(EXIT_FAILURE); \
218 } \
219 } while (0)
220#else
221#define CHECK_CUDA_SYNC() do {} while (0)
222#endif
223
224// NCCL check macro: shared by cuSOLVER MP (non-CAL path) and parallel device
225#if (defined(__CUSOLVERMP) && !defined(__USE_CAL)) || defined(__NCCL_PARALLEL_DEVICE)
226#include <nccl.h>
227
228#define CHECK_NCCL(func) \
229 do \
230 { \
231 ncclResult_t status = (func); \
232 if (status != ncclSuccess) \
233 { \
234 fprintf(stderr, "In File %s : NCCL API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
235 ncclGetErrorString(status), status); \
236 exit(EXIT_FAILURE); \
237 } \
238 } while (0)
239#endif
240
241// cuSOLVER MP support
242#ifdef __CUSOLVERMP
243#include <cusolverMp.h>
244
245#ifdef __USE_CAL
246#include <cal.h>
247
248static const char* _calGetErrorString(calError_t error)
249{
250 switch (error)
251 {
252 case CAL_OK:
253 return "CAL_OK";
254 case CAL_ERROR:
255 return "CAL_ERROR";
256 case CAL_ERROR_INVALID_PARAMETER:
257 return "CAL_ERROR_INVALID_PARAMETER";
258 case CAL_ERROR_INTERNAL:
259 return "CAL_ERROR_INTERNAL";
260 case CAL_ERROR_CUDA:
261 return "CAL_ERROR_CUDA";
262 case CAL_ERROR_UCC:
263 return "CAL_ERROR_UCC";
264 case CAL_ERROR_NOT_SUPPORTED:
265 return "CAL_ERROR_NOT_SUPPORTED";
266 case CAL_ERROR_INPROGRESS:
267 return "CAL_ERROR_INPROGRESS";
268 default:
269 return "<unknown>";
270 }
271}
272
273#define CHECK_CAL(func) \
274 do \
275 { \
276 calError_t status = (func); \
277 if (status != CAL_OK) \
278 { \
279 fprintf(stderr, "In File %s : CAL API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
280 _calGetErrorString(status), status); \
281 exit(EXIT_FAILURE); \
282 } \
283 } while (0)
284#endif // __USE_CAL
285
286#endif // __CUSOLVERMP
287
288#endif // __CUDA
289
290#ifdef __ROCM
291#include <hip/hip_runtime.h>
292#include <hipblas/hipblas.h>
293#include <hipfft/hipfft.h>
294#include <hipsolver/hipsolver.h>
295
296static const char* _hipblasGetErrorString(hipblasStatus_t error)
297{
298 switch (error)
299 {
300 case HIPBLAS_STATUS_SUCCESS:
301 return "HIPBLAS_STATUS_SUCCESS";
302 case HIPBLAS_STATUS_NOT_INITIALIZED:
303 return "HIPBLAS_STATUS_NOT_INITIALIZED";
304 case HIPBLAS_STATUS_ALLOC_FAILED:
305 return "HIPBLAS_STATUS_ALLOC_FAILED";
306 case HIPBLAS_STATUS_INVALID_VALUE:
307 return "HIPBLAS_STATUS_INVALID_VALUE";
308 case HIPBLAS_STATUS_ARCH_MISMATCH:
309 return "HIPBLAS_STATUS_ARCH_MISMATCH";
310 case HIPBLAS_STATUS_MAPPING_ERROR:
311 return "HIPBLAS_STATUS_MAPPING_ERROR";
312 case HIPBLAS_STATUS_EXECUTION_FAILED:
313 return "HIPBLAS_STATUS_EXECUTION_FAILED";
314 case HIPBLAS_STATUS_INTERNAL_ERROR:
315 return "HIPBLAS_STATUS_INTERNAL_ERROR";
316 case HIPBLAS_STATUS_NOT_SUPPORTED:
317 return "HIPBLAS_STATUS_NOT_SUPPORTED";
318 case HIPBLAS_STATUS_HANDLE_IS_NULLPTR:
319 return "HIPBLAS_STATUS_HANDLE_IS_NULLPTR";
320 default:
321 return "<unknown>";
322 }
323}
324
325static const char* _hipfftGetErrorString(hipfftResult_t error)
326{
327 switch (error)
328 {
329 case HIPFFT_SUCCESS:
330 return "HIPFFT_SUCCESS";
331 case HIPFFT_INVALID_PLAN:
332 return "HIPFFT_INVALID_PLAN";
333 case HIPFFT_ALLOC_FAILED:
334 return "HIPFFT_ALLOC_FAILED";
335 case HIPFFT_INVALID_TYPE:
336 return "HIPFFT_INVALID_TYPE";
337 case HIPFFT_INVALID_VALUE:
338 return "HIPFFT_INVALID_VALUE";
339 case HIPFFT_INTERNAL_ERROR:
340 return "HIPFFT_INTERNAL_ERROR";
341 case HIPFFT_EXEC_FAILED:
342 return "HIPFFT_EXEC_FAILED";
343 case HIPFFT_SETUP_FAILED:
344 return "HIPFFT_SETUP_FAILED";
345 case HIPFFT_INVALID_SIZE:
346 return "HIPFFT_INVALID_SIZE";
347 case HIPFFT_UNALIGNED_DATA:
348 return "HIPFFT_UNALIGNED_DATA";
349 case HIPFFT_INCOMPLETE_PARAMETER_LIST:
350 return "HIPFFT_INCOMPLETE_PARAMETER_LIST";
351 case HIPFFT_INVALID_DEVICE:
352 return "HIPFFT_INVALID_DEVICE";
353 case HIPFFT_PARSE_ERROR:
354 return "HIPFFT_PARSE_ERROR";
355 case HIPFFT_NO_WORKSPACE:
356 return "HIPFFT_NO_WORKSPACE";
357 case HIPFFT_NOT_IMPLEMENTED:
358 return "HIPFFT_NOT_IMPLEMENTED";
359 case HIPFFT_NOT_SUPPORTED:
360 return "HIPFFT_NOT_SUPPORTED";
361 default:
362 return "<unknown>";
363 }
364}
365
366static const char* _hipsolverGetErrorString(hipsolverStatus_t error)
367{
368 switch (error)
369 {
370 case HIPSOLVER_STATUS_SUCCESS:
371 return "HIPSOLVER_STATUS_SUCCESS";
372 case HIPSOLVER_STATUS_NOT_INITIALIZED:
373 return "HIPSOLVER_STATUS_NOT_INITIALIZED";
374 case HIPSOLVER_STATUS_ALLOC_FAILED:
375 return "HIPSOLVER_STATUS_ALLOC_FAILED";
376 case HIPSOLVER_STATUS_INVALID_VALUE:
377 return "HIPSOLVER_STATUS_INVALID_VALUE";
378 case HIPSOLVER_STATUS_MAPPING_ERROR:
379 return "HIPSOLVER_STATUS_MAPPING_ERROR";
380 case HIPSOLVER_STATUS_EXECUTION_FAILED:
381 return "HIPSOLVER_STATUS_EXECUTION_FAILED";
382 case HIPSOLVER_STATUS_INTERNAL_ERROR:
383 return "HIPSOLVER_STATUS_INTERNAL_ERROR";
384 case HIPSOLVER_STATUS_NOT_SUPPORTED:
385 return "HIPSOLVER_STATUS_NOT_SUPPORTED";
386 case HIPSOLVER_STATUS_ARCH_MISMATCH:
387 return "HIPSOLVER_STATUS_ARCH_MISMATCH";
388 case HIPSOLVER_STATUS_HANDLE_IS_NULLPTR:
389 return "HIPSOLVER_STATUS_HANDLE_IS_NULLPTR";
390 case HIPSOLVER_STATUS_INVALID_ENUM:
391 return "HIPSOLVER_STATUS_INVALID_ENUM";
392 case HIPSOLVER_STATUS_UNKNOWN:
393 return "HIPSOLVER_STATUS_UNKNOWN";
394 default:
395 return "<unknown>";
396 }
397}
398
399#define CHECK_CUDA(func) \
400 do \
401 { \
402 hipError_t status = (func); \
403 if (status != hipSuccess) \
404 { \
405 fprintf(stderr, "In File %s : HIP API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
406 hipGetErrorString(status), status); \
407 exit(EXIT_FAILURE); \
408 } \
409 } while (0)
410
411#define CHECK_CUBLAS(func) \
412 do \
413 { \
414 hipblasStatus_t status = (func); \
415 if (status != HIPBLAS_STATUS_SUCCESS) \
416 { \
417 fprintf(stderr, "In File %s : HIPBLAS API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
418 _hipblasGetErrorString(status), status); \
419 exit(EXIT_FAILURE); \
420 } \
421 } while (0)
422
423#define CHECK_CUSOLVER(func) \
424 do \
425 { \
426 hipsolverStatus_t status = (func); \
427 if (status != HIPSOLVER_STATUS_SUCCESS) \
428 { \
429 fprintf(stderr, "In File %s : HIPSOLVER API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
430 _hipsolverGetErrorString(status), status); \
431 exit(EXIT_FAILURE); \
432 } \
433 } while (0)
434
435#define CHECK_CUFFT(func) \
436 do \
437 { \
438 hipfftResult_t status = (func); \
439 if (status != HIPFFT_SUCCESS) \
440 { \
441 fprintf(stderr, "In File %s : HIPFFT API failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
442 _hipfftGetErrorString(status), status); \
443 exit(EXIT_FAILURE); \
444 } \
445 } while (0)
446
447#define CHECK_LAST_CUDA_ERROR(msg) \
448 do \
449 { \
450 hipError_t status = hipGetLastError(); \
451 if (status != hipSuccess) \
452 { \
453 fprintf(stderr, "%s(%d) : HIP error : %s : (%d) %s.\n", __FILE__, __LINE__, msg, \
454 static_cast<int>(status), hipGetErrorString(status)); \
455 exit(EXIT_FAILURE); \
456 } \
457 } while (0)
458
459#ifdef __DEBUG
460#define CHECK_CUDA_SYNC() \
461 do \
462 { \
463 hipError_t status = hipDeviceSynchronize(); \
464 if (status != hipSuccess) \
465 { \
466 fprintf(stderr, "In File %s : HIP sync failed at line %d with error: %s (%d)\n", __FILE__, __LINE__, \
467 hipGetErrorString(status), status); \
468 exit(EXIT_FAILURE); \
469 } \
470 } while (0)
471#else
472#define CHECK_CUDA_SYNC() do {} while (0)
473#endif
474
475#endif // __ROCM
476
477#endif // DEVICE_CHECK_H