xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-ref/ceed-cuda-ref.c (revision eb7e6cafeb5d6cc94d59355f95e7bc9ae3fc1c25)
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 //------------------------------------------------------------------------------
29*eb7e6cafSJeremy L Thompson int CeedGetCublasHandle_Cuda(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 //------------------------------------------------------------------------------
41*eb7e6cafSJeremy L Thompson static int CeedInit_Cuda_ref(const char *resource, Ceed ceed) {
42b11824b3SJeremy L Thompson   char *resource_root;
43bc246734SJeremy L Thompson   CeedCallBackend(CeedGetResourceRoot(ceed, resource, ":", &resource_root));
446574a04fSJeremy L Thompson   CeedCheck(!strcmp(resource_root, "/gpu/cuda/ref"), ceed, CEED_ERROR_BACKEND, "Cuda backend cannot use resource: %s", resource);
452b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&resource_root));
462b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetDeterministic(ceed, true));
470d0321e0SJeremy L Thompson 
480d0321e0SJeremy L Thompson   Ceed_Cuda *data;
492b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
502b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetData(ceed, data));
51*eb7e6cafSJeremy 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));
572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Cuda));
582b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate", CeedQFunctionCreate_Cuda));
592b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate", CeedQFunctionContextCreate_Cuda));
602b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Cuda));
612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Cuda));
620d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
630d0321e0SJeremy L Thompson }
640d0321e0SJeremy L Thompson 
650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
660d0321e0SJeremy L Thompson // Backend Register
670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
68*eb7e6cafSJeremy L Thompson CEED_INTERN int CeedRegister_Cuda(void) { return CeedRegister("/gpu/cuda/ref", CeedInit_Cuda_ref, 40); }
692a86cc9dSSebastian Grimberg 
700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
71