xref: /libCEED/backends/hip/ceed-hip-common.h (revision 4548da4e4ef44dc0f2704ad6d48ac0ca4a16bc83)
1 // Copyright (c) 2017-2022, 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 
8 #ifndef _ceed_common_hip_h
9 #define _ceed_common_hip_h
10 
11 #include <ceed.h>
12 #include <ceed/backend.h>
13 #include <hip/hip_runtime.h>
14 
15 #if (HIP_VERSION >= 50200000)
16 #include <hipblas/hipblas.h>  // IWYU pragma: export
17 #else
18 #include <hipblas.h>  // IWYU pragma: export
19 #endif
20 
21 #define QUOTE(...) #__VA_ARGS__
22 
23 #define CeedChk_Hip(ceed, x)                             \
24   do {                                                   \
25     hipError_t hip_result = x;                           \
26     if (hip_result != hipSuccess) {                      \
27       const char *msg = hipGetErrorName(hip_result);     \
28       return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
29     }                                                    \
30   } while (0)
31 
32 #define CeedChk_Hipblas(ceed, x)                             \
33   do {                                                       \
34     hipblasStatus_t hipblas_result = x;                      \
35     if (hipblas_result != HIPBLAS_STATUS_SUCCESS) {          \
36       const char *msg = hipblasGetErrorName(hipblas_result); \
37       return CeedError((ceed), CEED_ERROR_BACKEND, msg);     \
38     }                                                        \
39   } while (0)
40 
41 #define CeedCallHip(ceed, ...)        \
42   do {                                \
43     hipError_t ierr_q_ = __VA_ARGS__; \
44     CeedChk_Hip(ceed, ierr_q_);       \
45   } while (0)
46 
47 #define CeedCallHipblas(ceed, ...)         \
48   do {                                     \
49     hipblasStatus_t ierr_q_ = __VA_ARGS__; \
50     CeedChk_Hipblas(ceed, ierr_q_);        \
51   } while (0)
52 
53 #define CASE(name) \
54   case name:       \
55     return #name
56 // LCOV_EXCL_START
57 CEED_UNUSED static const char *hipblasGetErrorName(hipblasStatus_t error) {
58   switch (error) {
59     CASE(HIPBLAS_STATUS_SUCCESS);
60     CASE(HIPBLAS_STATUS_NOT_INITIALIZED);
61     CASE(HIPBLAS_STATUS_ALLOC_FAILED);
62     CASE(HIPBLAS_STATUS_INVALID_VALUE);
63     CASE(HIPBLAS_STATUS_ARCH_MISMATCH);
64     CASE(HIPBLAS_STATUS_MAPPING_ERROR);
65     CASE(HIPBLAS_STATUS_EXECUTION_FAILED);
66     CASE(HIPBLAS_STATUS_INTERNAL_ERROR);
67     default:
68       return "HIPBLAS_STATUS_UNKNOWN_ERROR";
69   }
70 }
71 // LCOV_EXCL_STOP
72 
73 typedef struct {
74   int             opt_block_size;
75   int             device_id;
76   hipblasHandle_t hipblas_handle;
77 } Ceed_Hip;
78 
79 CEED_INTERN int CeedInit_Hip(Ceed ceed, const char *resource);
80 
81 CEED_INTERN int CeedDestroy_Hip(Ceed ceed);
82 
83 #endif  // _ceed_hip_common_h
84