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