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_cuda_common_h 97fcac036SJeremy L Thompson #define _ceed_cuda_common_h 107fcac036SJeremy L Thompson 117fcac036SJeremy L Thompson #include <ceed/ceed.h> 127fcac036SJeremy L Thompson #include <ceed/backend.h> 137fcac036SJeremy L Thompson #include <cublas_v2.h> 147fcac036SJeremy L Thompson #include <cuda.h> 157fcac036SJeremy L Thompson #include <cuda_runtime.h> 167fcac036SJeremy L Thompson 177fcac036SJeremy L Thompson #define QUOTE(...) #__VA_ARGS__ 187fcac036SJeremy L Thompson 197fcac036SJeremy L Thompson #define CeedChk_Cu(ceed, x) \ 207fcac036SJeremy L Thompson do { \ 21*c9c2c079SJeremy L Thompson CUresult cuda_result = (CUresult)x; \ 227fcac036SJeremy L Thompson if (cuda_result != CUDA_SUCCESS) { \ 237fcac036SJeremy L Thompson const char *msg; \ 247fcac036SJeremy L Thompson cuGetErrorName(cuda_result, &msg); \ 257fcac036SJeremy L Thompson return CeedError((ceed), CEED_ERROR_BACKEND, msg); \ 267fcac036SJeremy L Thompson } \ 277fcac036SJeremy L Thompson } while (0) 287fcac036SJeremy L Thompson 297fcac036SJeremy L Thompson #define CeedChk_Cublas(ceed, x) \ 307fcac036SJeremy L Thompson do { \ 317fcac036SJeremy L Thompson cublasStatus_t cublas_result = x; \ 327fcac036SJeremy L Thompson if (cublas_result != CUBLAS_STATUS_SUCCESS) { \ 337fcac036SJeremy L Thompson const char *msg = cublasGetErrorName(cublas_result); \ 347fcac036SJeremy L Thompson return CeedError((ceed), CEED_ERROR_BACKEND, msg); \ 357fcac036SJeremy L Thompson } \ 367fcac036SJeremy L Thompson } while (0) 377fcac036SJeremy L Thompson 387fcac036SJeremy L Thompson #define CASE(name) case name: return #name 397fcac036SJeremy L Thompson // LCOV_EXCL_START 407fcac036SJeremy L Thompson static const char *cublasGetErrorName(cublasStatus_t error) { 417fcac036SJeremy L Thompson switch (error) { 427fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_SUCCESS); 437fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_NOT_INITIALIZED); 447fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_ALLOC_FAILED); 457fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_INVALID_VALUE); 467fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_ARCH_MISMATCH); 477fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_MAPPING_ERROR); 487fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_EXECUTION_FAILED); 497fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_INTERNAL_ERROR); 507fcac036SJeremy L Thompson default: return "CUBLAS_STATUS_UNKNOWN_ERROR"; 517fcac036SJeremy L Thompson } 527fcac036SJeremy L Thompson } 537fcac036SJeremy L Thompson // LCOV_EXCL_STOP 547fcac036SJeremy L Thompson 557fcac036SJeremy L Thompson typedef struct { 560d0321e0SJeremy L Thompson int device_id; 570d0321e0SJeremy L Thompson cublasHandle_t cublas_handle; 580d0321e0SJeremy L Thompson struct cudaDeviceProp device_prop; 597fcac036SJeremy L Thompson } Ceed_Cuda; 607fcac036SJeremy L Thompson 61b11824b3SJeremy L Thompson CEED_INTERN int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, 62b11824b3SJeremy L Thompson char **resource_root); 63b11824b3SJeremy L Thompson 64f87d896cSJeremy L Thompson CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource); 657fcac036SJeremy L Thompson 667fcac036SJeremy L Thompson CEED_INTERN int CeedDestroy_Cuda(Ceed ceed); 677fcac036SJeremy L Thompson 687fcac036SJeremy L Thompson #endif // _ceed_cuda_common_h