xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.h (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1 // Copyright (c) 2017-2024, 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 #if (HIP_VERSION >= 50200000)
15 #include <hipblas/hipblas.h>  // IWYU pragma: export
16 #else
17 #include <hipblas.h>  // IWYU pragma: export
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 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                    device_id;
74   hipblasHandle_t        hipblas_handle;
75   struct hipDeviceProp_t device_prop;
76   int                    opt_block_size;
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 CEED_INTERN int CeedSetDeviceBoolArray_Hip(Ceed ceed, const bool *source_array, CeedCopyMode copy_mode, CeedSize num_values,
84                                            const bool **target_array_owned, const bool **target_array_borrowed, const bool **target_array);
85 CEED_INTERN int CeedSetDeviceCeedInt8Array_Hip(Ceed ceed, const CeedInt8 *source_array, CeedCopyMode copy_mode, CeedSize num_values,
86                                                const CeedInt8 **target_array_owned, const CeedInt8 **target_array_borrowed,
87                                                const CeedInt8 **target_array);
88 CEED_INTERN int CeedSetDeviceCeedIntArray_Hip(Ceed ceed, const CeedInt *source_array, CeedCopyMode copy_mode, CeedSize num_values,
89                                               const CeedInt **target_array_owned, const CeedInt **target_array_borrowed,
90                                               const CeedInt **target_array);
91 CEED_INTERN int CeedSetDeviceCeedScalarArray_Hip(Ceed ceed, const CeedScalar *source_array, CeedCopyMode copy_mode, CeedSize num_values,
92                                                  const CeedScalar **target_array_owned, const CeedScalar **target_array_borrowed,
93                                                  const CeedScalar **target_array);
94 
95 #endif  // CEED_COMMON_HIP_H
96