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