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_cuda_common_h 9 #define _ceed_cuda_common_h 10 11 #include <ceed/ceed.h> 12 #include <ceed/backend.h> 13 #include <cublas_v2.h> 14 #include <cuda.h> 15 #include <cuda_runtime.h> 16 17 #define QUOTE(...) #__VA_ARGS__ 18 19 #define CeedChk_Cu(ceed, x) \ 20 do { \ 21 CUresult cuda_result = x; \ 22 if (cuda_result != CUDA_SUCCESS) { \ 23 const char *msg; \ 24 cuGetErrorName(cuda_result, &msg); \ 25 return CeedError((ceed), CEED_ERROR_BACKEND, msg); \ 26 } \ 27 } while (0) 28 29 #define CeedChk_Cublas(ceed, x) \ 30 do { \ 31 cublasStatus_t cublas_result = x; \ 32 if (cublas_result != CUBLAS_STATUS_SUCCESS) { \ 33 const char *msg = cublasGetErrorName(cublas_result); \ 34 return CeedError((ceed), CEED_ERROR_BACKEND, msg); \ 35 } \ 36 } while (0) 37 38 #define CASE(name) case name: return #name 39 // LCOV_EXCL_START 40 static const char *cublasGetErrorName(cublasStatus_t error) { 41 switch (error) { 42 CASE(CUBLAS_STATUS_SUCCESS); 43 CASE(CUBLAS_STATUS_NOT_INITIALIZED); 44 CASE(CUBLAS_STATUS_ALLOC_FAILED); 45 CASE(CUBLAS_STATUS_INVALID_VALUE); 46 CASE(CUBLAS_STATUS_ARCH_MISMATCH); 47 CASE(CUBLAS_STATUS_MAPPING_ERROR); 48 CASE(CUBLAS_STATUS_EXECUTION_FAILED); 49 CASE(CUBLAS_STATUS_INTERNAL_ERROR); 50 default: return "CUBLAS_STATUS_UNKNOWN_ERROR"; 51 } 52 } 53 // LCOV_EXCL_STOP 54 55 typedef struct { 56 int device_id; 57 cublasHandle_t cublas_handle; 58 struct cudaDeviceProp device_prop; 59 } Ceed_Cuda; 60 61 CEED_INTERN int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, 62 char **resource_root); 63 64 CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource); 65 66 CEED_INTERN int CeedDestroy_Cuda(Ceed ceed); 67 68 #endif // _ceed_cuda_common_h