xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-ref/ceed-cuda-ref.c (revision 2a86cc9d4dbfce2964c7e8927a1e6db8d19a41fc)
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.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
82b730f8bSJeremy L Thompson #include "ceed-cuda-ref.h"
92b730f8bSJeremy L Thompson 
1049aac155SJeremy L Thompson #include <ceed.h>
110d0321e0SJeremy L Thompson #include <ceed/backend.h>
120d0321e0SJeremy L Thompson #include <cublas_v2.h>
1349aac155SJeremy L Thompson #include <stdbool.h>
140d0321e0SJeremy L Thompson #include <string.h>
150d0321e0SJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
1749aac155SJeremy L Thompson 
180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
190d0321e0SJeremy L Thompson // CUDA preferred MemType
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
21437930d1SJeremy L Thompson static int CeedGetPreferredMemType_Cuda(CeedMemType *mem_type) {
22437930d1SJeremy L Thompson   *mem_type = CEED_MEM_DEVICE;
230d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
240d0321e0SJeremy L Thompson }
250d0321e0SJeremy L Thompson 
260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
270d0321e0SJeremy L Thompson // Get CUBLAS handle
280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
290d0321e0SJeremy L Thompson int CeedCudaGetCublasHandle(Ceed ceed, cublasHandle_t *handle) {
300d0321e0SJeremy L Thompson   Ceed_Cuda *data;
312b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
320d0321e0SJeremy L Thompson 
332b730f8bSJeremy L Thompson   if (!data->cublas_handle) CeedCallCublas(ceed, cublasCreate(&data->cublas_handle));
340d0321e0SJeremy L Thompson   *handle = data->cublas_handle;
350d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
360d0321e0SJeremy L Thompson }
370d0321e0SJeremy L Thompson 
380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
390d0321e0SJeremy L Thompson // Backend Init
400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
410d0321e0SJeremy L Thompson static int CeedInit_Cuda(const char *resource, Ceed ceed) {
42b11824b3SJeremy L Thompson   char *resource_root;
432b730f8bSJeremy L Thompson   CeedCallBackend(CeedCudaGetResourceRoot(ceed, resource, &resource_root));
442b730f8bSJeremy L Thompson   if (strcmp(resource_root, "/gpu/cuda/ref")) {
450d0321e0SJeremy L Thompson     // LCOV_EXCL_START
462b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Cuda backend cannot use resource: %s", resource);
470d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
482b730f8bSJeremy L Thompson   }
492b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&resource_root));
502b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetDeterministic(ceed, true));
510d0321e0SJeremy L Thompson 
520d0321e0SJeremy L Thompson   Ceed_Cuda *data;
532b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
542b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetData(ceed, data));
552b730f8bSJeremy L Thompson   CeedCallBackend(CeedCudaInit(ceed, resource));
560d0321e0SJeremy L Thompson 
572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType", CeedGetPreferredMemType_Cuda));
582b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", CeedVectorCreate_Cuda));
592b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Cuda));
602b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Cuda));
612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Cuda));
622b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreateBlocked", CeedElemRestrictionCreateBlocked_Cuda));
632b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate", CeedQFunctionCreate_Cuda));
642b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate", CeedQFunctionContextCreate_Cuda));
652b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Cuda));
662b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Cuda));
670d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
680d0321e0SJeremy L Thompson }
690d0321e0SJeremy L Thompson 
700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
710d0321e0SJeremy L Thompson // Backend Register
720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
732b730f8bSJeremy L Thompson CEED_INTERN int CeedRegister_Cuda(void) { return CeedRegister("/gpu/cuda/ref", CeedInit_Cuda, 40); }
74*2a86cc9dSSebastian Grimberg 
750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
76