xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision 709403c1d8a3197f71934fa05519eedc9835a633)
1 // Copyright (c) 2017-2022, 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   CeedQFunction_Ref *impl;
19   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
20 
21   void *ctx_data = NULL;
22   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
23 
24   CeedQFunctionUser f = NULL;
25   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
26 
27   CeedInt num_in, num_out;
28   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
29 
30   for (CeedInt i = 0; i < num_in; i++) {
31     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]));
32   }
33   for (CeedInt i = 0; i < num_out; i++) {
34     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
35   }
36 
37   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
38 
39   for (CeedInt i = 0; i < num_in; i++) {
40     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
41   }
42   for (CeedInt i = 0; i < num_out; i++) {
43     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
44   }
45   CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data));
46 
47   return CEED_ERROR_SUCCESS;
48 }
49 
50 //------------------------------------------------------------------------------
51 // QFunction Destroy
52 //------------------------------------------------------------------------------
53 static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
54   CeedQFunction_Ref *impl;
55   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
56 
57   CeedCallBackend(CeedFree(&impl->inputs));
58   CeedCallBackend(CeedFree(&impl->outputs));
59   CeedCallBackend(CeedFree(&impl));
60 
61   return CEED_ERROR_SUCCESS;
62 }
63 
64 //------------------------------------------------------------------------------
65 // QFunction Create
66 //------------------------------------------------------------------------------
67 int CeedQFunctionCreate_Ref(CeedQFunction qf) {
68   Ceed ceed;
69   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
70 
71   CeedQFunction_Ref *impl;
72   CeedCallBackend(CeedCalloc(1, &impl));
73   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
74   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
75   CeedCallBackend(CeedQFunctionSetData(qf, impl));
76 
77   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Ref));
78   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Ref));
79 
80   return CEED_ERROR_SUCCESS;
81 }
82 
83 //------------------------------------------------------------------------------
84