xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.h (revision 7fcac03628df7c326a56de618266f195cb34c506)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #ifndef _ceed_cuda_common_h
18 #define _ceed_cuda_common_h
19 
20 #include <ceed/ceed.h>
21 #include <ceed/backend.h>
22 #include <cublas_v2.h>
23 #include <cuda.h>
24 #include <cuda_runtime.h>
25 
26 #define QUOTE(...) #__VA_ARGS__
27 
28 #define CUDA_MAX_PATH 256
29 
30 #define CeedChk_Cu(ceed, x) \
31 do { \
32   CUresult cuda_result = x; \
33   if (cuda_result != CUDA_SUCCESS) { \
34     const char *msg; \
35     cuGetErrorName(cuda_result, &msg); \
36     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
37   } \
38 } while (0)
39 
40 #define CeedChk_Cublas(ceed, x) \
41 do { \
42   cublasStatus_t cublas_result = x; \
43   if (cublas_result != CUBLAS_STATUS_SUCCESS) { \
44     const char *msg = cublasGetErrorName(cublas_result); \
45     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
46    } \
47 } while (0)
48 
49 #define CASE(name) case name: 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: return "CUBLAS_STATUS_UNKNOWN_ERROR";
62   }
63 }
64 // LCOV_EXCL_STOP
65 
66 typedef struct {
67   int deviceId;
68   cublasHandle_t cublasHandle;
69   struct cudaDeviceProp deviceProp;
70 } Ceed_Cuda;
71 
72 CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource, int nrc);
73 
74 CEED_INTERN int CeedDestroy_Cuda(Ceed ceed);
75 
76 #endif // _ceed_cuda_common_h