xref: /libCEED/backends/hip/ceed-hip-common.h (revision 5dfaedb85d2aa5da89951bb5d8f41d61be09bbf6)
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/ceed.h>
12 #include <ceed/backend.h>
13 #include <hip/hip_runtime.h>
14 #if (HIP_VERSION >= 50200000)
15 #include <hipblas/hipblas.h>
16 #else
17 #include <hipblas.h>
18 #endif
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 CASE(name) case name: return #name
41 // LCOV_EXCL_START
42 CEED_UNUSED static const char *hipblasGetErrorName(hipblasStatus_t error) {
43   switch (error) {
44     CASE(HIPBLAS_STATUS_SUCCESS);
45     CASE(HIPBLAS_STATUS_NOT_INITIALIZED);
46     CASE(HIPBLAS_STATUS_ALLOC_FAILED);
47     CASE(HIPBLAS_STATUS_INVALID_VALUE);
48     CASE(HIPBLAS_STATUS_ARCH_MISMATCH);
49     CASE(HIPBLAS_STATUS_MAPPING_ERROR);
50     CASE(HIPBLAS_STATUS_EXECUTION_FAILED);
51     CASE(HIPBLAS_STATUS_INTERNAL_ERROR);
52   default: return "HIPBLAS_STATUS_UNKNOWN_ERROR";
53   }
54 }
55 // LCOV_EXCL_STOP
56 
57 typedef struct {
58   int opt_block_size;
59   int device_id;
60   hipblasHandle_t hipblas_handle;
61 } Ceed_Hip;
62 
63 CEED_INTERN int CeedHipGetResourceRoot(Ceed ceed, const char *resource,
64                                        char **resource_root);
65 
66 CEED_INTERN int CeedHipInit(Ceed ceed, const char *resource);
67 
68 CEED_INTERN int CeedDestroy_Hip(Ceed ceed);
69 
70 #endif // _ceed_hip_common_h
71