xref: /libCEED/backends/hip/ceed-hip-common.h (revision a0154adecfab8547cdc0febbbf40ac009dbe9d1d)
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 #include <hipblas.h>
15 
16 #define QUOTE(...) #__VA_ARGS__
17 
18 #define CeedChk_Hip(ceed, x) \
19 do { \
20   hipError_t hip_result = x; \
21   if (hip_result != hipSuccess) { \
22     const char *msg = hipGetErrorName(hip_result); \
23     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
24   } \
25 } while (0)
26 
27 #define CeedChk_Hipblas(ceed, x) \
28 do { \
29   hipblasStatus_t hipblas_result = x; \
30   if (hipblas_result != HIPBLAS_STATUS_SUCCESS) { \
31     const char *msg = hipblasGetErrorName(hipblas_result); \
32     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
33    } \
34 } while (0)
35 
36 #define CASE(name) case name: return #name
37 // LCOV_EXCL_START
38 CEED_UNUSED static const char *hipblasGetErrorName(hipblasStatus_t error) {
39   switch (error) {
40     CASE(HIPBLAS_STATUS_SUCCESS);
41     CASE(HIPBLAS_STATUS_NOT_INITIALIZED);
42     CASE(HIPBLAS_STATUS_ALLOC_FAILED);
43     CASE(HIPBLAS_STATUS_INVALID_VALUE);
44     CASE(HIPBLAS_STATUS_ARCH_MISMATCH);
45     CASE(HIPBLAS_STATUS_MAPPING_ERROR);
46     CASE(HIPBLAS_STATUS_EXECUTION_FAILED);
47     CASE(HIPBLAS_STATUS_INTERNAL_ERROR);
48   default: return "HIPBLAS_STATUS_UNKNOWN_ERROR";
49   }
50 }
51 // LCOV_EXCL_STOP
52 
53 typedef struct {
54   int opt_block_size;
55   int device_id;
56   hipblasHandle_t hipblas_handle;
57 } Ceed_Hip;
58 
59 CEED_INTERN int CeedHipInit(Ceed ceed, const char *resource);
60 
61 CEED_INTERN int CeedDestroy_Hip(Ceed ceed);
62 
63 #endif // _ceed_hip_common_h
64