xref: /libCEED/include/ceed/ceed.h (revision 20a91ff9e4e8bae7f9d22a0703edb132353bc8a8)
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 /// @file
18 /// Public header for user and utility components of libCEED
19 #ifndef _ceed_h
20 #define _ceed_h
21 
22 /// @defgroup Ceed Ceed: core components
23 /// @defgroup CeedVector CeedVector: storing and manipulating vectors
24 /// @defgroup CeedElemRestriction CeedElemRestriction: restriction from local vectors to elements
25 /// @defgroup CeedBasis CeedBasis: fully discrete finite element-like objects
26 /// @defgroup CeedQFunction CeedQFunction: independent operations at quadrature points
27 /// @defgroup CeedOperator CeedOperator: composed FE-type operations on vectors
28 ///
29 /// @page FunctionCategories libCEED: Types of Functions
30 ///    libCEED provides three different header files depending upon the type of
31 ///    functions a user requires.
32 /// @section Utility Utility Functions
33 ///    These functions are intended general utilities that may be useful to
34 ///    libCEED developers and users. These functions can generally be found in
35 ///    "ceed.h".
36 /// @section User User Functions
37 ///    These functions are intended to be used by general users of libCEED
38 ///    and can generally be found in "ceed.h".
39 /// @section Advanced Advanced Functions
40 ///    These functions are intended to be used by advanced users of libCEED
41 ///    and can generally be found in "ceed.h".
42 /// @section Backend Backend Developer Functions
43 ///    These functions are intended to be used by backend developers of
44 ///    libCEED and can generally be found in "ceed-backend.h".
45 /// @section Developer Library Developer Functions
46 ///    These functions are intended to be used by library developers of
47 ///    libCEED and can generally be found in "ceed-impl.h".
48 
49 #if !defined(CEED_SKIP_VISIBILITY)
50 #  define CEED_VISIBILITY(mode) __attribute__((visibility(#mode)))
51 #else
52 #  define CEED_VISIBILITY(mode)
53 #endif
54 
55 /**
56   CEED_EXTERN is used in this header to denote all publicly visible symbols.
57 
58   No other file should declare publicly visible symbols, thus it should never be
59   used outside ceed.h.
60  */
61 #ifdef __cplusplus
62 #  define CEED_EXTERN extern "C" CEED_VISIBILITY(default)
63 #else
64 #  define CEED_EXTERN extern CEED_VISIBILITY(default)
65 #endif
66 
67 /**
68   @ingroup CeedQFunction
69   This macro populates the correct function annotations for User QFunction
70     source for code generation backends or populates default values for CPU
71     backends. It also creates a variable `name_loc` populated with the correct
72     source path for creating the respective User QFunction.
73 **/
74 #ifndef CEED_QFUNCTION
75 #define CEED_QFUNCTION(name) \
76   static const char name ## _loc[] = __FILE__ ":" #name;        \
77   static int name
78 #endif
79 
80 /**
81   @ingroup CeedQFunction
82   This macro populates the correct function annotations for User QFunction
83     helper function source for code generation backends or populates default
84     values for CPU backends.
85 **/
86 #ifndef CEED_QFUNCTION_HELPER
87 #define CEED_QFUNCTION_HELPER static inline
88 #endif
89 
90 /**
91   @ingroup CeedQFunction
92   Using VLA syntax to reshape User QFunction inputs and outputs can make
93     user code more readable. VLA is a C99 feature that is not supported by
94     the C++ dialect used by CUDA. This macro allows users to use the VLA
95     syntax with the CUDA backends.
96 **/
97 #ifndef CEED_Q_VLA
98 #  define CEED_Q_VLA Q
99 #endif
100 
101 /**
102   @ingroup Ceed
103   This macro provides the appropriate SIMD Pragma for the compilation
104     environment. Code generation backends may redefine this macro, as needed.
105 **/
106 #ifndef CeedPragmaSIMD
107 #  if defined(__INTEL_COMPILER)
108 #    define CeedPragmaSIMD _Pragma("vector")
109 // Cannot use Intel pragma ivdep because it miscompiles unpacking symmetric tensors, as in
110 // Poisson2DApply, where the SIMD loop body contains temporaries such as the following.
111 //
112 //     const CeedScalar dXdxdXdxT[2][2] = {{qd[i+0*Q], qd[i+2*Q]},
113 //                                         {qd[i+2*Q], qd[i+1*Q]}};
114 //     for (int j=0; j<2; j++)
115 //        vg[i+j*Q] = (du[0] * dXdxdXdxT[0][j] + du[1] * dXdxdXdxT[1][j]);
116 //
117 // Miscompilation with pragma ivdep observed with icc (ICC) 19.0.5.281 20190815
118 // at -O2 and above.
119 #  elif defined(__GNUC__) && __GNUC__ >= 5
120 #    define CeedPragmaSIMD _Pragma("GCC ivdep")
121 #  elif defined(_OPENMP) && _OPENMP >= 201307 // OpenMP-4.0 (July, 2013)
122 #    define CeedPragmaSIMD _Pragma("omp simd")
123 #  else
124 #    define CeedPragmaSIMD
125 #  endif
126 #endif
127 
128 #include <stdarg.h>
129 #include <stdbool.h>
130 #include <stddef.h>
131 #include <stdint.h>
132 #include <stdio.h>
133 
134 /// Integer type, used for indexing
135 /// @ingroup Ceed
136 typedef int32_t CeedInt;
137 #define CeedInt_FMT "d"
138 
139 /// Integer type, used array sizes
140 /// @ingroup Ceed
141 typedef ptrdiff_t CeedSize;
142 
143 /// Scalar (floating point) types
144 ///
145 /// @ingroup Ceed
146 typedef enum {
147   /// Single precision
148   CEED_SCALAR_FP32,
149   /// Double precision
150   CEED_SCALAR_FP64
151 } CeedScalarType;
152 /// Base scalar type for the library to use: change which header is
153 /// included to change the precision.
154 #include "ceed-f64.h"
155 
156 /// Library context created by CeedInit()
157 /// @ingroup CeedUser
158 typedef struct Ceed_private *Ceed;
159 /// Non-blocking Ceed interfaces return a CeedRequest.
160 /// To perform an operation immediately, pass \ref CEED_REQUEST_IMMEDIATE instead.
161 /// @ingroup CeedUser
162 typedef struct CeedRequest_private *CeedRequest;
163 /// Handle for vectors over the field \ref CeedScalar
164 /// @ingroup CeedVectorUser
165 typedef struct CeedVector_private *CeedVector;
166 /// Handle for object describing restriction to elements
167 /// @ingroup CeedElemRestrictionUser
168 typedef struct CeedElemRestriction_private *CeedElemRestriction;
169 /// Handle for object describing discrete finite element evaluations
170 /// @ingroup CeedBasisUser
171 typedef struct CeedBasis_private *CeedBasis;
172 /// Handle for object describing CeedQFunction fields
173 /// @ingroup CeedQFunctionBackend
174 typedef struct CeedQFunctionField_private *CeedQFunctionField;
175 /// Handle for object describing functions evaluated independently at quadrature points
176 /// @ingroup CeedQFunctionUser
177 typedef struct CeedQFunction_private *CeedQFunction;
178 /// Handle for object describing CeedOperator fields
179 /// @ingroup CeedOperatorBackend
180 typedef struct CeedOperatorField_private *CeedOperatorField;
181 /// Handle for object describing context data for CeedQFunctions
182 /// @ingroup CeedQFunctionUser
183 typedef struct CeedQFunctionContext_private *CeedQFunctionContext;
184 /// Handle for object describing registered fields for CeedQFunctionContext
185 /// @ingroup CeedQFunctionUser
186 typedef struct CeedContextFieldLabel_private *CeedContextFieldLabel;
187 /// Handle for object describing FE-type operators acting on vectors
188 ///
189 /// Given an element restriction \f$E\f$, basis evaluator \f$B\f$, and
190 ///   quadrature function\f$f\f$, a CeedOperator expresses operations of the form
191 ///   $$ E^T B^T f(B E u) $$
192 ///   acting on the vector \f$u\f$.
193 /// @ingroup CeedOperatorUser
194 typedef struct CeedOperator_private *CeedOperator;
195 
196 CEED_EXTERN int CeedRegistryGetList(size_t *n, char ***const resources, CeedInt **array);
197 CEED_EXTERN int CeedInit(const char *resource, Ceed *ceed);
198 CEED_EXTERN int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy);
199 CEED_EXTERN int CeedGetResource(Ceed ceed, const char **resource);
200 CEED_EXTERN int CeedIsDeterministic(Ceed ceed, bool *is_deterministic);
201 CEED_EXTERN int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root);
202 CEED_EXTERN int CeedView(Ceed ceed, FILE *stream);
203 CEED_EXTERN int CeedDestroy(Ceed *ceed);
204 
205 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int,
206                               const char *, ...);
207 /// Raise an error on ceed object
208 ///
209 /// @param ceed Ceed library context or NULL
210 /// @param ecode Error code (int)
211 /// @param ... printf-style format string followed by arguments as needed
212 ///
213 /// @ingroup Ceed
214 /// @sa CeedSetErrorHandler()
215 #if defined(__clang__)
216 /// Use nonstandard ternary to convince the compiler/clang-tidy that this
217 /// function never returns zero.
218 #  define CeedError(ceed, ecode, ...)                                     \
219   (CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__), (ecode))
220 #else
221 #  define CeedError(ceed, ecode, ...)                                     \
222   CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__) ?: (ecode)
223 #endif
224 
225 /// Ceed error handlers
226 CEED_EXTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int,
227                                 const char *, va_list *);
228 CEED_EXTERN int CeedErrorStore(Ceed, const char *, int, const char *, int,
229                                const char *, va_list *);
230 CEED_EXTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int,
231                                const char *, va_list *);
232 CEED_EXTERN int CeedErrorExit(Ceed, const char *, int, const char *, int,
233                               const char *, va_list *);
234 typedef int (*CeedErrorHandler)(Ceed, const char *, int,
235                                 const char *, int, const char *,
236                                 va_list *);
237 CEED_EXTERN int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler eh);
238 CEED_EXTERN int CeedGetErrorMessage(Ceed, const char **err_msg);
239 CEED_EXTERN int CeedResetErrorMessage(Ceed, const char **err_msg);
240 
241 /// libCEED library version numbering
242 /// @ingroup Ceed
243 #define CEED_VERSION_MAJOR 0
244 #define CEED_VERSION_MINOR 10
245 #define CEED_VERSION_PATCH 1
246 #define CEED_VERSION_RELEASE false
247 
248 /// Compile-time check that the the current library version is at least as
249 /// recent as the specified version. This macro is typically used in
250 /// @code
251 /// #if CEED_VERSION_GE(0, 8, 0)
252 ///   code path that needs at least 0.8.0
253 /// #else
254 ///   fallback code for older versions
255 /// #endif
256 /// @endcode
257 ///
258 /// A non-release version always compares as positive infinity.
259 ///
260 /// @param major   Major version
261 /// @param minor   Minor version
262 /// @param patch   Patch (subminor) version
263 ///
264 /// @ingroup Ceed
265 /// @sa CeedGetVersion()
266 #define CEED_VERSION_GE(major, minor, patch)                                   \
267   (!CEED_VERSION_RELEASE ||                                                    \
268    (CEED_VERSION_MAJOR > major ||                                              \
269     (CEED_VERSION_MAJOR == major &&                                            \
270      (CEED_VERSION_MINOR > minor ||                                            \
271       (CEED_VERSION_MINOR == minor && CEED_VERSION_PATCH >= patch)))))
272 
273 CEED_EXTERN int CeedGetVersion(int *major, int *minor, int *patch,
274                                bool *release);
275 
276 CEED_EXTERN int CeedGetScalarType(CeedScalarType *scalar_type);
277 
278 /// Ceed Errors
279 ///
280 /// This enum is used to specify the type of error returned by a function.
281 /// A zero error code is success, negative error codes indicate terminal errors
282 /// and positive error codes indicate nonterminal errors. With nonterminal errors
283 /// the object state has not been modifiend, but with terminal errors the object
284 /// data is likely modified or corrupted.
285 /// @ingroup Ceed
286 typedef enum {
287   /// Success error code
288   CEED_ERROR_SUCCESS      = 0,
289   /// Minor error, generic
290   CEED_ERROR_MINOR        = 1,
291   /// Minor error, dimension mismatch in inputs
292   CEED_ERROR_DIMENSION    = 2,
293   /// Minor error, incomplete object setup
294   CEED_ERROR_INCOMPLETE   = 3,
295   /// Minor error, incompatible arguments/configuration
296   CEED_ERROR_INCOMPATIBLE = 4,
297   /// Minor error, access lock problem
298   CEED_ERROR_ACCESS       = 5,
299   /// Major error, generic
300   CEED_ERROR_MAJOR        = -1,
301   /// Major error, internal backend error
302   CEED_ERROR_BACKEND      = -2,
303   /// Major error, operation unsupported by current backend
304   CEED_ERROR_UNSUPPORTED  = -3,
305 } CeedErrorType;
306 CEED_EXTERN const char *const *CeedErrorTypes;
307 
308 /// Specify memory type
309 ///
310 /// Many Ceed interfaces take or return pointers to memory.  This enum is used to
311 /// specify where the memory being provided or requested must reside.
312 /// @ingroup Ceed
313 typedef enum {
314   /// Memory resides on the host
315   CEED_MEM_HOST,
316   /// Memory resides on a device (corresponding to \ref Ceed resource)
317   CEED_MEM_DEVICE,
318 } CeedMemType;
319 CEED_EXTERN const char *const CeedMemTypes[];
320 
321 CEED_EXTERN int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type);
322 
323 /// Conveys ownership status of arrays passed to Ceed interfaces.
324 /// @ingroup Ceed
325 typedef enum {
326   /// Implementation will copy the values and not store the passed pointer.
327   CEED_COPY_VALUES,
328   /// Implementation can use and modify the data provided by the user, but does
329   /// not take ownership.
330   CEED_USE_POINTER,
331   /// Implementation takes ownership of the pointer and will free using
332   /// CeedFree() when done using it.  The user should not assume that the
333   /// pointer remains valid after ownership has been transferred.  Note that
334   /// arrays allocated using C++ operator new or other allocators cannot
335   /// generally be freed using CeedFree().  CeedFree() is capable of freeing any
336   /// memory that can be freed using free(3).
337   CEED_OWN_POINTER,
338 } CeedCopyMode;
339 CEED_EXTERN const char *const CeedCopyModes[];
340 
341 /// Denotes type of vector norm to be computed
342 /// @ingroup CeedVector
343 typedef enum {
344   /// L_1 norm: sum_i |x_i|
345   CEED_NORM_1,
346   /// L_2 norm: sqrt(sum_i |x_i|^2)
347   CEED_NORM_2,
348   /// L_Infinity norm: max_i |x_i|
349   CEED_NORM_MAX,
350 } CeedNormType;
351 
352 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedSize len, CeedVector *vec);
353 CEED_EXTERN int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy);
354 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type,
355                                    CeedCopyMode copy_mode, CeedScalar *array);
356 CEED_EXTERN int CeedVectorSetValue(CeedVector vec, CeedScalar value);
357 CEED_EXTERN int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type);
358 CEED_EXTERN int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type,
359                                     CeedScalar **array);
360 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type,
361                                    CeedScalar **array);
362 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type,
363                                        const CeedScalar **array);
364 CEED_EXTERN int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type,
365                                         CeedScalar **array);
366 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array);
367 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec,
368     const CeedScalar **array);
369 CEED_EXTERN int CeedVectorNorm(CeedVector vec, CeedNormType type,
370                                CeedScalar *norm);
371 CEED_EXTERN int CeedVectorScale(CeedVector x, CeedScalar alpha);
372 CEED_EXTERN int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x);
373 CEED_EXTERN int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y);
374 CEED_EXTERN int CeedVectorReciprocal(CeedVector vec);
375 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream);
376 CEED_EXTERN int CeedVectorGetCeed(CeedVector vec, Ceed *ceed);
377 CEED_EXTERN int CeedVectorGetLength(CeedVector vec, CeedSize *length);
378 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec);
379 
380 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE;
381 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED;
382 CEED_EXTERN int CeedRequestWait(CeedRequest *req);
383 
384 /// Argument for CeedOperatorSetField that vector is collocated with
385 /// quadrature points, only used with CeedEvalMode CEED_EVAL_NONE
386 /// @ingroup CeedBasis
387 CEED_EXTERN const CeedBasis CEED_BASIS_COLLOCATED;
388 
389 /// Argument for CeedOperatorSetField to use active input or output
390 /// @ingroup CeedVector
391 CEED_EXTERN const CeedVector CEED_VECTOR_ACTIVE;
392 
393 /// Argument for CeedOperatorSetField to use no vector, used with
394 /// qfunction input with eval mode CEED_EVAL_WEIGHT
395 /// @ingroup CeedVector
396 CEED_EXTERN const CeedVector CEED_VECTOR_NONE;
397 
398 /// Argument for CeedOperatorSetField to use no ElemRestriction, only used with
399 /// eval mode CEED_EVAL_WEIGHT.
400 /// @ingroup CeedElemRestriction
401 CEED_EXTERN const CeedElemRestriction CEED_ELEMRESTRICTION_NONE;
402 
403 /// Argument for CeedOperatorCreate that QFunction is not created by user.
404 /// Only used for QFunctions dqf and dqfT. If implemented, a backend may
405 /// attempt to provide the action of these QFunctions.
406 /// @ingroup CeedQFunction
407 CEED_EXTERN const CeedQFunction CEED_QFUNCTION_NONE;
408 
409 /// Denotes whether a linear transformation or its transpose should be applied
410 /// @ingroup CeedBasis
411 typedef enum {
412   /// Apply the linear transformation
413   CEED_NOTRANSPOSE,
414   /// Apply the transpose
415   CEED_TRANSPOSE
416 } CeedTransposeMode;
417 CEED_EXTERN const char *const CeedTransposeModes[];
418 
419 /// Argument for CeedElemRestrictionCreateStrided that L-vector is in
420 /// the Ceed backend's preferred layout. This argument should only be used
421 /// with vectors created by a Ceed backend.
422 /// @ingroup CeedElemRestriction
423 CEED_EXTERN const CeedInt CEED_STRIDES_BACKEND[3];
424 
425 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt num_elem,
426     CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
427     CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets,
428     CeedElemRestriction *rstr);
429 CEED_EXTERN int CeedElemRestrictionCreateOriented(Ceed ceed, CeedInt num_elem,
430     CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
431     CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets,
432     const bool *orient, CeedElemRestriction *rstr);
433 CEED_EXTERN int CeedElemRestrictionCreateStrided(Ceed ceed,
434     CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedSize l_size,
435     const CeedInt strides[3], CeedElemRestriction *rstr);
436 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt num_elem,
437     CeedInt elem_size, CeedInt blk_size, CeedInt num_comp, CeedInt comp_stride,
438     CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
439     const CeedInt *offsets, CeedElemRestriction *rstr);
440 CEED_EXTERN int CeedElemRestrictionCreateBlockedStrided(Ceed ceed,
441     CeedInt num_elem, CeedInt elem_size, CeedInt blk_size, CeedInt num_comp,
442     CeedSize l_size, const CeedInt strides[3], CeedElemRestriction *rstr);
443 CEED_EXTERN int CeedElemRestrictionReferenceCopy(CeedElemRestriction rstr,
444     CeedElemRestriction *rstr_copy);
445 CEED_EXTERN int CeedElemRestrictionCreateVector(CeedElemRestriction rstr,
446     CeedVector *lvec, CeedVector *evec);
447 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction rstr,
448     CeedTransposeMode t_mode, CeedVector u, CeedVector ru, CeedRequest *request);
449 CEED_EXTERN int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr,
450     CeedInt block, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
451     CeedRequest *request);
452 CEED_EXTERN int CeedElemRestrictionGetCeed(CeedElemRestriction rstr,
453     Ceed *ceed);
454 CEED_EXTERN int CeedElemRestrictionGetCompStride(CeedElemRestriction rstr,
455     CeedInt *comp_stride);
456 CEED_EXTERN int CeedElemRestrictionGetNumElements(CeedElemRestriction rstr,
457     CeedInt *num_elem);
458 CEED_EXTERN int CeedElemRestrictionGetElementSize(CeedElemRestriction rstr,
459     CeedInt *elem_size);
460 CEED_EXTERN int CeedElemRestrictionGetLVectorSize(CeedElemRestriction rstr,
461     CeedSize *l_size);
462 CEED_EXTERN int CeedElemRestrictionGetNumComponents(CeedElemRestriction rstr,
463     CeedInt *num_comp);
464 CEED_EXTERN int CeedElemRestrictionGetNumBlocks(CeedElemRestriction rstr,
465     CeedInt *num_blk);
466 CEED_EXTERN int CeedElemRestrictionGetBlockSize(CeedElemRestriction rstr,
467     CeedInt *blk_size);
468 CEED_EXTERN int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr,
469     CeedVector mult);
470 CEED_EXTERN int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream);
471 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *rstr);
472 
473 // The formalism here is that we have the structure
474 //  \int_\Omega v^T f_0(u, \nabla u, qdata) + (\nabla v)^T f_1(u, \nabla u, qdata)
475 // where gradients are with respect to the reference element.
476 
477 /// Basis evaluation mode
478 ///
479 /// Modes can be bitwise ORed when passing to most functions.
480 /// @ingroup CeedBasis
481 typedef enum {
482   /// Perform no evaluation (either because there is no data or it is already at
483   /// quadrature points)
484   CEED_EVAL_NONE   = 0,
485   /// Interpolate from nodes to quadrature points
486   CEED_EVAL_INTERP = 1,
487   /// Evaluate gradients at quadrature points from input in a nodal basis
488   CEED_EVAL_GRAD   = 2,
489   /// Evaluate divergence at quadrature points from input in a nodal basis
490   CEED_EVAL_DIV    = 4,
491   /// Evaluate curl at quadrature points from input in a nodal basis
492   CEED_EVAL_CURL   = 8,
493   /// Using no input, evaluate quadrature weights on the reference element
494   CEED_EVAL_WEIGHT = 16,
495 } CeedEvalMode;
496 CEED_EXTERN const char *const CeedEvalModes[];
497 
498 /// Type of quadrature; also used for location of nodes
499 /// @ingroup CeedBasis
500 typedef enum {
501   /// Gauss-Legendre quadrature
502   CEED_GAUSS         = 0,
503   /// Gauss-Legendre-Lobatto quadrature
504   CEED_GAUSS_LOBATTO = 1,
505 } CeedQuadMode;
506 CEED_EXTERN const char *const CeedQuadModes[];
507 
508 /// Type of basis shape to create non-tensor H1 element basis
509 ///
510 /// Dimension can be extracted with bitwise AND
511 /// (CeedElemTopology & 2**(dim + 2)) == TRUE
512 /// @ingroup CeedBasis
513 typedef enum {
514   /// Line
515   CEED_TOPOLOGY_LINE     = 1 << 16 | 0,
516   /// Triangle - 2D shape
517   CEED_TOPOLOGY_TRIANGLE = 2 << 16 | 1,
518   /// Quadralateral - 2D shape
519   CEED_TOPOLOGY_QUAD     = 2 << 16 | 2,
520   /// Tetrahedron - 3D shape
521   CEED_TOPOLOGY_TET      = 3 << 16 | 3,
522   /// Pyramid - 3D shape
523   CEED_TOPOLOGY_PYRAMID  = 3 << 16 | 4,
524   /// Prism - 3D shape
525   CEED_TOPOLOGY_PRISM    = 3 << 16 | 5,
526   /// Hexehedron - 3D shape
527   CEED_TOPOLOGY_HEX      = 3 << 16 | 6,
528 } CeedElemTopology;
529 CEED_EXTERN const char *const CeedElemTopologies[];
530 
531 CEED_EXTERN int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim,
532     CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis);
533 CEED_EXTERN int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp,
534                                         CeedInt P_1d, CeedInt Q_1d,
535                                         const CeedScalar *interp_1d,
536                                         const CeedScalar *grad_1d,
537                                         const CeedScalar *q_ref_1d,
538                                         const CeedScalar *q_weight_1d,
539                                         CeedBasis *basis);
540 CEED_EXTERN int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo,
541                                   CeedInt num_comp,
542                                   CeedInt num_nodes, CeedInt nqpts,
543                                   const CeedScalar *interp,
544                                   const CeedScalar *grad,
545                                   const CeedScalar *q_ref,
546                                   const CeedScalar *q_weights, CeedBasis *basis);
547 CEED_EXTERN int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo,
548                                     CeedInt num_comp,
549                                     CeedInt num_nodes, CeedInt nqpts,
550                                     const CeedScalar *interp,
551                                     const CeedScalar *div,
552                                     const CeedScalar *q_ref,
553                                     const CeedScalar *q_weights, CeedBasis *basis);
554 CEED_EXTERN int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project);
555 CEED_EXTERN int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy);
556 CEED_EXTERN int CeedBasisView(CeedBasis basis, FILE *stream);
557 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedInt num_elem,
558                                CeedTransposeMode t_mode,
559                                CeedEvalMode eval_mode, CeedVector u, CeedVector v);
560 CEED_EXTERN int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed);
561 CEED_EXTERN int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim);
562 CEED_EXTERN int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo);
563 CEED_EXTERN int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedInt *Q_comp);
564 CEED_EXTERN int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp);
565 CEED_EXTERN int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P);
566 CEED_EXTERN int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d);
567 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q);
568 CEED_EXTERN int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis,
569     CeedInt *Q_1d);
570 CEED_EXTERN int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref);
571 CEED_EXTERN int CeedBasisGetQWeights(CeedBasis basis,
572                                      const CeedScalar **q_weights);
573 CEED_EXTERN int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp);
574 CEED_EXTERN int CeedBasisGetInterp1D(CeedBasis basis,
575                                      const CeedScalar **interp_1d);
576 CEED_EXTERN int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad);
577 CEED_EXTERN int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d);
578 CEED_EXTERN int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div);
579 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis);
580 
581 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d,
582                                     CeedScalar *q_weight_1d);
583 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d,
584                                       CeedScalar *q_weight_1d);
585 CEED_EXTERN int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau,
586                                     CeedInt m, CeedInt n);
587 CEED_EXTERN int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat,
588     CeedScalar *lambda, CeedInt n);
589 CEED_EXTERN int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A,
590     CeedScalar *mat_B, CeedScalar *x, CeedScalar *lambda, CeedInt n);
591 
592 /** Handle for the user provided CeedQFunction callback function
593 
594  @param[in,out] ctx  User-defined context set using CeedQFunctionSetContext() or NULL
595  @param[in] Q        Number of quadrature points at which to evaluate
596  @param[in] in       Array of pointers to each input argument in the order provided
597                        by the user in CeedQFunctionAddInput().  Each array has shape
598                        `[dim, num_comp, Q]` where `dim` is the geometric dimension for
599                        \ref CEED_EVAL_GRAD (`dim=1` for \ref CEED_EVAL_INTERP) and
600                        `num_comp` is the number of field components (`num_comp=1` for
601                        scalar fields).  This results in indexing the `i`th input at
602                        quadrature point `j` as `in[i][(d*num_comp + c)*Q + j]`.
603  @param[out]   out   Array of pointers to each output array in the order provided
604                        using CeedQFunctionAddOutput().  The shapes are as above for
605                        \a in.
606 
607  @return An error code: 0 - success, otherwise - failure
608 
609  @ingroup CeedQFunction
610 **/
611 typedef int (*CeedQFunctionUser)(void *ctx, const CeedInt Q,
612                                  const CeedScalar *const *in,
613                                  CeedScalar *const *out);
614 
615 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
616     CeedQFunctionUser f, const char *source, CeedQFunction *qf);
617 CEED_EXTERN int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name,
618     CeedQFunction *qf);
619 CEED_EXTERN int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size,
620     CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf);
621 CEED_EXTERN int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy);
622 CEED_EXTERN int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
623                                       CeedInt size, CeedEvalMode eval_mode);
624 CEED_EXTERN int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
625                                        CeedInt size, CeedEvalMode eval_mode);
626 CEED_EXTERN int CeedQFunctionGetFields(CeedQFunction qf,
627                                        CeedInt *num_input_fields,
628                                        CeedQFunctionField **input_fields,
629                                        CeedInt *num_output_fields,
630                                        CeedQFunctionField **output_fields);
631 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf,
632                                         CeedQFunctionContext ctx);
633 CEED_EXTERN int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable);
634 CEED_EXTERN int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops);
635 CEED_EXTERN int CeedQFunctionView(CeedQFunction qf, FILE *stream);
636 CEED_EXTERN int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed);
637 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
638                                    CeedVector *u, CeedVector *v);
639 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf);
640 
641 CEED_EXTERN int CeedQFunctionFieldGetName(CeedQFunctionField qf_field,
642     char **field_name);
643 CEED_EXTERN int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field,
644     CeedInt *size);
645 CEED_EXTERN int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
646     CeedEvalMode *eval_mode);
647 
648 /// Denotes type of data stored in a CeedQFunctionContext field
649 /// @ingroup CeedQFunction
650 typedef enum {
651   /// Double precision value
652   CEED_CONTEXT_FIELD_DOUBLE = 1,
653   /// 32 bit integer value
654   CEED_CONTEXT_FIELD_INT32  = 2,
655 } CeedContextFieldType;
656 CEED_EXTERN const char *const CeedContextFieldTypes[];
657 
658 /** Handle for the user provided CeedQFunctionContextDataDestroy callback function
659 
660  @param[in,out] data  User-CeedQFunctionContext data
661 
662  @return An error code: 0 - success, otherwise - failure
663 
664  @ingroup CeedQFunction
665 **/
666 typedef int (*CeedQFunctionContextDataDestroyUser)(void *data);
667 
668 CEED_EXTERN int CeedQFunctionContextCreate(Ceed ceed,
669     CeedQFunctionContext *ctx);
670 CEED_EXTERN int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx,
671     CeedQFunctionContext *ctx_copy);
672 CEED_EXTERN int CeedQFunctionContextSetData(CeedQFunctionContext ctx,
673     CeedMemType mem_type, CeedCopyMode copy_mode, size_t size, void *data);
674 CEED_EXTERN int CeedQFunctionContextTakeData(CeedQFunctionContext ctx,
675     CeedMemType mem_type, void *data);
676 CEED_EXTERN int CeedQFunctionContextGetData(CeedQFunctionContext ctx,
677     CeedMemType mem_type, void *data);
678 CEED_EXTERN int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx,
679     CeedMemType mem_type, void *data);
680 CEED_EXTERN int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx,
681     void *data);
682 CEED_EXTERN int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx,
683     void *data);
684 CEED_EXTERN int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx,
685     const char *field_name, size_t field_offset, size_t num_values,
686     const char *field_description);
687 CEED_EXTERN int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx,
688     const char *field_name, size_t field_offset, size_t num_values,
689     const char *field_description);
690 CEED_EXTERN int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx,
691     const CeedContextFieldLabel **field_labels, CeedInt *num_fields);
692 CEED_EXTERN int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label,
693     const char **field_name, const char **field_description, size_t *num_values,
694     CeedContextFieldType *field_type);
695 CEED_EXTERN int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx,
696     size_t *ctx_size);
697 CEED_EXTERN int CeedQFunctionContextView(CeedQFunctionContext ctx,
698     FILE *stream);
699 CEED_EXTERN int CeedQFunctionContextSetDataDestroy(CeedQFunctionContext ctx,
700     CeedMemType f_mem_type, CeedQFunctionContextDataDestroyUser f);
701 CEED_EXTERN int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx);
702 
703 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedQFunction qf,
704                                    CeedQFunction dqf, CeedQFunction dqfT,
705                                    CeedOperator *op);
706 CEED_EXTERN int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op);
707 CEED_EXTERN int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy);
708 CEED_EXTERN int CeedOperatorSetField(CeedOperator op, const char *field_name,
709                                      CeedElemRestriction r, CeedBasis b,
710                                      CeedVector v);
711 CEED_EXTERN int CeedOperatorGetFields(CeedOperator op,
712                                       CeedInt *num_input_fields,
713                                       CeedOperatorField **input_fields,
714                                       CeedInt *num_output_fields,
715                                       CeedOperatorField **output_fields);
716 CEED_EXTERN int CeedCompositeOperatorAddSub(CeedOperator composite_op,
717     CeedOperator sub_op);
718 CEED_EXTERN int CeedOperatorCheckReady(CeedOperator op);
719 CEED_EXTERN int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size);
720 CEED_EXTERN int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data);
721 CEED_EXTERN int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update);
722 CEED_EXTERN int CeedOperatorLinearAssembleQFunction(CeedOperator op,
723     CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request);
724 CEED_EXTERN int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op,
725     CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request);
726 CEED_EXTERN int CeedOperatorLinearAssembleDiagonal(CeedOperator op,
727     CeedVector assembled, CeedRequest *request);
728 CEED_EXTERN int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op,
729     CeedVector assembled, CeedRequest *request);
730 CEED_EXTERN int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op,
731     CeedVector assembled, CeedRequest *request);
732 CEED_EXTERN int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op,
733     CeedVector assembled, CeedRequest *request);
734 CEED_EXTERN int CeedOperatorLinearAssembleSymbolic(CeedOperator op,
735      CeedSize *num_entries, CeedInt **rows, CeedInt **cols);
736 CEED_EXTERN int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values);
737 CEED_EXTERN int CeedOperatorMultigridLevelCreate(CeedOperator op_fine,
738     CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
739     CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict);
740 CEED_EXTERN int CeedOperatorMultigridLevelCreateTensorH1(
741   CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse,
742   CeedBasis basis_coarse, const CeedScalar *interp_c_to_f, CeedOperator *op_coarse,
743   CeedOperator *op_prolong, CeedOperator *op_restrict);
744 CEED_EXTERN int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine,
745     CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
746     const CeedScalar *interp_c_to_f, CeedOperator *op_coarse,
747     CeedOperator *op_prolong, CeedOperator *op_restrict);
748 CEED_EXTERN int CeedOperatorCreateFDMElementInverse(CeedOperator op,
749     CeedOperator *fdm_inv, CeedRequest *request);
750 CEED_EXTERN int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts);
751 CEED_EXTERN int CeedOperatorSetName(CeedOperator op, const char *name);
752 CEED_EXTERN int CeedOperatorView(CeedOperator op, FILE *stream);
753 CEED_EXTERN int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed);
754 CEED_EXTERN int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem);
755 CEED_EXTERN int CeedOperatorGetNumQuadraturePoints(CeedOperator op,
756     CeedInt *num_qpts);
757 CEED_EXTERN int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops);
758 CEED_EXTERN int CeedOperatorContextGetFieldLabel(CeedOperator op,
759     const char *field_name, CeedContextFieldLabel *field_label);
760 CEED_EXTERN int CeedOperatorContextSetDouble(CeedOperator op,
761     CeedContextFieldLabel field_label, double *values);
762 CEED_EXTERN int CeedOperatorContextSetInt32(CeedOperator op,
763     CeedContextFieldLabel field_label, int *values);
764 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in,
765                                   CeedVector out, CeedRequest *request);
766 CEED_EXTERN int CeedOperatorApplyAdd(CeedOperator op, CeedVector in,
767                                      CeedVector out, CeedRequest *request);
768 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
769 
770 CEED_EXTERN int CeedOperatorFieldGetName(CeedOperatorField op_field,
771     char **field_name);
772 CEED_EXTERN int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field,
773     CeedElemRestriction *rstr);
774 CEED_EXTERN int CeedOperatorFieldGetBasis(CeedOperatorField op_field,
775     CeedBasis *basis);
776 CEED_EXTERN int CeedOperatorFieldGetVector(CeedOperatorField op_field,
777     CeedVector *vec);
778 
779 /**
780   @brief Return integer power
781 
782   @param[in] base   The base to exponentiate
783   @param[in] power  The power to raise the base to
784 
785   @return base^power
786 
787   @ref Utility
788 **/
789 static inline CeedInt CeedIntPow(CeedInt base, CeedInt power) {
790   CeedInt result = 1;
791   while (power) {
792     if (power & 1) result *= base;
793     power >>= 1;
794     base *= base;
795   }
796   return result;
797 }
798 
799 /**
800   @brief Return minimum of two integers
801 
802   @param[in] a  The first integer to compare
803   @param[in] b  The second integer to compare
804 
805   @return The minimum of the two integers
806 
807   @ref Utility
808 **/
809 static inline CeedInt CeedIntMin(CeedInt a, CeedInt b) { return a < b ? a : b; }
810 
811 /**
812   @brief Return maximum of two integers
813 
814   @param[in] a  The first integer to compare
815   @param[in] b  The second integer to compare
816 
817   @return The maximum of the two integers
818 
819   @ref Utility
820 **/
821 static inline CeedInt CeedIntMax(CeedInt a, CeedInt b) { return a > b ? a : b; }
822 
823 // Used to ensure initialization before CeedInit()
824 CEED_EXTERN int CeedRegisterAll(void);
825 // Used to ensure initialization before CeedQFunctionCreate*()
826 CEED_EXTERN int CeedQFunctionRegisterAll(void);
827 
828 #endif
829