xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 1dfeef1d2914276d712b7070e9737e3de33781c5)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // 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-impl.h>
18 #include <ceed-backend.h>
19 #include <string.h>
20 
21 /// @file
22 /// Implementation of public CeedQFunction interfaces
23 ///
24 /// @addtogroup CeedQFunction
25 /// @{
26 
27 /**
28   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
29 
30   @param ceed       A Ceed object where the CeedQFunction will be created
31   @param vlength    Vector length.  Caller must ensure that number of quadrature
32                     points is a multiple of vlength.
33   @param f          Function pointer to evaluate action at quadrature points.
34                     See below.
35   @param focca      OCCA identifier "file.c:function_name" for definition of `f`
36   @param[out] qf    Address of the variable where the newly created
37                      CeedQFunction will be stored
38 
39   @return An error code: 0 - success, otherwise - failure
40 
41   The arguments of the call-back 'function' are:
42 
43    1. [void *ctx][in/out] - user data, this is the 'ctx' pointer stored in
44               the CeedQFunction, set by calling CeedQFunctionSetContext
45 
46    2. [CeedInt nq][in] - number of quadrature points to process
47 
48    3. [const CeedScalar *const *u][in] - input fields data at quadrature pts, listed in the order given by the user
49 
50    4. [CeedScalar *const *v][out] - output fields data at quadrature points, again listed in order given by the user
51 
52   @ref Basic
53 **/
54 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
55                                 int (*f)(void*, CeedInt, const CeedScalar *const*, CeedScalar *const*),
56                                 const char *focca, CeedQFunction *qf) {
57   int ierr;
58   char *focca_copy;
59 
60   if (!ceed->QFunctionCreate) {
61     Ceed delegate;
62     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
63 
64     if (!delegate)
65       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
66 
67     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, focca, qf);
68     CeedChk(ierr);
69     return 0;
70   }
71 
72   ierr = CeedCalloc(1,qf); CeedChk(ierr);
73   (*qf)->ceed = ceed;
74   ceed->refcount++;
75   (*qf)->refcount = 1;
76   (*qf)->vlength = vlength;
77   (*qf)->function = f;
78   ierr = CeedCalloc(strlen(focca)+1, &focca_copy); CeedChk(ierr);
79   strcpy(focca_copy, focca);
80   (*qf)->focca = focca_copy;
81   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
82   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
83   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
84   return 0;
85 }
86 
87 /**
88   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
89 
90   @param f          CeedQFunctionField
91   @param fieldname  Name of QFunction field
92   @param ncomp      Number of components per quadrature node
93   @param emode      \ref CEED_EVAL_NONE to use values directly,
94                       \ref CEED_EVAL_INTERP to use interpolated values,
95                       \ref CEED_EVAL_GRAD to use gradients.
96 
97   @return An error code: 0 - success, otherwise - failure
98 
99   @ref Developer
100 **/
101 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
102                                  CeedInt ncomp, CeedEvalMode emode) {
103   size_t len = strlen(fieldname);
104   char *tmp;
105   int ierr;
106   ierr = CeedCalloc(1,f); CeedChk(ierr);
107 
108   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
109   memcpy(tmp, fieldname, len+1);
110   (*f)->fieldname = tmp;
111   (*f)->ncomp = ncomp;
112   (*f)->emode = emode;
113   return 0;
114 }
115 
116 /**
117   @brief Add a CeedQFunction input
118 
119   @param qf         CeedQFunction
120   @param fieldname  Name of QFunction field
121   @param ncomp      Number of components per quadrature node
122   @param emode      \ref CEED_EVAL_NONE to use values directly,
123                       \ref CEED_EVAL_INTERP to use interpolated values,
124                       \ref CEED_EVAL_GRAD to use gradients.
125 
126   @return An error code: 0 - success, otherwise - failure
127 
128   @ref Basic
129 **/
130 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
131                           CeedInt ncomp, CeedEvalMode emode) {
132   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
133                                    fieldname, ncomp, emode);
134   CeedChk(ierr);
135   qf->numinputfields++;
136   return 0;
137 }
138 
139 /**
140   @brief Add a CeedQFunction output
141 
142   @param qf         CeedQFunction
143   @param fieldname  Name of QFunction field
144   @param ncomp      Number of components per quadrature node
145   @param emode      \ref CEED_EVAL_NONE to use values directly,
146                       \ref CEED_EVAL_INTERP to use interpolated values,
147                       \ref CEED_EVAL_GRAD to use gradients.
148 
149   @return An error code: 0 - success, otherwise - failure
150 
151   @ref Basic
152 **/
153 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
154                            CeedInt ncomp, CeedEvalMode emode) {
155   if (emode == CEED_EVAL_WEIGHT)
156     return CeedError(qf->ceed, 1,
157                      "Cannot create qfunction output with CEED_EVAL_WEIGHT");
158   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
159                                    fieldname, ncomp, emode);
160   CeedChk(ierr);
161   qf->numoutputfields++;
162   return 0;
163 }
164 
165 /**
166   @brief Get the Ceed associated with a CeedQFunction
167 
168   @param qf              CeedQFunction
169   @param[out] ceed       Variable to store Ceed
170 
171   @return An error code: 0 - success, otherwise - failure
172 
173   @ref Advanced
174 **/
175 
176 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
177   *ceed = qf->ceed;
178   return 0;
179 }
180 
181 /**
182   @brief Get the vector length of a CeedQFunction
183 
184   @param qf              CeedQFunction
185   @param[out] veclength  Variable to store vector length
186 
187   @return An error code: 0 - success, otherwise - failure
188 
189   @ref Advanced
190 **/
191 
192 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
193   *vlength = qf->vlength;
194   return 0;
195 }
196 
197 /**
198   @brief Get the number of inputs and outputs to a CeedQFunction
199 
200   @param qf              CeedQFunction
201   @param[out] numinput   Variable to store number of input fields
202   @param[out] numoutput  Variable to store number of output fields
203 
204   @return An error code: 0 - success, otherwise - failure
205 
206   @ref Advanced
207 **/
208 
209 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
210                             CeedInt *numoutput) {
211   if (numinput) *numinput = qf->numinputfields;
212   if (numoutput) *numoutput = qf->numoutputfields;
213   return 0;
214 }
215 
216 /**
217   @brief Get the FOCCA string for a CeedQFunction
218 
219   @param qf              CeedQFunction
220   @param[out] focca      Variable to store focca string
221 
222   @return An error code: 0 - success, otherwise - failure
223 
224   @ref Advanced
225 **/
226 
227 int CeedQFunctionGetFOCCA(CeedQFunction qf, char* *focca) {
228   *focca = (char*) qf->focca;
229   return 0;
230 }
231 
232 /**
233   @brief Get the User Function for a CeedQFunction
234 
235   @param qf              CeedQFunction
236   @param[out] f          Variable to store user function
237 
238   @return An error code: 0 - success, otherwise - failure
239 
240   @ref Advanced
241 **/
242 
243 int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) {
244   *f = (int (*)())qf->function;
245   return 0;
246 }
247 
248 /**
249   @brief Get global context size for a CeedQFunction
250 
251   @param qf              CeedQFunction
252   @param[out] ctxsize    Variable to store size of context data values
253 
254   @return An error code: 0 - success, otherwise - failure
255 
256   @ref Advanced
257 **/
258 
259 int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
260   *ctxsize = qf->ctxsize;
261   return 0;
262 }
263 
264 /**
265   @brief Get global context for a CeedQFunction
266 
267   @param qf              CeedQFunction
268   @param[out] ctx        Variable to store context data values
269 
270   @return An error code: 0 - success, otherwise - failure
271 
272   @ref Advanced
273 **/
274 
275 int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) {
276   *ctx = qf->ctx;
277   return 0;
278 }
279 
280 /**
281   @brief Get backend data of a CeedQFunction
282 
283   @param qf              CeedQFunction
284   @param[out] data       Variable to store data
285 
286   @return An error code: 0 - success, otherwise - failure
287 
288   @ref Advanced
289 **/
290 
291 int CeedQFunctionGetData(CeedQFunction qf, void* *data) {
292   *data = qf->data;
293   return 0;
294 }
295 
296 /**
297   @brief Set backend data of a CeedQFunction
298 
299   @param[out] qf         CeedQFunction
300   @param data            Data to set
301 
302   @return An error code: 0 - success, otherwise - failure
303 
304   @ref Advanced
305 **/
306 
307 int CeedQFunctionSetData(CeedQFunction qf, void* *data) {
308   qf->data = *data;
309   return 0;
310 }
311 
312 /**
313   @brief Set global context for a CeedQFunction
314 
315   @param qf       CeedQFunction
316   @param ctx      Context data to set
317   @param ctxsize  Size of context data values
318 
319   @return An error code: 0 - success, otherwise - failure
320 
321   @ref Basic
322 **/
323 int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
324   qf->ctx = ctx;
325   qf->ctxsize = ctxsize;
326   return 0;
327 }
328 
329 /**
330   @brief Apply the action of a CeedQFunction
331 
332   @param qf      CeedQFunction
333   @param Q       Number of quadrature points
334   @param[in] u   Array of input data arrays
335   @param[out] v  Array of output data arrays
336 
337   @return An error code: 0 - success, otherwise - failure
338 
339   @ref Advanced
340 **/
341 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
342                        const CeedScalar *const *u,
343                        CeedScalar *const *v) {
344   int ierr;
345   if (!qf->Apply)
346     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
347   if (Q % qf->vlength)
348     return CeedError(qf->ceed, 2,
349                      "Number of quadrature points %d must be a multiple of %d",
350                      Q, qf->vlength);
351   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
352   return 0;
353 }
354 
355 /**
356   @brief Get the CeedQFunctionFields of a CeedQFunction
357 
358   @param qf                 CeedQFunction
359   @param[out] inputfields   Variable to store inputfields
360   @param[out] outputfields  Variable to store outputfields
361 
362   @return An error code: 0 - success, otherwise - failure
363 
364   @ref Advanced
365 **/
366 
367 int CeedQFunctionGetFields(CeedQFunction qf,
368                            CeedQFunctionField* *inputfields,
369                            CeedQFunctionField* *outputfields) {
370   if (inputfields) *inputfields = qf->inputfields;
371   if (outputfields) *outputfields = qf->outputfields;
372   return 0;
373 }
374 
375 /**
376   @brief Get the name of a CeedQFunctionField
377 
378   @param qffield         CeedQFunctionField
379   @param[out] fieldname  Variable to store the field name
380 
381   @return An error code: 0 - success, otherwise - failure
382 
383   @ref Advanced
384 **/
385 
386 int CeedQFunctionFieldGetName(CeedQFunctionField qffield,
387                               char* *fieldname) {
388   *fieldname = (char *)qffield->fieldname;
389   return 0;
390 }
391 
392 /**
393   @brief Get the number of components of a CeedQFunctionField
394 
395   @param qffield         CeedQFunctionField
396   @param[out] numcomp    Variable to store the number of components
397 
398   @return An error code: 0 - success, otherwise - failure
399 
400   @ref Advanced
401 **/
402 
403 int CeedQFunctionFieldGetNumComponents(CeedQFunctionField qffield,
404                                        CeedInt *numcomp) {
405   *numcomp = qffield->ncomp;
406   return 0;
407 }
408 
409 /**
410   @brief Get the CeedEvalMode of a CeedQFunctionField
411 
412   @param qffield         CeedQFunctionField
413   @param[out] vec        Variable to store the number of components
414 
415   @return An error code: 0 - success, otherwise - failure
416 
417   @ref Advanced
418 **/
419 
420 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
421                                   CeedEvalMode *emode) {
422   *emode = qffield->emode;
423   return 0;
424 }
425 
426 /**
427   @brief Destroy a CeedQFunction
428 
429   @param qf CeedQFunction to destroy
430 
431   @return An error code: 0 - success, otherwise - failure
432 
433   @ref Basic
434 **/
435 int CeedQFunctionDestroy(CeedQFunction *qf) {
436   int ierr;
437 
438   if (!*qf || --(*qf)->refcount > 0) return 0;
439   // Backend destroy
440   if ((*qf)->Destroy) {
441     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
442   }
443   // Free fields
444   for (int i=0; i<(*qf)->numinputfields; i++) {
445     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
446     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
447   }
448   for (int i=0; i<(*qf)->numoutputfields; i++) {
449     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
450     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
451   }
452   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
453   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
454 
455   ierr = CeedFree(&(*qf)->focca); CeedChk(ierr);
456   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
457   ierr = CeedFree(qf); CeedChk(ierr);
458   return 0;
459 }
460 
461 /// @}
462