xref: /libCEED/backends/cuda/ceed-cuda-common.h (revision 356036fa84f714fa73ef64c9a80ce2028dde816f)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #ifndef _ceed_cuda_common_h
9 #define _ceed_cuda_common_h
10 
11 #include <ceed.h>
12 #include <ceed/backend.h>
13 #include <cublas_v2.h>
14 #include <cuda.h>
15 
16 #define QUOTE(...) #__VA_ARGS__
17 
18 #define CeedChk_Cu(ceed, x)                              \
19   do {                                                   \
20     CUresult cuda_result = (CUresult)x;                  \
21     if (cuda_result != CUDA_SUCCESS) {                   \
22       const char *msg;                                   \
23       cuGetErrorName(cuda_result, &msg);                 \
24       return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
25     }                                                    \
26   } while (0)
27 
28 #define CeedChk_Cublas(ceed, x)                            \
29   do {                                                     \
30     cublasStatus_t cublas_result = x;                      \
31     if (cublas_result != CUBLAS_STATUS_SUCCESS) {          \
32       const char *msg = cublasGetErrorName(cublas_result); \
33       return CeedError((ceed), CEED_ERROR_BACKEND, msg);   \
34     }                                                      \
35   } while (0)
36 
37 #define CeedCallCuda(ceed, ...) \
38   do {                          \
39     int ierr_q_ = __VA_ARGS__;  \
40     CeedChk_Cu(ceed, ierr_q_);  \
41   } while (0)
42 
43 #define CeedCallCublas(ceed, ...)  \
44   do {                             \
45     int ierr_q_ = __VA_ARGS__;     \
46     CeedChk_Cublas(ceed, ierr_q_); \
47   } while (0)
48 
49 #define CASE(name) \
50   case name:       \
51     return #name
52 // LCOV_EXCL_START
53 static const char *cublasGetErrorName(cublasStatus_t error) {
54   switch (error) {
55     CASE(CUBLAS_STATUS_SUCCESS);
56     CASE(CUBLAS_STATUS_NOT_INITIALIZED);
57     CASE(CUBLAS_STATUS_ALLOC_FAILED);
58     CASE(CUBLAS_STATUS_INVALID_VALUE);
59     CASE(CUBLAS_STATUS_ARCH_MISMATCH);
60     CASE(CUBLAS_STATUS_MAPPING_ERROR);
61     CASE(CUBLAS_STATUS_EXECUTION_FAILED);
62     CASE(CUBLAS_STATUS_INTERNAL_ERROR);
63     default:
64       return "CUBLAS_STATUS_UNKNOWN_ERROR";
65   }
66 }
67 // LCOV_EXCL_STOP
68 
69 typedef struct {
70   int                   device_id;
71   cublasHandle_t        cublas_handle;
72   struct cudaDeviceProp device_prop;
73 } Ceed_Cuda;
74 
75 CEED_INTERN int CeedInit_Cuda(Ceed ceed, const char *resource);
76 
77 CEED_INTERN int CeedDestroy_Cuda(Ceed ceed);
78 
79 #endif  // _ceed_cuda_common_h
80