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