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