xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-qfunction.c (revision 46dc07349c44057b9efae59cd6a2d41f419237bd)
1241a4b83SYohann // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2241a4b83SYohann // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3241a4b83SYohann // All Rights reserved. See files LICENSE and NOTICE for details.
4241a4b83SYohann //
5241a4b83SYohann // This file is part of CEED, a collection of benchmarks, miniapps, software
6241a4b83SYohann // libraries and APIs for efficient high-order finite element and spectral
7241a4b83SYohann // element discretizations for exascale applications. For more information and
8241a4b83SYohann // source code availability see http://github.com/ceed.
9241a4b83SYohann //
10241a4b83SYohann // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11241a4b83SYohann // a collaborative effort of two U.S. Department of Energy organizations (Office
12241a4b83SYohann // of Science and the National Nuclear Security Administration) responsible for
13241a4b83SYohann // the planning and preparation of a capable exascale ecosystem, including
14241a4b83SYohann // software, applications, hardware, advanced system engineering and early
15241a4b83SYohann // testbed platforms, in support of the nation's exascale computing imperative.
16241a4b83SYohann 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <cuda_runtime.h>
20241a4b83SYohann #include <stdio.h>
213d576824SJeremy L Thompson #include <string.h>
22241a4b83SYohann #include "ceed-cuda-gen.h"
23241a4b83SYohann 
24ab213215SJeremy L Thompson //------------------------------------------------------------------------------
25ab213215SJeremy L Thompson // Apply QFunction
26ab213215SJeremy L Thompson //------------------------------------------------------------------------------
27241a4b83SYohann static int CeedQFunctionApply_Cuda_gen(CeedQFunction qf, CeedInt Q,
28241a4b83SYohann                                        CeedVector *U, CeedVector *V) {
29241a4b83SYohann   int ierr;
30241a4b83SYohann   Ceed ceed;
31e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
32e15f9bd0SJeremy L Thompson   return CeedError(ceed, CEED_ERROR_BACKEND,
33e15f9bd0SJeremy L Thompson                    "Backend does not implement QFunctionApply");
34241a4b83SYohann }
35241a4b83SYohann 
36ab213215SJeremy L Thompson //------------------------------------------------------------------------------
37ab213215SJeremy L Thompson // Destroy QFunction
38ab213215SJeremy L Thompson //------------------------------------------------------------------------------
39241a4b83SYohann static int CeedQFunctionDestroy_Cuda_gen(CeedQFunction qf) {
40241a4b83SYohann   int ierr;
41241a4b83SYohann   CeedQFunction_Cuda_gen *data;
42e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr);
43241a4b83SYohann   Ceed ceed;
44e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
45241a4b83SYohann   ierr = cudaFree(data->d_c); CeedChk_Cu(ceed, ierr);
46e15f9bd0SJeremy L Thompson   ierr = CeedFree(&data->qFunctionSource); CeedChkBackend(ierr);
47e15f9bd0SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
48e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
49241a4b83SYohann }
50241a4b83SYohann 
51ab213215SJeremy L Thompson //------------------------------------------------------------------------------
52ab213215SJeremy L Thompson // Create QFunction
53ab213215SJeremy L Thompson //------------------------------------------------------------------------------
54241a4b83SYohann int CeedQFunctionCreate_Cuda_gen(CeedQFunction qf) {
55241a4b83SYohann   int ierr;
56241a4b83SYohann   Ceed ceed;
57241a4b83SYohann   CeedQFunctionGetCeed(qf, &ceed);
58241a4b83SYohann   CeedQFunction_Cuda_gen *data;
59e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &data); CeedChkBackend(ierr);
60e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, data); CeedChkBackend(ierr);
61241a4b83SYohann 
623d3250a0SJeremy L Thompson   // Read QFunction source
6343e1b16fSJeremy L Thompson   ierr = CeedQFunctionGetKernelName(qf, &data->qFunctionName);
6443e1b16fSJeremy L Thompson   CeedChkBackend(ierr);
65*46dc0734SJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading QFunction User Source -----\n");
663d3250a0SJeremy L Thompson   ierr = CeedQFunctionLoadSourceToBuffer(qf, &data->qFunctionSource);
673d3250a0SJeremy L Thompson   CeedChkBackend(ierr);
68*46dc0734SJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading QFunction User Source Complete! -----\n");
693d3250a0SJeremy L Thompson   if (!data->qFunctionSource)
703d3250a0SJeremy L Thompson     // LCOV_EXCL_START
713d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
723d3250a0SJeremy L Thompson                      "/gpu/cuda/gen backend requires QFunction source code file");
733d3250a0SJeremy L Thompson   // LCOV_EXCL_STOP
74241a4b83SYohann 
75241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
76e15f9bd0SJeremy L Thompson                                 CeedQFunctionApply_Cuda_gen); CeedChkBackend(ierr);
77241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
78e15f9bd0SJeremy L Thompson                                 CeedQFunctionDestroy_Cuda_gen); CeedChkBackend(ierr);
79e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80241a4b83SYohann }
81ab213215SJeremy L Thompson //------------------------------------------------------------------------------
82