xref: /libCEED/backends/cuda-ref/ceed-cuda-ref.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
6*3d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
80d0321e0SJeremy L Thompson #include <ceed/ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
100d0321e0SJeremy L Thompson #include <cublas_v2.h>
110d0321e0SJeremy L Thompson #include <cuda.h>
120d0321e0SJeremy L Thompson #include <cuda_runtime.h>
130d0321e0SJeremy L Thompson #include <string.h>
140d0321e0SJeremy L Thompson #include "ceed-cuda-ref.h"
150d0321e0SJeremy L Thompson 
160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
170d0321e0SJeremy L Thompson // CUDA preferred MemType
180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
19437930d1SJeremy L Thompson static int CeedGetPreferredMemType_Cuda(CeedMemType *mem_type) {
20437930d1SJeremy L Thompson   *mem_type = CEED_MEM_DEVICE;
210d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
220d0321e0SJeremy L Thompson }
230d0321e0SJeremy L Thompson 
240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
250d0321e0SJeremy L Thompson // Get CUBLAS handle
260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
270d0321e0SJeremy L Thompson int CeedCudaGetCublasHandle(Ceed ceed, cublasHandle_t *handle) {
280d0321e0SJeremy L Thompson   int ierr;
290d0321e0SJeremy L Thompson   Ceed_Cuda *data;
300d0321e0SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
310d0321e0SJeremy L Thompson 
320d0321e0SJeremy L Thompson   if (!data->cublas_handle) {
330d0321e0SJeremy L Thompson     ierr = cublasCreate(&data->cublas_handle); CeedChk_Cublas(ceed, ierr);
340d0321e0SJeremy L Thompson   }
350d0321e0SJeremy L Thompson   *handle = data->cublas_handle;
360d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
370d0321e0SJeremy L Thompson }
380d0321e0SJeremy L Thompson 
390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
400d0321e0SJeremy L Thompson // Backend Init
410d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
420d0321e0SJeremy L Thompson static int CeedInit_Cuda(const char *resource, Ceed ceed) {
430d0321e0SJeremy L Thompson   int ierr;
440d0321e0SJeremy L Thompson 
450d0321e0SJeremy L Thompson   if (strcmp(resource, "/gpu/cuda/ref"))
460d0321e0SJeremy L Thompson     // LCOV_EXCL_START
470d0321e0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
480d0321e0SJeremy L Thompson                      "Cuda backend cannot use resource: %s", resource);
490d0321e0SJeremy L Thompson   // LCOV_EXCL_STOP
500d0321e0SJeremy L Thompson   ierr = CeedSetDeterministic(ceed, true); CeedChk(ierr);
510d0321e0SJeremy L Thompson 
520d0321e0SJeremy L Thompson   Ceed_Cuda *data;
530d0321e0SJeremy L Thompson   ierr = CeedCalloc(1, &data); CeedChkBackend(ierr);
540d0321e0SJeremy L Thompson   ierr = CeedSetData(ceed, data); CeedChkBackend(ierr);
550d0321e0SJeremy L Thompson   ierr = CeedCudaInit(ceed, resource); CeedChkBackend(ierr);
560d0321e0SJeremy L Thompson 
570d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType",
580d0321e0SJeremy L Thompson                                 CeedGetPreferredMemType_Cuda); CeedChkBackend(ierr);
590d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate",
600d0321e0SJeremy L Thompson                                 CeedVectorCreate_Cuda); CeedChkBackend(ierr);
610d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1",
620d0321e0SJeremy L Thompson                                 CeedBasisCreateTensorH1_Cuda); CeedChkBackend(ierr);
630d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1",
640d0321e0SJeremy L Thompson                                 CeedBasisCreateH1_Cuda); CeedChkBackend(ierr);
650d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate",
660d0321e0SJeremy L Thompson                                 CeedElemRestrictionCreate_Cuda); CeedChkBackend(ierr);
670d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed,
680d0321e0SJeremy L Thompson                                 "ElemRestrictionCreateBlocked",
690d0321e0SJeremy L Thompson                                 CeedElemRestrictionCreateBlocked_Cuda);
700d0321e0SJeremy L Thompson   CeedChkBackend(ierr);
710d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate",
720d0321e0SJeremy L Thompson                                 CeedQFunctionCreate_Cuda); CeedChkBackend(ierr);
730d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate",
740d0321e0SJeremy L Thompson                                 CeedQFunctionContextCreate_Cuda); CeedChkBackend(ierr);
750d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate",
760d0321e0SJeremy L Thompson                                 CeedOperatorCreate_Cuda); CeedChkBackend(ierr);
770d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "CompositeOperatorCreate",
780d0321e0SJeremy L Thompson                                 CeedCompositeOperatorCreate_Cuda); CeedChkBackend(ierr);
790d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy",
800d0321e0SJeremy L Thompson                                 CeedDestroy_Cuda); CeedChkBackend(ierr);
810d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
820d0321e0SJeremy L Thompson }
830d0321e0SJeremy L Thompson 
840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
850d0321e0SJeremy L Thompson // Backend Register
860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
870d0321e0SJeremy L Thompson CEED_INTERN int CeedRegister_Cuda(void) {
880d0321e0SJeremy L Thompson   return CeedRegister("/gpu/cuda/ref", CeedInit_Cuda, 40);
890d0321e0SJeremy L Thompson }
900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
91