xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 //------------------------------------------------------------------------------
CeedQFunctionApply_Ref(CeedQFunction qf,CeedInt Q,CeedVector * U,CeedVector * V)172b730f8bSJeremy L Thompson static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
18441428dfSJeremy L Thompson   void              *ctx_data = NULL;
19d1d35e2fSjeremylt   CeedInt            num_in, num_out;
20ad70ee2cSJeremy L Thompson   CeedQFunctionUser  f = NULL;
21ad70ee2cSJeremy L Thompson   CeedQFunction_Ref *impl;
22ad70ee2cSJeremy L Thompson 
23ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
24ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
25ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
27aedaa0e5Sjeremylt 
282b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
292b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]));
30aedaa0e5Sjeremylt   }
312b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
322b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
33aedaa0e5Sjeremylt   }
34aedaa0e5Sjeremylt 
352b730f8bSJeremy L Thompson   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
36aedaa0e5Sjeremylt 
372b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
382b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
39aedaa0e5Sjeremylt   }
402b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
412b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
42aedaa0e5Sjeremylt   }
432b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data));
44e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4521617c04Sjeremylt }
4621617c04Sjeremylt 
47f10650afSjeremylt //------------------------------------------------------------------------------
48f10650afSjeremylt // QFunction Destroy
49f10650afSjeremylt //------------------------------------------------------------------------------
CeedQFunctionDestroy_Ref(CeedQFunction qf)5021617c04Sjeremylt static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
51aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
52aedaa0e5Sjeremylt 
53ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
542b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->inputs));
552b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->outputs));
562b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
57e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5821617c04Sjeremylt }
5921617c04Sjeremylt 
60f10650afSjeremylt //------------------------------------------------------------------------------
61f10650afSjeremylt // QFunction Create
62f10650afSjeremylt //------------------------------------------------------------------------------
CeedQFunctionCreate_Ref(CeedQFunction qf)6321617c04Sjeremylt int CeedQFunctionCreate_Ref(CeedQFunction qf) {
64fe2413ffSjeremylt   Ceed               ceed;
65aedaa0e5Sjeremylt   CeedQFunction_Ref *impl;
66ad70ee2cSJeremy L Thompson 
67ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
682b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
692b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
702b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
712b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, impl));
722b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Ref));
732b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Ref));
749bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
75e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7621617c04Sjeremylt }
772a86cc9dSSebastian Grimberg 
78f10650afSjeremylt //------------------------------------------------------------------------------
79