xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.h (revision f87d896ccdb3ae7eb58212d71cf20ac055d48f9d)
17fcac036SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
27fcac036SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
37fcac036SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
47fcac036SJeremy L Thompson //
57fcac036SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
67fcac036SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
77fcac036SJeremy L Thompson // element discretizations for exascale applications. For more information and
87fcac036SJeremy L Thompson // source code availability see http://github.com/ceed.
97fcac036SJeremy L Thompson //
107fcac036SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
117fcac036SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
127fcac036SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
137fcac036SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
147fcac036SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
157fcac036SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
167fcac036SJeremy L Thompson 
177fcac036SJeremy L Thompson #ifndef _ceed_cuda_common_h
187fcac036SJeremy L Thompson #define _ceed_cuda_common_h
197fcac036SJeremy L Thompson 
207fcac036SJeremy L Thompson #include <ceed/ceed.h>
217fcac036SJeremy L Thompson #include <ceed/backend.h>
227fcac036SJeremy L Thompson #include <cublas_v2.h>
237fcac036SJeremy L Thompson #include <cuda.h>
247fcac036SJeremy L Thompson #include <cuda_runtime.h>
257fcac036SJeremy L Thompson 
267fcac036SJeremy L Thompson #define QUOTE(...) #__VA_ARGS__
277fcac036SJeremy L Thompson 
287fcac036SJeremy L Thompson #define CeedChk_Cu(ceed, x) \
297fcac036SJeremy L Thompson do { \
307fcac036SJeremy L Thompson   CUresult cuda_result = x; \
317fcac036SJeremy L Thompson   if (cuda_result != CUDA_SUCCESS) { \
327fcac036SJeremy L Thompson     const char *msg; \
337fcac036SJeremy L Thompson     cuGetErrorName(cuda_result, &msg); \
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 CeedChk_Cublas(ceed, x) \
397fcac036SJeremy L Thompson do { \
407fcac036SJeremy L Thompson   cublasStatus_t cublas_result = x; \
417fcac036SJeremy L Thompson   if (cublas_result != CUBLAS_STATUS_SUCCESS) { \
427fcac036SJeremy L Thompson     const char *msg = cublasGetErrorName(cublas_result); \
437fcac036SJeremy L Thompson     return CeedError((ceed), CEED_ERROR_BACKEND, msg); \
447fcac036SJeremy L Thompson    } \
457fcac036SJeremy L Thompson } while (0)
467fcac036SJeremy L Thompson 
477fcac036SJeremy L Thompson #define CASE(name) case name: return #name
487fcac036SJeremy L Thompson // LCOV_EXCL_START
497fcac036SJeremy L Thompson static const char *cublasGetErrorName(cublasStatus_t error) {
507fcac036SJeremy L Thompson   switch (error) {
517fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_SUCCESS);
527fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_NOT_INITIALIZED);
537fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_ALLOC_FAILED);
547fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_INVALID_VALUE);
557fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_ARCH_MISMATCH);
567fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_MAPPING_ERROR);
577fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_EXECUTION_FAILED);
587fcac036SJeremy L Thompson     CASE(CUBLAS_STATUS_INTERNAL_ERROR);
597fcac036SJeremy L Thompson   default: return "CUBLAS_STATUS_UNKNOWN_ERROR";
607fcac036SJeremy L Thompson   }
617fcac036SJeremy L Thompson }
627fcac036SJeremy L Thompson // LCOV_EXCL_STOP
637fcac036SJeremy L Thompson 
647fcac036SJeremy L Thompson typedef struct {
657fcac036SJeremy L Thompson   int deviceId;
667fcac036SJeremy L Thompson   cublasHandle_t cublasHandle;
677fcac036SJeremy L Thompson   struct cudaDeviceProp deviceProp;
687fcac036SJeremy L Thompson } Ceed_Cuda;
697fcac036SJeremy L Thompson 
70*f87d896cSJeremy L Thompson CEED_INTERN int CeedCudaInit(Ceed ceed, const char *resource);
717fcac036SJeremy L Thompson 
727fcac036SJeremy L Thompson CEED_INTERN int CeedDestroy_Cuda(Ceed ceed);
737fcac036SJeremy L Thompson 
747fcac036SJeremy L Thompson #endif // _ceed_cuda_common_h