xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.h (revision ce5711eec5714674c81066ef9b319f74987eeefb)
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 
1149aac155SJeremy L Thompson #include <ceed.h>
127fcac036SJeremy L Thompson #include <ceed/backend.h>
13*ce5711eeSJeremy L Thompson #include <hip/hip_runtime.h>
14*ce5711eeSJeremy L Thompson 
150df8cb37SJeremy L Thompson #if (HIP_VERSION >= 50200000)
1649aac155SJeremy L Thompson #include <hipblas/hipblas.h>  // IWYU pragma: export
170df8cb37SJeremy L Thompson #else
1849aac155SJeremy L Thompson #include <hipblas.h>  // IWYU pragma: export
190df8cb37SJeremy L Thompson #endif
207fcac036SJeremy L Thompson 
217fcac036SJeremy L Thompson #define QUOTE(...) #__VA_ARGS__
227fcac036SJeremy L Thompson 
237fcac036SJeremy L Thompson #define CeedChk_Hip(ceed, x)                             \
247fcac036SJeremy L Thompson   do {                                                   \
257fcac036SJeremy L Thompson     hipError_t hip_result = x;                           \
267fcac036SJeremy L Thompson     if (hip_result != hipSuccess) {                      \
277fcac036SJeremy L Thompson       const char *msg = hipGetErrorName(hip_result);     \
287fcac036SJeremy L Thompson       return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
297fcac036SJeremy L Thompson     }                                                    \
307fcac036SJeremy L Thompson   } while (0)
317fcac036SJeremy L Thompson 
327fcac036SJeremy L Thompson #define CeedChk_Hipblas(ceed, x)                             \
337fcac036SJeremy L Thompson   do {                                                       \
347fcac036SJeremy L Thompson     hipblasStatus_t hipblas_result = x;                      \
357fcac036SJeremy L Thompson     if (hipblas_result != HIPBLAS_STATUS_SUCCESS) {          \
367fcac036SJeremy L Thompson       const char *msg = hipblasGetErrorName(hipblas_result); \
377fcac036SJeremy L Thompson       return CeedError((ceed), CEED_ERROR_BACKEND, msg);     \
387fcac036SJeremy L Thompson     }                                                        \
397fcac036SJeremy L Thompson   } while (0)
407fcac036SJeremy L Thompson 
412b730f8bSJeremy L Thompson #define CeedCallHip(ceed, ...)        \
422b730f8bSJeremy L Thompson   do {                                \
432b730f8bSJeremy L Thompson     hipError_t ierr_q_ = __VA_ARGS__; \
442b730f8bSJeremy L Thompson     CeedChk_Hip(ceed, ierr_q_);       \
456574a04fSJeremy L Thompson   } while (0)
462b730f8bSJeremy L Thompson 
472b730f8bSJeremy L Thompson #define CeedCallHipblas(ceed, ...)         \
482b730f8bSJeremy L Thompson   do {                                     \
492b730f8bSJeremy L Thompson     hipblasStatus_t ierr_q_ = __VA_ARGS__; \
502b730f8bSJeremy L Thompson     CeedChk_Hipblas(ceed, ierr_q_);        \
516574a04fSJeremy L Thompson   } while (0)
522b730f8bSJeremy L Thompson 
532b730f8bSJeremy L Thompson #define CASE(name) \
542b730f8bSJeremy L Thompson   case name:       \
552b730f8bSJeremy L Thompson     return #name
567fcac036SJeremy L Thompson // LCOV_EXCL_START
577fcac036SJeremy L Thompson CEED_UNUSED static const char *hipblasGetErrorName(hipblasStatus_t error) {
587fcac036SJeremy L Thompson   switch (error) {
597fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_SUCCESS);
607fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_NOT_INITIALIZED);
617fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_ALLOC_FAILED);
627fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_INVALID_VALUE);
637fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_ARCH_MISMATCH);
647fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_MAPPING_ERROR);
657fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_EXECUTION_FAILED);
667fcac036SJeremy L Thompson     CASE(HIPBLAS_STATUS_INTERNAL_ERROR);
672b730f8bSJeremy L Thompson     default:
682b730f8bSJeremy L Thompson       return "HIPBLAS_STATUS_UNKNOWN_ERROR";
697fcac036SJeremy L Thompson   }
707fcac036SJeremy L Thompson }
717fcac036SJeremy L Thompson // LCOV_EXCL_STOP
727fcac036SJeremy L Thompson 
737fcac036SJeremy L Thompson typedef struct {
740d0321e0SJeremy L Thompson   int             opt_block_size;
750d0321e0SJeremy L Thompson   int             device_id;
760d0321e0SJeremy L Thompson   hipblasHandle_t hipblas_handle;
777fcac036SJeremy L Thompson } Ceed_Hip;
787fcac036SJeremy L Thompson 
792b730f8bSJeremy L Thompson CEED_INTERN int CeedHipGetResourceRoot(Ceed ceed, const char *resource, char **resource_root);
80b11824b3SJeremy L Thompson 
81f87d896cSJeremy L Thompson CEED_INTERN int CeedHipInit(Ceed ceed, const char *resource);
827fcac036SJeremy L Thompson 
837fcac036SJeremy L Thompson CEED_INTERN int CeedDestroy_Hip(Ceed ceed);
847fcac036SJeremy L Thompson 
857fcac036SJeremy L Thompson #endif  // _ceed_hip_common_h
86