xref: /libCEED/backends/ref/ceed-ref-qfunction.c (revision f10650af6497af6d0949b45f2eee824400fc9b71)
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-ref.h"
18 
19 //------------------------------------------------------------------------------
20 // QFunction Apply
21 //------------------------------------------------------------------------------
22 static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q,
23                                   CeedVector *U, CeedVector *V) {
24   int ierr;
25   CeedQFunction_Ref *impl;
26   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChk(ierr);
27 
28   void *ctx;
29   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
30 
31   CeedQFunctionUser f = NULL;
32   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChk(ierr);
33 
34   CeedInt nIn, nOut;
35   ierr = CeedQFunctionGetNumArgs(qf, &nIn, &nOut); CeedChk(ierr);
36 
37   for (int i = 0; i<nIn; i++) {
38     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
39     CeedChk(ierr);
40   }
41   for (int i = 0; i<nOut; i++) {
42     ierr = CeedVectorGetArray(V[i], CEED_MEM_HOST, &impl->outputs[i]);
43     CeedChk(ierr);
44   }
45 
46   ierr = f(ctx, Q, impl->inputs, impl->outputs); CeedChk(ierr);
47 
48   for (int i = 0; i<nIn; i++) {
49     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChk(ierr);
50   }
51   for (int i = 0; i<nOut; i++) {
52     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChk(ierr);
53   }
54 
55   return 0;
56 }
57 
58 //------------------------------------------------------------------------------
59 // QFunction Destroy
60 //------------------------------------------------------------------------------
61 static int CeedQFunctionDestroy_Ref(CeedQFunction qf) {
62   int ierr;
63   CeedQFunction_Ref *impl;
64   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChk(ierr);
65 
66   ierr = CeedFree(&impl->inputs); CeedChk(ierr);
67   ierr = CeedFree(&impl->outputs); CeedChk(ierr);
68   ierr = CeedFree(&impl); CeedChk(ierr);
69 
70   return 0;
71 }
72 
73 //------------------------------------------------------------------------------
74 // QFunction Create
75 //------------------------------------------------------------------------------
76 int CeedQFunctionCreate_Ref(CeedQFunction qf) {
77   int ierr;
78   Ceed ceed;
79   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
80 
81   CeedQFunction_Ref *impl;
82   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
83   ierr = CeedCalloc(16, &impl->inputs); CeedChk(ierr);
84   ierr = CeedCalloc(16, &impl->outputs); CeedChk(ierr);
85   ierr = CeedQFunctionSetData(qf, (void *)&impl); CeedChk(ierr);
86 
87   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
88                                 CeedQFunctionApply_Ref); CeedChk(ierr);
89   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
90                                 CeedQFunctionDestroy_Ref); CeedChk(ierr);
91 
92   return 0;
93 }
94 //------------------------------------------------------------------------------
95