xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-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_common_hip_h
18 #define _ceed_common_hip_h
19 
20 #include <ceed/ceed.h>
21 #include <ceed/backend.h>
22 #include <hip/hip_runtime.h>
23 #include <hipblas.h>
24 
25 #define HIP_MAX_PATH 256
26 
27 #define QUOTE(...) #__VA_ARGS__
28 
29 #define CeedChk_Hip(ceed, x) \
30 do { \
31   hipError_t hip_result = x; \
32   if (hip_result != hipSuccess) { \
33     const char *msg = hipGetErrorName(hip_result); \
34     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
35   } \
36 } while (0)
37 
38 #define CeedChk_Hipblas(ceed, x) \
39 do { \
40   hipblasStatus_t hipblas_result = x; \
41   if (hipblas_result != HIPBLAS_STATUS_SUCCESS) { \
42     const char *msg = hipblasGetErrorName(hipblas_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 CEED_UNUSED static const char *hipblasGetErrorName(hipblasStatus_t error) {
50   switch (error) {
51     CASE(HIPBLAS_STATUS_SUCCESS);
52     CASE(HIPBLAS_STATUS_NOT_INITIALIZED);
53     CASE(HIPBLAS_STATUS_ALLOC_FAILED);
54     CASE(HIPBLAS_STATUS_INVALID_VALUE);
55     CASE(HIPBLAS_STATUS_ARCH_MISMATCH);
56     CASE(HIPBLAS_STATUS_MAPPING_ERROR);
57     CASE(HIPBLAS_STATUS_EXECUTION_FAILED);
58     CASE(HIPBLAS_STATUS_INTERNAL_ERROR);
59   default: return "HIPBLAS_STATUS_UNKNOWN_ERROR";
60   }
61 }
62 // LCOV_EXCL_STOP
63 
64 typedef struct {
65   int optblocksize;
66   int deviceId;
67   hipblasHandle_t hipblasHandle;
68 } Ceed_Hip;
69 
70 CEED_INTERN int CeedHipInit(Ceed ceed, const char *resource, int nrc);
71 
72 CEED_INTERN int CeedDestroy_Hip(Ceed ceed);
73 
74 #endif // _ceed_hip_common_h
75