xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunction.c (revision 3d576824e8d990e1f48c6609089904bee9170514)
121617c04Sjeremylt // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
221617c04Sjeremylt // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
321617c04Sjeremylt // All Rights reserved. See files LICENSE and NOTICE for details.
421617c04Sjeremylt //
521617c04Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
621617c04Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
721617c04Sjeremylt // element discretizations for exascale applications. For more information and
821617c04Sjeremylt // source code availability see http://github.com/ceed.
921617c04Sjeremylt //
1021617c04Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
1121617c04Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
1221617c04Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
1321617c04Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
1421617c04Sjeremylt // software, applications, hardware, advanced system engineering and early
1521617c04Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
1621617c04Sjeremylt 
17*3d576824SJeremy L Thompson #include <ceed.h>
18*3d576824SJeremy L Thompson #include <ceed-backend.h>
19*3d576824SJeremy L Thompson #include <stddef.h>
2021617c04Sjeremylt #include "ceed-ref.h"
2121617c04Sjeremylt 
22f10650afSjeremylt //------------------------------------------------------------------------------
23f10650afSjeremylt // QFunction Apply
24f10650afSjeremylt //------------------------------------------------------------------------------
256ddacda3Sjeremylt static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q,
26aedaa0e5Sjeremylt                                   CeedVector *U, CeedVector *V) {
2721617c04Sjeremylt   int ierr;
28aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
29777ff853SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &impl); CeedChk(ierr);
30aedaa0e5Sjeremylt 
31777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
324ce2993fSjeremylt   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
33777ff853SJeremy L Thompson   void *ctxData = NULL;
34777ff853SJeremy L Thompson   if (ctx) {
35777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctxData);
36777ff853SJeremy L Thompson     CeedChk(ierr);
37777ff853SJeremy L Thompson   }
38aedaa0e5Sjeremylt 
39692c2638Sjeremylt   CeedQFunctionUser f = NULL;
40d4f68153Sjeremylt   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChk(ierr);
41aedaa0e5Sjeremylt 
42aedaa0e5Sjeremylt   CeedInt nIn, nOut;
43aedaa0e5Sjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &nIn, &nOut); CeedChk(ierr);
44aedaa0e5Sjeremylt 
45aedaa0e5Sjeremylt   for (int i = 0; i<nIn; i++) {
46aedaa0e5Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
47aedaa0e5Sjeremylt     CeedChk(ierr);
48aedaa0e5Sjeremylt   }
49aedaa0e5Sjeremylt   for (int i = 0; i<nOut; i++) {
50aedaa0e5Sjeremylt     ierr = CeedVectorGetArray(V[i], CEED_MEM_HOST, &impl->outputs[i]);
51aedaa0e5Sjeremylt     CeedChk(ierr);
52aedaa0e5Sjeremylt   }
53aedaa0e5Sjeremylt 
54777ff853SJeremy L Thompson   ierr = f(ctxData, Q, impl->inputs, impl->outputs); CeedChk(ierr);
55aedaa0e5Sjeremylt 
56aedaa0e5Sjeremylt   for (int i = 0; i<nIn; i++) {
57aedaa0e5Sjeremylt     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChk(ierr);
58aedaa0e5Sjeremylt   }
59aedaa0e5Sjeremylt   for (int i = 0; i<nOut; i++) {
60aedaa0e5Sjeremylt     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChk(ierr);
61aedaa0e5Sjeremylt   }
62777ff853SJeremy L Thompson   if (ctx) {
63777ff853SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(ctx, &ctxData); CeedChk(ierr);
64777ff853SJeremy L Thompson   }
65aedaa0e5Sjeremylt 
6621617c04Sjeremylt   return 0;
6721617c04Sjeremylt }
6821617c04Sjeremylt 
69f10650afSjeremylt //------------------------------------------------------------------------------
70f10650afSjeremylt // QFunction Destroy
71f10650afSjeremylt //------------------------------------------------------------------------------
7221617c04Sjeremylt static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
73aedaa0e5Sjeremylt   int ierr;
74aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
75777ff853SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &impl); CeedChk(ierr);
76aedaa0e5Sjeremylt 
77aedaa0e5Sjeremylt   ierr = CeedFree(&impl->inputs); CeedChk(ierr);
78aedaa0e5Sjeremylt   ierr = CeedFree(&impl->outputs); CeedChk(ierr);
79aedaa0e5Sjeremylt   ierr = CeedFree(&impl); CeedChk(ierr);
80aedaa0e5Sjeremylt 
8121617c04Sjeremylt   return 0;
8221617c04Sjeremylt }
8321617c04Sjeremylt 
84f10650afSjeremylt //------------------------------------------------------------------------------
85f10650afSjeremylt // QFunction Create
86f10650afSjeremylt //------------------------------------------------------------------------------
8721617c04Sjeremylt int CeedQFunctionCreate_Ref(CeedQFunction qf) {
88fe2413ffSjeremylt   int ierr;
89fe2413ffSjeremylt   Ceed ceed;
90fe2413ffSjeremylt   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
91fe2413ffSjeremylt 
92aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
93aedaa0e5Sjeremylt   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
94aedaa0e5Sjeremylt   ierr = CeedCalloc(16, &impl->inputs); CeedChk(ierr);
95aedaa0e5Sjeremylt   ierr = CeedCalloc(16, &impl->outputs); CeedChk(ierr);
96777ff853SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, impl); CeedChk(ierr);
97aedaa0e5Sjeremylt 
98fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
99fe2413ffSjeremylt                                 CeedQFunctionApply_Ref); CeedChk(ierr);
100fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
101fe2413ffSjeremylt                                 CeedQFunctionDestroy_Ref); CeedChk(ierr);
102aedaa0e5Sjeremylt 
10321617c04Sjeremylt   return 0;
10421617c04Sjeremylt }
105f10650afSjeremylt //------------------------------------------------------------------------------
106