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