xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunction.c (revision 2a86cc9d4dbfce2964c7e8927a1e6db8d19a41fc)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
321617c04Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
521617c04Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
721617c04Sjeremylt 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <stddef.h>
112b730f8bSJeremy L Thompson 
1221617c04Sjeremylt #include "ceed-ref.h"
1321617c04Sjeremylt 
14f10650afSjeremylt //------------------------------------------------------------------------------
15f10650afSjeremylt // QFunction Apply
16f10650afSjeremylt //------------------------------------------------------------------------------
172b730f8bSJeremy L Thompson static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
18aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
192b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
20aedaa0e5Sjeremylt 
21441428dfSJeremy L Thompson   void *ctx_data = NULL;
222b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
23aedaa0e5Sjeremylt 
24692c2638Sjeremylt   CeedQFunctionUser f = NULL;
252b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
26aedaa0e5Sjeremylt 
27d1d35e2fSjeremylt   CeedInt num_in, num_out;
282b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
29aedaa0e5Sjeremylt 
302b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
312b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]));
32aedaa0e5Sjeremylt   }
332b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
342b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
35aedaa0e5Sjeremylt   }
36aedaa0e5Sjeremylt 
372b730f8bSJeremy L Thompson   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
38aedaa0e5Sjeremylt 
392b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
402b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
41aedaa0e5Sjeremylt   }
422b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
432b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
44aedaa0e5Sjeremylt   }
452b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data));
46aedaa0e5Sjeremylt 
47e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4821617c04Sjeremylt }
4921617c04Sjeremylt 
50f10650afSjeremylt //------------------------------------------------------------------------------
51f10650afSjeremylt // QFunction Destroy
52f10650afSjeremylt //------------------------------------------------------------------------------
5321617c04Sjeremylt static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
54aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
552b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
56aedaa0e5Sjeremylt 
572b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->inputs));
582b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->outputs));
592b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
60aedaa0e5Sjeremylt 
61e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6221617c04Sjeremylt }
6321617c04Sjeremylt 
64f10650afSjeremylt //------------------------------------------------------------------------------
65f10650afSjeremylt // QFunction Create
66f10650afSjeremylt //------------------------------------------------------------------------------
6721617c04Sjeremylt int CeedQFunctionCreate_Ref(CeedQFunction qf) {
68fe2413ffSjeremylt   Ceed ceed;
692b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
70fe2413ffSjeremylt 
71aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
722b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
732b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
742b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
752b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, impl));
76aedaa0e5Sjeremylt 
772b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Ref));
782b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Ref));
79aedaa0e5Sjeremylt 
80e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8121617c04Sjeremylt }
82*2a86cc9dSSebastian Grimberg 
83f10650afSjeremylt //------------------------------------------------------------------------------
84