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