xref: /libCEED/include/ceed-impl.h (revision 60f9e2d6e4644aed5c818838050a6aeac7d17af3)
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   bool isDeterministic;
123   void *data;
124   bool debug;
125   foffset *foffsets;
126 };
127 
128 struct CeedVector_private {
129   Ceed ceed;
130   int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *);
131   int (*SetValue)(CeedVector, CeedScalar);
132   int (*SyncArray)(CeedVector, CeedMemType);
133   int (*GetArray)(CeedVector, CeedMemType, CeedScalar **);
134   int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **);
135   int (*RestoreArray)(CeedVector);
136   int (*RestoreArrayRead)(CeedVector);
137   int (*Norm)(CeedVector, CeedNormType, CeedScalar *);
138   int (*Destroy)(CeedVector);
139   int refcount;
140   CeedInt length;
141   uint64_t state;
142   uint64_t numreaders;
143   void *data;
144 };
145 
146 struct CeedElemRestriction_private {
147   Ceed ceed;
148   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector,
149                CeedRequest *);
150   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector,
151                     CeedVector, CeedRequest *);
152   int (*GetOffsets)(CeedElemRestriction, CeedMemType, const CeedInt **);
153   int (*Destroy)(CeedElemRestriction);
154   int refcount;
155   CeedInt nelem;            /* number of elements */
156   CeedInt elemsize;         /* number of nodes per element */
157   CeedInt ncomp;            /* number of components */
158   CeedInt compstride;       /* Component stride for L-vector ordering */
159   CeedInt lsize;            /* size of the L-vector, can be used for checking
160                                  for correct vector sizes */
161   CeedInt blksize;          /* number of elements in a batch */
162   CeedInt nblk;             /* number of blocks of elements */
163   CeedInt *strides;         /* strides between [nodes, components, elements] */
164   CeedInt layout[3];        /* E-vector layout [nodes, components, elements] */
165   uint64_t numreaders;      /* number of instances of offset read only access */
166   void *data;               /* place for the backend to store any data */
167 };
168 
169 struct CeedBasis_private {
170   Ceed ceed;
171   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode,
172                CeedVector, CeedVector);
173   int (*Destroy)(CeedBasis);
174   int refcount;
175   bool tensorbasis;      /* flag for tensor basis */
176   CeedInt dim;           /* topological dimension */
177   CeedInt ncomp;         /* number of field components (1 for scalar fields) */
178   CeedInt P1d;           /* number of nodes in one dimension */
179   CeedInt Q1d;           /* number of quadrature points in one dimension */
180   CeedInt P;             /* total number of nodes */
181   CeedInt Q;             /* total number of quadrature points */
182   CeedScalar *qref1d;    /* Array of length Q1d holding the locations of
183                               quadrature points on the 1D reference
184                               element [-1, 1] */
185   CeedScalar *qweight1d; /* array of length Q1d holding the quadrature weights on
186                               the reference element */
187   CeedScalar
188   *interp;    /* row-major matrix of shape [Q, P] expressing the values of
189                    nodal basis functions at quadrature points */
190   CeedScalar
191   *interp1d;  /* row-major matrix of shape [Q1d, P1d] expressing the values of
192                    nodal basis functions at quadrature points */
193   CeedScalar
194   *grad;      /* row-major matrix of shape [dim*Q, P] matrix expressing
195                    derivatives of nodal basis functions at quadrature points */
196   CeedScalar
197   *grad1d;    /* row-major matrix of shape [Q1d, P1d] matrix expressing
198                    derivatives of nodal basis functions at quadrature points */
199   CeedTensorContract contract; /* tensor contraction object */
200   void *data;                  /* place for the backend to store any data */
201 };
202 
203 struct CeedTensorContract_private {
204   Ceed ceed;
205   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt,
206                const CeedScalar *restrict, CeedTransposeMode, const CeedInt,
207                const CeedScalar *restrict, CeedScalar *restrict);
208   int (*Destroy)(CeedTensorContract);
209   int refcount;
210   void *data;
211 };
212 
213 struct CeedQFunctionField_private {
214   const char *fieldname;
215   CeedInt size;
216   CeedEvalMode emode;
217 };
218 
219 struct CeedQFunction_private {
220   Ceed ceed;
221   int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *);
222   int (*Destroy)(CeedQFunction);
223   int refcount;
224   CeedInt vlength;    /* Number of quadrature points must be padded to a
225                            multiple of vlength */
226   CeedQFunctionField *inputfields;
227   CeedQFunctionField *outputfields;
228   CeedInt numinputfields, numoutputfields;
229   CeedQFunctionUser function;
230   const char *sourcepath;
231   const char *qfname;
232   bool fortranstatus;
233   bool identity;
234   void *ctx;      /* user context for function */
235   size_t ctxsize; /* size of user context; may be used to copy to a device */
236   void *data;     /* place for the backend to store any data */
237 };
238 
239 /// Struct to handle the context data to use the Fortran QFunction stub
240 /// @ingroup CeedQFunction
241 typedef struct {
242   CeedScalar *innerctx;
243   size_t innerctxsize;
244   void (*f)(void *ctx, int *nq,
245             const CeedScalar *u,const CeedScalar *u1,
246             const CeedScalar *u2,const CeedScalar *u3,
247             const CeedScalar *u4,const CeedScalar *u5,
248             const CeedScalar *u6,const CeedScalar *u7,
249             const CeedScalar *u8,const CeedScalar *u9,
250             const CeedScalar *u10,const CeedScalar *u11,
251             const CeedScalar *u12,const CeedScalar *u13,
252             const CeedScalar *u14,const CeedScalar *u15,
253             CeedScalar *v,CeedScalar *v1,CeedScalar *v2,
254             CeedScalar *v3,CeedScalar *v4,CeedScalar *v5,
255             CeedScalar *v6,CeedScalar *v7,CeedScalar *v8,
256             CeedScalar *v9, CeedScalar *v10,CeedScalar *v11,
257             CeedScalar *v12,CeedScalar *v13,CeedScalar *v14,
258             CeedScalar *v15, int *err);
259 } fContext;
260 
261 struct CeedOperatorField_private {
262   CeedElemRestriction Erestrict; /* Restriction from L-vector */
263   CeedBasis basis;               /* Basis or CEED_BASIS_COLLOCATED for
264                                       collocated fields */
265   CeedVector vec;                /* State vector for passive fields or
266                                       CEED_VECTOR_NONE for no vector */
267 };
268 
269 struct CeedOperator_private {
270   Ceed ceed;
271   CeedOperator opfallback;
272   CeedQFunction qffallback;
273   int refcount;
274   int (*LinearAssembleQFunction)(CeedOperator, CeedVector *,
275                                  CeedElemRestriction *, CeedRequest *);
276   int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *);
277   int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *);
278   int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector,
279                                           CeedRequest *);
280   int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector,
281       CeedRequest *);
282   int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *);
283   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
284   int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
285   int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
286   int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
287   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector,
288                        CeedVector, CeedRequest *);
289   int (*Destroy)(CeedOperator);
290   CeedOperatorField *inputfields;
291   CeedOperatorField *outputfields;
292   CeedInt numelements; /// Number of elements
293   CeedInt numqpoints;  /// Number of quadrature points over all elements
294   CeedInt nfields;     /// Number of fields that have been set
295   CeedQFunction qf;
296   CeedQFunction dqf;
297   CeedQFunction dqfT;
298   bool setupdone;
299   bool composite;
300   bool hasrestriction;
301   CeedOperator *suboperators;
302   CeedInt numsub;
303   void *data;
304 };
305 
306 CEED_INTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int,
307                                 const char *, va_list);
308 CEED_INTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int,
309                                const char *, va_list);
310 CEED_INTERN int CeedErrorExit(Ceed, const char *, int, const char *, int,
311                               const char *, va_list);
312 CEED_INTERN int CeedSetErrorHandler(Ceed ceed,
313                                     int (*eh)(Ceed, const char *, int,
314                                         const char *, int, const char *,
315                                         va_list));
316 
317 #endif
318