xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision 441428df2c2036d88c450263ab42e401d8838e73)
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 
31*441428dfSJeremy L Thompson   void *ctx_data = NULL;
32*441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data);
33e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
34aedaa0e5Sjeremylt 
35692c2638Sjeremylt   CeedQFunctionUser f = NULL;
36e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChkBackend(ierr);
37aedaa0e5Sjeremylt 
38d1d35e2fSjeremylt   CeedInt num_in, num_out;
39d1d35e2fSjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &num_in, &num_out); CeedChkBackend(ierr);
40aedaa0e5Sjeremylt 
41d1d35e2fSjeremylt   for (int i = 0; i<num_in; i++) {
42aedaa0e5Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
43e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
44aedaa0e5Sjeremylt   }
45d1d35e2fSjeremylt   for (int i = 0; i<num_out; i++) {
469c774eddSJeremy L Thompson     ierr = CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]);
47e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
48aedaa0e5Sjeremylt   }
49aedaa0e5Sjeremylt 
50*441428dfSJeremy L Thompson   ierr = f(ctx_data, Q, impl->inputs, impl->outputs); CeedChkBackend(ierr);
51aedaa0e5Sjeremylt 
52d1d35e2fSjeremylt   for (int i = 0; i<num_in; i++) {
53e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChkBackend(ierr);
54aedaa0e5Sjeremylt   }
55d1d35e2fSjeremylt   for (int i = 0; i<num_out; i++) {
56e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChkBackend(ierr);
57aedaa0e5Sjeremylt   }
58*441428dfSJeremy L Thompson   ierr = CeedQFunctionRestoreContextData(qf, &ctx_data); CeedChkBackend(ierr);
59aedaa0e5Sjeremylt 
60e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6121617c04Sjeremylt }
6221617c04Sjeremylt 
63f10650afSjeremylt //------------------------------------------------------------------------------
64f10650afSjeremylt // QFunction Destroy
65f10650afSjeremylt //------------------------------------------------------------------------------
6621617c04Sjeremylt static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
67aedaa0e5Sjeremylt   int ierr;
68aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
69e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr);
70aedaa0e5Sjeremylt 
71e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->inputs); CeedChkBackend(ierr);
72e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->outputs); CeedChkBackend(ierr);
73e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
74aedaa0e5Sjeremylt 
75e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7621617c04Sjeremylt }
7721617c04Sjeremylt 
78f10650afSjeremylt //------------------------------------------------------------------------------
79f10650afSjeremylt // QFunction Create
80f10650afSjeremylt //------------------------------------------------------------------------------
8121617c04Sjeremylt int CeedQFunctionCreate_Ref(CeedQFunction qf) {
82fe2413ffSjeremylt   int ierr;
83fe2413ffSjeremylt   Ceed ceed;
84e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
85fe2413ffSjeremylt 
86aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
87e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
88bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->inputs); CeedChkBackend(ierr);
89bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->outputs); CeedChkBackend(ierr);
90e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, impl); CeedChkBackend(ierr);
91aedaa0e5Sjeremylt 
92fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
93e15f9bd0SJeremy L Thompson                                 CeedQFunctionApply_Ref); CeedChkBackend(ierr);
94fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
95e15f9bd0SJeremy L Thompson                                 CeedQFunctionDestroy_Ref); CeedChkBackend(ierr);
96aedaa0e5Sjeremylt 
97e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9821617c04Sjeremylt }
99f10650afSjeremylt //------------------------------------------------------------------------------
100