xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-ref/ceed-cuda-ref.c (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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>
1249aac155SJeremy L Thompson #include <stdbool.h>
130d0321e0SJeremy L Thompson #include <string.h>
140d0321e0SJeremy L Thompson 
1549aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
1649aac155SJeremy L Thompson 
170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180d0321e0SJeremy L Thompson // CUDA preferred MemType
190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
20437930d1SJeremy L Thompson static int CeedGetPreferredMemType_Cuda(CeedMemType *mem_type) {
21437930d1SJeremy L Thompson   *mem_type = CEED_MEM_DEVICE;
220d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
230d0321e0SJeremy L Thompson }
240d0321e0SJeremy L Thompson 
250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
260d0321e0SJeremy L Thompson // Get CUBLAS handle
270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
28eb7e6cafSJeremy L Thompson int CeedGetCublasHandle_Cuda(Ceed ceed, cublasHandle_t *handle) {
290d0321e0SJeremy L Thompson   Ceed_Cuda *data;
300d0321e0SJeremy L Thompson 
31ca735530SJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
322b730f8bSJeremy L Thompson   if (!data->cublas_handle) CeedCallCublas(ceed, cublasCreate(&data->cublas_handle));
330d0321e0SJeremy L Thompson   *handle = data->cublas_handle;
340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
350d0321e0SJeremy L Thompson }
360d0321e0SJeremy L Thompson 
370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
380d0321e0SJeremy L Thompson // Backend Init
390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
40eb7e6cafSJeremy L Thompson static int CeedInit_Cuda_ref(const char *resource, Ceed ceed) {
41ca735530SJeremy L Thompson   Ceed_Cuda *data;
42b11824b3SJeremy L Thompson   char      *resource_root;
43ca735530SJeremy L Thompson 
44bc246734SJeremy L Thompson   CeedCallBackend(CeedGetResourceRoot(ceed, resource, ":", &resource_root));
456574a04fSJeremy L Thompson   CeedCheck(!strcmp(resource_root, "/gpu/cuda/ref"), ceed, CEED_ERROR_BACKEND, "Cuda backend cannot use resource: %s", resource);
462b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&resource_root));
472b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetDeterministic(ceed, true));
480d0321e0SJeremy L Thompson 
492b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
502b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetData(ceed, data));
51eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedInit_Cuda(ceed, resource));
520d0321e0SJeremy L Thompson 
532b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType", CeedGetPreferredMemType_Cuda));
542b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", CeedVectorCreate_Cuda));
552b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Cuda));
562b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Cuda));
57d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateHdiv", CeedBasisCreateHdiv_Cuda));
58d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateHcurl", CeedBasisCreateHcurl_Cuda));
592b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Cuda));
60b20a4af9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreateAtPoints", CeedElemRestrictionCreate_Cuda));
612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate", CeedQFunctionCreate_Cuda));
622b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate", CeedQFunctionContextCreate_Cuda));
632b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Cuda));
64756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreateAtPoints", CeedOperatorCreateAtPoints_Cuda));
652b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Cuda));
660d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
670d0321e0SJeremy L Thompson }
680d0321e0SJeremy L Thompson 
690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
700d0321e0SJeremy L Thompson // Backend Register
710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
72eb7e6cafSJeremy L Thompson CEED_INTERN int CeedRegister_Cuda(void) { return CeedRegister("/gpu/cuda/ref", CeedInit_Cuda_ref, 40); }
732a86cc9dSSebastian Grimberg 
740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
75