xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-qfunction.c (revision c8c3fa7d27bffffddcff68a8a1d51314e0358a98)
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.h>
9 #include <ceed/backend.h>
10 #include <cuda_runtime.h>
11 
12 #include "../cuda/ceed-cuda-common.h"
13 #include "ceed-cuda-gen.h"
14 
15 //------------------------------------------------------------------------------
16 // Apply QFunction
17 //------------------------------------------------------------------------------
18 static int CeedQFunctionApply_Cuda_gen(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
19   Ceed ceed;
20   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
21   return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement QFunctionApply");
22 }
23 
24 //------------------------------------------------------------------------------
25 // Destroy QFunction
26 //------------------------------------------------------------------------------
27 static int CeedQFunctionDestroy_Cuda_gen(CeedQFunction qf) {
28   CeedQFunction_Cuda_gen *data;
29   CeedCallBackend(CeedQFunctionGetData(qf, &data));
30   Ceed ceed;
31   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
32   CeedCallCuda(ceed, cudaFree(data->d_c));
33   CeedCallBackend(CeedFree(&data->q_function_source));
34   CeedCallBackend(CeedFree(&data));
35   return CEED_ERROR_SUCCESS;
36 }
37 
38 //------------------------------------------------------------------------------
39 // Create QFunction
40 //------------------------------------------------------------------------------
41 int CeedQFunctionCreate_Cuda_gen(CeedQFunction qf) {
42   Ceed ceed;
43   CeedQFunctionGetCeed(qf, &ceed);
44   CeedQFunction_Cuda_gen *data;
45   CeedCallBackend(CeedCalloc(1, &data));
46   CeedCallBackend(CeedQFunctionSetData(qf, data));
47 
48   // Read QFunction source
49   CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->q_function_name));
50   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading QFunction User Source -----\n");
51   CeedCallBackend(CeedQFunctionLoadSourceToBuffer(qf, &data->q_function_source));
52   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading QFunction User Source Complete! -----\n");
53   CeedCheck(data->q_function_source, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/cuda/gen backend requires QFunction source code file");
54 
55   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Cuda_gen));
56   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Cuda_gen));
57   return CEED_ERROR_SUCCESS;
58 }
59 
60 //------------------------------------------------------------------------------
61