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