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