xref: /libCEED/include/ceed.h (revision f0d2f928220c74787e2a8b65d655802dcef082ab) !
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 CeedDestroy(Ceed *ceed);
118 
119 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int,
120                               const char *, ...);
121 /// Raise an error on ceed object
122 ///
123 /// @param ceed Ceed library context or NULL
124 /// @param ecode Error code (int)
125 /// @param ... printf-style format string followed by arguments as needed
126 ///
127 /// @ingroup Ceed
128 /// @sa CeedSetErrorHandler()
129 #if defined(__clang__)
130 // Use nonstandard ternary to convince the compiler/clang-tidy that this
131 // function never returns zero.
132 #  define CeedError(ceed, ecode, ...)                                     \
133   (CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__) ?: (ecode))
134 #else
135 #  define CeedError(ceed, ecode, ...)                                     \
136   CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__) ?: (ecode)
137 #endif
138 /// Specify memory type
139 ///
140 /// Many Ceed interfaces take or return pointers to memory.  This enum is used to
141 /// specify where the memory being provided or requested must reside.
142 /// @ingroup Ceed
143 typedef enum {
144   /// Memory resides on the host
145   CEED_MEM_HOST,
146   /// Memory resides on a device (corresponding to \ref Ceed resource)
147   CEED_MEM_DEVICE,
148 } CeedMemType;
149 
150 CEED_EXTERN int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type);
151 
152 /// Conveys ownership status of arrays passed to Ceed interfaces.
153 /// @ingroup Ceed
154 typedef enum {
155   /// Implementation will copy the values and not store the passed pointer.
156   CEED_COPY_VALUES,
157   /// Implementation can use and modify the data provided by the user, but does
158   /// not take ownership.
159   CEED_USE_POINTER,
160   /// Implementation takes ownership of the pointer and will free using
161   /// CeedFree() when done using it.  The user should not assume that the
162   /// pointer remains valid after ownership has been transferred.  Note that
163   /// arrays allocated using C++ operator new or other allocators cannot
164   /// generally be freed using CeedFree().  CeedFree() is capable of freeing any
165   /// memory that can be freed using free(3).
166   CEED_OWN_POINTER,
167 } CeedCopyMode;
168 
169 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedInt len, CeedVector *vec);
170 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mtype,
171                                    CeedCopyMode cmode, CeedScalar *array);
172 CEED_EXTERN int CeedVectorSetValue(CeedVector vec, CeedScalar value);
173 CEED_EXTERN int CeedVectorSyncArray(CeedVector vec, CeedMemType mtype);
174 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mtype,
175                                    CeedScalar **array);
176 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype,
177                                        const CeedScalar **array);
178 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array);
179 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec,
180     const CeedScalar **array);
181 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream);
182 CEED_EXTERN int CeedVectorGetLength(CeedVector vec, CeedInt *length);
183 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec);
184 
185 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE;
186 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED;
187 CEED_EXTERN int CeedRequestWait(CeedRequest *req);
188 
189 /// Argument for CeedOperatorSetField that vector is collocated with
190 /// quadrature points, used with QFunction eval mode CEED_EVAL_NONE
191 /// or CEED_EVAL_INTERP only, not with CEED_EVAL_GRAD, CEED_EVAL_DIV,
192 /// or CEED_EVAL_CURL
193 /// @ingroup CeedBasis
194 CEED_EXTERN CeedBasis CEED_BASIS_COLLOCATED;
195 
196 /// Argument for CeedOperatorSetField to use active input or output
197 /// @ingroup CeedVector
198 CEED_EXTERN CeedVector CEED_VECTOR_ACTIVE;
199 
200 /// Argument for CeedOperatorSetField to use no vector, used with
201 /// qfunction input with eval mode CEED_EVAL_WEIGHTS
202 /// @ingroup CeedVector
203 CEED_EXTERN CeedVector CEED_VECTOR_NONE;
204 
205 /// Denotes whether a linear transformation or its transpose should be applied
206 /// @ingroup CeedBasis
207 typedef enum {
208   /// Apply the linear transformation
209   CEED_NOTRANSPOSE,
210   /// Apply the transpose
211   CEED_TRANSPOSE
212 } CeedTransposeMode;
213 
214 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelem,
215     CeedInt elemsize, CeedInt nnodes, CeedInt ncomp, CeedMemType mtype,
216     CeedCopyMode cmode,
217     const CeedInt *indices, CeedElemRestriction *rstr);
218 CEED_EXTERN int CeedElemRestrictionCreateIdentity(Ceed ceed, CeedInt nelem,
219     CeedInt elemsize, CeedInt nnodes, CeedInt ncomp, CeedElemRestriction *rstr);
220 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelem,
221     CeedInt elemsize, CeedInt blksize, CeedInt nnodes, CeedInt ncomp,
222     CeedMemType mtype,
223     CeedCopyMode cmode, const CeedInt *indices, CeedElemRestriction *rstr);
224 CEED_EXTERN int CeedElemRestrictionCreateVector(CeedElemRestriction rstr,
225     CeedVector *lvec, CeedVector *evec);
226 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction rstr,
227     CeedTransposeMode tmode, CeedTransposeMode lmode, CeedVector u,
228     CeedVector ru, CeedRequest *request);
229 CEED_EXTERN int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr,
230     CeedInt block, CeedTransposeMode tmode, CeedTransposeMode lmode,
231     CeedVector u, CeedVector ru, CeedRequest *request);
232 CEED_EXTERN int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr,
233     CeedVector mult);
234 CEED_EXTERN int CeedElemRestrictionCreateVector(CeedElemRestriction rstr,
235     CeedVector *lvec, CeedVector *evec);
236 CEED_EXTERN int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream);
237 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *rstr);
238 
239 // The formalism here is that we have the structure
240 //   \int_\Omega v^T f_0(u, \nabla u, qdata) + (\nabla v)^T f_1(u, \nabla u, qdata)
241 // where gradients are with respect to the reference element.
242 
243 /// Basis evaluation mode
244 ///
245 /// Modes can be bitwise ORed when passing to most functions.
246 /// @ingroup CeedBasis
247 typedef enum {
248   /// Perform no evaluation (either because there is no data or it is already at
249   /// quadrature points)
250   CEED_EVAL_NONE   = 0,
251   /// Interpolate from nodes to quadrature points
252   CEED_EVAL_INTERP = 1,
253   /// Evaluate gradients at quadrature points from input in a nodal basis
254   CEED_EVAL_GRAD   = 2,
255   /// Evaluate divergence at quadrature points from input in a nodal basis
256   CEED_EVAL_DIV    = 4,
257   /// Evaluate curl at quadrature points from input in a nodal basis
258   CEED_EVAL_CURL   = 8,
259   /// Using no input, evaluate quadrature weights on the reference element
260   CEED_EVAL_WEIGHT = 16,
261 } CeedEvalMode;
262 
263 /// Type of quadrature; also used for location of nodes
264 /// @ingroup CeedBasis
265 typedef enum {
266   /// Gauss-Legendre quadrature
267   CEED_GAUSS = 0,
268   /// Gauss-Legendre-Lobatto quadrature
269   CEED_GAUSS_LOBATTO = 1,
270 } CeedQuadMode;
271 
272 /// Type of basis shape to create non-tensor H1 element basis
273 ///
274 /// Dimension can be extracted with bitwise AND
275 /// (CeedElemTopology & 2**(dim + 2)) == TRUE
276 /// @ingroup CeedBasis
277 typedef enum {
278   /// Line
279   CEED_LINE = 1 << 16 | 0,
280   /// Triangle - 2D shape
281   CEED_TRIANGLE = 2 << 16 | 1,
282   /// Quadralateral - 2D shape
283   CEED_QUAD = 2 << 16 | 2,
284   /// Tetrahedron - 3D shape
285   CEED_TET = 3 << 16 | 3,
286   /// Pyramid - 3D shape
287   CEED_PYRAMID = 3 << 16 | 4,
288   /// Prism - 3D shape
289   CEED_PRISM = 3 << 16 | 5,
290   /// Hexehedron - 3D shape
291   CEED_HEX = 3 << 16 | 6,
292 } CeedElemTopology;
293 
294 CEED_EXTERN int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim,
295     CeedInt ncomp, CeedInt P, CeedInt Q, CeedQuadMode qmode, CeedBasis *basis);
296 CEED_EXTERN int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt ncomp,
297                                         CeedInt P1d, CeedInt Q1d,
298                                         const CeedScalar *interp1d,
299                                         const CeedScalar *grad1d,
300                                         const CeedScalar *qref1d,
301                                         const CeedScalar *qweight1d,
302                                         CeedBasis *basis);
303 CEED_EXTERN int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo,
304                                   CeedInt ncomp,
305                                   CeedInt nnodes, CeedInt nqpts,
306                                   const CeedScalar *interp,
307                                   const CeedScalar *grad,
308                                   const CeedScalar *qref,
309                                   const CeedScalar *qweight, CeedBasis *basis);
310 CEED_EXTERN int CeedBasisView(CeedBasis basis, FILE *stream);
311 CEED_EXTERN int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P);
312 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q);
313 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedInt nelem,
314                                CeedTransposeMode tmode,
315                                CeedEvalMode emode, CeedVector u, CeedVector v);
316 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis);
317 
318 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *qref1d,
319                                     CeedScalar *qweight1d);
320 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *qref1d,
321                                       CeedScalar *qweight1d);
322 CEED_EXTERN int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau,
323                                     CeedInt m, CeedInt n);
324 CEED_EXTERN int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat,
325     CeedScalar *lambda, CeedInt n);
326 CEED_EXTERN int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *matA,
327     CeedScalar *matB, CeedScalar *x, CeedScalar *lambda, CeedInt n);
328 
329 /// Handle for the object describing the user CeedQFunction
330 ///
331 /// @param ctx - user-defined context set using CeedQFunctionSetContext() or NULL
332 ///
333 /// @param Q - number of quadrature points at which to evaluate
334 ///
335 /// @param in - array of pointers to each input argument in the order provided
336 ///             by the user in CeedQFunctionAddInput().  Each array has shape
337 ///             `[dim, ncomp, Q]` where `dim` is the geometric dimension for
338 ///             \ref CEED_EVAL_GRAD (`dim=1` for \ref CEED_EVAL_INTERP) and
339 ///             `ncomp` is the number of field components (`ncomp=1` for
340 ///             scalar fields).  This results in indexing the `i`th input at
341 ///             quadrature point `j` as `in[i][(d*ncomp + c)*Q + j]`.
342 ///
343 /// @param out - array of pointers to each output array in the order provided
344 ///              using CeedQFunctionAddOutput().  The shapes are as above for
345 ///              \a in.
346 ///
347 /// @return 0 on success, nonzero for failure.
348 ///
349 /// @ingroup CeedQFunction
350 typedef int (*CeedQFunctionUser)(void *ctx, const CeedInt Q,
351                                  const CeedScalar *const *in,
352                                  CeedScalar *const *out);
353 
354 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
355     CeedQFunctionUser f, const char *source, CeedQFunction *qf);
356 CEED_EXTERN int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name,
357     CeedQFunction *qf);
358 CEED_EXTERN int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size,
359     CeedQFunction *qf);
360 CEED_EXTERN int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
361                                       CeedInt size, CeedEvalMode emode);
362 CEED_EXTERN int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
363                                        CeedInt size, CeedEvalMode emode);
364 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf, void *ctx,
365                                         size_t ctxsize);
366 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
367                                    CeedVector *u, CeedVector *v);
368 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf);
369 
370 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedQFunction qf,
371                                    CeedQFunction dqf, CeedQFunction dqfT,
372                                    CeedOperator *op);
373 CEED_EXTERN int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op);
374 CEED_EXTERN int CeedOperatorSetField(CeedOperator op, const char *fieldname,
375                                      CeedElemRestriction r,
376                                      CeedTransposeMode lmode, CeedBasis b,
377                                      CeedVector v);
378 CEED_EXTERN int CeedCompositeOperatorAddSub(CeedOperator compositeop,
379     CeedOperator subop);
380 CEED_EXTERN int CeedOperatorAssembleLinearQFunction(CeedOperator op,
381     CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request);
382 CEED_EXTERN int CeedOperatorAssembleLinearDiagonal(CeedOperator op,
383     CeedVector *assembled, CeedRequest *request);
384 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in,
385                                   CeedVector out, CeedRequest *request);
386 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
387 
388 /**
389   @brief Return integer power
390 
391   @param[in] base   The base to exponentiate
392   @param[in] power  The power to raise the base to
393 
394   @return base^power
395 
396   @ref Utility
397 **/
398 static inline CeedInt CeedIntPow(CeedInt base, CeedInt power) {
399   CeedInt result = 1;
400   while (power) {
401     if (power & 1) result *= base;
402     power >>= 1;
403     base *= base;
404   }
405   return result;
406 }
407 
408 /**
409   @brief Return minimum of two integers
410 
411   @param[in] a  The first integer to compare
412   @param[in] b  The second integer to compare
413 
414   @return The minimum of the two integers
415 
416   @ref Utility
417 **/
418 static inline CeedInt CeedIntMin(CeedInt a, CeedInt b) { return a < b ? a : b; }
419 
420 #endif
421