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.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 #endif // _ceed_hip_common_h 84