xref: /libCEED/backends/cuda/ceed-cuda-common.h (revision fa619ecc52f58ebd3ff3ef012ebe7a24b3c56483)
1 // Copyright (c) 2017-2025, 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 #pragma once
8 
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <cublas_v2.h>
12 #include <cuda.h>
13 
14 #define QUOTE(...) #__VA_ARGS__
15 
16 #define CeedChk_Cu(ceed, x)                              \
17   do {                                                   \
18     CUresult cuda_result = (CUresult)x;                  \
19     if (cuda_result != CUDA_SUCCESS) {                   \
20       const char *msg;                                   \
21       cuGetErrorName(cuda_result, &msg);                 \
22       return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
23     }                                                    \
24   } while (0)
25 
26 #define CeedChk_Cublas(ceed, x)                            \
27   do {                                                     \
28     cublasStatus_t cublas_result = x;                      \
29     if (cublas_result != CUBLAS_STATUS_SUCCESS) {          \
30       const char *msg = cublasGetErrorName(cublas_result); \
31       return CeedError((ceed), CEED_ERROR_BACKEND, msg);   \
32     }                                                      \
33   } while (0)
34 
35 #define CeedCallCuda(ceed, ...) \
36   do {                          \
37     int ierr_q_ = __VA_ARGS__;  \
38     CeedChk_Cu(ceed, ierr_q_);  \
39   } while (0)
40 
41 #define CeedCallCublas(ceed, ...)  \
42   do {                             \
43     int ierr_q_ = __VA_ARGS__;     \
44     CeedChk_Cublas(ceed, ierr_q_); \
45   } while (0)
46 
47 #define CASE(name) \
48   case name:       \
49     return #name
50 // LCOV_EXCL_START
51 static const char *cublasGetErrorName(cublasStatus_t error) {
52   switch (error) {
53     CASE(CUBLAS_STATUS_SUCCESS);
54     CASE(CUBLAS_STATUS_NOT_INITIALIZED);
55     CASE(CUBLAS_STATUS_ALLOC_FAILED);
56     CASE(CUBLAS_STATUS_INVALID_VALUE);
57     CASE(CUBLAS_STATUS_ARCH_MISMATCH);
58     CASE(CUBLAS_STATUS_MAPPING_ERROR);
59     CASE(CUBLAS_STATUS_EXECUTION_FAILED);
60     CASE(CUBLAS_STATUS_INTERNAL_ERROR);
61     default:
62       return "CUBLAS_STATUS_UNKNOWN_ERROR";
63   }
64 }
65 // LCOV_EXCL_STOP
66 
67 typedef struct {
68   int                   device_id;
69   bool                  use_llvm_version;
70   int                   llvm_version;
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