xref: /libCEED/backends/cuda/ceed-cuda-common.h (revision d7c981f323dbd942037809ac525cdf904a4875ff)
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 CeedChk_Cu(ceed, x) \
29 do { \
30   CUresult cuda_result = x; \
31   if (cuda_result != CUDA_SUCCESS) { \
32     const char *msg; \
33     cuGetErrorName(cuda_result, &msg); \
34     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
35   } \
36 } while (0)
37 
38 #define CeedChk_Cublas(ceed, x) \
39 do { \
40   cublasStatus_t cublas_result = x; \
41   if (cublas_result != CUBLAS_STATUS_SUCCESS) { \
42     const char *msg = cublasGetErrorName(cublas_result); \
43     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
44    } \
45 } while (0)
46 
47 #define CASE(name) case name: return #name
48 // LCOV_EXCL_START
49 static const char *cublasGetErrorName(cublasStatus_t error) {
50   switch (error) {
51     CASE(CUBLAS_STATUS_SUCCESS);
52     CASE(CUBLAS_STATUS_NOT_INITIALIZED);
53     CASE(CUBLAS_STATUS_ALLOC_FAILED);
54     CASE(CUBLAS_STATUS_INVALID_VALUE);
55     CASE(CUBLAS_STATUS_ARCH_MISMATCH);
56     CASE(CUBLAS_STATUS_MAPPING_ERROR);
57     CASE(CUBLAS_STATUS_EXECUTION_FAILED);
58     CASE(CUBLAS_STATUS_INTERNAL_ERROR);
59   default: return "CUBLAS_STATUS_UNKNOWN_ERROR";
60   }
61 }
62 // LCOV_EXCL_STOP
63 
64 typedef struct {
65   int deviceId;
66   cublasHandle_t cublasHandle;
67   struct cudaDeviceProp deviceProp;
68 } Ceed_Cuda;
69 
70 CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource, int nrc);
71 
72 CEED_INTERN int CeedDestroy_Cuda(Ceed ceed);
73 
74 #endif // _ceed_cuda_common_h