xref: /libCEED/include/ceed-impl.h (revision 77d1c127eaba12da4c1761ef74a16ca3fc16e493)
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 *, CeedElemRestriction);
102   int (*ElemRestrictionCreateOriented)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, CeedElemRestriction);
103   int (*ElemRestrictionCreateCurlOriented)(CeedMemType, CeedCopyMode, const CeedInt *, const CeedInt *, CeedElemRestriction);
104   int (*ElemRestrictionCreateBlocked)(CeedMemType, CeedCopyMode, const CeedInt *, CeedElemRestriction);
105   int (*ElemRestrictionCreateBlockedOriented)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, CeedElemRestriction);
106   int (*ElemRestrictionCreateBlockedCurlOriented)(CeedMemType, CeedCopyMode, const CeedInt *, const CeedInt *, CeedElemRestriction);
107   int (*BasisCreateTensorH1)(CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, CeedBasis);
108   int (*BasisCreateH1)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
109                        CeedBasis);
110   int (*BasisCreateHdiv)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
111                          CeedBasis);
112   int (*BasisCreateHcurl)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
113                           CeedBasis);
114   int (*TensorContractCreate)(CeedBasis, CeedTensorContract);
115   int (*QFunctionCreate)(CeedQFunction);
116   int (*QFunctionContextCreate)(CeedQFunctionContext);
117   int (*OperatorCreate)(CeedOperator);
118   int (*CompositeOperatorCreate)(CeedOperator);
119   int      ref_count;
120   void    *data;
121   bool     is_debug;
122   bool     has_valid_op_fallback_resource;
123   bool     is_deterministic;
124   char     err_msg[CEED_MAX_RESOURCE_LEN];
125   FOffset *f_offsets;
126 };
127 
128 struct CeedVector_private {
129   Ceed ceed;
130   int (*HasValidArray)(CeedVector, bool *);
131   int (*HasBorrowedArrayOfType)(CeedVector, CeedMemType, bool *);
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 (*GetArrayWrite)(CeedVector, CeedMemType, CeedScalar **);
139   int (*RestoreArray)(CeedVector);
140   int (*RestoreArrayRead)(CeedVector);
141   int (*Norm)(CeedVector, CeedNormType, CeedScalar *);
142   int (*Scale)(CeedVector, CeedScalar);
143   int (*AXPY)(CeedVector, CeedScalar, CeedVector);
144   int (*AXPBY)(CeedVector, CeedScalar, CeedScalar, CeedVector);
145   int (*PointwiseMult)(CeedVector, CeedVector, CeedVector);
146   int (*Reciprocal)(CeedVector);
147   int (*Destroy)(CeedVector);
148   int      ref_count;
149   CeedSize length;
150   uint64_t state;
151   uint64_t num_readers;
152   void    *data;
153 };
154 
155 struct CeedElemRestriction_private {
156   Ceed                ceed;
157   CeedElemRestriction rstr_signed;
158   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
159   int (*ApplyUnsigned)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
160   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
161   int (*GetOffsets)(CeedElemRestriction, CeedMemType, const CeedInt **);
162   int (*GetOrientations)(CeedElemRestriction, CeedMemType, const bool **);
163   int (*GetCurlOrientations)(CeedElemRestriction, CeedMemType, const CeedInt **);
164   int (*Destroy)(CeedElemRestriction);
165   int      ref_count;
166   CeedInt  num_elem;    /* number of elements */
167   CeedInt  elem_size;   /* number of nodes per element */
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   CeedInt  blk_size;    /* number of elements in a batch */
172   CeedInt  num_blk;     /* number of blocks of elements */
173   CeedInt *strides;     /* strides between [nodes, components, elements] */
174   CeedInt  layout[3];   /* E-vector layout [nodes, components, elements] */
175   uint64_t num_readers; /* number of instances of offset read only access */
176   void    *data;        /* place for the backend to store any data */
177 };
178 
179 struct CeedBasis_private {
180   Ceed ceed;
181   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector);
182   int (*ApplyAtPoints)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector, CeedVector);
183   int (*Destroy)(CeedBasis);
184   int                ref_count;
185   bool               is_tensor_basis; /* flag for tensor basis */
186   CeedInt            dim;             /* topological dimension */
187   CeedElemTopology   topo;            /* element topology */
188   CeedInt            num_comp;        /* number of field components (1 for scalar fields) */
189   CeedInt            P_1d;            /* number of nodes in one dimension */
190   CeedInt            Q_1d;            /* number of quadrature points in one dimension */
191   CeedInt            P;               /* total number of nodes */
192   CeedInt            Q;               /* total number of quadrature points */
193   CeedFESpace        fe_space;        /* initialized in basis constructor with 1, 2, 3 for H^1, H(div), and H(curl) FE space */
194   CeedTensorContract contract;        /* tensor contraction object */
195   CeedScalar        *q_ref_1d;        /* array of length Q1d holding the locations of quadrature points on the 1D reference element [-1, 1] */
196   CeedScalar        *q_weight_1d;     /* array of length Q1d holding the quadrature weights on the reference element */
197   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
198                          quadrature points */
199   CeedScalar *interp_1d; /* row-major matrix of shape [Q1d, P1d] expressing the values of nodal basis functions at quadrature points */
200   CeedScalar *grad;      /* row-major matrix of shape [dim * Q, P] matrix expressing derivatives of nodal basis functions at quadrature points */
201   CeedScalar *grad_1d;   /* row-major matrix of shape [Q1d, P1d] matrix expressing derivatives of nodal basis functions at quadrature points */
202   CeedScalar *div; /* row-major matrix of shape [Q, P] expressing the divergence of basis functions at quadrature points for H(div) discretizations */
203   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
204                        quadrature points for H(curl) discretizations */
205   CeedVector vec_chebyshev;
206   CeedBasis  basis_chebyshev; /* basis interpolating from nodes to Chebyshev polynomial coefficients */
207   void      *data;            /* place for the backend to store any data */
208 };
209 
210 struct CeedTensorContract_private {
211   Ceed ceed;
212   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt, const CeedScalar *restrict, CeedTransposeMode, const CeedInt,
213                const CeedScalar *restrict, CeedScalar *restrict);
214   int (*Destroy)(CeedTensorContract);
215   int   ref_count;
216   void *data;
217 };
218 
219 struct CeedQFunctionField_private {
220   const char  *field_name;
221   CeedInt      size;
222   CeedEvalMode eval_mode;
223 };
224 
225 struct CeedQFunction_private {
226   Ceed ceed;
227   int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *);
228   int (*SetCUDAUserFunction)(CeedQFunction, void *);
229   int (*SetHIPUserFunction)(CeedQFunction, void *);
230   int (*Destroy)(CeedQFunction);
231   int                  ref_count;
232   CeedInt              vec_length; /* Number of quadrature points must be padded to a multiple of vec_length */
233   CeedQFunctionField  *input_fields;
234   CeedQFunctionField  *output_fields;
235   CeedInt              num_input_fields, num_output_fields;
236   CeedQFunctionUser    function;
237   CeedInt              user_flop_estimate;
238   const char          *user_source;
239   const char          *source_path;
240   const char          *kernel_name;
241   const char          *gallery_name;
242   bool                 is_gallery;
243   bool                 is_identity;
244   bool                 is_fortran;
245   bool                 is_immutable;
246   bool                 is_context_writable;
247   CeedQFunctionContext ctx;  /* user context for function */
248   void                *data; /* place for the backend to store any data */
249 };
250 
251 struct CeedQFunctionContext_private {
252   Ceed ceed;
253   int  ref_count;
254   int (*HasValidData)(CeedQFunctionContext, bool *);
255   int (*HasBorrowedDataOfType)(CeedQFunctionContext, CeedMemType, bool *);
256   int (*SetData)(CeedQFunctionContext, CeedMemType, CeedCopyMode, void *);
257   int (*TakeData)(CeedQFunctionContext, CeedMemType, void *);
258   int (*GetData)(CeedQFunctionContext, CeedMemType, void *);
259   int (*GetDataRead)(CeedQFunctionContext, CeedMemType, void *);
260   int (*RestoreData)(CeedQFunctionContext);
261   int (*RestoreDataRead)(CeedQFunctionContext);
262   int (*DataDestroy)(CeedQFunctionContext);
263   int (*Destroy)(CeedQFunctionContext);
264   CeedQFunctionContextDataDestroyUser data_destroy_function;
265   CeedMemType                         data_destroy_mem_type;
266   CeedInt                             num_fields;
267   CeedInt                             max_fields;
268   CeedContextFieldLabel              *field_labels;
269   uint64_t                            state;
270   uint64_t                            num_readers;
271   size_t                              ctx_size;
272   void                               *data;
273 };
274 
275 /// Struct to handle the context data to use the Fortran QFunction stub
276 /// @ingroup CeedQFunction
277 struct CeedFortranContext_private {
278   CeedQFunctionContext inner_ctx;
279   void (*f)(void *ctx, int *nq, const CeedScalar *u, const CeedScalar *u1, const CeedScalar *u2, const CeedScalar *u3, const CeedScalar *u4,
280             const CeedScalar *u5, const CeedScalar *u6, const CeedScalar *u7, const CeedScalar *u8, const CeedScalar *u9, const CeedScalar *u10,
281             const CeedScalar *u11, const CeedScalar *u12, const CeedScalar *u13, const CeedScalar *u14, const CeedScalar *u15, CeedScalar *v,
282             CeedScalar *v1, CeedScalar *v2, CeedScalar *v3, CeedScalar *v4, CeedScalar *v5, CeedScalar *v6, CeedScalar *v7, CeedScalar *v8,
283             CeedScalar *v9, CeedScalar *v10, CeedScalar *v11, CeedScalar *v12, CeedScalar *v13, CeedScalar *v14, CeedScalar *v15, int *err);
284 };
285 typedef struct CeedFortranContext_private *CeedFortranContext;
286 
287 struct CeedContextFieldLabel_private {
288   const char            *name;
289   const char            *description;
290   CeedContextFieldType   type;
291   size_t                 size;
292   size_t                 num_values;
293   size_t                 offset;
294   CeedInt                num_sub_labels;
295   CeedContextFieldLabel *sub_labels;
296   bool                   from_op;
297 };
298 
299 struct CeedOperatorField_private {
300   CeedElemRestriction elem_rstr;  /* Restriction from L-vector */
301   CeedBasis           basis;      /* Basis or CEED_BASIS_COLLOCATED for collocated fields */
302   CeedVector          vec;        /* State vector for passive fields or CEED_VECTOR_NONE for no vector */
303   const char         *field_name; /* matching QFunction field name */
304 };
305 
306 struct CeedQFunctionAssemblyData_private {
307   Ceed                ceed;
308   int                 ref_count;
309   bool                is_setup;
310   bool                reuse_data;
311   bool                needs_data_update;
312   CeedVector          vec;
313   CeedElemRestriction rstr;
314 };
315 
316 struct CeedOperatorAssemblyData_private {
317   Ceed                 ceed;
318   CeedInt              num_active_bases;
319   CeedBasis           *active_bases;
320   CeedElemRestriction *active_elem_rstrs;
321   CeedInt             *num_eval_modes_in, *num_eval_modes_out;
322   CeedEvalMode       **eval_modes_in, **eval_modes_out;
323   CeedScalar         **assembled_bases_in, **assembled_bases_out;
324   CeedSize           **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
325 };
326 
327 struct CeedOperator_private {
328   Ceed         ceed;
329   CeedOperator op_fallback, op_fallback_parent;
330   int          ref_count;
331   int (*LinearAssembleQFunction)(CeedOperator, CeedVector *, CeedElemRestriction *, CeedRequest *);
332   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *);
333   int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *);
334   int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *);
335   int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
336   int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
337   int (*LinearAssembleSymbolic)(CeedOperator, CeedSize *, CeedInt **, CeedInt **);
338   int (*LinearAssemble)(CeedOperator, CeedVector);
339   int (*LinearAssembleSingle)(CeedOperator, CeedInt, CeedVector);
340   int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *);
341   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
342   int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
343   int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
344   int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
345   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector, CeedVector, CeedRequest *);
346   int (*Destroy)(CeedOperator);
347   CeedOperatorField        *input_fields;
348   CeedOperatorField        *output_fields;
349   CeedSize                  input_size, output_size;
350   CeedInt                   num_elem;   /* Number of elements */
351   CeedInt                   num_qpts;   /* Number of quadrature points over all elements */
352   CeedInt                   num_fields; /* Number of fields that have been set */
353   CeedQFunction             qf;
354   CeedQFunction             dqf;
355   CeedQFunction             dqfT;
356   const char               *name;
357   bool                      is_immutable;
358   bool                      is_interface_setup;
359   bool                      is_backend_setup;
360   bool                      is_composite;
361   bool                      has_restriction;
362   CeedQFunctionAssemblyData qf_assembled;
363   CeedOperatorAssemblyData  op_assembled;
364   CeedOperator             *sub_operators;
365   CeedInt                   num_suboperators;
366   void                     *data;
367   CeedInt                   num_context_labels;
368   CeedInt                   max_context_labels;
369   CeedContextFieldLabel    *context_labels;
370 };
371 
372 CEED_INTERN int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback);
373 
374 #endif
375