xref: /libCEED/backends/cuda-ref/ceed-cuda-ref.c (revision b2e3f8ecbfa285d0f4ffde9b24c57cc13f0319fb)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include "ceed-cuda-ref.h"
9 
10 #include <ceed.h>
11 #include <ceed/backend.h>
12 #include <cublas_v2.h>
13 #include <stdbool.h>
14 #include <string.h>
15 
16 #include "../cuda/ceed-cuda-common.h"
17 
18 //------------------------------------------------------------------------------
19 // CUDA preferred MemType
20 //------------------------------------------------------------------------------
21 static int CeedGetPreferredMemType_Cuda(CeedMemType *mem_type) {
22   *mem_type = CEED_MEM_DEVICE;
23   return CEED_ERROR_SUCCESS;
24 }
25 
26 //------------------------------------------------------------------------------
27 // Get CUBLAS handle
28 //------------------------------------------------------------------------------
29 int CeedCudaGetCublasHandle(Ceed ceed, cublasHandle_t *handle) {
30   Ceed_Cuda *data;
31   CeedCallBackend(CeedGetData(ceed, &data));
32 
33   if (!data->cublas_handle) CeedCallCublas(ceed, cublasCreate(&data->cublas_handle));
34   *handle = data->cublas_handle;
35   return CEED_ERROR_SUCCESS;
36 }
37 
38 //------------------------------------------------------------------------------
39 // Backend Init
40 //------------------------------------------------------------------------------
41 static int CeedInit_Cuda(const char *resource, Ceed ceed) {
42   char *resource_root;
43   CeedCallBackend(CeedCudaGetResourceRoot(ceed, resource, &resource_root));
44   if (strcmp(resource_root, "/gpu/cuda/ref")) {
45     // LCOV_EXCL_START
46     return CeedError(ceed, CEED_ERROR_BACKEND, "Cuda backend cannot use resource: %s", resource);
47     // LCOV_EXCL_STOP
48   }
49   CeedCallBackend(CeedFree(&resource_root));
50   CeedCallBackend(CeedSetDeterministic(ceed, true));
51 
52   Ceed_Cuda *data;
53   CeedCallBackend(CeedCalloc(1, &data));
54   CeedCallBackend(CeedSetData(ceed, data));
55   CeedCallBackend(CeedCudaInit(ceed, resource));
56 
57   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType", CeedGetPreferredMemType_Cuda));
58   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", CeedVectorCreate_Cuda));
59   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Cuda));
60   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Cuda));
61   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Cuda));
62   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreateBlocked", CeedElemRestrictionCreateBlocked_Cuda));
63   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate", CeedQFunctionCreate_Cuda));
64   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate", CeedQFunctionContextCreate_Cuda));
65   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Cuda));
66   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Cuda));
67   return CEED_ERROR_SUCCESS;
68 }
69 
70 //------------------------------------------------------------------------------
71 // Backend Register
72 //------------------------------------------------------------------------------
73 CEED_INTERN int CeedRegister_Cuda(void) { return CeedRegister("/gpu/cuda/ref", CeedInit_Cuda, 40); }
74 //------------------------------------------------------------------------------
75