xref: /libCEED/include/ceed.h (revision 34138859218ba2ce39a42cdecc70f28818628fe8)
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 Basic User Functions
37 ///    These functions are intended to be used by general users of the libCEED
38 ///    interface. These functions can generally be found in "ceed.h".
39 /// @section Advanced Backend Developer Functions
40 ///    These functions are intended to be used by backend developers of the
41 ///    libCEED interface. These functions can generally be found in
42 ///    "ceed-backend.h".
43 /// @section Developer Frontend Developer Functions
44 ///    These functions are intended to be used by frontend developers of the
45 ///    libCEED interface. These functions can generally be found in "ceed-impl.h".
46 
47 /**
48   CEED_EXTERN is used in this header to denote all publicly visible symbols.
49 
50   No other file should declare publicly visible symbols, thus it should never be
51   used outside ceed.h.
52  */
53 #ifdef __cplusplus
54 #  define CEED_EXTERN extern "C"
55 #else
56 #  define CEED_EXTERN extern
57 #endif
58 
59 #ifndef CEED_QFUNCTION
60 #define CEED_QFUNCTION(name) \
61   static const char name ## _loc[] = __FILE__ ":" #name;        \
62   static int name
63 #endif
64 
65 #ifndef CeedPragmaSIMD
66 #  if defined(__GNUC__) && __GNUC__ >= 5
67 #    define CeedPragmaSIMD _Pragma("GCC ivdep")
68 #  elif defined(_OPENMP) && _OPENMP >= 201307 // OpenMP-4.0 (July, 2013)
69 #    define CeedPragmaSIMD _Pragma("omp simd")
70 #  else
71 #    define CeedPragmaSIMD
72 #  endif
73 #endif
74 
75 #include <assert.h>
76 #include <stdint.h>
77 #include <stddef.h>
78 #include <stdarg.h>
79 #include <stdio.h>
80 #include <stdbool.h>
81 
82 /// We can discuss ways to avoid forcing these to be compile-time decisions,
83 ///   but let's leave that for later.
84 /// Integer type, used for indexing
85 /// @ingroup Ceed
86 typedef int32_t CeedInt;
87 /// Scalar (floating point) type
88 /// @ingroup Ceed
89 typedef double CeedScalar;
90 
91 /// Library context created by CeedInit()
92 /// @ingroup Ceed
93 typedef struct Ceed_private *Ceed;
94 /// Non-blocking Ceed interfaces return a CeedRequest.
95 /// To perform an operation immediately, pass \ref CEED_REQUEST_IMMEDIATE instead.
96 /// @ingroup Ceed
97 typedef struct CeedRequest_private *CeedRequest;
98 /// Handle for vectors over the field \ref CeedScalar
99 /// @ingroup CeedVector
100 typedef struct CeedVector_private *CeedVector;
101 /// Handle for object describing restriction to elements
102 /// @ingroup CeedElemRestriction
103 typedef struct CeedElemRestriction_private *CeedElemRestriction;
104 /// Handle for object describing discrete finite element evaluations
105 /// @ingroup CeedBasis
106 typedef struct CeedBasis_private *CeedBasis;
107 /// Handle for object describing functions evaluated independently at quadrature points
108 /// @ingroup CeedQFunction
109 typedef struct CeedQFunction_private *CeedQFunction;
110 /// Handle for object describing FE-type operators acting on vectors
111 ///
112 /// Given an element restriction \f$E\f$, basis evaluator \f$B\f$, and
113 ///   quadrature function\f$f\f$, a CeedOperator expresses operations of the form
114 ///   $$ E^T B^T f(B E u) $$
115 ///   acting on the vector \f$u\f$.
116 /// @ingroup CeedOperator
117 typedef struct CeedOperator_private *CeedOperator;
118 
119 CEED_EXTERN int CeedInit(const char *resource, Ceed *ceed);
120 CEED_EXTERN int CeedGetResource(Ceed ceed, const char **resource);
121 CEED_EXTERN int CeedDestroy(Ceed *ceed);
122 
123 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int,
124                               const char *, ...);
125 /// Raise an error on ceed object
126 ///
127 /// @param ceed Ceed library context or NULL
128 /// @param ecode Error code (int)
129 /// @param ... printf-style format string followed by arguments as needed
130 ///
131 /// @ingroup Ceed
132 /// @sa CeedSetErrorHandler()
133 #if defined(__clang__)
134 /// Use nonstandard ternary to convince the compiler/clang-tidy that this
135 /// function never returns zero.
136 #  define CeedError(ceed, ecode, ...)                                     \
137   (CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__) ?: (ecode))
138 #else
139 #  define CeedError(ceed, ecode, ...)                                     \
140   CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__) ?: (ecode)
141 #endif
142 /// Specify memory type
143 ///
144 /// Many Ceed interfaces take or return pointers to memory.  This enum is used to
145 /// specify where the memory being provided or requested must reside.
146 /// @ingroup Ceed
147 typedef enum {
148   /// Memory resides on the host
149   CEED_MEM_HOST,
150   /// Memory resides on a device (corresponding to \ref Ceed resource)
151   CEED_MEM_DEVICE,
152 } CeedMemType;
153 
154 CEED_EXTERN const char *const CeedMemTypes[];
155 
156 CEED_EXTERN int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type);
157 
158 /// Conveys ownership status of arrays passed to Ceed interfaces.
159 /// @ingroup Ceed
160 typedef enum {
161   /// Implementation will copy the values and not store the passed pointer.
162   CEED_COPY_VALUES,
163   /// Implementation can use and modify the data provided by the user, but does
164   /// not take ownership.
165   CEED_USE_POINTER,
166   /// Implementation takes ownership of the pointer and will free using
167   /// CeedFree() when done using it.  The user should not assume that the
168   /// pointer remains valid after ownership has been transferred.  Note that
169   /// arrays allocated using C++ operator new or other allocators cannot
170   /// generally be freed using CeedFree().  CeedFree() is capable of freeing any
171   /// memory that can be freed using free(3).
172   CEED_OWN_POINTER,
173 } CeedCopyMode;
174 
175 CEED_EXTERN const char *const CeedCopyModes[];
176 
177 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedInt len, CeedVector *vec);
178 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mtype,
179                                    CeedCopyMode cmode, CeedScalar *array);
180 CEED_EXTERN int CeedVectorSetValue(CeedVector vec, CeedScalar value);
181 CEED_EXTERN int CeedVectorSyncArray(CeedVector vec, CeedMemType mtype);
182 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mtype,
183                                    CeedScalar **array);
184 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype,
185                                        const CeedScalar **array);
186 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array);
187 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec,
188     const CeedScalar **array);
189 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream);
190 CEED_EXTERN int CeedVectorGetLength(CeedVector vec, CeedInt *length);
191 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec);
192 
193 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE;
194 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED;
195 CEED_EXTERN int CeedRequestWait(CeedRequest *req);
196 
197 /// Argument for CeedOperatorSetField that vector is collocated with
198 /// quadrature points, used with QFunction eval mode CEED_EVAL_NONE
199 /// or CEED_EVAL_INTERP only, not with CEED_EVAL_GRAD, CEED_EVAL_DIV,
200 /// or CEED_EVAL_CURL
201 /// @ingroup CeedBasis
202 CEED_EXTERN CeedBasis CEED_BASIS_COLLOCATED;
203 
204 /// Argument for CeedOperatorSetField to use active input or output
205 /// @ingroup CeedVector
206 CEED_EXTERN CeedVector CEED_VECTOR_ACTIVE;
207 
208 /// Argument for CeedOperatorSetField to use no vector, used with
209 /// qfunction input with eval mode CEED_EVAL_WEIGHTS
210 /// @ingroup CeedVector
211 CEED_EXTERN CeedVector CEED_VECTOR_NONE;
212 
213 /// Argument for CeedOperatorCreate that QFunction is not created by user.
214 /// Only used for QFunctions dqf and dqfT. If implemented, a backend may
215 /// attempt to provide the action of these QFunctions.
216 /// @ingroup CeedQFunction
217 CEED_EXTERN CeedQFunction CEED_QFUNCTION_NONE;
218 
219 /// Denotes whether a linear transformation or its transpose should be applied
220 /// @ingroup CeedBasis
221 typedef enum {
222   /// Apply the linear transformation
223   CEED_NOTRANSPOSE,
224   /// Apply the transpose
225   CEED_TRANSPOSE
226 } CeedTransposeMode;
227 
228 CEED_EXTERN const char *const CeedTransposeModes[];
229 
230 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelem,
231     CeedInt elemsize, CeedInt nnodes, CeedInt ncomp, CeedMemType mtype,
232     CeedCopyMode cmode,
233     const CeedInt *indices, CeedElemRestriction *rstr);
234 CEED_EXTERN int CeedElemRestrictionCreateIdentity(Ceed ceed, CeedInt nelem,
235     CeedInt elemsize, CeedInt nnodes, CeedInt ncomp, CeedElemRestriction *rstr);
236 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelem,
237     CeedInt elemsize, CeedInt blksize, CeedInt nnodes, CeedInt ncomp,
238     CeedMemType mtype,
239     CeedCopyMode cmode, const CeedInt *indices, CeedElemRestriction *rstr);
240 CEED_EXTERN int CeedElemRestrictionCreateVector(CeedElemRestriction rstr,
241     CeedVector *lvec, CeedVector *evec);
242 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction rstr,
243     CeedTransposeMode tmode, CeedTransposeMode lmode, CeedVector u,
244     CeedVector ru, CeedRequest *request);
245 CEED_EXTERN int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr,
246     CeedInt block, CeedTransposeMode tmode, CeedTransposeMode lmode,
247     CeedVector u, CeedVector ru, CeedRequest *request);
248 CEED_EXTERN int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr,
249     CeedVector mult);
250 CEED_EXTERN int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream);
251 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *rstr);
252 
253 /// The formalism here is that we have the structure
254 ///   \int_\Omega v^T f_0(u, \nabla u, qdata) + (\nabla v)^T f_1(u, \nabla u, qdata)
255 /// where gradients are with respect to the reference element.
256 
257 /// Basis evaluation mode
258 ///
259 /// Modes can be bitwise ORed when passing to most functions.
260 /// @ingroup CeedBasis
261 typedef enum {
262   /// Perform no evaluation (either because there is no data or it is already at
263   /// quadrature points)
264   CEED_EVAL_NONE   = 0,
265   /// Interpolate from nodes to quadrature points
266   CEED_EVAL_INTERP = 1,
267   /// Evaluate gradients at quadrature points from input in a nodal basis
268   CEED_EVAL_GRAD   = 2,
269   /// Evaluate divergence at quadrature points from input in a nodal basis
270   CEED_EVAL_DIV    = 4,
271   /// Evaluate curl at quadrature points from input in a nodal basis
272   CEED_EVAL_CURL   = 8,
273   /// Using no input, evaluate quadrature weights on the reference element
274   CEED_EVAL_WEIGHT = 16,
275 } CeedEvalMode;
276 
277 CEED_EXTERN const char *const CeedEvalModes[];
278 
279 /// Type of quadrature; also used for location of nodes
280 /// @ingroup CeedBasis
281 typedef enum {
282   /// Gauss-Legendre quadrature
283   CEED_GAUSS = 0,
284   /// Gauss-Legendre-Lobatto quadrature
285   CEED_GAUSS_LOBATTO = 1,
286 } CeedQuadMode;
287 
288 CEED_EXTERN const char *const CeedQuadModes[];
289 
290 /// Type of basis shape to create non-tensor H1 element basis
291 ///
292 /// Dimension can be extracted with bitwise AND
293 /// (CeedElemTopology & 2**(dim + 2)) == TRUE
294 /// @ingroup CeedBasis
295 typedef enum {
296   /// Line
297   CEED_LINE = 1 << 16 | 0,
298   /// Triangle - 2D shape
299   CEED_TRIANGLE = 2 << 16 | 1,
300   /// Quadralateral - 2D shape
301   CEED_QUAD = 2 << 16 | 2,
302   /// Tetrahedron - 3D shape
303   CEED_TET = 3 << 16 | 3,
304   /// Pyramid - 3D shape
305   CEED_PYRAMID = 3 << 16 | 4,
306   /// Prism - 3D shape
307   CEED_PRISM = 3 << 16 | 5,
308   /// Hexehedron - 3D shape
309   CEED_HEX = 3 << 16 | 6,
310 } CeedElemTopology;
311 
312 CEED_EXTERN const char *const CeedElemTopologies[];
313 
314 CEED_EXTERN int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim,
315     CeedInt ncomp, CeedInt P, CeedInt Q, CeedQuadMode qmode, CeedBasis *basis);
316 CEED_EXTERN int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt ncomp,
317                                         CeedInt P1d, CeedInt Q1d,
318                                         const CeedScalar *interp1d,
319                                         const CeedScalar *grad1d,
320                                         const CeedScalar *qref1d,
321                                         const CeedScalar *qweight1d,
322                                         CeedBasis *basis);
323 CEED_EXTERN int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo,
324                                   CeedInt ncomp,
325                                   CeedInt nnodes, CeedInt nqpts,
326                                   const CeedScalar *interp,
327                                   const CeedScalar *grad,
328                                   const CeedScalar *qref,
329                                   const CeedScalar *qweight, CeedBasis *basis);
330 CEED_EXTERN int CeedBasisView(CeedBasis basis, FILE *stream);
331 CEED_EXTERN int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P);
332 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q);
333 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedInt nelem,
334                                CeedTransposeMode tmode,
335                                CeedEvalMode emode, CeedVector u, CeedVector v);
336 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis);
337 
338 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *qref1d,
339                                     CeedScalar *qweight1d);
340 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *qref1d,
341                                       CeedScalar *qweight1d);
342 CEED_EXTERN int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau,
343                                     CeedInt m, CeedInt n);
344 CEED_EXTERN int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat,
345     CeedScalar *lambda, CeedInt n);
346 CEED_EXTERN int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *matA,
347     CeedScalar *matB, CeedScalar *x, CeedScalar *lambda, CeedInt n);
348 
349 /** Handle for the object describing the user CeedQFunction
350 
351  @param ctx user-defined context set using CeedQFunctionSetContext() or NULL
352 
353  @param Q   number of quadrature points at which to evaluate
354 
355  @param in  array of pointers to each input argument in the order provided
356               by the user in CeedQFunctionAddInput().  Each array has shape
357               `[dim, ncomp, Q]` where `dim` is the geometric dimension for
358               \ref CEED_EVAL_GRAD (`dim=1` for \ref CEED_EVAL_INTERP) and
359               `ncomp` is the number of field components (`ncomp=1` for
360               scalar fields).  This results in indexing the `i`th input at
361               quadrature point `j` as `in[i][(d*ncomp + c)*Q + j]`.
362 
363  @param out array of pointers to each output array in the order provided
364               using CeedQFunctionAddOutput().  The shapes are as above for
365               \a in.
366 
367  @return An error code: 0 - success, otherwise - failure
368 
369  @ingroup CeedQFunction
370 **/
371 typedef int (*CeedQFunctionUser)(void *ctx, const CeedInt Q,
372                                  const CeedScalar *const *in,
373                                  CeedScalar *const *out);
374 
375 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
376     CeedQFunctionUser f, const char *source, CeedQFunction *qf);
377 CEED_EXTERN int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name,
378     CeedQFunction *qf);
379 CEED_EXTERN int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size,
380     CeedEvalMode inmode, CeedEvalMode outmode, CeedQFunction *qf);
381 CEED_EXTERN int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
382                                       CeedInt size, CeedEvalMode emode);
383 CEED_EXTERN int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
384                                        CeedInt size, CeedEvalMode emode);
385 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf, void *ctx,
386                                         size_t ctxsize);
387 CEED_EXTERN int CeedQFunctionView(CeedQFunction qf, FILE *stream);
388 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
389                                    CeedVector *u, CeedVector *v);
390 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf);
391 
392 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedQFunction qf,
393                                    CeedQFunction dqf, CeedQFunction dqfT,
394                                    CeedOperator *op);
395 CEED_EXTERN int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op);
396 CEED_EXTERN int CeedOperatorSetField(CeedOperator op, const char *fieldname,
397                                      CeedElemRestriction r,
398                                      CeedTransposeMode lmode, CeedBasis b,
399                                      CeedVector v);
400 CEED_EXTERN int CeedCompositeOperatorAddSub(CeedOperator compositeop,
401     CeedOperator subop);
402 CEED_EXTERN int CeedOperatorAssembleLinearQFunction(CeedOperator op,
403     CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request);
404 CEED_EXTERN int CeedOperatorAssembleLinearDiagonal(CeedOperator op,
405     CeedVector *assembled, CeedRequest *request);
406 CEED_EXTERN int CeedOperatorView(CeedOperator op, FILE *stream);
407 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in,
408                                   CeedVector out, CeedRequest *request);
409 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
410 
411 /**
412   @brief Return integer power
413 
414   @param[in] base   The base to exponentiate
415   @param[in] power  The power to raise the base to
416 
417   @return base^power
418 
419   @ref Utility
420 **/
421 static inline CeedInt CeedIntPow(CeedInt base, CeedInt power) {
422   CeedInt result = 1;
423   while (power) {
424     if (power & 1) result *= base;
425     power >>= 1;
426     base *= base;
427   }
428   return result;
429 }
430 
431 /**
432   @brief Return minimum of two integers
433 
434   @param[in] a  The first integer to compare
435   @param[in] b  The second integer to compare
436 
437   @return The minimum of the two integers
438 
439   @ref Utility
440 **/
441 static inline CeedInt CeedIntMin(CeedInt a, CeedInt b) { return a < b ? a : b; }
442 
443 #endif
444