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