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