xref: /libCEED/interface/ceed-qfunction.c (revision 3d576824e8d990e1f48c6609089904bee9170514)
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.h>
18 #include <ceed-backend.h>
19 #include <ceed-impl.h>
20 #include <limits.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <string.h>
24 
25 /// @file
26 /// Implementation of public CeedQFunction interfaces
27 
28 /// @cond DOXYGEN_SKIP
29 static struct CeedQFunction_private ceed_qfunction_none;
30 /// @endcond
31 
32 /// @addtogroup CeedQFunctionUser
33 /// @{
34 
35 // Indicate that no QFunction is provided by the user
36 const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
37 
38 /// @}
39 
40 /// @cond DOXYGEN_SKIP
41 static struct {
42   char name[CEED_MAX_RESOURCE_LEN];
43   char source[CEED_MAX_RESOURCE_LEN];
44   CeedInt vlength;
45   CeedQFunctionUser f;
46   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
47 } qfunctions[1024];
48 static size_t num_qfunctions;
49 /// @endcond
50 
51 /// ----------------------------------------------------------------------------
52 /// CeedQFunction Library Internal Functions
53 /// ----------------------------------------------------------------------------
54 /// @addtogroup CeedQFunctionDeveloper
55 /// @{
56 
57 /**
58   @brief Register a gallery QFunction
59 
60   @param name     Name for this backend to respond to
61   @param source   Absolute path to source of QFunction,
62                     "\path\CEED_DIR\gallery\folder\file.h:function_name"
63   @param vlength  Vector length.  Caller must ensure that number of quadrature
64                     points is a multiple of vlength.
65   @param f        Function pointer to evaluate action at quadrature points.
66                     See \ref CeedQFunctionUser.
67   @param init     Initialization function called by CeedQFunctionInit() when the
68                     QFunction is selected.
69 
70   @return An error code: 0 - success, otherwise - failure
71 
72   @ref Developer
73 **/
74 int CeedQFunctionRegister(const char *name, const char *source,
75                           CeedInt vlength, CeedQFunctionUser f,
76                           int (*init)(Ceed, const char *, CeedQFunction)) {
77   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0]))
78     // LCOV_EXCL_START
79     return CeedError(NULL, 1, "Too many gallery QFunctions");
80   // LCOV_EXCL_STOP
81 
82   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
83   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
84   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
85   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
86   qfunctions[num_qfunctions].vlength = vlength;
87   qfunctions[num_qfunctions].f = f;
88   qfunctions[num_qfunctions].init = init;
89   num_qfunctions++;
90   return 0;
91 }
92 
93 /**
94   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
95 
96   @param f          CeedQFunctionField
97   @param fieldname  Name of QFunction field
98   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
99                       (ncomp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
100   @param emode      \ref CEED_EVAL_NONE to use values directly,
101                       \ref CEED_EVAL_INTERP to use interpolated values,
102                       \ref CEED_EVAL_GRAD to use gradients,
103                       \ref CEED_EVAL_WEIGHT to use quadrature weights.
104 
105   @return An error code: 0 - success, otherwise - failure
106 
107   @ref Developer
108 **/
109 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
110                                  CeedInt size, CeedEvalMode emode) {
111   size_t len = strlen(fieldname);
112   char *tmp;
113   int ierr;
114   ierr = CeedCalloc(1,f); CeedChk(ierr);
115 
116   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
117   memcpy(tmp, fieldname, len+1);
118   (*f)->fieldname = tmp;
119   (*f)->size = size;
120   (*f)->emode = emode;
121   return 0;
122 }
123 
124 /**
125   @brief View a field of a CeedQFunction
126 
127   @param[in] field        QFunction field to view
128   @param[in] fieldnumber  Number of field being viewed
129   @param[in] in           true for input field, false for output
130   @param[in] stream       Stream to view to, e.g., stdout
131 
132   @return An error code: 0 - success, otherwise - failure
133 
134   @ref Utility
135 **/
136 static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt fieldnumber,
137                                   bool in, FILE *stream) {
138   const char *inout = in ? "Input" : "Output";
139   fprintf(stream, "    %s Field [%d]:\n"
140           "      Name: \"%s\"\n"
141           "      Size: %d\n"
142           "      EvalMode: \"%s\"\n",
143           inout, fieldnumber, field->fieldname, field->size,
144           CeedEvalModes[field->emode]);
145 
146   return 0;
147 }
148 
149 
150 /**
151   @brief Set flag to determine if Fortran interface is used
152 
153   @param qf                  CeedQFunction
154   @param status              Boolean value to set as Fortran status
155 
156   @return An error code: 0 - success, otherwise - failure
157 
158   @ref Backend
159 **/
160 int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
161   qf->fortranstatus = status;
162   return 0;
163 }
164 
165 /// @}
166 
167 /// ----------------------------------------------------------------------------
168 /// CeedQFunction Backend API
169 /// ----------------------------------------------------------------------------
170 /// @addtogroup CeedQFunctionBackend
171 /// @{
172 
173 /**
174   @brief Get the Ceed associated with a CeedQFunction
175 
176   @param qf              CeedQFunction
177   @param[out] ceed       Variable to store Ceed
178 
179   @return An error code: 0 - success, otherwise - failure
180 
181   @ref Backend
182 **/
183 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
184   *ceed = qf->ceed;
185   return 0;
186 }
187 
188 /**
189   @brief Get the vector length of a CeedQFunction
190 
191   @param qf            CeedQFunction
192   @param[out] vlength  Variable to store vector length
193 
194   @return An error code: 0 - success, otherwise - failure
195 
196   @ref Backend
197 **/
198 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
199   *vlength = qf->vlength;
200   return 0;
201 }
202 
203 /**
204   @brief Get the number of inputs and outputs to a CeedQFunction
205 
206   @param qf              CeedQFunction
207   @param[out] numinput   Variable to store number of input fields
208   @param[out] numoutput  Variable to store number of output fields
209 
210   @return An error code: 0 - success, otherwise - failure
211 
212   @ref Backend
213 **/
214 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
215                             CeedInt *numoutput) {
216   if (numinput) *numinput = qf->numinputfields;
217   if (numoutput) *numoutput = qf->numoutputfields;
218   return 0;
219 }
220 
221 /**
222   @brief Get the source path string for a CeedQFunction
223 
224   @param qf              CeedQFunction
225   @param[out] source     Variable to store source path string
226 
227   @return An error code: 0 - success, otherwise - failure
228 
229   @ref Backend
230 **/
231 int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
232   *source = (char *) qf->sourcepath;
233   return 0;
234 }
235 
236 /**
237   @brief Get the User Function for a CeedQFunction
238 
239   @param qf              CeedQFunction
240   @param[out] f          Variable to store user function
241 
242   @return An error code: 0 - success, otherwise - failure
243 
244   @ref Backend
245 **/
246 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
247   *f = qf->function;
248   return 0;
249 }
250 
251 /**
252   @brief Get global context for a CeedQFunction.
253          Note: For QFunctions from the Fortran interface, this
254                function will return the Fortran context
255                CeedQFunctionContext.
256 
257   @param qf              CeedQFunction
258   @param[out] ctx        Variable to store CeedQFunctionContext
259 
260   @return An error code: 0 - success, otherwise - failure
261 
262   @ref Backend
263 **/
264 int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
265   *ctx = qf->ctx;
266   return 0;
267 }
268 
269 /**
270   @brief Get true user context for a CeedQFunction
271          Note: For all QFunctions this function will return the user
272                CeedQFunctionContext and not interface context
273                CeedQFunctionContext, if any such object exists.
274 
275   @param qf              CeedQFunction
276   @param[out] ctx        Variable to store CeedQFunctionContext
277 
278   @return An error code: 0 - success, otherwise - failure
279   @ref Backend
280 **/
281 int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
282   int ierr;
283   if (qf->fortranstatus) {
284     CeedFortranContext fctx = NULL;
285     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fctx);
286     CeedChk(ierr);
287     *ctx = fctx->innerctx;
288     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fctx); CeedChk(ierr);
289   } else {
290     *ctx = qf->ctx;
291   }
292 
293 
294   return 0;
295 }
296 
297 /**
298   @brief Determine if QFunction is identity
299 
300   @param qf               CeedQFunction
301   @param[out] isidentity  Variable to store identity status
302 
303   @return An error code: 0 - success, otherwise - failure
304 
305   @ref Backend
306 **/
307 int CeedQFunctionIsIdentity(CeedQFunction qf, bool *isidentity) {
308   *isidentity = qf->identity;
309   return 0;
310 }
311 
312 /**
313   @brief Get backend data of a CeedQFunction
314 
315   @param qf              CeedQFunction
316   @param[out] data       Variable to store data
317 
318   @return An error code: 0 - success, otherwise - failure
319 
320   @ref Backend
321 **/
322 int CeedQFunctionGetData(CeedQFunction qf, void *data) {
323   *(void **)data = qf->data;
324   return 0;
325 }
326 
327 /**
328   @brief Set backend data of a CeedQFunction
329 
330   @param[out] qf         CeedQFunction
331   @param data            Data to set
332 
333   @return An error code: 0 - success, otherwise - failure
334 
335   @ref Backend
336 **/
337 int CeedQFunctionSetData(CeedQFunction qf, void *data) {
338   qf->data = data;
339   return 0;
340 }
341 
342 /**
343   @brief Get the CeedQFunctionFields of a CeedQFunction
344 
345   @param qf                 CeedQFunction
346   @param[out] inputfields   Variable to store inputfields
347   @param[out] outputfields  Variable to store outputfields
348 
349   @return An error code: 0 - success, otherwise - failure
350 
351   @ref Backend
352 **/
353 int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields,
354                            CeedQFunctionField **outputfields) {
355   if (inputfields)
356     *inputfields = qf->inputfields;
357   if (outputfields)
358     *outputfields = qf->outputfields;
359   return 0;
360 }
361 
362 /**
363   @brief Get the name of a CeedQFunctionField
364 
365   @param qffield         CeedQFunctionField
366   @param[out] fieldname  Variable to store the field name
367 
368   @return An error code: 0 - success, otherwise - failure
369 
370   @ref Backend
371 **/
372 int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) {
373   *fieldname = (char *)qffield->fieldname;
374   return 0;
375 }
376 
377 /**
378   @brief Get the number of components of a CeedQFunctionField
379 
380   @param qffield    CeedQFunctionField
381   @param[out] size  Variable to store the size of the field
382 
383   @return An error code: 0 - success, otherwise - failure
384 
385   @ref Backend
386 **/
387 int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
388   *size = qffield->size;
389   return 0;
390 }
391 
392 /**
393   @brief Get the CeedEvalMode of a CeedQFunctionField
394 
395   @param qffield         CeedQFunctionField
396   @param[out] emode      Variable to store the field evaluation mode
397 
398   @return An error code: 0 - success, otherwise - failure
399 
400   @ref Backend
401 **/
402 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
403                                   CeedEvalMode *emode) {
404   *emode = qffield->emode;
405   return 0;
406 }
407 
408 /// @}
409 
410 /// ----------------------------------------------------------------------------
411 /// CeedQFunction Public API
412 /// ----------------------------------------------------------------------------
413 /// @addtogroup CeedQFunctionUser
414 /// @{
415 
416 /**
417   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
418 
419   @param ceed       A Ceed object where the CeedQFunction will be created
420   @param vlength    Vector length. Caller must ensure that number of quadrature
421                       points is a multiple of vlength.
422   @param f          Function pointer to evaluate action at quadrature points.
423                       See \ref CeedQFunctionUser.
424   @param source     Absolute path to source of QFunction,
425                       "\abs_path\file.h:function_name".
426                       For support across all backends, this source must only
427                       contain constructs supported by C99, C++11, and CUDA.
428   @param[out] qf    Address of the variable where the newly created
429                       CeedQFunction will be stored
430 
431   @return An error code: 0 - success, otherwise - failure
432 
433   See \ref CeedQFunctionUser for details on the call-back function @a f's
434     arguments.
435 
436   @ref User
437 **/
438 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f,
439                                 const char *source, CeedQFunction *qf) {
440   int ierr;
441   char *source_copy;
442 
443   if (!ceed->QFunctionCreate) {
444     Ceed delegate;
445     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
446 
447     if (!delegate)
448       // LCOV_EXCL_START
449       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
450     // LCOV_EXCL_STOP
451 
452     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
453     CeedChk(ierr);
454     return 0;
455   }
456 
457   ierr = CeedCalloc(1, qf); CeedChk(ierr);
458   (*qf)->ceed = ceed;
459   ceed->refcount++;
460   (*qf)->refcount = 1;
461   (*qf)->vlength = vlength;
462   (*qf)->identity = 0;
463   (*qf)->function = f;
464   size_t slen = strlen(source) + 1;
465   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
466   memcpy(source_copy, source, slen);
467   (*qf)->sourcepath = source_copy;
468   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
469   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
470   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
471   return 0;
472 }
473 
474 /**
475   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
476 
477   @param ceed       A Ceed object where the CeedQFunction will be created
478   @param name       Name of QFunction to use from gallery
479   @param[out] qf    Address of the variable where the newly created
480                       CeedQFunction will be stored
481 
482   @return An error code: 0 - success, otherwise - failure
483 
484   @ref User
485 **/
486 int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
487                                       CeedQFunction *qf) {
488   int ierr;
489   size_t matchlen = 0, matchidx = UINT_MAX;
490   char *name_copy;
491 
492   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
493   // Find matching backend
494   if (!name) return CeedError(ceed, 1, "No QFunction name provided");
495   for (size_t i=0; i<num_qfunctions; i++) {
496     size_t n;
497     const char *currname = qfunctions[i].name;
498     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
499     if (n > matchlen) {
500       matchlen = n;
501       matchidx = i;
502     }
503   }
504   if (!matchlen)
505     // LCOV_EXCL_START
506     return CeedError(ceed, 1, "No suitable gallery QFunction");
507   // LCOV_EXCL_STOP
508 
509   // Create QFunction
510   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
511                                      qfunctions[matchidx].f,
512                                      qfunctions[matchidx].source, qf);
513   CeedChk(ierr);
514 
515   // QFunction specific setup
516   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
517 
518   // Copy name
519   size_t slen = strlen(name) + 1;
520   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
521   memcpy(name_copy, name, slen);
522   (*qf)->qfname = name_copy;
523 
524   return 0;
525 }
526 
527 /**
528   @brief Create an identity CeedQFunction. Inputs are written into outputs in
529            the order given. This is useful for CeedOperators that can be
530            represented with only the action of a CeedRestriction and CeedBasis,
531            such as restriction and prolongation operators for p-multigrid.
532            Backends may optimize CeedOperators with this CeedQFunction to avoid
533            the copy of input data to output fields by using the same memory
534            location for both.
535 
536   @param ceed         A Ceed object where the CeedQFunction will be created
537   @param[in] size     Size of the qfunction fields
538   @param[in] inmode   CeedEvalMode for input to CeedQFunction
539   @param[in] outmode  CeedEvalMode for output to CeedQFunction
540   @param[out] qf      Address of the variable where the newly created
541                         CeedQFunction will be stored
542 
543   @return An error code: 0 - success, otherwise - failure
544 
545   @ref User
546 **/
547 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode,
548                                 CeedEvalMode outmode, CeedQFunction *qf) {
549   int ierr;
550 
551   if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE)
552     // LCOV_EXCL_START
553     return CeedError(ceed, 1, "CEED_EVAL_NONE for a both the input and "
554                      "output does not make sense with an identity QFunction");
555   // LCOV_EXCL_STOP
556 
557   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
558   ierr = CeedQFunctionAddInput(*qf, "input", size, inmode); CeedChk(ierr);
559   ierr = CeedQFunctionAddOutput(*qf, "output", size, outmode); CeedChk(ierr);
560 
561   (*qf)->identity = 1;
562   CeedInt *sizeData;
563   ierr = CeedCalloc(1, &sizeData); CeedChk(ierr);
564   sizeData[0] = size;
565   CeedQFunctionContext ctx;
566   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
567   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
568                                      sizeof(*sizeData), (void *)sizeData);
569   CeedChk(ierr);
570   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
571   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
572 
573   return 0;
574 }
575 
576 /**
577   @brief Add a CeedQFunction input
578 
579   @param qf         CeedQFunction
580   @param fieldname  Name of QFunction field
581   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
582                       (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
583   @param emode      \ref CEED_EVAL_NONE to use values directly,
584                       \ref CEED_EVAL_INTERP to use interpolated values,
585                       \ref CEED_EVAL_GRAD to use gradients.
586 
587   @return An error code: 0 - success, otherwise - failure
588 
589   @ref User
590 **/
591 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size,
592                           CeedEvalMode emode) {
593   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
594                                    fieldname, size, emode);
595   CeedChk(ierr);
596   qf->numinputfields++;
597   return 0;
598 }
599 
600 /**
601   @brief Add a CeedQFunction output
602 
603   @param qf         CeedQFunction
604   @param fieldname  Name of QFunction field
605   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
606                       (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
607   @param emode      \ref CEED_EVAL_NONE to use values directly,
608                       \ref CEED_EVAL_INTERP to use interpolated values,
609                       \ref CEED_EVAL_GRAD to use gradients.
610 
611   @return An error code: 0 - success, otherwise - failure
612 
613   @ref User
614 **/
615 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
616                            CeedInt size, CeedEvalMode emode) {
617   if (emode == CEED_EVAL_WEIGHT)
618     // LCOV_EXCL_START
619     return CeedError(qf->ceed, 1, "Cannot create QFunction output with "
620                      "CEED_EVAL_WEIGHT");
621   // LCOV_EXCL_STOP
622   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
623                                    fieldname, size, emode);
624   CeedChk(ierr);
625   qf->numoutputfields++;
626   return 0;
627 }
628 
629 /**
630   @brief Set global context for a CeedQFunction
631 
632   @param qf       CeedQFunction
633   @param ctx      Context data to set
634 
635   @return An error code: 0 - success, otherwise - failure
636 
637   @ref User
638 **/
639 int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
640   qf->ctx = ctx;
641   ctx->refcount++;
642   return 0;
643 }
644 
645 /**
646   @brief View a CeedQFunction
647 
648   @param[in] qf      CeedQFunction to view
649   @param[in] stream  Stream to write; typically stdout/stderr or a file
650 
651   @return Error code: 0 - success, otherwise - failure
652 
653   @ref User
654 **/
655 int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
656   int ierr;
657 
658   fprintf(stream, "%sCeedQFunction %s\n",
659           qf->qfname ? "Gallery " : "User ", qf->qfname ? qf->qfname : "");
660 
661   fprintf(stream, "  %d Input Field%s:\n", qf->numinputfields,
662           qf->numinputfields>1 ? "s" : "");
663   for (CeedInt i=0; i<qf->numinputfields; i++) {
664     ierr = CeedQFunctionFieldView(qf->inputfields[i], i, 1, stream);
665     CeedChk(ierr);
666   }
667 
668   fprintf(stream, "  %d Output Field%s:\n", qf->numoutputfields,
669           qf->numoutputfields>1 ? "s" : "");
670   for (CeedInt i=0; i<qf->numoutputfields; i++) {
671     ierr = CeedQFunctionFieldView(qf->outputfields[i], i, 0, stream);
672     CeedChk(ierr);
673   }
674   return 0;
675 }
676 
677 /**
678   @brief Apply the action of a CeedQFunction
679 
680   @param qf      CeedQFunction
681   @param Q       Number of quadrature points
682   @param[in] u   Array of input CeedVectors
683   @param[out] v  Array of output CeedVectors
684 
685   @return An error code: 0 - success, otherwise - failure
686 
687   @ref User
688 **/
689 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
690                        CeedVector *u, CeedVector *v) {
691   int ierr;
692   if (!qf->Apply)
693     // LCOV_EXCL_START
694     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
695   // LCOV_EXCL_STOP
696   if (Q % qf->vlength)
697     // LCOV_EXCL_START
698     return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a "
699                      "multiple of %d", Q, qf->vlength);
700   // LCOV_EXCL_STOP
701   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
702   return 0;
703 }
704 
705 /**
706   @brief Destroy a CeedQFunction
707 
708   @param qf CeedQFunction to destroy
709 
710   @return An error code: 0 - success, otherwise - failure
711 
712   @ref User
713 **/
714 int CeedQFunctionDestroy(CeedQFunction *qf) {
715   int ierr;
716 
717   if (!*qf || --(*qf)->refcount > 0) return 0;
718   // Backend destroy
719   if ((*qf)->Destroy) {
720     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
721   }
722   // Free fields
723   for (int i=0; i<(*qf)->numinputfields; i++) {
724     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
725     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
726   }
727   for (int i=0; i<(*qf)->numoutputfields; i++) {
728     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
729     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
730   }
731   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
732   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
733 
734   // User context data object
735   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
736 
737   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
738   ierr = CeedFree(&(*qf)->qfname); CeedChk(ierr);
739   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
740   ierr = CeedFree(qf); CeedChk(ierr);
741   return 0;
742 }
743 
744 /// @}
745