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 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 Function Categories 30 /// @section Utility Utility Functions 31 /// These functions are intended general utilities that may be useful to libCEED developers and users. 32 /// @section Basic User Functions 33 /// These functions are intended to be used by general users of the libCEED interface. 34 /// @section Advanced Backend Developer Functions 35 /// These functions are intended to be used by backend developers of the libCEED interface. 36 /// @section Developer Frontend Developer Functions 37 /// These functions are intended to be used by frontend developers of the libCEED interface. 38 39 /** 40 CEED_EXTERN is used in this header to denote all publicly visible symbols. 41 42 No other file should declare publicly visible symbols, thus it should never be 43 used outside ceed.h. 44 */ 45 #ifdef __cplusplus 46 # define CEED_EXTERN extern "C" 47 #else 48 # define CEED_EXTERN extern 49 #endif 50 51 #include <assert.h> 52 #include <stdint.h> 53 #include <stddef.h> 54 #include <stdarg.h> 55 #include <stdio.h> 56 57 // We can discuss ways to avoid forcing these to be compile-time decisions, but let's leave that for later. 58 /// Integer type, used for indexing 59 /// @ingroup Ceed 60 typedef int32_t CeedInt; 61 /// Scalar (floating point) type 62 /// @ingroup Ceed 63 typedef double CeedScalar; 64 65 /// Library context created by CeedInit() 66 /// @ingroup Ceed 67 typedef struct Ceed_private *Ceed; 68 /// Non-blocking Ceed interfaces return a CeedRequest. 69 /// To perform an operation immediately, pass \ref CEED_REQUEST_IMMEDIATE instead. 70 /// @ingroup Ceed 71 typedef struct CeedRequest_private *CeedRequest; 72 /// Handle for vectors over the field \ref CeedScalar 73 /// @ingroup CeedVector 74 typedef struct CeedVector_private *CeedVector; 75 /// Handle for object describing restriction to elements 76 /// @ingroup CeedElemRestriction 77 typedef struct CeedElemRestriction_private *CeedElemRestriction; 78 /// Handle for object describing discrete finite element evaluations 79 /// @ingroup CeedBasis 80 typedef struct CeedBasis_private *CeedBasis; 81 /// Handle for object describing functions evaluated independently at quadrature points 82 /// @ingroup CeedQFunction 83 typedef struct CeedQFunction_private *CeedQFunction; 84 /// Handle for object describing FE-type operators acting on vectors 85 /// 86 /// Given an element restriction \f$E\f$, basis evaluator \f$B\f$, and quadrature function 87 /// \f$f\f$, a CeedOperator expresses operations of the form 88 /// $$ E^T B^T f(B E u) $$ 89 /// acting on the vector \f$u\f$. 90 typedef struct CeedOperator_private *CeedOperator; 91 92 CEED_EXTERN int CeedRegister(const char *prefix, 93 int (*init)(const char *, Ceed), unsigned int priority); 94 95 CEED_EXTERN int CeedInit(const char *resource, Ceed *ceed); 96 CEED_EXTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int, 97 const char *, va_list); 98 CEED_EXTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int, 99 const char *, va_list); 100 CEED_EXTERN int CeedErrorExit(Ceed, const char *, int, const char *, int, 101 const char *, va_list); 102 CEED_EXTERN int CeedSetErrorHandler(Ceed ceed, 103 int (eh)(Ceed, const char *, int, const char *, 104 int, const char *, va_list)); 105 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int, 106 const char *, ...); 107 /// Raise an error on ceed object 108 /// 109 /// @param ceed Ceed library context or NULL 110 /// @param ecode Error code (int) 111 /// @param ... printf-style format string followed by arguments as needed 112 /// 113 /// @ingroup Ceed 114 /// @sa CeedSetErrorHandler() 115 #define CeedError(ceed, ecode, ...) \ 116 CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__) 117 CEED_EXTERN int CeedDestroy(Ceed *ceed); 118 119 /// Specify memory type 120 /// 121 /// Many Ceed interfaces take or return pointers to memory. This enum is used to 122 /// specify where the memory being provided or requested must reside. 123 /// @ingroup Ceed 124 typedef enum { 125 /// Memory resides on the host 126 CEED_MEM_HOST, 127 /// Memory resides on a device (corresponding to \ref Ceed resource) 128 CEED_MEM_DEVICE, 129 } CeedMemType; 130 131 /// Conveys ownership status of arrays passed to Ceed interfaces. 132 /// @ingroup Ceed 133 typedef enum { 134 /// Implementation will copy the values and not store the passed pointer. 135 CEED_COPY_VALUES, 136 /// Implementation can use and modify the data provided by the user, but does 137 /// not take ownership. 138 CEED_USE_POINTER, 139 /// Implementation takes ownership of the pointer and will free using 140 /// CeedFree() when done using it. The user should not assume that the 141 /// pointer remains valid after ownership has been transferred. Note that 142 /// arrays allocated using C++ operator new or other allocators cannot 143 /// generally be freed using CeedFree(). CeedFree() is capable of freeing any 144 /// memory that can be freed using free(3). 145 CEED_OWN_POINTER, 146 } CeedCopyMode; 147 148 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedInt len, CeedVector *vec); 149 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mtype, 150 CeedCopyMode cmode, CeedScalar *array); 151 CEED_EXTERN int CeedVectorSetValue(CeedVector vec, CeedScalar value); 152 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mtype, 153 CeedScalar **array); 154 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype, 155 const CeedScalar **array); 156 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array); 157 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec, 158 const CeedScalar **array); 159 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream); 160 CEED_EXTERN int CeedVectorGetLength(CeedVector vec, CeedInt *length); 161 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec); 162 163 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE; 164 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED; 165 CEED_EXTERN int CeedRequestWait(CeedRequest *req); 166 167 /// Argument for CeedOperatorSetField that vector is collocated with 168 /// quadrature points, used with qfunction eval mode CEED_EVAL_NONE 169 /// or CEED_EVAL_INTERP only, not with CEED_EVAL_GRAD, CEED_EVAL_DIV, 170 /// or CEED_EVAL_CURL 171 /// @ingroup CeedBasis 172 CEED_EXTERN CeedBasis CEED_BASIS_COLLOCATED; 173 174 /// Argument for CeedOperatorSetField to use active input or output 175 /// @ingroup CeedVector 176 CEED_EXTERN CeedVector CEED_VECTOR_ACTIVE; 177 178 /// Argument for CeedOperatorSetField to use no vector, used with 179 /// qfunction input with eval mode CEED_EVAL_WEIGHTS 180 /// @ingroup CeedVector 181 CEED_EXTERN CeedVector CEED_VECTOR_NONE; 182 183 /// Denotes whether a linear transformation or its transpose should be applied 184 /// @ingroup CeedBasis 185 typedef enum { 186 /// Apply the linear transformation 187 CEED_NOTRANSPOSE, 188 /// Apply the transpose 189 CEED_TRANSPOSE 190 } CeedTransposeMode; 191 192 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelem, 193 CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedMemType mtype, 194 CeedCopyMode cmode, 195 const CeedInt *indices, CeedElemRestriction *r); 196 CEED_EXTERN int CeedElemRestrictionCreateIdentity(Ceed ceed, CeedInt nelem, 197 CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedElemRestriction *r); 198 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelem, 199 CeedInt elemsize, CeedInt blksize, CeedInt ndof, CeedInt ncomp, 200 CeedMemType mtype, 201 CeedCopyMode cmode, const CeedInt *indices, CeedElemRestriction *r); 202 CEED_EXTERN int CeedElemRestrictionCreateVector(CeedElemRestriction r, 203 CeedVector *lvec, 204 CeedVector *evec); 205 CEED_EXTERN int CeedElemRestrictionGetNumElements(CeedElemRestriction r, 206 CeedInt *numelements); 207 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction r, 208 CeedTransposeMode tmode, CeedTransposeMode lmode, CeedVector u, 209 CeedVector ru, CeedRequest *request); 210 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *r); 211 212 // The formalism here is that we have the structure 213 // \int_\Omega v^T f_0(u, \nabla u, qdata) + (\nabla v)^T f_1(u, \nabla u, qdata) 214 // where gradients are with respect to the reference element. 215 216 /// Basis evaluation mode 217 /// 218 /// Modes can be bitwise ORed when passing to most functions. 219 /// @ingroup CeedBasis 220 typedef enum { 221 /// Perform no evaluation (either because there is no data or it is already at 222 /// quadrature points) 223 CEED_EVAL_NONE = 0, 224 /// Interpolate from nodes to quadrature points 225 CEED_EVAL_INTERP = 1, 226 /// Evaluate gradients at quadrature points from input in a nodal basis 227 CEED_EVAL_GRAD = 2, 228 /// Evaluate divergence at quadrature points from input in a nodal basis 229 CEED_EVAL_DIV = 4, 230 /// Evaluate curl at quadrature points from input in a nodal basis 231 CEED_EVAL_CURL = 8, 232 /// Using no input, evaluate quadrature weights on the reference element 233 CEED_EVAL_WEIGHT = 16, 234 } CeedEvalMode; 235 236 /// Type of quadrature; also used for location of nodes 237 /// @ingroup CeedBasis 238 typedef enum { 239 /// Gauss-Legendre quadrature 240 CEED_GAUSS = 0, 241 /// Gauss-Legendre-Lobatto quadrature 242 CEED_GAUSS_LOBATTO = 1, 243 } CeedQuadMode; 244 245 /// Type of basis shape to create non-tensor H1 element basis 246 /// 247 /// Dimension can be extracted with bitwise AND 248 /// (CeedElemTopology & 2**(dim + 2)) == TRUE 249 /// @ingroup CeedBasis 250 typedef enum { 251 /// Line 252 CEED_LINE = 1 << 16 | 0, 253 /// Triangle - 2D shape 254 CEED_TRIANGLE = 2 << 16 | 1, 255 /// Quadralateral - 2D shape 256 CEED_QUAD = 2 << 16 | 2, 257 /// Tetrahedron - 3D shape 258 CEED_TET = 3 << 16 | 3, 259 /// Pyramid - 3D shape 260 CEED_PYRAMID = 3 << 16 | 4, 261 /// Prism - 3D shape 262 CEED_PRISM = 3 << 16 | 5, 263 /// Hexehedron - 3D shape 264 CEED_HEX = 3 << 16 | 6, 265 } CeedElemTopology; 266 267 CEED_EXTERN int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, 268 CeedInt ncomp, CeedInt P, CeedInt Q, CeedQuadMode qmode, CeedBasis *basis); 269 CEED_EXTERN int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt ncomp, 270 CeedInt P1d, CeedInt Q1d, const CeedScalar *interp1d, const CeedScalar *grad1d, 271 const CeedScalar *qref1d, const CeedScalar *qweight1d, CeedBasis *basis); 272 CEED_EXTERN int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, 273 CeedInt ncomp, 274 CeedInt ndof, CeedInt nqpts, 275 const CeedScalar *interp, const CeedScalar *grad, 276 const CeedScalar *qref, const CeedScalar *qweight, CeedBasis *basis); 277 CEED_EXTERN int CeedBasisView(CeedBasis basis, FILE *stream); 278 CEED_EXTERN int CeedQRFactorization(CeedScalar *mat, CeedScalar *tau, CeedInt m, 279 CeedInt n); 280 CEED_EXTERN int CeedBasisGetCollocatedGrad(CeedBasis basis, 281 CeedScalar *colograd1d); 282 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedInt nelem, 283 CeedTransposeMode tmode, 284 CeedEvalMode emode, const CeedScalar *u, CeedScalar *v); 285 CEED_EXTERN int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P); 286 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q); 287 CEED_EXTERN int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim); 288 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis); 289 290 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *qref1d, 291 CeedScalar *qweight1d); 292 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *qref1d, 293 CeedScalar *qweight1d); 294 295 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, 296 int (*f)(void *ctx, CeedInt nq, const CeedScalar *const *u, 297 CeedScalar *const *v), const char *focca, CeedQFunction *qf); 298 CEED_EXTERN int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, 299 CeedInt ncomp, CeedEvalMode emode); 300 CEED_EXTERN int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname, 301 CeedInt ncomp, CeedEvalMode emode); 302 CEED_EXTERN int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput, 303 CeedInt *numoutput); 304 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, 305 size_t ctxsize); 306 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 307 const CeedScalar *const *u, 308 CeedScalar *const *v); 309 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf); 310 311 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, 312 CeedQFunction dqf, CeedQFunction dqfT, 313 CeedOperator *op); 314 CEED_EXTERN int CeedOperatorSetField(CeedOperator op, const char *fieldname, 315 CeedElemRestriction r, CeedBasis b, 316 CeedVector v); 317 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in, 318 CeedVector out, CeedRequest *request); 319 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op); 320 321 /** 322 @brief Return integer power 323 324 @param[in] base The base to exponentiate 325 @param[in] power The power to raise the base to 326 327 @return base^power 328 329 @ref Utility 330 **/ 331 static inline CeedInt CeedIntPow(CeedInt base, CeedInt power) { 332 CeedInt result = 1; 333 while (power) { 334 if (power & 1) result *= base; 335 power >>= 1; 336 base *= base; 337 } 338 return result; 339 } 340 341 /** 342 @brief Return mimimum of two integers 343 344 @param[in] a The first integer to compare 345 @param[in] b The second integer to compare 346 347 @return The minimum of the two integers 348 349 @ref Utility 350 **/ 351 static inline CeedInt CeedIntMin(CeedInt a, CeedInt b) { return a < b ? a : b; } 352 353 #endif 354