xref: /libCEED/interface/ceed-qfunction.c (revision 2d50dd3d74639bc22e590e33d130299cde8043e9)
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 #include <limits.h>
21 
22 /// @cond DOXYGEN_SKIP
23 static struct {
24   char name[CEED_MAX_RESOURCE_LEN];
25   char source[CEED_MAX_RESOURCE_LEN];
26   CeedInt vlength;
27   CeedQFunctionUser f;
28   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
29 } qfunctions[1024];
30 static size_t num_qfunctions;
31 /// @endcond
32 
33 /// @file
34 /// Implementation of public CeedQFunction interfaces
35 ///
36 /// @addtogroup CeedQFunction
37 /// @{
38 
39 /**
40   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
41 
42   @param ceed       A Ceed object where the CeedQFunction will be created
43   @param vlength    Vector length.  Caller must ensure that number of quadrature
44                     points is a multiple of vlength.
45   @param f          Function pointer to evaluate action at quadrature points.
46                     See \ref CeedQFunctionUser.
47   @param source     Absolute path to source of QFunction,
48                       "\abs_path\file.h:function_name"
49   @param[out] qf    Address of the variable where the newly created
50                       CeedQFunction will be stored
51 
52   @return An error code: 0 - success, otherwise - failure
53 
54   See \ref CeedQFunctionUser for details on the call-back function @a f's
55     arguments.
56 
57   @ref Basic
58 **/
59 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
60                                 CeedQFunctionUser f,
61                                 const char *source, CeedQFunction *qf) {
62   int ierr;
63   char *source_copy;
64 
65   if (!ceed->QFunctionCreate) {
66     Ceed delegate;
67     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
68 
69     if (!delegate)
70       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
71 
72     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
73     CeedChk(ierr);
74     return 0;
75   }
76 
77   ierr = CeedCalloc(1,qf); CeedChk(ierr);
78   (*qf)->ceed = ceed;
79   ceed->refcount++;
80   (*qf)->refcount = 1;
81   (*qf)->vlength = vlength;
82   (*qf)->function = f;
83   ierr = CeedCalloc(strlen(source)+1, &source_copy); CeedChk(ierr);
84   strncpy(source_copy, source, strlen(source));
85   (*qf)->sourcepath = source_copy;
86   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
87   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
88   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
89   return 0;
90 }
91 
92 /**
93   @brief Register a gallery QFunction
94 
95   @param name    Name for this backend to respond to
96   @param source  Absolute path to source of QFunction,
97                    "\path\CEED_DIR\gallery\folder\file.h:function_name"
98   @param vlength Vector length.  Caller must ensure that number of quadrature
99                    points is a multiple of vlength.
100   @param f       Function pointer to evaluate action at quadrature points.
101                    See \ref CeedQFunctionUser.
102   @param init    Initialization function called by CeedQFunctionInit() when the
103                    QFunction is selected.
104 
105   @return An error code: 0 - success, otherwise - failure
106 
107   @ref Advanced
108 **/
109 int CeedQFunctionRegister(const char *name, const char *source,
110                           CeedInt vlength, CeedQFunctionUser f,
111                           int (*init)(Ceed, const char *, CeedQFunction)) {
112   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0])) {
113     return CeedError(NULL, 1, "Too many gallery QFunctions");
114   }
115   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
116   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
117   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
118   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
119   qfunctions[num_qfunctions].vlength = vlength;
120   qfunctions[num_qfunctions].f = f;
121   qfunctions[num_qfunctions].init = init;
122   num_qfunctions++;
123   return 0;
124 }
125 
126 /**
127   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
128 
129   @param ceed       A Ceed object where the CeedQFunction will be created
130   @param name       Name of QFunction to use from gallery
131   @param[out] qf    Address of the variable where the newly created
132                       CeedQFunction will be stored
133 
134   @return An error code: 0 - success, otherwise - failure
135 
136   @ref Basic
137 **/
138 int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
139                                       CeedQFunction *qf) {
140   int ierr;
141   size_t matchlen = 0, matchidx = UINT_MAX;
142 
143   // Find matching backend
144   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
145   for (size_t i=0; i<num_qfunctions; i++) {
146     size_t n;
147     const char *currname = qfunctions[i].name;
148     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
149     if (n > matchlen) {
150       matchlen = n;
151       matchidx = i;
152     }
153   }
154   if (!matchlen) return CeedError(NULL, 1, "No suitable gallery QFunction");
155 
156   // Create QFunction
157   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
158                                      qfunctions[matchidx].f,
159                                      qfunctions[matchidx].source, qf);
160   CeedChk(ierr);
161 
162   // QFunction specific setup
163   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
164 
165   return 0;
166 }
167 
168 /**
169   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
170 
171   @param f          CeedQFunctionField
172   @param fieldname  Name of QFunction field
173   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
174                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
175   @param emode      \ref CEED_EVAL_NONE to use values directly,
176                       \ref CEED_EVAL_INTERP to use interpolated values,
177                       \ref CEED_EVAL_GRAD to use gradients.
178 
179   @return An error code: 0 - success, otherwise - failure
180 
181   @ref Developer
182 **/
183 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
184                                  CeedInt size, CeedEvalMode emode) {
185   size_t len = strlen(fieldname);
186   char *tmp;
187   int ierr;
188   ierr = CeedCalloc(1,f); CeedChk(ierr);
189 
190   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
191   memcpy(tmp, fieldname, len+1);
192   (*f)->fieldname = tmp;
193   (*f)->size = size;
194   (*f)->emode = emode;
195   return 0;
196 }
197 
198 /**
199   @brief Add a CeedQFunction input
200 
201   @param qf         CeedQFunction
202   @param fieldname  Name of QFunction field
203   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
204                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
205   @param emode      \ref CEED_EVAL_NONE to use values directly,
206                       \ref CEED_EVAL_INTERP to use interpolated values,
207                       \ref CEED_EVAL_GRAD to use gradients.
208 
209   @return An error code: 0 - success, otherwise - failure
210 
211   @ref Basic
212 **/
213 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
214                           CeedInt size, CeedEvalMode emode) {
215   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
216                                    fieldname, size, emode);
217   CeedChk(ierr);
218   qf->numinputfields++;
219   return 0;
220 }
221 
222 /**
223   @brief Add a CeedQFunction output
224 
225   @param qf         CeedQFunction
226   @param fieldname  Name of QFunction field
227   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
228                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
229   @param emode      \ref CEED_EVAL_NONE to use values directly,
230                       \ref CEED_EVAL_INTERP to use interpolated values,
231                       \ref CEED_EVAL_GRAD to use gradients.
232 
233   @return An error code: 0 - success, otherwise - failure
234 
235   @ref Basic
236 **/
237 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
238                            CeedInt size, CeedEvalMode emode) {
239   if (emode == CEED_EVAL_WEIGHT)
240     return CeedError(qf->ceed, 1,
241                      "Cannot create QFunction output with CEED_EVAL_WEIGHT");
242   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
243                                    fieldname, size, emode);
244   CeedChk(ierr);
245   qf->numoutputfields++;
246   return 0;
247 }
248 
249 /**
250   @brief Get the Ceed associated with a CeedQFunction
251 
252   @param qf              CeedQFunction
253   @param[out] ceed       Variable to store Ceed
254 
255   @return An error code: 0 - success, otherwise - failure
256 
257   @ref Advanced
258 **/
259 
260 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
261   *ceed = qf->ceed;
262   return 0;
263 }
264 
265 /**
266   @brief Get the vector length of a CeedQFunction
267 
268   @param qf            CeedQFunction
269   @param[out] vlength  Variable to store vector length
270 
271   @return An error code: 0 - success, otherwise - failure
272 
273   @ref Advanced
274 **/
275 
276 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
277   *vlength = qf->vlength;
278   return 0;
279 }
280 
281 /**
282   @brief Get the number of inputs and outputs to a CeedQFunction
283 
284   @param qf              CeedQFunction
285   @param[out] numinput   Variable to store number of input fields
286   @param[out] numoutput  Variable to store number of output fields
287 
288   @return An error code: 0 - success, otherwise - failure
289 
290   @ref Advanced
291 **/
292 
293 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
294                             CeedInt *numoutput) {
295   if (numinput) *numinput = qf->numinputfields;
296   if (numoutput) *numoutput = qf->numoutputfields;
297   return 0;
298 }
299 
300 /**
301   @brief Get the source path string for a CeedQFunction
302 
303   @param qf              CeedQFunction
304   @param[out] source     Variable to store source path string
305 
306   @return An error code: 0 - success, otherwise - failure
307 
308   @ref Advanced
309 **/
310 
311 int CeedQFunctionGetSourcePath(CeedQFunction qf, char* *source) {
312   *source = (char *) qf->sourcepath;
313   return 0;
314 }
315 
316 /**
317   @brief Get the User Function for a CeedQFunction
318 
319   @param qf              CeedQFunction
320   @param[out] f          Variable to store user function
321 
322   @return An error code: 0 - success, otherwise - failure
323 
324   @ref Advanced
325 **/
326 
327 int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) {
328   *f = (int (*)())qf->function;
329   return 0;
330 }
331 
332 /**
333   @brief Get global context size for a CeedQFunction
334 
335   @param qf              CeedQFunction
336   @param[out] ctxsize    Variable to store size of context data values
337 
338   @return An error code: 0 - success, otherwise - failure
339 
340   @ref Advanced
341 **/
342 
343 int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
344   if (qf->fortranstatus) {
345     fContext *fctx = qf->ctx;
346     *ctxsize = fctx->innerctxsize;
347   } else {
348     *ctxsize = qf->ctxsize;
349   }
350   return 0;
351 }
352 
353 /**
354   @brief Get global context for a CeedQFunction
355 
356   @param qf              CeedQFunction
357   @param[out] ctx        Variable to store context data values
358 
359   @return An error code: 0 - success, otherwise - failure
360 
361   @ref Advanced
362 **/
363 
364 int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) {
365   *ctx = qf->ctx;
366   return 0;
367 }
368 
369 /**
370   @brief Determine if Fortran interface was used
371 
372   @param qf                  CeedQFunction
373   @param[out] fortranstatus  Variable to store Fortran status
374 
375   @return An error code: 0 - success, otherwise - failure
376 
377   @ref Advanced
378 **/
379 
380 int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
381   *fortranstatus = qf->fortranstatus;
382   return 0;
383 }
384 
385 /**
386   @brief Get true user context for a CeedQFunction
387 
388   @param qf              CeedQFunction
389   @param[out] ctx        Variable to store context data values
390 
391   @return An error code: 0 - success, otherwise - failure
392 
393   @ref Advanced
394 **/
395 
396 int CeedQFunctionGetInnerContext(CeedQFunction qf, void* *ctx) {
397   if (qf->fortranstatus) {
398     fContext *fctx = qf->ctx;
399     *ctx = fctx->innerctx;
400   } else {
401     *ctx = qf->ctx;
402   }
403 
404 
405   return 0;
406 }
407 
408 /**
409   @brief Get backend data of a CeedQFunction
410 
411   @param qf              CeedQFunction
412   @param[out] data       Variable to store data
413 
414   @return An error code: 0 - success, otherwise - failure
415 
416   @ref Advanced
417 **/
418 
419 int CeedQFunctionGetData(CeedQFunction qf, void* *data) {
420   *data = qf->data;
421   return 0;
422 }
423 
424 /**
425   @brief Set backend data of a CeedQFunction
426 
427   @param[out] qf         CeedQFunction
428   @param data            Data to set
429 
430   @return An error code: 0 - success, otherwise - failure
431 
432   @ref Advanced
433 **/
434 
435 int CeedQFunctionSetData(CeedQFunction qf, void* *data) {
436   qf->data = *data;
437   return 0;
438 }
439 
440 /**
441   @brief Set global context for a CeedQFunction
442 
443   @param qf       CeedQFunction
444   @param ctx      Context data to set
445   @param ctxsize  Size of context data values
446 
447   @return An error code: 0 - success, otherwise - failure
448 
449   @ref Basic
450 **/
451 int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
452   qf->ctx = ctx;
453   qf->ctxsize = ctxsize;
454   return 0;
455 }
456 
457 /**
458   @brief Apply the action of a CeedQFunction
459 
460   @param qf      CeedQFunction
461   @param Q       Number of quadrature points
462   @param[in] u   Array of input data arrays
463   @param[out] v  Array of output data arrays
464 
465   @return An error code: 0 - success, otherwise - failure
466 
467   @ref Advanced
468 **/
469 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
470                        CeedVector *u, CeedVector *v) {
471   int ierr;
472   if (!qf->Apply)
473     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
474   if (Q % qf->vlength)
475     return CeedError(qf->ceed, 2,
476                      "Number of quadrature points %d must be a multiple of %d",
477                      Q, qf->vlength);
478   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
479   return 0;
480 }
481 
482 /**
483   @brief Get the CeedQFunctionFields of a CeedQFunction
484 
485   @param qf                 CeedQFunction
486   @param[out] inputfields   Variable to store inputfields
487   @param[out] outputfields  Variable to store outputfields
488 
489   @return An error code: 0 - success, otherwise - failure
490 
491   @ref Advanced
492 **/
493 
494 int CeedQFunctionGetFields(CeedQFunction qf,
495                            CeedQFunctionField* *inputfields,
496                            CeedQFunctionField* *outputfields) {
497   if (inputfields) *inputfields = qf->inputfields;
498   if (outputfields) *outputfields = qf->outputfields;
499   return 0;
500 }
501 
502 /**
503   @brief Get the name of a CeedQFunctionField
504 
505   @param qffield         CeedQFunctionField
506   @param[out] fieldname  Variable to store the field name
507 
508   @return An error code: 0 - success, otherwise - failure
509 
510   @ref Advanced
511 **/
512 
513 int CeedQFunctionFieldGetName(CeedQFunctionField qffield,
514                               char* *fieldname) {
515   *fieldname = (char *)qffield->fieldname;
516   return 0;
517 }
518 
519 /**
520   @brief Get the number of components of a CeedQFunctionField
521 
522   @param qffield    CeedQFunctionField
523   @param[out] size  Variable to store the size of the field
524 
525   @return An error code: 0 - success, otherwise - failure
526 
527   @ref Advanced
528 **/
529 
530 int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
531   *size = qffield->size;
532   return 0;
533 }
534 
535 /**
536   @brief Get the CeedEvalMode of a CeedQFunctionField
537 
538   @param qffield         CeedQFunctionField
539   @param[out] emode      Variable to store the field evaluation mode
540 
541   @return An error code: 0 - success, otherwise - failure
542 
543   @ref Advanced
544 **/
545 
546 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
547                                   CeedEvalMode *emode) {
548   *emode = qffield->emode;
549   return 0;
550 }
551 
552 /**
553   @brief Destroy a CeedQFunction
554 
555   @param qf CeedQFunction to destroy
556 
557   @return An error code: 0 - success, otherwise - failure
558 
559   @ref Basic
560 **/
561 int CeedQFunctionDestroy(CeedQFunction *qf) {
562   int ierr;
563 
564   if (!*qf || --(*qf)->refcount > 0) return 0;
565   // Backend destroy
566   if ((*qf)->Destroy) {
567     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
568   }
569   // Free fields
570   for (int i=0; i<(*qf)->numinputfields; i++) {
571     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
572     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
573   }
574   for (int i=0; i<(*qf)->numoutputfields; i++) {
575     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
576     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
577   }
578   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
579   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
580 
581   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
582   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
583   ierr = CeedFree(qf); CeedChk(ierr);
584   return 0;
585 }
586 
587 /// @}
588