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