xref: /libCEED/include/ceed-impl.h (revision 5ebd836c59d60a2e5e1cb67f6731404c7da26f85)
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 (*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  l_layout[3]; /* L-vector layout [nodes, components, elements] */
176   CeedInt  e_layout[3]; /* E-vector layout [nodes, components, elements] */
177   CeedRestrictionType
178            rstr_type;   /* initialized in element restriction constructor for default, oriented, curl-oriented, or strided element restriction */
179   uint64_t num_readers; /* number of instances of offset read only access */
180   void    *data;        /* place for the backend to store any data */
181 };
182 
183 struct CeedBasis_private {
184   Ceed ceed;
185   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector);
186   int (*ApplyAtPoints)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector, CeedVector);
187   int (*Destroy)(CeedBasis);
188   int                ref_count;
189   bool               is_tensor_basis; /* flag for tensor basis */
190   CeedInt            dim;             /* topological dimension */
191   CeedElemTopology   topo;            /* element topology */
192   CeedInt            num_comp;        /* number of field components (1 for scalar fields) */
193   CeedInt            P_1d;            /* number of nodes in one dimension */
194   CeedInt            Q_1d;            /* number of quadrature points in one dimension */
195   CeedInt            P;               /* total number of nodes */
196   CeedInt            Q;               /* total number of quadrature points */
197   CeedFESpace        fe_space;        /* initialized in basis constructor with 1, 2, 3 for H^1, H(div), and H(curl) FE space */
198   CeedTensorContract contract;        /* tensor contraction object */
199   CeedScalar        *q_ref_1d;        /* array of length Q1d holding the locations of quadrature points on the 1D reference element [-1, 1] */
200   CeedScalar        *q_weight_1d;     /* array of length Q1d holding the quadrature weights on the reference element */
201   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
202                          quadrature points */
203   CeedScalar *interp_1d; /* row-major matrix of shape [Q1d, P1d] expressing the values of nodal basis functions at quadrature points */
204   CeedScalar *grad;      /* row-major matrix of shape [dim * Q, P] matrix expressing derivatives of nodal basis functions at quadrature points */
205   CeedScalar *grad_1d;   /* row-major matrix of shape [Q1d, P1d] matrix expressing derivatives of nodal basis functions at quadrature points */
206   CeedScalar *div; /* row-major matrix of shape [Q, P] expressing the divergence of basis functions at quadrature points for H(div) discretizations */
207   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
208                        quadrature points for H(curl) discretizations */
209   CeedVector  vec_chebyshev;
210   CeedBasis   basis_chebyshev; /* basis interpolating from nodes to Chebyshev polynomial coefficients */
211   void       *data;            /* place for the backend to store any data */
212 };
213 
214 struct CeedTensorContract_private {
215   Ceed ceed;
216   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt, const CeedScalar *restrict, CeedTransposeMode, const CeedInt,
217                const CeedScalar *restrict, CeedScalar *restrict);
218   int (*Destroy)(CeedTensorContract);
219   int   ref_count;
220   void *data;
221 };
222 
223 struct CeedQFunctionField_private {
224   const char  *field_name;
225   CeedInt      size;
226   CeedEvalMode eval_mode;
227 };
228 
229 struct CeedQFunction_private {
230   Ceed ceed;
231   int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *);
232   int (*SetCUDAUserFunction)(CeedQFunction, void *);
233   int (*SetHIPUserFunction)(CeedQFunction, void *);
234   int (*Destroy)(CeedQFunction);
235   int                  ref_count;
236   CeedInt              vec_length; /* Number of quadrature points must be padded to a multiple of vec_length */
237   CeedQFunctionField  *input_fields;
238   CeedQFunctionField  *output_fields;
239   CeedInt              num_input_fields, num_output_fields;
240   CeedQFunctionUser    function;
241   CeedInt              user_flop_estimate;
242   const char          *user_source;
243   const char          *source_path;
244   const char          *kernel_name;
245   const char          *gallery_name;
246   bool                 is_gallery;
247   bool                 is_identity;
248   bool                 is_fortran;
249   bool                 is_immutable;
250   bool                 is_context_writable;
251   CeedQFunctionContext ctx;  /* user context for function */
252   void                *data; /* place for the backend to store any data */
253 };
254 
255 struct CeedQFunctionContext_private {
256   Ceed ceed;
257   int  ref_count;
258   int (*HasValidData)(CeedQFunctionContext, bool *);
259   int (*HasBorrowedDataOfType)(CeedQFunctionContext, CeedMemType, bool *);
260   int (*SetData)(CeedQFunctionContext, CeedMemType, CeedCopyMode, void *);
261   int (*TakeData)(CeedQFunctionContext, CeedMemType, void *);
262   int (*GetData)(CeedQFunctionContext, CeedMemType, void *);
263   int (*GetDataRead)(CeedQFunctionContext, CeedMemType, void *);
264   int (*RestoreData)(CeedQFunctionContext);
265   int (*RestoreDataRead)(CeedQFunctionContext);
266   int (*DataDestroy)(CeedQFunctionContext);
267   int (*Destroy)(CeedQFunctionContext);
268   CeedQFunctionContextDataDestroyUser data_destroy_function;
269   CeedMemType                         data_destroy_mem_type;
270   CeedInt                             num_fields;
271   CeedInt                             max_fields;
272   CeedContextFieldLabel              *field_labels;
273   uint64_t                            state;
274   uint64_t                            num_readers;
275   size_t                              ctx_size;
276   void                               *data;
277 };
278 
279 /// Struct to handle the context data to use the Fortran QFunction stub
280 /// @ingroup CeedQFunction
281 struct CeedFortranContext_private {
282   CeedQFunctionContext inner_ctx;
283   void (*f)(void *ctx, int *nq, const CeedScalar *u, const CeedScalar *u1, const CeedScalar *u2, const CeedScalar *u3, const CeedScalar *u4,
284             const CeedScalar *u5, const CeedScalar *u6, const CeedScalar *u7, const CeedScalar *u8, const CeedScalar *u9, const CeedScalar *u10,
285             const CeedScalar *u11, const CeedScalar *u12, const CeedScalar *u13, const CeedScalar *u14, const CeedScalar *u15, CeedScalar *v,
286             CeedScalar *v1, CeedScalar *v2, CeedScalar *v3, CeedScalar *v4, CeedScalar *v5, CeedScalar *v6, CeedScalar *v7, CeedScalar *v8,
287             CeedScalar *v9, CeedScalar *v10, CeedScalar *v11, CeedScalar *v12, CeedScalar *v13, CeedScalar *v14, CeedScalar *v15, int *err);
288 };
289 typedef struct CeedFortranContext_private *CeedFortranContext;
290 
291 struct CeedContextFieldLabel_private {
292   const char            *name;
293   const char            *description;
294   CeedContextFieldType   type;
295   size_t                 size;
296   size_t                 num_values;
297   size_t                 offset;
298   CeedInt                num_sub_labels;
299   CeedContextFieldLabel *sub_labels;
300   bool                   from_op;
301 };
302 
303 struct CeedOperatorField_private {
304   CeedElemRestriction elem_rstr;  /* Restriction from L-vector */
305   CeedBasis           basis;      /* Basis or CEED_BASIS_NONE for collocated fields */
306   CeedVector          vec;        /* State vector for passive fields or CEED_VECTOR_NONE for no vector */
307   const char         *field_name; /* matching QFunction field name */
308 };
309 
310 struct CeedQFunctionAssemblyData_private {
311   Ceed                ceed;
312   int                 ref_count;
313   bool                is_setup;
314   bool                reuse_data;
315   bool                needs_data_update;
316   CeedVector          vec;
317   CeedElemRestriction rstr;
318 };
319 
320 struct CeedOperatorAssemblyData_private {
321   Ceed                 ceed;
322   CeedInt              num_active_bases_in, num_active_bases_out;
323   CeedBasis           *active_bases_in, *active_bases_out;
324   CeedElemRestriction *active_elem_rstrs_in, *active_elem_rstrs_out;
325   CeedInt             *num_eval_modes_in, *num_eval_modes_out;
326   CeedEvalMode       **eval_modes_in, **eval_modes_out;
327   CeedScalar         **assembled_bases_in, **assembled_bases_out;
328   CeedSize           **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
329 };
330 
331 struct CeedOperator_private {
332   Ceed         ceed;
333   CeedOperator op_fallback, op_fallback_parent;
334   int          ref_count;
335   int (*LinearAssembleQFunction)(CeedOperator, CeedVector *, CeedElemRestriction *, CeedRequest *);
336   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *);
337   int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *);
338   int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *);
339   int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
340   int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
341   int (*LinearAssembleSymbolic)(CeedOperator, CeedSize *, CeedInt **, CeedInt **);
342   int (*LinearAssemble)(CeedOperator, CeedVector);
343   int (*LinearAssembleSingle)(CeedOperator, CeedInt, CeedVector);
344   int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *);
345   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
346   int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
347   int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
348   int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
349   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector, CeedVector, CeedRequest *);
350   int (*Destroy)(CeedOperator);
351   CeedOperatorField        *input_fields;
352   CeedOperatorField        *output_fields;
353   CeedSize                  input_size, output_size;
354   CeedInt                   num_elem;   /* Number of elements */
355   CeedInt                   num_qpts;   /* Number of quadrature points over all elements */
356   CeedInt                   num_fields; /* Number of fields that have been set */
357   CeedQFunction             qf;
358   CeedQFunction             dqf;
359   CeedQFunction             dqfT;
360   const char               *name;
361   bool                      is_immutable;
362   bool                      is_interface_setup;
363   bool                      is_backend_setup;
364   bool                      is_composite;
365   bool                      is_at_points;
366   bool                      has_restriction;
367   CeedQFunctionAssemblyData qf_assembled;
368   CeedOperatorAssemblyData  op_assembled;
369   CeedOperator             *sub_operators;
370   CeedInt                   num_suboperators;
371   void                     *data;
372   CeedInt                   num_context_labels;
373   CeedInt                   max_context_labels;
374   CeedContextFieldLabel    *context_labels;
375   CeedElemRestriction       rstr_points, first_points_rstr;
376   CeedVector                point_coords;
377 };
378