xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.h (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
37fcac036SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57fcac036SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
77fcac036SJeremy L Thompson 
87fcac036SJeremy L Thompson #ifndef _ceed_common_hip_h
97fcac036SJeremy L Thompson #define _ceed_common_hip_h
107fcac036SJeremy L Thompson 
117fcac036SJeremy L Thompson #include <ceed/backend.h>
129e201c85SYohann #include <ceed/jit-source/hip/hip-types.h>
137fcac036SJeremy L Thompson #include <hip/hip_runtime.h>
140df8cb37SJeremy L Thompson #if (HIP_VERSION >= 50200000)
150df8cb37SJeremy L Thompson #include <hipblas/hipblas.h>
160df8cb37SJeremy L Thompson #else
177fcac036SJeremy L Thompson #include <hipblas.h>
180df8cb37SJeremy L Thompson #endif
197fcac036SJeremy L Thompson 
207fcac036SJeremy L Thompson #define QUOTE(...) #__VA_ARGS__
217fcac036SJeremy L Thompson 
227fcac036SJeremy L Thompson #define CeedChk_Hip(ceed, x)                             \
237fcac036SJeremy L Thompson   do {                                                   \
247fcac036SJeremy L Thompson     hipError_t hip_result = x;                           \
257fcac036SJeremy L Thompson     if (hip_result != hipSuccess) {                      \
267fcac036SJeremy L Thompson       const char *msg = hipGetErrorName(hip_result);     \
277fcac036SJeremy L Thompson       return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
287fcac036SJeremy L Thompson     }                                                    \
297fcac036SJeremy L Thompson   } while (0)
307fcac036SJeremy L Thompson 
317fcac036SJeremy L Thompson #define CeedChk_Hipblas(ceed, x)                             \
327fcac036SJeremy L Thompson   do {                                                       \
337fcac036SJeremy L Thompson     hipblasStatus_t hipblas_result = x;                      \
347fcac036SJeremy L Thompson     if (hipblas_result != HIPBLAS_STATUS_SUCCESS) {          \
357fcac036SJeremy L Thompson       const char *msg = hipblasGetErrorName(hipblas_result); \
367fcac036SJeremy L Thompson       return CeedError((ceed), CEED_ERROR_BACKEND, msg);     \
377fcac036SJeremy L Thompson     }                                                        \
387fcac036SJeremy L Thompson   } while (0)
397fcac036SJeremy L Thompson 
40*2b730f8bSJeremy L Thompson #define CeedCallHip(ceed, ...)        \
41*2b730f8bSJeremy L Thompson   do {                                \
42*2b730f8bSJeremy L Thompson     hipError_t ierr_q_ = __VA_ARGS__; \
43*2b730f8bSJeremy L Thompson     CeedChk_Hip(ceed, ierr_q_);       \
44*2b730f8bSJeremy L Thompson   } while (0);
45*2b730f8bSJeremy L Thompson 
46*2b730f8bSJeremy L Thompson #define CeedCallHipblas(ceed, ...)         \
47*2b730f8bSJeremy L Thompson   do {                                     \
48*2b730f8bSJeremy L Thompson     hipblasStatus_t ierr_q_ = __VA_ARGS__; \
49*2b730f8bSJeremy L Thompson     CeedChk_Hipblas(ceed, ierr_q_);        \
50*2b730f8bSJeremy L Thompson   } while (0);
51*2b730f8bSJeremy L Thompson 
52*2b730f8bSJeremy L Thompson #define CASE(name) \
53*2b730f8bSJeremy L Thompson   case name:       \
54*2b730f8bSJeremy L Thompson     return #name
557fcac036SJeremy L Thompson // LCOV_EXCL_START
567fcac036SJeremy L Thompson CEED_UNUSED static const char *hipblasGetErrorName(hipblasStatus_t error) {
577fcac036SJeremy L Thompson   switch (error) {
587fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_SUCCESS);
597fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_NOT_INITIALIZED);
607fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_ALLOC_FAILED);
617fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_INVALID_VALUE);
627fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_ARCH_MISMATCH);
637fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_MAPPING_ERROR);
647fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_EXECUTION_FAILED);
657fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_INTERNAL_ERROR);
66*2b730f8bSJeremy L Thompson     default:
67*2b730f8bSJeremy L Thompson       return "HIPBLAS_STATUS_UNKNOWN_ERROR";
687fcac036SJeremy L Thompson   }
697fcac036SJeremy L Thompson }
707fcac036SJeremy L Thompson // LCOV_EXCL_STOP
717fcac036SJeremy L Thompson 
727fcac036SJeremy L Thompson typedef struct {
730d0321e0SJeremy L Thompson   int             opt_block_size;
740d0321e0SJeremy L Thompson   int             device_id;
750d0321e0SJeremy L Thompson   hipblasHandle_t hipblas_handle;
767fcac036SJeremy L Thompson } Ceed_Hip;
777fcac036SJeremy L Thompson 
78*2b730f8bSJeremy L Thompson CEED_INTERN int CeedHipGetResourceRoot(Ceed ceed, const char *resource, char **resource_root);
79b11824b3SJeremy L Thompson 
80f87d896cSJeremy L Thompson CEED_INTERN int CeedHipInit(Ceed ceed, const char *resource);
817fcac036SJeremy L Thompson 
827fcac036SJeremy L Thompson CEED_INTERN int CeedDestroy_Hip(Ceed ceed);
837fcac036SJeremy L Thompson 
847fcac036SJeremy L Thompson #endif  // _ceed_hip_common_h
85