xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision 1f9221fe62e2635d1a8dba99f0dac116b51a901d)
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   void *ctx_data = NULL;
32   ierr = CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data);
33   CeedChkBackend(ierr);
34 
35   CeedQFunctionUser f = NULL;
36   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChkBackend(ierr);
37 
38   CeedInt num_in, num_out;
39   ierr = CeedQFunctionGetNumArgs(qf, &num_in, &num_out); CeedChkBackend(ierr);
40 
41   for (int i = 0; i<num_in; i++) {
42     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
43     CeedChkBackend(ierr);
44   }
45   for (int i = 0; i<num_out; i++) {
46     ierr = CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]);
47     CeedChkBackend(ierr);
48   }
49 
50   ierr = f(ctx_data, Q, impl->inputs, impl->outputs); CeedChkBackend(ierr);
51 
52   for (int i = 0; i<num_in; i++) {
53     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChkBackend(ierr);
54   }
55   for (int i = 0; i<num_out; i++) {
56     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChkBackend(ierr);
57   }
58   ierr = CeedQFunctionRestoreContextData(qf, &ctx_data); CeedChkBackend(ierr);
59 
60   return CEED_ERROR_SUCCESS;
61 }
62 
63 //------------------------------------------------------------------------------
64 // QFunction Destroy
65 //------------------------------------------------------------------------------
66 static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
67   int ierr;
68   CeedQFunction_Ref *impl;
69   ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr);
70 
71   ierr = CeedFree(&impl->inputs); CeedChkBackend(ierr);
72   ierr = CeedFree(&impl->outputs); CeedChkBackend(ierr);
73   ierr = CeedFree(&impl); CeedChkBackend(ierr);
74 
75   return CEED_ERROR_SUCCESS;
76 }
77 
78 //------------------------------------------------------------------------------
79 // QFunction Create
80 //------------------------------------------------------------------------------
81 int CeedQFunctionCreate_Ref(CeedQFunction qf) {
82   int ierr;
83   Ceed ceed;
84   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
85 
86   CeedQFunction_Ref *impl;
87   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
88   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->inputs); CeedChkBackend(ierr);
89   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->outputs); CeedChkBackend(ierr);
90   ierr = CeedQFunctionSetData(qf, impl); CeedChkBackend(ierr);
91 
92   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
93                                 CeedQFunctionApply_Ref); CeedChkBackend(ierr);
94   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
95                                 CeedQFunctionDestroy_Ref); CeedChkBackend(ierr);
96 
97   return CEED_ERROR_SUCCESS;
98 }
99 //------------------------------------------------------------------------------
100