xref: /libCEED/backends/cuda/ceed-cuda-common.h (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1 // Copyright (c) 2017-2024, 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 CEED_INTERN int CeedSetDeviceBoolArray_Cuda(Ceed ceed, const bool *source_array, CeedCopyMode copy_mode, CeedSize num_values,
80                                             const bool **target_array_owned, const bool **target_array_borrowed, const bool **target_array);
81 CEED_INTERN int CeedSetDeviceCeedInt8Array_Cuda(Ceed ceed, const CeedInt8 *source_array, CeedCopyMode copy_mode, CeedSize num_values,
82                                                 const CeedInt8 **target_array_owned, const CeedInt8 **target_array_borrowed,
83                                                 const CeedInt8 **target_array);
84 CEED_INTERN int CeedSetDeviceCeedIntArray_Cuda(Ceed ceed, const CeedInt *source_array, CeedCopyMode copy_mode, CeedSize num_values,
85                                                const CeedInt **target_array_owned, const CeedInt **target_array_borrowed,
86                                                const CeedInt **target_array);
87 CEED_INTERN int CeedSetDeviceCeedScalarArray_Cuda(Ceed ceed, const CeedScalar *source_array, CeedCopyMode copy_mode, CeedSize num_values,
88                                                   const CeedScalar **target_array_owned, const CeedScalar **target_array_borrowed,
89                                                   const CeedScalar **target_array);
90 
91 #endif  // CEED_CUDA_COMMON_H
92