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