xref: /libCEED/include/ceed-impl.h (revision 78eab309c7f181abe05aece5f6af256290074fda)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Private header for frontend components of libCEED
10 #ifndef CEED_IMPL_H
11 #define CEED_IMPL_H
12 
13 #include <ceed.h>
14 #include <ceed/backend.h>
15 #include <stdbool.h>
16 
17 CEED_INTERN const char *CeedJitSourceRootDefault;
18 
19 /** @defgroup CeedUser Public API for Ceed
20     @ingroup Ceed
21 */
22 /** @defgroup CeedBackend Backend API for Ceed
23     @ingroup Ceed
24 */
25 /** @defgroup CeedDeveloper Internal library functions for Ceed
26     @ingroup Ceed
27 */
28 /** @defgroup CeedVectorUser Public API for CeedVector
29     @ingroup CeedVector
30 */
31 /** @defgroup CeedVectorBackend Backend API for CeedVector
32     @ingroup CeedVector
33 */
34 /** @defgroup CeedVectorDeveloper Internal library functions for CeedVector
35     @ingroup CeedVector
36 */
37 /** @defgroup CeedElemRestrictionUser Public API for CeedElemRestriction
38     @ingroup CeedElemRestriction
39 */
40 /** @defgroup CeedElemRestrictionBackend Backend API for CeedElemRestriction
41     @ingroup CeedElemRestriction
42 */
43 /** @defgroup CeedElemRestrictionDeveloper Internal library functions for CeedElemRestriction
44     @ingroup CeedElemRestriction
45 */
46 /** @defgroup CeedBasisUser Public API for CeedBasis
47     @ingroup CeedBasis
48 */
49 /** @defgroup CeedBasisBackend Backend API for CeedBasis
50     @ingroup CeedBasis
51 */
52 /** @defgroup CeedBasisDeveloper Internal library functions for CeedBasis
53     @ingroup CeedBasis
54 */
55 /** @defgroup CeedQFunctionUser Public API for CeedQFunction
56     @ingroup CeedQFunction
57 */
58 /** @defgroup CeedQFunctionBackend Backend API for CeedQFunction
59     @ingroup CeedQFunction
60 */
61 /** @defgroup CeedQFunctionDeveloper Internal library functions for CeedQFunction
62     @ingroup CeedQFunction
63 */
64 /** @defgroup CeedOperatorUser Public API for CeedOperator
65     @ingroup CeedOperator
66 */
67 /** @defgroup CeedOperatorBackend Backend API for CeedOperator
68     @ingroup CeedOperator
69 */
70 /** @defgroup CeedOperatorDeveloper Internal library functions for CeedOperator
71     @ingroup CeedOperator
72 */
73 
74 // Lookup table field for backend functions
75 typedef struct {
76   const char *func_name;
77   size_t      offset;
78 } FOffset;
79 
80 // Lookup table field for object delegates
81 typedef struct {
82   char *obj_name;
83   Ceed  delegate;
84 } ObjDelegate;
85 
86 struct Ceed_private {
87   const char  *resource;
88   Ceed         delegate;
89   Ceed         parent;
90   ObjDelegate *obj_delegates;
91   int          obj_delegate_count;
92   Ceed         op_fallback_ceed, op_fallback_parent;
93   const char  *op_fallback_resource;
94   char       **jit_source_roots;
95   CeedInt      num_jit_source_roots;
96   int (*Error)(Ceed, const char *, int, const char *, int, const char *, va_list *);
97   int (*SetStream)(Ceed, void *);
98   int (*GetPreferredMemType)(CeedMemType *);
99   int (*Destroy)(Ceed);
100   int (*VectorCreate)(CeedSize, CeedVector);
101   int (*ElemRestrictionCreate)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
102   int (*ElemRestrictionCreateAtPoints)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
103   int (*ElemRestrictionCreateBlocked)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
104   int (*BasisCreateTensorH1)(CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, CeedBasis);
105   int (*BasisCreateH1)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
106                        CeedBasis);
107   int (*BasisCreateHdiv)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
108                          CeedBasis);
109   int (*BasisCreateHcurl)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
110                           CeedBasis);
111   int (*TensorContractCreate)(CeedTensorContract);
112   int (*QFunctionCreate)(CeedQFunction);
113   int (*QFunctionContextCreate)(CeedQFunctionContext);
114   int (*OperatorCreate)(CeedOperator);
115   int (*CompositeOperatorCreate)(CeedOperator);
116   int      ref_count;
117   void    *data;
118   bool     is_debug;
119   bool     has_valid_op_fallback_resource;
120   bool     is_deterministic;
121   char     err_msg[CEED_MAX_RESOURCE_LEN];
122   FOffset *f_offsets;
123 };
124 
125 struct CeedVector_private {
126   Ceed ceed;
127   int (*HasValidArray)(CeedVector, bool *);
128   int (*HasBorrowedArrayOfType)(CeedVector, CeedMemType, bool *);
129   int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *);
130   int (*SetValue)(CeedVector, CeedScalar);
131   int (*SyncArray)(CeedVector, CeedMemType);
132   int (*TakeArray)(CeedVector, CeedMemType, CeedScalar **);
133   int (*GetArray)(CeedVector, CeedMemType, CeedScalar **);
134   int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **);
135   int (*GetArrayWrite)(CeedVector, CeedMemType, CeedScalar **);
136   int (*RestoreArray)(CeedVector);
137   int (*RestoreArrayRead)(CeedVector);
138   int (*Norm)(CeedVector, CeedNormType, CeedScalar *);
139   int (*Scale)(CeedVector, CeedScalar);
140   int (*AXPY)(CeedVector, CeedScalar, CeedVector);
141   int (*AXPBY)(CeedVector, CeedScalar, CeedScalar, CeedVector);
142   int (*PointwiseMult)(CeedVector, CeedVector, CeedVector);
143   int (*Reciprocal)(CeedVector);
144   int (*Destroy)(CeedVector);
145   int      ref_count;
146   CeedSize length;
147   uint64_t state;
148   uint64_t num_readers;
149   void    *data;
150 };
151 
152 struct CeedElemRestriction_private {
153   Ceed                ceed;
154   CeedElemRestriction rstr_base;
155   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
156   int (*ApplyUnsigned)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
157   int (*ApplyUnoriented)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
158   int (*ApplyAtPointsInElement)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
159   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
160   int (*GetOffsets)(CeedElemRestriction, CeedMemType, const CeedInt **);
161   int (*GetOrientations)(CeedElemRestriction, CeedMemType, const bool **);
162   int (*GetCurlOrientations)(CeedElemRestriction, CeedMemType, const CeedInt8 **);
163   int (*Destroy)(CeedElemRestriction);
164   int      ref_count;
165   CeedInt  num_elem;    /* number of elements */
166   CeedInt  elem_size;   /* number of nodes per element */
167   CeedInt  num_points;  /* number of points, for points restriction */
168   CeedInt  num_comp;    /* number of components */
169   CeedInt  comp_stride; /* Component stride for L-vector ordering */
170   CeedSize l_size;      /* size of the L-vector, can be used for checking for correct vector sizes */
171   CeedSize e_size;      /* minimum size of the E-vector, can be used for checking for correct vector sizes */
172   CeedInt  block_size;  /* number of elements in a batch */
173   CeedInt  num_block;   /* number of blocks of elements */
174   CeedInt *strides;     /* strides between [nodes, components, elements] */
175   CeedInt  layout[3];   /* E-vector layout [nodes, components, elements] */
176   CeedRestrictionType
177            rstr_type;   /* initialized in element restriction constructor for default, oriented, curl-oriented, or strided element restriction */
178   uint64_t num_readers; /* number of instances of offset read only access */
179   void    *data;        /* place for the backend to store any data */
180 };
181 
182 struct CeedBasis_private {
183   Ceed ceed;
184   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector);
185   int (*ApplyAtPoints)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector, CeedVector);
186   int (*Destroy)(CeedBasis);
187   int                ref_count;
188   bool               is_tensor_basis; /* flag for tensor basis */
189   CeedInt            dim;             /* topological dimension */
190   CeedElemTopology   topo;            /* element topology */
191   CeedInt            num_comp;        /* number of field components (1 for scalar fields) */
192   CeedInt            P_1d;            /* number of nodes in one dimension */
193   CeedInt            Q_1d;            /* number of quadrature points in one dimension */
194   CeedInt            P;               /* total number of nodes */
195   CeedInt            Q;               /* total number of quadrature points */
196   CeedFESpace        fe_space;        /* initialized in basis constructor with 1, 2, 3 for H^1, H(div), and H(curl) FE space */
197   CeedTensorContract contract;        /* tensor contraction object */
198   CeedScalar        *q_ref_1d;        /* array of length Q1d holding the locations of quadrature points on the 1D reference element [-1, 1] */
199   CeedScalar        *q_weight_1d;     /* array of length Q1d holding the quadrature weights on the reference element */
200   CeedScalar *interp; /* row-major matrix of shape [Q, P] or [dim * Q, P] expressing the values of nodal basis functions or vector basis functions at
201                          quadrature points */
202   CeedScalar *interp_1d; /* row-major matrix of shape [Q1d, P1d] expressing the values of nodal basis functions at quadrature points */
203   CeedScalar *grad;      /* row-major matrix of shape [dim * Q, P] matrix expressing derivatives of nodal basis functions at quadrature points */
204   CeedScalar *grad_1d;   /* row-major matrix of shape [Q1d, P1d] matrix expressing derivatives of nodal basis functions at quadrature points */
205   CeedScalar *div; /* row-major matrix of shape [Q, P] expressing the divergence of basis functions at quadrature points for H(div) discretizations */
206   CeedScalar *curl; /* row-major matrix of shape [curl_dim * Q, P], curl_dim = 1 if dim < 3 else dim, expressing the curl of basis functions at
207                        quadrature points for H(curl) discretizations */
208   CeedVector vec_chebyshev;
209   CeedBasis  basis_chebyshev; /* basis interpolating from nodes to Chebyshev polynomial coefficients */
210   void      *data;            /* place for the backend to store any data */
211 };
212 
213 struct CeedTensorContract_private {
214   Ceed ceed;
215   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt, 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 multiple of vec_length */
236   CeedQFunctionField  *input_fields;
237   CeedQFunctionField  *output_fields;
238   CeedInt              num_input_fields, num_output_fields;
239   CeedQFunctionUser    function;
240   CeedInt              user_flop_estimate;
241   const char          *user_source;
242   const char          *source_path;
243   const char          *kernel_name;
244   const char          *gallery_name;
245   bool                 is_gallery;
246   bool                 is_identity;
247   bool                 is_fortran;
248   bool                 is_immutable;
249   bool                 is_context_writable;
250   CeedQFunctionContext ctx;  /* user context for function */
251   void                *data; /* place for the backend to store any data */
252 };
253 
254 struct CeedQFunctionContext_private {
255   Ceed ceed;
256   int  ref_count;
257   int (*HasValidData)(CeedQFunctionContext, bool *);
258   int (*HasBorrowedDataOfType)(CeedQFunctionContext, CeedMemType, bool *);
259   int (*SetData)(CeedQFunctionContext, CeedMemType, CeedCopyMode, void *);
260   int (*TakeData)(CeedQFunctionContext, CeedMemType, void *);
261   int (*GetData)(CeedQFunctionContext, CeedMemType, void *);
262   int (*GetDataRead)(CeedQFunctionContext, CeedMemType, void *);
263   int (*RestoreData)(CeedQFunctionContext);
264   int (*RestoreDataRead)(CeedQFunctionContext);
265   int (*DataDestroy)(CeedQFunctionContext);
266   int (*Destroy)(CeedQFunctionContext);
267   CeedQFunctionContextDataDestroyUser data_destroy_function;
268   CeedMemType                         data_destroy_mem_type;
269   CeedInt                             num_fields;
270   CeedInt                             max_fields;
271   CeedContextFieldLabel              *field_labels;
272   uint64_t                            state;
273   uint64_t                            num_readers;
274   size_t                              ctx_size;
275   void                               *data;
276 };
277 
278 /// Struct to handle the context data to use the Fortran QFunction stub
279 /// @ingroup CeedQFunction
280 struct CeedFortranContext_private {
281   CeedQFunctionContext inner_ctx;
282   void (*f)(void *ctx, int *nq, const CeedScalar *u, const CeedScalar *u1, const CeedScalar *u2, const CeedScalar *u3, const CeedScalar *u4,
283             const CeedScalar *u5, const CeedScalar *u6, const CeedScalar *u7, const CeedScalar *u8, const CeedScalar *u9, const CeedScalar *u10,
284             const CeedScalar *u11, const CeedScalar *u12, const CeedScalar *u13, const CeedScalar *u14, const CeedScalar *u15, CeedScalar *v,
285             CeedScalar *v1, CeedScalar *v2, CeedScalar *v3, CeedScalar *v4, CeedScalar *v5, CeedScalar *v6, CeedScalar *v7, CeedScalar *v8,
286             CeedScalar *v9, CeedScalar *v10, CeedScalar *v11, CeedScalar *v12, CeedScalar *v13, CeedScalar *v14, CeedScalar *v15, int *err);
287 };
288 typedef struct CeedFortranContext_private *CeedFortranContext;
289 
290 struct CeedContextFieldLabel_private {
291   const char            *name;
292   const char            *description;
293   CeedContextFieldType   type;
294   size_t                 size;
295   size_t                 num_values;
296   size_t                 offset;
297   CeedInt                num_sub_labels;
298   CeedContextFieldLabel *sub_labels;
299   bool                   from_op;
300 };
301 
302 struct CeedOperatorField_private {
303   CeedElemRestriction elem_rstr;  /* Restriction from L-vector */
304   CeedBasis           basis;      /* Basis or CEED_BASIS_NONE for collocated fields */
305   CeedVector          vec;        /* State vector for passive fields or CEED_VECTOR_NONE for no vector */
306   const char         *field_name; /* matching QFunction field name */
307 };
308 
309 struct CeedQFunctionAssemblyData_private {
310   Ceed                ceed;
311   int                 ref_count;
312   bool                is_setup;
313   bool                reuse_data;
314   bool                needs_data_update;
315   CeedVector          vec;
316   CeedElemRestriction rstr;
317 };
318 
319 struct CeedOperatorAssemblyData_private {
320   Ceed                 ceed;
321   CeedInt              num_active_bases_in, num_active_bases_out;
322   CeedBasis           *active_bases_in, *active_bases_out;
323   CeedElemRestriction *active_elem_rstrs_in, *active_elem_rstrs_out;
324   CeedInt             *num_eval_modes_in, *num_eval_modes_out;
325   CeedEvalMode       **eval_modes_in, **eval_modes_out;
326   CeedScalar         **assembled_bases_in, **assembled_bases_out;
327   CeedSize           **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
328 };
329 
330 struct CeedOperator_private {
331   Ceed         ceed;
332   CeedOperator op_fallback, op_fallback_parent;
333   int          ref_count;
334   int (*LinearAssembleQFunction)(CeedOperator, CeedVector *, CeedElemRestriction *, CeedRequest *);
335   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *);
336   int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *);
337   int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *);
338   int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
339   int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
340   int (*LinearAssembleSymbolic)(CeedOperator, CeedSize *, CeedInt **, CeedInt **);
341   int (*LinearAssemble)(CeedOperator, CeedVector);
342   int (*LinearAssembleSingle)(CeedOperator, CeedInt, CeedVector);
343   int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *);
344   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
345   int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
346   int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
347   int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
348   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector, CeedVector, CeedRequest *);
349   int (*Destroy)(CeedOperator);
350   CeedOperatorField        *input_fields;
351   CeedOperatorField        *output_fields;
352   CeedSize                  input_size, output_size;
353   CeedInt                   num_elem;   /* Number of elements */
354   CeedInt                   num_qpts;   /* Number of quadrature points over all elements */
355   CeedInt                   num_fields; /* Number of fields that have been set */
356   CeedQFunction             qf;
357   CeedQFunction             dqf;
358   CeedQFunction             dqfT;
359   const char               *name;
360   bool                      is_immutable;
361   bool                      is_interface_setup;
362   bool                      is_backend_setup;
363   bool                      is_composite;
364   bool                      has_restriction;
365   CeedQFunctionAssemblyData qf_assembled;
366   CeedOperatorAssemblyData  op_assembled;
367   CeedOperator             *sub_operators;
368   CeedInt                   num_suboperators;
369   void                     *data;
370   CeedInt                   num_context_labels;
371   CeedInt                   max_context_labels;
372   CeedContextFieldLabel    *context_labels;
373 };
374 
375 #endif  // CEED_IMPL_H
376