xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision d416dc2b8eb8ab8cb4fa3546f1e63962299dc06a)
1 // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed.h>
9 #include <ceed/backend.h>
10 #include <stddef.h>
11 
12 #include "ceed-ref.h"
13 
14 //------------------------------------------------------------------------------
15 // QFunction Apply
16 //------------------------------------------------------------------------------
17 static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
18   void              *ctx_data = NULL;
19   CeedInt            num_in, num_out;
20   CeedQFunctionUser  f = NULL;
21   CeedQFunction_Ref *impl;
22 
23   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
24   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
25   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
26   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
27 
28   for (CeedInt i = 0; i < num_in; i++) {
29     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]));
30   }
31   for (CeedInt i = 0; i < num_out; i++) {
32     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
33   }
34 
35   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
36 
37   for (CeedInt i = 0; i < num_in; i++) {
38     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
39   }
40   for (CeedInt i = 0; i < num_out; i++) {
41     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
42   }
43   CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data));
44   return CEED_ERROR_SUCCESS;
45 }
46 
47 //------------------------------------------------------------------------------
48 // QFunction Destroy
49 //------------------------------------------------------------------------------
50 static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
51   CeedQFunction_Ref *impl;
52 
53   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
54   CeedCallBackend(CeedFree(&impl->inputs));
55   CeedCallBackend(CeedFree(&impl->outputs));
56   CeedCallBackend(CeedFree(&impl));
57   return CEED_ERROR_SUCCESS;
58 }
59 
60 //------------------------------------------------------------------------------
61 // QFunction Create
62 //------------------------------------------------------------------------------
63 int CeedQFunctionCreate_Ref(CeedQFunction qf) {
64   Ceed               ceed;
65   CeedQFunction_Ref *impl;
66 
67   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
68   CeedCallBackend(CeedCalloc(1, &impl));
69   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
70   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
71   CeedCallBackend(CeedQFunctionSetData(qf, impl));
72   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Ref));
73   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Ref));
74   CeedCallBackend(CeedDestroy(&ceed));
75   return CEED_ERROR_SUCCESS;
76 }
77 
78 //------------------------------------------------------------------------------
79