xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-qfunction.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3241a4b83SYohann //
4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5241a4b83SYohann //
6*3d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7241a4b83SYohann 
8ec3da8bcSJed Brown #include <ceed/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <cuda_runtime.h>
11241a4b83SYohann #include <stdio.h>
123d576824SJeremy L Thompson #include <string.h>
13241a4b83SYohann #include "ceed-cuda-gen.h"
14241a4b83SYohann 
15ab213215SJeremy L Thompson //------------------------------------------------------------------------------
16ab213215SJeremy L Thompson // Apply QFunction
17ab213215SJeremy L Thompson //------------------------------------------------------------------------------
18241a4b83SYohann static int CeedQFunctionApply_Cuda_gen(CeedQFunction qf, CeedInt Q,
19241a4b83SYohann                                        CeedVector *U, CeedVector *V) {
20241a4b83SYohann   int ierr;
21241a4b83SYohann   Ceed ceed;
22e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
23e15f9bd0SJeremy L Thompson   return CeedError(ceed, CEED_ERROR_BACKEND,
24e15f9bd0SJeremy L Thompson                    "Backend does not implement QFunctionApply");
25241a4b83SYohann }
26241a4b83SYohann 
27ab213215SJeremy L Thompson //------------------------------------------------------------------------------
28ab213215SJeremy L Thompson // Destroy QFunction
29ab213215SJeremy L Thompson //------------------------------------------------------------------------------
30241a4b83SYohann static int CeedQFunctionDestroy_Cuda_gen(CeedQFunction qf) {
31241a4b83SYohann   int ierr;
32241a4b83SYohann   CeedQFunction_Cuda_gen *data;
33e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr);
34241a4b83SYohann   Ceed ceed;
35e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
36241a4b83SYohann   ierr = cudaFree(data->d_c); CeedChk_Cu(ceed, ierr);
37e15f9bd0SJeremy L Thompson   ierr = CeedFree(&data->qFunctionSource); CeedChkBackend(ierr);
38e15f9bd0SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
39e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
40241a4b83SYohann }
41241a4b83SYohann 
42ab213215SJeremy L Thompson //------------------------------------------------------------------------------
43ab213215SJeremy L Thompson // Create QFunction
44ab213215SJeremy L Thompson //------------------------------------------------------------------------------
45241a4b83SYohann int CeedQFunctionCreate_Cuda_gen(CeedQFunction qf) {
46241a4b83SYohann   int ierr;
47241a4b83SYohann   Ceed ceed;
48241a4b83SYohann   CeedQFunctionGetCeed(qf, &ceed);
49241a4b83SYohann   CeedQFunction_Cuda_gen *data;
50e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &data); CeedChkBackend(ierr);
51e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, data); CeedChkBackend(ierr);
52241a4b83SYohann 
533d3250a0SJeremy L Thompson   // Read QFunction source
5443e1b16fSJeremy L Thompson   ierr = CeedQFunctionGetKernelName(qf, &data->qFunctionName);
5543e1b16fSJeremy L Thompson   CeedChkBackend(ierr);
5646dc0734SJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading QFunction User Source -----\n");
573d3250a0SJeremy L Thompson   ierr = CeedQFunctionLoadSourceToBuffer(qf, &data->qFunctionSource);
583d3250a0SJeremy L Thompson   CeedChkBackend(ierr);
5946dc0734SJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading QFunction User Source Complete! -----\n");
603d3250a0SJeremy L Thompson   if (!data->qFunctionSource)
613d3250a0SJeremy L Thompson     // LCOV_EXCL_START
623d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
633d3250a0SJeremy L Thompson                      "/gpu/cuda/gen backend requires QFunction source code file");
643d3250a0SJeremy L Thompson   // LCOV_EXCL_STOP
65241a4b83SYohann 
66241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
67e15f9bd0SJeremy L Thompson                                 CeedQFunctionApply_Cuda_gen); CeedChkBackend(ierr);
68241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
69e15f9bd0SJeremy L Thompson                                 CeedQFunctionDestroy_Cuda_gen); CeedChkBackend(ierr);
70e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
71241a4b83SYohann }
72ab213215SJeremy L Thompson //------------------------------------------------------------------------------
73