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