xref: /libCEED/include/ceed-impl.h (revision bd33150addfd586a5f97816a8026f45b5a27ec2c)
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 /// Private header for frontend components of libCEED
19 #ifndef _ceed_impl_h
20 #define _ceed_impl_h
21 
22 #include <ceed.h>
23 #include <ceed-backend.h>
24 #include <stdbool.h>
25 
26 /** @defgroup CeedUser Public API for Ceed
27     @ingroup Ceed
28 */
29 /** @defgroup CeedBackend Backend API for Ceed
30     @ingroup Ceed
31 */
32 /** @defgroup CeedDeveloper Internal library functions for Ceed
33     @ingroup Ceed
34 */
35 /** @defgroup CeedVectorUser Public API for CeedVector
36     @ingroup CeedVector
37 */
38 /** @defgroup CeedVectorBackend Backend API for CeedVector
39     @ingroup CeedVector
40 */
41 /** @defgroup CeedVectorDeveloper Internal library functions for CeedVector
42     @ingroup CeedVector
43 */
44 /** @defgroup CeedElemRestrictionUser Public API for CeedElemRestriction
45     @ingroup CeedElemRestriction
46 */
47 /** @defgroup CeedElemRestrictionBackend Backend API for CeedElemRestriction
48     @ingroup CeedElemRestriction
49 */
50 /** @defgroup CeedElemRestrictionDeveloper Internal library functions for CeedElemRestriction
51     @ingroup CeedElemRestriction
52 */
53 /** @defgroup CeedBasisUser Public API for CeedBasis
54     @ingroup CeedBasis
55 */
56 /** @defgroup CeedBasisBackend Backend API for CeedBasis
57     @ingroup CeedBasis
58 */
59 /** @defgroup CeedBasisDeveloper Internal library functions for CeedBasis
60     @ingroup CeedBasis
61 */
62 /** @defgroup CeedQFunctionUser Public API for CeedQFunction
63     @ingroup CeedQFunction
64 */
65 /** @defgroup CeedQFunctionBackend Backend API for CeedQFunction
66     @ingroup CeedQFunction
67 */
68 /** @defgroup CeedQFunctionDeveloper Internal library functions for CeedQFunction
69     @ingroup CeedQFunction
70 */
71 /** @defgroup CeedOperatorUser Public API for CeedOperator
72     @ingroup CeedOperator
73 */
74 /** @defgroup CeedOperatorBackend Backend API for CeedOperator
75     @ingroup CeedOperator
76 */
77 /** @defgroup CeedOperatorDeveloper Internal library functions for CeedOperator
78     @ingroup CeedOperator
79 */
80 
81 // Lookup table field for backend functions
82 typedef struct {
83   const char *fname;
84   size_t offset;
85 } foffset;
86 
87 // Lookup table field for object delegates
88 typedef struct {
89   char *objname;
90   Ceed delegate;
91 } objdelegate;
92 
93 struct Ceed_private {
94   const char *resource;
95   Ceed delegate;
96   Ceed parent;
97   objdelegate *objdelegates;
98   int objdelegatecount;
99   Ceed opfallbackceed, opfallbackparent;
100   const char *opfallbackresource;
101   int (*Error)(Ceed, const char *, int, const char *, int, const char *,
102                va_list);
103   int (*GetPreferredMemType)(CeedMemType *);
104   int (*Destroy)(Ceed);
105   int (*VectorCreate)(CeedInt, CeedVector);
106   int (*ElemRestrictionCreate)(CeedMemType, CeedCopyMode,
107                                const CeedInt *, CeedElemRestriction);
108   int (*ElemRestrictionCreateBlocked)(CeedMemType, CeedCopyMode,
109                                       const CeedInt *, CeedElemRestriction);
110   int (*BasisCreateTensorH1)(CeedInt, CeedInt, CeedInt, const CeedScalar *,
111                              const CeedScalar *, const CeedScalar *,
112                              const CeedScalar *, CeedBasis);
113   int (*BasisCreateH1)(CeedElemTopology, CeedInt, CeedInt, CeedInt,
114                        const CeedScalar *,
115                        const CeedScalar *, const CeedScalar *,
116                        const CeedScalar *, CeedBasis);
117   int (*TensorContractCreate)(CeedBasis, CeedTensorContract);
118   int (*QFunctionCreate)(CeedQFunction);
119   int (*OperatorCreate)(CeedOperator);
120   int (*CompositeOperatorCreate)(CeedOperator);
121   int refcount;
122   void *data;
123   foffset *foffsets;
124 };
125 
126 struct CeedVector_private {
127   Ceed ceed;
128   int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *);
129   int (*SetValue)(CeedVector, CeedScalar);
130   int (*SyncArray)(CeedVector, CeedMemType);
131   int (*GetArray)(CeedVector, CeedMemType, CeedScalar **);
132   int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **);
133   int (*RestoreArray)(CeedVector);
134   int (*RestoreArrayRead)(CeedVector);
135   int (*Norm)(CeedVector, CeedNormType, CeedScalar *);
136   int (*Destroy)(CeedVector);
137   int refcount;
138   CeedInt length;
139   uint64_t state;
140   uint64_t numreaders;
141   void *data;
142 };
143 
144 struct CeedElemRestriction_private {
145   Ceed ceed;
146   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector,
147                CeedRequest *);
148   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector,
149                     CeedVector, CeedRequest *);
150   int (*GetOffsets)(CeedElemRestriction, CeedMemType, const CeedInt **);
151   int (*Destroy)(CeedElemRestriction);
152   int refcount;
153   CeedInt nelem;            /* number of elements */
154   CeedInt elemsize;         /* number of nodes per element */
155   CeedInt ncomp;            /* number of components */
156   CeedInt compstride;       /* Component stride for L-vector ordering */
157   CeedInt lsize;            /* size of the L-vector, can be used for checking
158                                  for correct vector sizes */
159   CeedInt blksize;          /* number of elements in a batch */
160   CeedInt nblk;             /* number of blocks of elements */
161   CeedInt *strides;         /* strides between [nodes, components, elements] */
162   void *data;               /* place for the backend to store any data */
163 };
164 
165 struct CeedBasis_private {
166   Ceed ceed;
167   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode,
168                CeedVector, CeedVector);
169   int (*Destroy)(CeedBasis);
170   int refcount;
171   bool tensorbasis;      /* flag for tensor basis */
172   CeedInt dim;           /* topological dimension */
173   CeedInt ncomp;         /* number of field components (1 for scalar fields) */
174   CeedInt P1d;           /* number of nodes in one dimension */
175   CeedInt Q1d;           /* number of quadrature points in one dimension */
176   CeedInt P;             /* total number of nodes */
177   CeedInt Q;             /* total number of quadrature points */
178   CeedScalar *qref1d;    /* Array of length Q1d holding the locations of
179                               quadrature points on the 1D reference
180                               element [-1, 1] */
181   CeedScalar *qweight1d; /* array of length Q1d holding the quadrature weights on
182                               the reference element */
183   CeedScalar
184   *interp;    /* row-major matrix of shape [Q, P] expressing the values of
185                    nodal basis functions at quadrature points */
186   CeedScalar
187   *interp1d;  /* row-major matrix of shape [Q1d, P1d] expressing the values of
188                    nodal basis functions at quadrature points */
189   CeedScalar
190   *grad;      /* row-major matrix of shape [dim*Q, P] matrix expressing
191                    derivatives of nodal basis functions at quadrature points */
192   CeedScalar
193   *grad1d;    /* row-major matrix of shape [Q1d, P1d] matrix expressing
194                    derivatives of nodal basis functions at quadrature points */
195   CeedTensorContract contract; /* tensor contraction object */
196   void *data;                  /* place for the backend to store any data */
197 };
198 
199 struct CeedTensorContract_private {
200   Ceed ceed;
201   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt,
202                const CeedScalar *restrict, CeedTransposeMode, const CeedInt,
203                const CeedScalar *restrict, CeedScalar *restrict);
204   int (*Destroy)(CeedTensorContract);
205   int refcount;
206   void *data;
207 };
208 
209 struct CeedQFunctionField_private {
210   const char *fieldname;
211   CeedInt size;
212   CeedEvalMode emode;
213 };
214 
215 struct CeedQFunction_private {
216   Ceed ceed;
217   int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *);
218   int (*Destroy)(CeedQFunction);
219   int refcount;
220   CeedInt vlength;    /* Number of quadrature points must be padded to a
221                            multiple of vlength */
222   CeedQFunctionField *inputfields;
223   CeedQFunctionField *outputfields;
224   CeedInt numinputfields, numoutputfields;
225   CeedQFunctionUser function;
226   const char *sourcepath;
227   const char *qfname;
228   bool fortranstatus;
229   bool identity;
230   void *ctx;      /* user context for function */
231   size_t ctxsize; /* size of user context; may be used to copy to a device */
232   void *data;     /* place for the backend to store any data */
233 };
234 
235 /// Struct to handle the context data to use the Fortran QFunction stub
236 /// @ingroup CeedQFunction
237 typedef struct {
238   CeedScalar *innerctx;
239   size_t innerctxsize;
240   void (*f)(void *ctx, int *nq,
241             const CeedScalar *u,const CeedScalar *u1,
242             const CeedScalar *u2,const CeedScalar *u3,
243             const CeedScalar *u4,const CeedScalar *u5,
244             const CeedScalar *u6,const CeedScalar *u7,
245             const CeedScalar *u8,const CeedScalar *u9,
246             const CeedScalar *u10,const CeedScalar *u11,
247             const CeedScalar *u12,const CeedScalar *u13,
248             const CeedScalar *u14,const CeedScalar *u15,
249             CeedScalar *v,CeedScalar *v1,CeedScalar *v2,
250             CeedScalar *v3,CeedScalar *v4,CeedScalar *v5,
251             CeedScalar *v6,CeedScalar *v7,CeedScalar *v8,
252             CeedScalar *v9, CeedScalar *v10,CeedScalar *v11,
253             CeedScalar *v12,CeedScalar *v13,CeedScalar *v14,
254             CeedScalar *v15, int *err);
255 } fContext;
256 
257 struct CeedOperatorField_private {
258   CeedElemRestriction Erestrict; /* Restriction from L-vector */
259   CeedBasis basis;               /* Basis or CEED_BASIS_COLLOCATED for
260                                       collocated fields */
261   CeedVector vec;                /* State vector for passive fields or
262                                       CEED_VECTOR_NONE for no vector */
263 };
264 
265 struct CeedOperator_private {
266   Ceed ceed;
267   CeedOperator opfallback;
268   CeedQFunction qffallback;
269   int refcount;
270   int (*AssembleLinearQFunction)(CeedOperator, CeedVector *,
271                                  CeedElemRestriction *, CeedRequest *);
272   int (*AssembleLinearDiagonal)(CeedOperator, CeedVector *, CeedRequest *);
273   int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *);
274   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
275   int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
276   int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
277   int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
278   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector,
279                        CeedVector, CeedRequest *);
280   int (*Destroy)(CeedOperator);
281   CeedOperatorField *inputfields;
282   CeedOperatorField *outputfields;
283   CeedInt numelements; /// Number of elements
284   CeedInt numqpoints;  /// Number of quadrature points over all elements
285   CeedInt nfields;     /// Number of fields that have been set
286   CeedQFunction qf;
287   CeedQFunction dqf;
288   CeedQFunction dqfT;
289   bool setupdone;
290   bool composite;
291   bool hasrestriction;
292   CeedOperator *suboperators;
293   CeedInt numsub;
294   void *data;
295 };
296 
297 CEED_INTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int,
298                                 const char *, va_list);
299 CEED_INTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int,
300                                const char *, va_list);
301 CEED_INTERN int CeedErrorExit(Ceed, const char *, int, const char *, int,
302                               const char *, va_list);
303 CEED_INTERN int CeedSetErrorHandler(Ceed ceed,
304                                     int (*eh)(Ceed, const char *, int,
305                                         const char *, int, const char *,
306                                         va_list));
307 
308 #endif
309