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