xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.h (revision 94b7b29b41ad8a17add4c577886859ef16f89dec)
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 
8*94b7b29bSJeremy L Thompson #ifndef CEED_COMMON_HIP_H
9*94b7b29bSJeremy L Thompson #define CEED_COMMON_HIP_H
107fcac036SJeremy L Thompson 
1149aac155SJeremy L Thompson #include <ceed.h>
127fcac036SJeremy L Thompson #include <ceed/backend.h>
13ce5711eeSJeremy L Thompson #include <hip/hip_runtime.h>
140df8cb37SJeremy L Thompson #if (HIP_VERSION >= 50200000)
1549aac155SJeremy L Thompson #include <hipblas/hipblas.h>  // IWYU pragma: export
160df8cb37SJeremy L Thompson #else
1749aac155SJeremy L Thompson #include <hipblas.h>  // IWYU pragma: export
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 
402b730f8bSJeremy L Thompson #define CeedCallHip(ceed, ...)        \
412b730f8bSJeremy L Thompson   do {                                \
422b730f8bSJeremy L Thompson     hipError_t ierr_q_ = __VA_ARGS__; \
432b730f8bSJeremy L Thompson     CeedChk_Hip(ceed, ierr_q_);       \
446574a04fSJeremy L Thompson   } while (0)
452b730f8bSJeremy L Thompson 
462b730f8bSJeremy L Thompson #define CeedCallHipblas(ceed, ...)         \
472b730f8bSJeremy L Thompson   do {                                     \
482b730f8bSJeremy L Thompson     hipblasStatus_t ierr_q_ = __VA_ARGS__; \
492b730f8bSJeremy L Thompson     CeedChk_Hipblas(ceed, ierr_q_);        \
506574a04fSJeremy L Thompson   } while (0)
512b730f8bSJeremy L Thompson 
522b730f8bSJeremy L Thompson #define CASE(name) \
532b730f8bSJeremy L Thompson   case name:       \
542b730f8bSJeremy 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);
662b730f8bSJeremy L Thompson     default:
672b730f8bSJeremy 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                    device_id;
740d0321e0SJeremy L Thompson   hipblasHandle_t        hipblas_handle;
75b2165e7aSSebastian Grimberg   struct hipDeviceProp_t device_prop;
76b2165e7aSSebastian Grimberg   int                    opt_block_size;
777fcac036SJeremy L Thompson } Ceed_Hip;
787fcac036SJeremy L Thompson 
79eb7e6cafSJeremy L Thompson CEED_INTERN int CeedInit_Hip(Ceed ceed, const char *resource);
807fcac036SJeremy L Thompson 
817fcac036SJeremy L Thompson CEED_INTERN int CeedDestroy_Hip(Ceed ceed);
827fcac036SJeremy L Thompson 
83*94b7b29bSJeremy L Thompson #endif  // CEED_COMMON_HIP_H
84