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