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/backend.h> 129e201c85SYohann #include <ceed/jit-source/cuda/cuda-types.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 { \ 21c9c2c079SJeremy 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 38*2b730f8bSJeremy L Thompson #define CeedCallCuda(ceed, ...) \ 39*2b730f8bSJeremy L Thompson do { \ 40*2b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 41*2b730f8bSJeremy L Thompson CeedChk_Cu(ceed, ierr_q_); \ 42*2b730f8bSJeremy L Thompson } while (0); 43*2b730f8bSJeremy L Thompson 44*2b730f8bSJeremy L Thompson #define CeedCallCublas(ceed, ...) \ 45*2b730f8bSJeremy L Thompson do { \ 46*2b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 47*2b730f8bSJeremy L Thompson CeedChk_Cublas(ceed, ierr_q_); \ 48*2b730f8bSJeremy L Thompson } while (0); 49*2b730f8bSJeremy L Thompson 50*2b730f8bSJeremy L Thompson #define CASE(name) \ 51*2b730f8bSJeremy L Thompson case name: \ 52*2b730f8bSJeremy L Thompson return #name 537fcac036SJeremy L Thompson // LCOV_EXCL_START 547fcac036SJeremy L Thompson static const char *cublasGetErrorName(cublasStatus_t error) { 557fcac036SJeremy L Thompson switch (error) { 567fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_SUCCESS); 577fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_NOT_INITIALIZED); 587fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_ALLOC_FAILED); 597fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_INVALID_VALUE); 607fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_ARCH_MISMATCH); 617fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_MAPPING_ERROR); 627fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_EXECUTION_FAILED); 637fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_INTERNAL_ERROR); 64*2b730f8bSJeremy L Thompson default: 65*2b730f8bSJeremy L Thompson return "CUBLAS_STATUS_UNKNOWN_ERROR"; 667fcac036SJeremy L Thompson } 677fcac036SJeremy L Thompson } 687fcac036SJeremy L Thompson // LCOV_EXCL_STOP 697fcac036SJeremy L Thompson 707fcac036SJeremy L Thompson typedef struct { 710d0321e0SJeremy L Thompson int device_id; 720d0321e0SJeremy L Thompson cublasHandle_t cublas_handle; 730d0321e0SJeremy L Thompson struct cudaDeviceProp device_prop; 747fcac036SJeremy L Thompson } Ceed_Cuda; 757fcac036SJeremy L Thompson 76*2b730f8bSJeremy L Thompson CEED_INTERN int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, char **resource_root); 77b11824b3SJeremy L Thompson 78f87d896cSJeremy L Thompson CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource); 797fcac036SJeremy L Thompson 807fcac036SJeremy L Thompson CEED_INTERN int CeedDestroy_Cuda(Ceed ceed); 817fcac036SJeremy L Thompson 827fcac036SJeremy L Thompson #endif // _ceed_cuda_common_h