xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision 9df49d7ef0a77c7a3baec2427d8a7274681409b6)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include <ceed/ceed.h>
18 #include <ceed/backend.h>
19 #include <stddef.h>
20 #include "ceed-ref.h"
21 
22 //------------------------------------------------------------------------------
23 // QFunction Apply
24 //------------------------------------------------------------------------------
25 static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q,
26                                   CeedVector *U, CeedVector *V) {
27   int ierr;
28   CeedQFunction_Ref *impl;
29   ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr);
30 
31   CeedQFunctionContext ctx;
32   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChkBackend(ierr);
33   void *ctxData = NULL;
34   if (ctx) {
35     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctxData);
36     CeedChkBackend(ierr);
37   }
38 
39   CeedQFunctionUser f = NULL;
40   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChkBackend(ierr);
41 
42   CeedInt num_in, num_out;
43   ierr = CeedQFunctionGetNumArgs(qf, &num_in, &num_out); CeedChkBackend(ierr);
44 
45   for (int i = 0; i<num_in; i++) {
46     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
47     CeedChkBackend(ierr);
48   }
49   for (int i = 0; i<num_out; i++) {
50     ierr = CeedVectorGetArray(V[i], CEED_MEM_HOST, &impl->outputs[i]);
51     CeedChkBackend(ierr);
52   }
53 
54   ierr = f(ctxData, Q, impl->inputs, impl->outputs); CeedChkBackend(ierr);
55 
56   for (int i = 0; i<num_in; i++) {
57     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChkBackend(ierr);
58   }
59   for (int i = 0; i<num_out; i++) {
60     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChkBackend(ierr);
61   }
62   if (ctx) {
63     ierr = CeedQFunctionContextRestoreData(ctx, &ctxData); CeedChkBackend(ierr);
64   }
65 
66   return CEED_ERROR_SUCCESS;
67 }
68 
69 //------------------------------------------------------------------------------
70 // QFunction Destroy
71 //------------------------------------------------------------------------------
72 static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
73   int ierr;
74   CeedQFunction_Ref *impl;
75   ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr);
76 
77   ierr = CeedFree(&impl->inputs); CeedChkBackend(ierr);
78   ierr = CeedFree(&impl->outputs); CeedChkBackend(ierr);
79   ierr = CeedFree(&impl); CeedChkBackend(ierr);
80 
81   return CEED_ERROR_SUCCESS;
82 }
83 
84 //------------------------------------------------------------------------------
85 // QFunction Create
86 //------------------------------------------------------------------------------
87 int CeedQFunctionCreate_Ref(CeedQFunction qf) {
88   int ierr;
89   Ceed ceed;
90   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
91 
92   CeedQFunction_Ref *impl;
93   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
94   ierr = CeedCalloc(16, &impl->inputs); CeedChkBackend(ierr);
95   ierr = CeedCalloc(16, &impl->outputs); CeedChkBackend(ierr);
96   ierr = CeedQFunctionSetData(qf, impl); CeedChkBackend(ierr);
97 
98   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
99                                 CeedQFunctionApply_Ref); CeedChkBackend(ierr);
100   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
101                                 CeedQFunctionDestroy_Ref); CeedChkBackend(ierr);
102 
103   return CEED_ERROR_SUCCESS;
104 }
105 //------------------------------------------------------------------------------
106