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