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