xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision d1d35e2f02dc969aee8debf3fd943dd784aa847a)
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 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy 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;
29e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr);
30aedaa0e5Sjeremylt 
31777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
32e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChkBackend(ierr);
33777ff853SJeremy L Thompson   void *ctxData = NULL;
34777ff853SJeremy L Thompson   if (ctx) {
35777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctxData);
36e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
37777ff853SJeremy L Thompson   }
38aedaa0e5Sjeremylt 
39692c2638Sjeremylt   CeedQFunctionUser f = NULL;
40e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChkBackend(ierr);
41aedaa0e5Sjeremylt 
42*d1d35e2fSjeremylt   CeedInt num_in, num_out;
43*d1d35e2fSjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &num_in, &num_out); CeedChkBackend(ierr);
44aedaa0e5Sjeremylt 
45*d1d35e2fSjeremylt   for (int i = 0; i<num_in; i++) {
46aedaa0e5Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
47e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
48aedaa0e5Sjeremylt   }
49*d1d35e2fSjeremylt   for (int i = 0; i<num_out; i++) {
50aedaa0e5Sjeremylt     ierr = CeedVectorGetArray(V[i], CEED_MEM_HOST, &impl->outputs[i]);
51e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
52aedaa0e5Sjeremylt   }
53aedaa0e5Sjeremylt 
54e15f9bd0SJeremy L Thompson   ierr = f(ctxData, Q, impl->inputs, impl->outputs); CeedChkBackend(ierr);
55aedaa0e5Sjeremylt 
56*d1d35e2fSjeremylt   for (int i = 0; i<num_in; i++) {
57e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChkBackend(ierr);
58aedaa0e5Sjeremylt   }
59*d1d35e2fSjeremylt   for (int i = 0; i<num_out; i++) {
60e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChkBackend(ierr);
61aedaa0e5Sjeremylt   }
62777ff853SJeremy L Thompson   if (ctx) {
63e15f9bd0SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(ctx, &ctxData); CeedChkBackend(ierr);
64777ff853SJeremy L Thompson   }
65aedaa0e5Sjeremylt 
66e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6721617c04Sjeremylt }
6821617c04Sjeremylt 
69f10650afSjeremylt //------------------------------------------------------------------------------
70f10650afSjeremylt // QFunction Destroy
71f10650afSjeremylt //------------------------------------------------------------------------------
7221617c04Sjeremylt static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
73aedaa0e5Sjeremylt   int ierr;
74aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
75e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr);
76aedaa0e5Sjeremylt 
77e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->inputs); CeedChkBackend(ierr);
78e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->outputs); CeedChkBackend(ierr);
79e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
80aedaa0e5Sjeremylt 
81e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8221617c04Sjeremylt }
8321617c04Sjeremylt 
84f10650afSjeremylt //------------------------------------------------------------------------------
85f10650afSjeremylt // QFunction Create
86f10650afSjeremylt //------------------------------------------------------------------------------
8721617c04Sjeremylt int CeedQFunctionCreate_Ref(CeedQFunction qf) {
88fe2413ffSjeremylt   int ierr;
89fe2413ffSjeremylt   Ceed ceed;
90e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
91fe2413ffSjeremylt 
92aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
93e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
94e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(16, &impl->inputs); CeedChkBackend(ierr);
95e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(16, &impl->outputs); CeedChkBackend(ierr);
96e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, impl); CeedChkBackend(ierr);
97aedaa0e5Sjeremylt 
98fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
99e15f9bd0SJeremy L Thompson                                 CeedQFunctionApply_Ref); CeedChkBackend(ierr);
100fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
101e15f9bd0SJeremy L Thompson                                 CeedQFunctionDestroy_Ref); CeedChkBackend(ierr);
102aedaa0e5Sjeremylt 
103e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10421617c04Sjeremylt }
105f10650afSjeremylt //------------------------------------------------------------------------------
106