xref: /libCEED/include/ceed-impl.h (revision e8d902ca03f8f9018a8d626b82d32185d32586a7)
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/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 *func_name;
84   size_t offset;
85 } FOffset;
86 
87 // Lookup table field for object delegates
88 typedef struct {
89   char *obj_name;
90   Ceed delegate;
91 } ObjDelegate;
92 
93 struct Ceed_private {
94   const char *resource;
95   Ceed delegate;
96   Ceed parent;
97   ObjDelegate *obj_delegates;
98   int obj_delegate_count;
99   Ceed op_fallback_ceed, op_fallback_parent;
100   const char *op_fallback_resource;
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 (*QFunctionContextCreate)(CeedQFunctionContext);
120   int (*OperatorCreate)(CeedOperator);
121   int (*CompositeOperatorCreate)(CeedOperator);
122   int ref_count;
123   bool is_deterministic;
124   void *data;
125   bool debug;
126   char err_msg[CEED_MAX_RESOURCE_LEN];
127   FOffset *f_offsets;
128 };
129 
130 struct CeedVector_private {
131   Ceed ceed;
132   int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *);
133   int (*SetValue)(CeedVector, CeedScalar);
134   int (*SyncArray)(CeedVector, CeedMemType);
135   int (*TakeArray)(CeedVector, CeedMemType, CeedScalar **);
136   int (*GetArray)(CeedVector, CeedMemType, CeedScalar **);
137   int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **);
138   int (*RestoreArray)(CeedVector);
139   int (*RestoreArrayRead)(CeedVector);
140   int (*Norm)(CeedVector, CeedNormType, CeedScalar *);
141   int (*Scale)(CeedVector, CeedScalar);
142   int (*AXPY)(CeedVector, CeedScalar, CeedVector);
143   int (*PointwiseMult)(CeedVector, CeedVector, CeedVector);
144   int (*Reciprocal)(CeedVector);
145   int (*Destroy)(CeedVector);
146   int ref_count;
147   CeedInt length;
148   uint64_t state;
149   uint64_t num_readers;
150   void *data;
151 };
152 
153 struct CeedElemRestriction_private {
154   Ceed ceed;
155   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector,
156                CeedRequest *);
157   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector,
158                     CeedVector, CeedRequest *);
159   int (*GetOffsets)(CeedElemRestriction, CeedMemType, const CeedInt **);
160   int (*Destroy)(CeedElemRestriction);
161   int ref_count;
162   CeedInt num_elem;      /* number of elements */
163   CeedInt elem_size;     /* number of nodes per element */
164   CeedInt num_comp;      /* number of components */
165   CeedInt comp_stride;   /* Component stride for L-vector ordering */
166   CeedInt l_size;        /* size of the L-vector, can be used for checking
167                               for correct vector sizes */
168   CeedInt blk_size;      /* number of elements in a batch */
169   CeedInt num_blk;       /* number of blocks of elements */
170   CeedInt *strides;      /* strides between [nodes, components, elements] */
171   CeedInt layout[3];     /* E-vector layout [nodes, components, elements] */
172   uint64_t num_readers;  /* number of instances of offset read only access */
173   void *data;            /* place for the backend to store any data */
174 };
175 
176 struct CeedBasis_private {
177   Ceed ceed;
178   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode,
179                CeedVector, CeedVector);
180   int (*Destroy)(CeedBasis);
181   int ref_count;
182   bool tensor_basis;       /* flag for tensor basis */
183   CeedInt dim;             /* topological dimension */
184   CeedElemTopology topo;   /* element topology */
185   CeedInt num_comp;        /* number of field components (1 for scalar fields) */
186   CeedInt P_1d;            /* number of nodes in one dimension */
187   CeedInt Q_1d;            /* number of quadrature points in one dimension */
188   CeedInt P;               /* total number of nodes */
189   CeedInt Q;               /* total number of quadrature points */
190   CeedScalar *q_ref_1d;    /* Array of length Q1d holding the locations of
191                                 quadrature points on the 1D reference
192                                 element [-1, 1] */
193   CeedScalar
194   *q_weight_1d; /* array of length Q1d holding the quadrature weights on
195                                the reference element */
196   CeedScalar
197   *interp;    /* row-major matrix of shape [Q, P] expressing the values of
198                    nodal basis functions at quadrature points */
199   CeedScalar
200   *interp_1d; /* row-major matrix of shape [Q1d, P1d] expressing the values of
201                    nodal basis functions at quadrature points */
202   CeedScalar
203   *grad;      /* row-major matrix of shape [dim*Q, P] matrix expressing
204                    derivatives of nodal basis functions at quadrature points */
205   CeedScalar
206   *grad_1d;   /* row-major matrix of shape [Q1d, P1d] matrix expressing
207                    derivatives of nodal basis functions at quadrature points */
208   CeedTensorContract contract; /* tensor contraction object */
209   void *data;                  /* place for the backend to store any data */
210 };
211 
212 struct CeedTensorContract_private {
213   Ceed ceed;
214   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt,
215                const CeedScalar *restrict, CeedTransposeMode, const CeedInt,
216                const CeedScalar *restrict, CeedScalar *restrict);
217   int (*Destroy)(CeedTensorContract);
218   int ref_count;
219   void *data;
220 };
221 
222 struct CeedQFunctionField_private {
223   const char *field_name;
224   CeedInt size;
225   CeedEvalMode eval_mode;
226 };
227 
228 struct CeedQFunction_private {
229   Ceed ceed;
230   int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *);
231   int (*SetCUDAUserFunction)(CeedQFunction, void *);
232   int (*SetHIPUserFunction)(CeedQFunction, void *);
233   int (*Destroy)(CeedQFunction);
234   int ref_count;
235   CeedInt vec_length; /* Number of quadrature points must be padded to a
236                            multiple of vec_length */
237   CeedQFunctionField *input_fields;
238   CeedQFunctionField *output_fields;
239   CeedInt num_input_fields, num_output_fields;
240   CeedQFunctionUser function;
241   const char *source_path;
242   const char *kernel_name;
243   const char *gallery_name;
244   bool is_gallery;
245   bool is_identity;
246   bool is_fortran;
247   bool is_immutable;
248   CeedQFunctionContext ctx; /* user context for function */
249   void *data;          /* place for the backend to store any data */
250 };
251 
252 struct CeedQFunctionContext_private {
253   Ceed ceed;
254   int ref_count;
255   int (*SetData)(CeedQFunctionContext, CeedMemType, CeedCopyMode, void *);
256   int (*TakeData)(CeedQFunctionContext, CeedMemType, void *);
257   int (*GetData)(CeedQFunctionContext, CeedMemType, void *);
258   int (*RestoreData)(CeedQFunctionContext);
259   int (*Destroy)(CeedQFunctionContext);
260   uint64_t state;
261   size_t ctx_size;
262   void *data;
263 };
264 
265 /// Struct to handle the context data to use the Fortran QFunction stub
266 /// @ingroup CeedQFunction
267 struct CeedFortranContext_private {
268   CeedQFunctionContext innerctx;
269   void (*f)(void *ctx, int *nq,
270             const CeedScalar *u,const CeedScalar *u1,
271             const CeedScalar *u2,const CeedScalar *u3,
272             const CeedScalar *u4,const CeedScalar *u5,
273             const CeedScalar *u6,const CeedScalar *u7,
274             const CeedScalar *u8,const CeedScalar *u9,
275             const CeedScalar *u10,const CeedScalar *u11,
276             const CeedScalar *u12,const CeedScalar *u13,
277             const CeedScalar *u14,const CeedScalar *u15,
278             CeedScalar *v,CeedScalar *v1,CeedScalar *v2,
279             CeedScalar *v3,CeedScalar *v4,CeedScalar *v5,
280             CeedScalar *v6,CeedScalar *v7,CeedScalar *v8,
281             CeedScalar *v9, CeedScalar *v10,CeedScalar *v11,
282             CeedScalar *v12,CeedScalar *v13,CeedScalar *v14,
283             CeedScalar *v15, int *err);
284 };
285 typedef struct CeedFortranContext_private *CeedFortranContext;
286 
287 struct CeedOperatorField_private {
288   CeedElemRestriction elem_restr; /* Restriction from L-vector */
289   CeedBasis basis;                /* Basis or CEED_BASIS_COLLOCATED for
290                                        collocated fields */
291   CeedVector vec;                 /* State vector for passive fields or
292                                        CEED_VECTOR_NONE for no vector */
293   const char *field_name;          /* matching QFunction field name */
294 };
295 
296 struct CeedOperator_private {
297   Ceed ceed;
298   CeedOperator op_fallback;
299   CeedQFunction qf_fallback;
300   int ref_count;
301   int (*LinearAssembleQFunction)(CeedOperator, CeedVector *,
302                                  CeedElemRestriction *, CeedRequest *);
303   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector,
304                                        CeedElemRestriction, CeedRequest *);
305   int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *);
306   int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *);
307   int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector,
308                                           CeedRequest *);
309   int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector,
310       CeedRequest *);
311   int (*LinearAssembleSymbolic)(CeedOperator, CeedInt *, CeedInt **, CeedInt **);
312   int (*LinearAssemble)(CeedOperator, CeedVector);
313   int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *);
314   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
315   int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
316   int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
317   int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
318   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector,
319                        CeedVector, CeedRequest *);
320   int (*Destroy)(CeedOperator);
321   CeedOperatorField *input_fields;
322   CeedOperatorField *output_fields;
323   CeedInt num_elem;   /* Number of elements */
324   CeedInt num_qpts;   /* Number of quadrature points over all elements */
325   CeedInt num_fields; /* Number of fields that have been set */
326   CeedQFunction qf;
327   CeedQFunction dqf;
328   CeedQFunction dqfT;
329   bool is_immutable;
330   bool is_interface_setup;
331   bool is_backend_setup;
332   bool is_composite;
333   bool has_restriction;
334   bool has_qf_assembled;
335   CeedVector qf_assembled;
336   CeedElemRestriction qf_assembled_rstr;
337   CeedOperator *sub_operators;
338   CeedInt num_suboperators;
339   void *data;
340 };
341 
342 #endif
343