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 11*49aac155SJeremy L Thompson #include <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 167fcac036SJeremy L Thompson #define QUOTE(...) #__VA_ARGS__ 177fcac036SJeremy L Thompson 187fcac036SJeremy L Thompson #define CeedChk_Cu(ceed, x) \ 197fcac036SJeremy L Thompson do { \ 20c9c2c079SJeremy L Thompson CUresult cuda_result = (CUresult)x; \ 217fcac036SJeremy L Thompson if (cuda_result != CUDA_SUCCESS) { \ 227fcac036SJeremy L Thompson const char *msg; \ 237fcac036SJeremy L Thompson cuGetErrorName(cuda_result, &msg); \ 247fcac036SJeremy L Thompson return CeedError((ceed), CEED_ERROR_BACKEND, msg); \ 257fcac036SJeremy L Thompson } \ 267fcac036SJeremy L Thompson } while (0) 277fcac036SJeremy L Thompson 287fcac036SJeremy L Thompson #define CeedChk_Cublas(ceed, x) \ 297fcac036SJeremy L Thompson do { \ 307fcac036SJeremy L Thompson cublasStatus_t cublas_result = x; \ 317fcac036SJeremy L Thompson if (cublas_result != CUBLAS_STATUS_SUCCESS) { \ 327fcac036SJeremy L Thompson const char *msg = cublasGetErrorName(cublas_result); \ 337fcac036SJeremy L Thompson return CeedError((ceed), CEED_ERROR_BACKEND, msg); \ 347fcac036SJeremy L Thompson } \ 357fcac036SJeremy L Thompson } while (0) 367fcac036SJeremy L Thompson 372b730f8bSJeremy L Thompson #define CeedCallCuda(ceed, ...) \ 382b730f8bSJeremy L Thompson do { \ 392b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 402b730f8bSJeremy L Thompson CeedChk_Cu(ceed, ierr_q_); \ 412b730f8bSJeremy L Thompson } while (0); 422b730f8bSJeremy L Thompson 432b730f8bSJeremy L Thompson #define CeedCallCublas(ceed, ...) \ 442b730f8bSJeremy L Thompson do { \ 452b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 462b730f8bSJeremy L Thompson CeedChk_Cublas(ceed, ierr_q_); \ 472b730f8bSJeremy L Thompson } while (0); 482b730f8bSJeremy L Thompson 492b730f8bSJeremy L Thompson #define CASE(name) \ 502b730f8bSJeremy L Thompson case name: \ 512b730f8bSJeremy L Thompson return #name 527fcac036SJeremy L Thompson // LCOV_EXCL_START 537fcac036SJeremy L Thompson static const char *cublasGetErrorName(cublasStatus_t error) { 547fcac036SJeremy L Thompson switch (error) { 557fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_SUCCESS); 567fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_NOT_INITIALIZED); 577fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_ALLOC_FAILED); 587fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_INVALID_VALUE); 597fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_ARCH_MISMATCH); 607fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_MAPPING_ERROR); 617fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_EXECUTION_FAILED); 627fcac036SJeremy L Thompson CASE(CUBLAS_STATUS_INTERNAL_ERROR); 632b730f8bSJeremy L Thompson default: 642b730f8bSJeremy L Thompson return "CUBLAS_STATUS_UNKNOWN_ERROR"; 657fcac036SJeremy L Thompson } 667fcac036SJeremy L Thompson } 677fcac036SJeremy L Thompson // LCOV_EXCL_STOP 687fcac036SJeremy L Thompson 697fcac036SJeremy L Thompson typedef struct { 700d0321e0SJeremy L Thompson int device_id; 710d0321e0SJeremy L Thompson cublasHandle_t cublas_handle; 720d0321e0SJeremy L Thompson struct cudaDeviceProp device_prop; 737fcac036SJeremy L Thompson } Ceed_Cuda; 747fcac036SJeremy L Thompson 752b730f8bSJeremy L Thompson CEED_INTERN int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, char **resource_root); 76b11824b3SJeremy L Thompson 77f87d896cSJeremy L Thompson CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource); 787fcac036SJeremy L Thompson 797fcac036SJeremy L Thompson CEED_INTERN int CeedDestroy_Cuda(Ceed ceed); 807fcac036SJeremy L Thompson 817fcac036SJeremy L Thompson #endif // _ceed_cuda_common_h