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