xref: /libCEED/backends/cuda/ceed-cuda-common.h (revision b0d170e7bc2e5c930ee481a47eb73044935a48a4)
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/backend.h>
12 #include <ceed/jit-source/cuda/cuda-types.h>
13 #include <cublas_v2.h>
14 #include <cuda.h>
15 #include <cuda_runtime.h>
16 
17 #define QUOTE(...) #__VA_ARGS__
18 
19 #define CeedChk_Cu(ceed, x)                              \
20   do {                                                   \
21     CUresult cuda_result = (CUresult)x;                  \
22     if (cuda_result != CUDA_SUCCESS) {                   \
23       const char *msg;                                   \
24       cuGetErrorName(cuda_result, &msg);                 \
25       return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
26     }                                                    \
27   } while (0)
28 
29 #define CeedChk_Cublas(ceed, x)                            \
30   do {                                                     \
31     cublasStatus_t cublas_result = x;                      \
32     if (cublas_result != CUBLAS_STATUS_SUCCESS) {          \
33       const char *msg = cublasGetErrorName(cublas_result); \
34       return CeedError((ceed), CEED_ERROR_BACKEND, msg);   \
35     }                                                      \
36   } while (0)
37 
38 #define CeedCallCuda(ceed, ...) \
39   do {                          \
40     int ierr_q_ = __VA_ARGS__;  \
41     CeedChk_Cu(ceed, ierr_q_);  \
42   } while (0);
43 
44 #define CeedCallCublas(ceed, ...)  \
45   do {                             \
46     int ierr_q_ = __VA_ARGS__;     \
47     CeedChk_Cublas(ceed, ierr_q_); \
48   } while (0);
49 
50 #define CASE(name) \
51   case name:       \
52     return #name
53 // LCOV_EXCL_START
54 static const char *cublasGetErrorName(cublasStatus_t error) {
55   switch (error) {
56     CASE(CUBLAS_STATUS_SUCCESS);
57     CASE(CUBLAS_STATUS_NOT_INITIALIZED);
58     CASE(CUBLAS_STATUS_ALLOC_FAILED);
59     CASE(CUBLAS_STATUS_INVALID_VALUE);
60     CASE(CUBLAS_STATUS_ARCH_MISMATCH);
61     CASE(CUBLAS_STATUS_MAPPING_ERROR);
62     CASE(CUBLAS_STATUS_EXECUTION_FAILED);
63     CASE(CUBLAS_STATUS_INTERNAL_ERROR);
64     default:
65       return "CUBLAS_STATUS_UNKNOWN_ERROR";
66   }
67 }
68 // LCOV_EXCL_STOP
69 
70 typedef struct {
71   int                   device_id;
72   cublasHandle_t        cublas_handle;
73   struct cudaDeviceProp device_prop;
74 } Ceed_Cuda;
75 
76 CEED_INTERN int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, char **resource_root);
77 
78 CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource);
79 
80 CEED_INTERN int CeedDestroy_Cuda(Ceed ceed);
81 
82 #endif  // _ceed_cuda_common_h