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