xref: /libCEED/backends/ref/ceed-ref-operator.c (revision a2b73c81b297ab7cb43a3fb34592f4ddecd41176)
121617c04Sjeremylt // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
221617c04Sjeremylt // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
321617c04Sjeremylt // All Rights reserved. See files LICENSE and NOTICE for details.
421617c04Sjeremylt //
521617c04Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
621617c04Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
721617c04Sjeremylt // element discretizations for exascale applications. For more information and
821617c04Sjeremylt // source code availability see http://github.com/ceed.
921617c04Sjeremylt //
1021617c04Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
1121617c04Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
1221617c04Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
1321617c04Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
1421617c04Sjeremylt // software, applications, hardware, advanced system engineering and early
1521617c04Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
1621617c04Sjeremylt 
1721617c04Sjeremylt #include <ceed-impl.h>
1821617c04Sjeremylt #include <string.h>
1921617c04Sjeremylt #include "ceed-ref.h"
2021617c04Sjeremylt 
2121617c04Sjeremylt static int CeedOperatorDestroy_Ref(CeedOperator op) {
2221617c04Sjeremylt   CeedOperator_Ref *impl = op->data;
2321617c04Sjeremylt   int ierr;
2421617c04Sjeremylt 
25885ac19cSjeremylt   for (CeedInt i=0; i<impl->numein+impl->numeout; i++) {
26885ac19cSjeremylt     ierr = CeedVectorDestroy(&impl->evecs[i]); CeedChk(ierr);
27885ac19cSjeremylt   }
28885ac19cSjeremylt   ierr = CeedFree(&impl->evecs); CeedChk(ierr);
29885ac19cSjeremylt   ierr = CeedFree(&impl->edata); CeedChk(ierr);
30885ac19cSjeremylt 
31885ac19cSjeremylt   for (CeedInt i=0; i<impl->numqin+impl->numqout; i++) {
32885ac19cSjeremylt     ierr = CeedFree(&impl->qdata_alloc[i]); CeedChk(ierr);
33885ac19cSjeremylt   }
34885ac19cSjeremylt   ierr = CeedFree(&impl->qdata_alloc); CeedChk(ierr);
35885ac19cSjeremylt   ierr = CeedFree(&impl->qdata); CeedChk(ierr);
36885ac19cSjeremylt 
37885ac19cSjeremylt   ierr = CeedFree(&impl->indata); CeedChk(ierr);
38885ac19cSjeremylt   ierr = CeedFree(&impl->outdata); CeedChk(ierr);
39885ac19cSjeremylt 
4021617c04Sjeremylt   ierr = CeedFree(&op->data); CeedChk(ierr);
4121617c04Sjeremylt   return 0;
4221617c04Sjeremylt }
4321617c04Sjeremylt 
44885ac19cSjeremylt /*
45885ac19cSjeremylt   Setup infields or outfields
46885ac19cSjeremylt  */
47885ac19cSjeremylt static int CeedOperatorSetupFields_Ref(struct CeedQFunctionField qfields[16],
48885ac19cSjeremylt                                        struct CeedOperatorField ofields[16],
49885ac19cSjeremylt                                        CeedVector *evecs, CeedScalar **qdata,
50885ac19cSjeremylt                                        CeedScalar **qdata_alloc, CeedScalar **indata,
51135a076eSjeremylt                                        CeedInt starti, CeedInt startq,
52135a076eSjeremylt                                        CeedInt numfields, CeedInt Q) {
53135a076eSjeremylt   CeedInt dim, ierr, iq=startq, ncomp;
5421617c04Sjeremylt 
55885ac19cSjeremylt   // Loop over fields
56885ac19cSjeremylt   for (CeedInt i=0; i<numfields; i++) {
57885ac19cSjeremylt     CeedEvalMode emode = qfields[i].emode;
58135a076eSjeremylt 
59135a076eSjeremylt     if (emode != CEED_EVAL_WEIGHT) {
60135a076eSjeremylt       ierr = CeedElemRestrictionCreateVector(ofields[i].Erestrict, NULL, &evecs[i+starti]);
61135a076eSjeremylt       CeedChk(ierr);
62135a076eSjeremylt     }
63135a076eSjeremylt 
64885ac19cSjeremylt     switch(emode) {
65885ac19cSjeremylt     case CEED_EVAL_NONE:
66885ac19cSjeremylt       break; // No action
67885ac19cSjeremylt     case CEED_EVAL_INTERP:
68885ac19cSjeremylt       ncomp = qfields[i].ncomp;
69885ac19cSjeremylt       ierr = CeedMalloc(Q*ncomp, &qdata_alloc[iq]); CeedChk(ierr);
70885ac19cSjeremylt       qdata[i + starti] = qdata_alloc[iq];
71885ac19cSjeremylt       iq++;
72885ac19cSjeremylt       break;
73885ac19cSjeremylt     case CEED_EVAL_GRAD:
74885ac19cSjeremylt       ncomp = qfields[i].ncomp;
75885ac19cSjeremylt       dim = ofields[i].basis->dim;
76885ac19cSjeremylt       ierr = CeedMalloc(Q*ncomp*dim, &qdata_alloc[iq]); CeedChk(ierr);
77885ac19cSjeremylt       qdata[i + starti] = qdata_alloc[iq];
78885ac19cSjeremylt       iq++;
79885ac19cSjeremylt       break;
80885ac19cSjeremylt     case CEED_EVAL_WEIGHT: // Only on input fields
81885ac19cSjeremylt       ierr = CeedMalloc(Q, &qdata_alloc[iq]); CeedChk(ierr);
82d3181881Sjeremylt       ierr = CeedBasisApply(ofields[iq].basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT,
83885ac19cSjeremylt                             NULL, qdata_alloc[iq]); CeedChk(ierr);
84885ac19cSjeremylt       qdata[i] = qdata_alloc[iq];
85885ac19cSjeremylt       indata[i] = qdata[i];
86885ac19cSjeremylt       iq++;
87885ac19cSjeremylt       break;
88885ac19cSjeremylt     case CEED_EVAL_DIV:
89885ac19cSjeremylt       break; // Not implimented
90885ac19cSjeremylt     case CEED_EVAL_CURL:
91885ac19cSjeremylt       break; // Not implimented
9221617c04Sjeremylt     }
93885ac19cSjeremylt   }
9421617c04Sjeremylt   return 0;
9521617c04Sjeremylt }
9621617c04Sjeremylt 
97885ac19cSjeremylt /*
98885ac19cSjeremylt   CeedOperator needs to connect all the named fields (be they active or passive)
99885ac19cSjeremylt   to the named inputs and outputs of its CeedQFunction.
100885ac19cSjeremylt  */
101885ac19cSjeremylt static int CeedOperatorSetup_Ref(CeedOperator op) {
102885ac19cSjeremylt   if (op->setupdone) return 0;
103*a2b73c81Sjeremylt   CeedOperator_Ref *impl = op->data;
104885ac19cSjeremylt   CeedQFunction qf = op->qf;
105885ac19cSjeremylt   CeedInt Q = op->numqpoints;
10621617c04Sjeremylt   int ierr;
10721617c04Sjeremylt 
108885ac19cSjeremylt   // Count infield and outfield array sizes and evectors
109*a2b73c81Sjeremylt   impl->numein = qf->numinputfields;
110885ac19cSjeremylt   for (CeedInt i=0; i<qf->numinputfields; i++) {
111885ac19cSjeremylt     CeedEvalMode emode = qf->inputfields[i].emode;
112*a2b73c81Sjeremylt     impl->numqin += !!(emode & CEED_EVAL_INTERP) + !!(emode & CEED_EVAL_GRAD) + !!
113885ac19cSjeremylt                      (emode & CEED_EVAL_WEIGHT);
11421617c04Sjeremylt   }
115*a2b73c81Sjeremylt   impl->numeout = qf->numoutputfields;
116885ac19cSjeremylt   for (CeedInt i=0; i<qf->numoutputfields; i++) {
117885ac19cSjeremylt     CeedEvalMode emode = qf->outputfields[i].emode;
118*a2b73c81Sjeremylt     impl->numqout += !!(emode & CEED_EVAL_INTERP) + !!(emode & CEED_EVAL_GRAD);
119885ac19cSjeremylt   }
120885ac19cSjeremylt 
121885ac19cSjeremylt   // Allocate
122*a2b73c81Sjeremylt   ierr = CeedCalloc(impl->numein + impl->numeout, &impl->evecs); CeedChk(ierr);
123*a2b73c81Sjeremylt   ierr = CeedCalloc(impl->numein + impl->numeout, &impl->edata);
124885ac19cSjeremylt   CeedChk(ierr);
125885ac19cSjeremylt 
126*a2b73c81Sjeremylt   ierr = CeedCalloc(impl->numqin + impl->numqout, &impl->qdata_alloc);
127885ac19cSjeremylt   CeedChk(ierr);
128*a2b73c81Sjeremylt   ierr = CeedCalloc(qf->numinputfields + qf->numoutputfields, &impl->qdata);
129885ac19cSjeremylt   CeedChk(ierr);
130885ac19cSjeremylt 
131*a2b73c81Sjeremylt   ierr = CeedCalloc(16, &impl->indata); CeedChk(ierr);
132*a2b73c81Sjeremylt   ierr = CeedCalloc(16, &impl->outdata); CeedChk(ierr);
133885ac19cSjeremylt 
134885ac19cSjeremylt   // Set up infield and outfield pointer arrays
135885ac19cSjeremylt   // Infields
136885ac19cSjeremylt   ierr = CeedOperatorSetupFields_Ref(qf->inputfields, op->inputfields,
137*a2b73c81Sjeremylt                                      impl->evecs, impl->qdata, impl->qdata_alloc,
138*a2b73c81Sjeremylt                                      impl->indata, 0, 0,
139885ac19cSjeremylt                                      qf->numinputfields, Q); CeedChk(ierr);
140885ac19cSjeremylt 
141885ac19cSjeremylt   // Outfields
142885ac19cSjeremylt   ierr = CeedOperatorSetupFields_Ref(qf->outputfields, op->outputfields,
143*a2b73c81Sjeremylt                                      impl->evecs, impl->qdata, impl->qdata_alloc,
144*a2b73c81Sjeremylt                                      impl->indata, qf->numinputfields,
145*a2b73c81Sjeremylt                                      impl->numqin, qf->numoutputfields, Q); CeedChk(ierr);
146885ac19cSjeremylt 
1477ca8db16Sjeremylt   // Output Qvecs
1487ca8db16Sjeremylt   for (CeedInt i=0; i<qf->numoutputfields; i++) {
1497ca8db16Sjeremylt     CeedEvalMode emode = qf->outputfields[i].emode;
1507ca8db16Sjeremylt     if (emode != CEED_EVAL_NONE) {
151*a2b73c81Sjeremylt       impl->outdata[i] =  impl->qdata[i + qf->numinputfields];
1527ca8db16Sjeremylt     }
1537ca8db16Sjeremylt   }
1547ca8db16Sjeremylt 
155885ac19cSjeremylt   op->setupdone = 1;
156885ac19cSjeremylt 
157885ac19cSjeremylt   return 0;
158885ac19cSjeremylt }
159885ac19cSjeremylt 
160885ac19cSjeremylt static int CeedOperatorApply_Ref(CeedOperator op, CeedVector invec,
161885ac19cSjeremylt                                  CeedVector outvec, CeedRequest *request) {
162*a2b73c81Sjeremylt   CeedOperator_Ref *impl = op->data;
163885ac19cSjeremylt   CeedInt Q = op->numqpoints, elemsize;
164885ac19cSjeremylt   int ierr;
165885ac19cSjeremylt   CeedQFunction qf = op->qf;
166885ac19cSjeremylt   CeedTransposeMode lmode = CEED_NOTRANSPOSE;
167885ac19cSjeremylt 
168885ac19cSjeremylt   // Setup
169885ac19cSjeremylt   ierr = CeedOperatorSetup_Ref(op); CeedChk(ierr);
170885ac19cSjeremylt 
171885ac19cSjeremylt   // Input Evecs and Restriction
172135a076eSjeremylt   for (CeedInt i=0; i<qf->numinputfields; i++) {
173668048e2SJed Brown     CeedEvalMode emode = qf->inputfields[i].emode;
174135a076eSjeremylt     if (emode == CEED_EVAL_WEIGHT) { // Skip
175668048e2SJed Brown     } else {
1767ca8db16Sjeremylt       // Zero evec
177*a2b73c81Sjeremylt       ierr = CeedVectorSetValue(impl->evecs[i], 0.0); CeedChk(ierr);
178668048e2SJed Brown       // Active
179668048e2SJed Brown       if (op->inputfields[i].vec == CEED_VECTOR_ACTIVE) {
180668048e2SJed Brown         // Restrict
181668048e2SJed Brown         ierr = CeedElemRestrictionApply(op->inputfields[i].Erestrict, CEED_NOTRANSPOSE,
182*a2b73c81Sjeremylt                                         lmode, invec, impl->evecs[i],
183668048e2SJed Brown                                         request); CeedChk(ierr);
184668048e2SJed Brown         // Get evec
185*a2b73c81Sjeremylt         ierr = CeedVectorGetArrayRead(impl->evecs[i], CEED_MEM_HOST,
186*a2b73c81Sjeremylt                                       (const CeedScalar **) &impl->edata[i]); CeedChk(ierr);
187668048e2SJed Brown       } else {
188885ac19cSjeremylt         // Passive
1897ca8db16Sjeremylt         // Restrict
190885ac19cSjeremylt         ierr = CeedElemRestrictionApply(op->inputfields[i].Erestrict, CEED_NOTRANSPOSE,
191*a2b73c81Sjeremylt                                         lmode, op->inputfields[i].vec, impl->evecs[i],
192885ac19cSjeremylt                                         request); CeedChk(ierr);
1937ca8db16Sjeremylt         // Get evec
194*a2b73c81Sjeremylt         ierr = CeedVectorGetArrayRead(impl->evecs[i], CEED_MEM_HOST,
195*a2b73c81Sjeremylt                                       (const CeedScalar **) &impl->edata[i]); CeedChk(ierr);
196885ac19cSjeremylt       }
197885ac19cSjeremylt     }
198885ac19cSjeremylt   }
199885ac19cSjeremylt 
200885ac19cSjeremylt   // Output Evecs
201135a076eSjeremylt   for (CeedInt i=0; i<qf->numoutputfields; i++) {
202*a2b73c81Sjeremylt     ierr = CeedVectorGetArray(impl->evecs[i+impl->numein], CEED_MEM_HOST,
203*a2b73c81Sjeremylt                               &impl->edata[i + qf->numinputfields]); CeedChk(ierr);
204885ac19cSjeremylt   }
205885ac19cSjeremylt 
206885ac19cSjeremylt   // Loop through elements
207885ac19cSjeremylt   for (CeedInt e=0; e<op->numelements; e++) {
208885ac19cSjeremylt     // Input basis apply if needed
209885ac19cSjeremylt     for (CeedInt i=0; i<qf->numinputfields; i++) {
210135a076eSjeremylt       // Get elemsize, emode, ncomp
211885ac19cSjeremylt       elemsize = op->inputfields[i].Erestrict->elemsize;
212885ac19cSjeremylt       CeedEvalMode emode = qf->inputfields[i].emode;
213885ac19cSjeremylt       CeedInt ncomp = qf->inputfields[i].ncomp;
214885ac19cSjeremylt       // Basis action
215885ac19cSjeremylt       switch(emode) {
216885ac19cSjeremylt       case CEED_EVAL_NONE:
217*a2b73c81Sjeremylt         impl->indata[i] = &impl->edata[i][e*Q*ncomp];
218885ac19cSjeremylt         break;
219885ac19cSjeremylt       case CEED_EVAL_INTERP:
220d3181881Sjeremylt         ierr = CeedBasisApply(op->inputfields[i].basis, 1, CEED_NOTRANSPOSE,
221*a2b73c81Sjeremylt                               CEED_EVAL_INTERP, &impl->edata[i][e*elemsize*ncomp], impl->qdata[i]);
222885ac19cSjeremylt         CeedChk(ierr);
223*a2b73c81Sjeremylt         impl->indata[i] = impl->qdata[i];
224885ac19cSjeremylt         break;
225885ac19cSjeremylt       case CEED_EVAL_GRAD:
226d3181881Sjeremylt         ierr = CeedBasisApply(op->inputfields[i].basis, 1, CEED_NOTRANSPOSE,
227*a2b73c81Sjeremylt                               CEED_EVAL_GRAD, &impl->edata[i][e*elemsize*ncomp], impl->qdata[i]);
228885ac19cSjeremylt         CeedChk(ierr);
229*a2b73c81Sjeremylt         impl->indata[i] = impl->qdata[i];
230885ac19cSjeremylt         break;
231885ac19cSjeremylt       case CEED_EVAL_WEIGHT:
232885ac19cSjeremylt         break;  // No action
233885ac19cSjeremylt       case CEED_EVAL_DIV:
234885ac19cSjeremylt         break; // Not implimented
235885ac19cSjeremylt       case CEED_EVAL_CURL:
236885ac19cSjeremylt         break; // Not implimented
237885ac19cSjeremylt       }
238885ac19cSjeremylt     }
239885ac19cSjeremylt     // Output pointers
240885ac19cSjeremylt     for (CeedInt i=0; i<qf->numoutputfields; i++) {
241885ac19cSjeremylt       CeedEvalMode emode = qf->outputfields[i].emode;
242885ac19cSjeremylt       if (emode == CEED_EVAL_NONE) {
243885ac19cSjeremylt         CeedInt ncomp = qf->outputfields[i].ncomp;
244*a2b73c81Sjeremylt         impl->outdata[i] = &impl->edata[i + qf->numinputfields][e*Q*ncomp];
245885ac19cSjeremylt       }
246885ac19cSjeremylt     }
247885ac19cSjeremylt     // Q function
248*a2b73c81Sjeremylt     ierr = CeedQFunctionApply(op->qf, Q, (const CeedScalar * const*) impl->indata,
249*a2b73c81Sjeremylt                               impl->outdata); CeedChk(ierr);
250885ac19cSjeremylt 
251885ac19cSjeremylt     // Output basis apply if needed
252885ac19cSjeremylt     for (CeedInt i=0; i<qf->numoutputfields; i++) {
253135a076eSjeremylt       // Get elemsize, emode, ncomp
254885ac19cSjeremylt       elemsize = op->outputfields[i].Erestrict->elemsize;
255885ac19cSjeremylt       CeedInt ncomp = qf->outputfields[i].ncomp;
256885ac19cSjeremylt       CeedEvalMode emode = qf->outputfields[i].emode;
257885ac19cSjeremylt       // Basis action
258885ac19cSjeremylt       switch(emode) {
259885ac19cSjeremylt       case CEED_EVAL_NONE:
260885ac19cSjeremylt         break; // No action
261885ac19cSjeremylt       case CEED_EVAL_INTERP:
262d3181881Sjeremylt         ierr = CeedBasisApply(op->outputfields[i].basis, 1, CEED_TRANSPOSE,
263*a2b73c81Sjeremylt                               CEED_EVAL_INTERP, impl->outdata[i],
264*a2b73c81Sjeremylt                               &impl->edata[i + qf->numinputfields][e*elemsize*ncomp]); CeedChk(ierr);
265885ac19cSjeremylt         break;
266885ac19cSjeremylt       case CEED_EVAL_GRAD:
2670c7a96bbSjeremylt         ierr = CeedBasisApply(op->outputfields[i].basis, 1, CEED_TRANSPOSE,
2680c7a96bbSjeremylt                               CEED_EVAL_GRAD,
269*a2b73c81Sjeremylt                               impl->outdata[i], &impl->edata[i + qf->numinputfields][e*elemsize*ncomp]);
270885ac19cSjeremylt         CeedChk(ierr);
271885ac19cSjeremylt         break;
272885ac19cSjeremylt       case CEED_EVAL_WEIGHT:
273885ac19cSjeremylt         break; // Should not occur
274885ac19cSjeremylt       case CEED_EVAL_DIV:
275885ac19cSjeremylt         break; // Not implimented
276885ac19cSjeremylt       case CEED_EVAL_CURL:
277885ac19cSjeremylt         break; // Not implimented
278885ac19cSjeremylt       }
279885ac19cSjeremylt     }
280885ac19cSjeremylt   }
281885ac19cSjeremylt 
282885ac19cSjeremylt   // Output restriction
283135a076eSjeremylt   for (CeedInt i=0; i<qf->numoutputfields; i++) {
284*a2b73c81Sjeremylt     // Restore evec
285*a2b73c81Sjeremylt     ierr = CeedVectorRestoreArray(impl->evecs[i+impl->numein],
286*a2b73c81Sjeremylt                                     &impl->edata[i + qf->numinputfields]); CeedChk(ierr);
287668048e2SJed Brown     // Active
288668048e2SJed Brown     if (op->outputfields[i].vec == CEED_VECTOR_ACTIVE) {
2897ca8db16Sjeremylt       // Zero lvec
290*a2b73c81Sjeremylt       ierr = CeedVectorSetValue(outvec, 0.0); CeedChk(ierr);
2917ca8db16Sjeremylt       // Restrict
292885ac19cSjeremylt       ierr = CeedElemRestrictionApply(op->outputfields[i].Erestrict, CEED_TRANSPOSE,
293*a2b73c81Sjeremylt                                       lmode, impl->evecs[i+impl->numein], outvec, request); CeedChk(ierr);
294885ac19cSjeremylt     } else {
295885ac19cSjeremylt       // Passive
296668048e2SJed Brown       // Zero lvec
297*a2b73c81Sjeremylt       ierr = CeedVectorSetValue(op->outputfields[i].vec, 0.0); CeedChk(ierr);
298668048e2SJed Brown       // Restrict
299668048e2SJed Brown       ierr = CeedElemRestrictionApply(op->outputfields[i].Erestrict, CEED_TRANSPOSE,
300*a2b73c81Sjeremylt                                       lmode, impl->evecs[i+impl->numein], op->outputfields[i].vec,
301668048e2SJed Brown                                       request); CeedChk(ierr);
302885ac19cSjeremylt     }
303885ac19cSjeremylt   }
304885ac19cSjeremylt 
3057ca8db16Sjeremylt   // Restore input arrays
306135a076eSjeremylt   for (CeedInt i=0; i<qf->numinputfields; i++) {
3077ca8db16Sjeremylt     CeedEvalMode emode = qf->inputfields[i].emode;
308135a076eSjeremylt     if (emode == CEED_EVAL_WEIGHT) { // Skip
3097ca8db16Sjeremylt     } else {
310*a2b73c81Sjeremylt       ierr = CeedVectorRestoreArrayRead(impl->evecs[i],
311*a2b73c81Sjeremylt                                         (const CeedScalar **) &impl->edata[i]); CeedChk(ierr);
3127ca8db16Sjeremylt     }
3137ca8db16Sjeremylt   }
3147ca8db16Sjeremylt 
31521617c04Sjeremylt   return 0;
31621617c04Sjeremylt }
31721617c04Sjeremylt 
31821617c04Sjeremylt int CeedOperatorCreate_Ref(CeedOperator op) {
31921617c04Sjeremylt   CeedOperator_Ref *impl;
32021617c04Sjeremylt   int ierr;
32121617c04Sjeremylt 
32221617c04Sjeremylt   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
32321617c04Sjeremylt   op->data = impl;
32421617c04Sjeremylt   op->Destroy = CeedOperatorDestroy_Ref;
32521617c04Sjeremylt   op->Apply = CeedOperatorApply_Ref;
32621617c04Sjeremylt   return 0;
32721617c04Sjeremylt }
328