xref: /libCEED/include/ceed-impl.h (revision b0f67a9c1aeeb4d82b4724afaae1227ff4e81f15)
1 // Copyright (c) 2017-2026, 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 typedef struct CeedObject_private {
94   Ceed ceed;
95   int (*ViewFunction)(CeedObject, FILE *);
96   int ref_count;
97 } CeedObject_private;
98 
99 struct Ceed_private {
100   CeedObject_private obj;
101   const char        *resource;
102   Ceed               delegate;
103   Ceed               parent;
104   ObjDelegate       *obj_delegates;
105   int                obj_delegate_count;
106   Ceed               op_fallback_ceed;
107   char             **jit_source_roots;
108   char             **rust_source_roots;
109   CeedInt            num_rust_source_roots, max_rust_source_roots, num_rust_source_roots_readers;
110   CeedInt            num_jit_source_roots, max_jit_source_roots, num_jit_source_roots_readers;
111   bool               cuda_compile_with_clang;
112   char             **jit_defines;
113   CeedInt            num_jit_defines, max_jit_defines, num_jit_defines_readers;
114   CeedInt            num_tabs; /* Viewing offset */
115   int (*Error)(Ceed, const char *, int, const char *, int, const char *, va_list *);
116   int (*SetStream)(Ceed, void *);
117   int (*GetPreferredMemType)(CeedMemType *);
118   int (*Destroy)(Ceed);
119   int (*VectorCreate)(CeedSize, CeedVector);
120   int (*ElemRestrictionCreate)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
121   int (*ElemRestrictionCreateAtPoints)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
122   int (*ElemRestrictionCreateBlocked)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
123   int (*BasisCreateTensorH1)(CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, CeedBasis);
124   int (*BasisCreateH1)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
125                        CeedBasis);
126   int (*BasisCreateHdiv)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
127                          CeedBasis);
128   int (*BasisCreateHcurl)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
129                           CeedBasis);
130   int (*TensorContractCreate)(CeedTensorContract);
131   int (*QFunctionCreate)(CeedQFunction);
132   int (*QFunctionContextCreate)(CeedQFunctionContext);
133   int (*OperatorCreate)(CeedOperator);
134   int (*OperatorCreateAtPoints)(CeedOperator);
135   int (*CompositeOperatorCreate)(CeedOperator);
136   void           *data;
137   bool            is_debug;
138   bool            is_deterministic;
139   char            err_msg[CEED_MAX_RESOURCE_LEN];
140   FOffset        *f_offsets;
141   CeedWorkVectors work_vectors;
142 };
143 
144 struct CeedVector_private {
145   CeedObject_private obj;
146   int (*HasValidArray)(CeedVector, bool *);
147   int (*HasBorrowedArrayOfType)(CeedVector, CeedMemType, bool *);
148   int (*CopyStrided)(CeedVector, CeedSize, CeedSize, CeedSize, CeedVector);
149   int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *);
150   int (*SetValue)(CeedVector, CeedScalar);
151   int (*SetValueStrided)(CeedVector, CeedSize, CeedSize, CeedSize, CeedScalar);
152   int (*SyncArray)(CeedVector, CeedMemType);
153   int (*TakeArray)(CeedVector, CeedMemType, CeedScalar **);
154   int (*GetArray)(CeedVector, CeedMemType, CeedScalar **);
155   int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **);
156   int (*GetArrayWrite)(CeedVector, CeedMemType, CeedScalar **);
157   int (*RestoreArray)(CeedVector);
158   int (*RestoreArrayRead)(CeedVector);
159   int (*Norm)(CeedVector, CeedNormType, CeedScalar *);
160   int (*Scale)(CeedVector, CeedScalar);
161   int (*AXPY)(CeedVector, CeedScalar, CeedVector);
162   int (*AXPBY)(CeedVector, CeedScalar, CeedScalar, CeedVector);
163   int (*PointwiseMult)(CeedVector, CeedVector, CeedVector);
164   int (*Reciprocal)(CeedVector);
165   int (*Destroy)(CeedVector);
166   CeedSize length;
167   uint64_t state;
168   uint64_t num_readers;
169   CeedInt  num_tabs; /* Viewing offset */
170   void    *data;
171 };
172 
173 struct CeedElemRestriction_private {
174   CeedObject_private  obj;
175   CeedElemRestriction rstr_base;
176   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
177   int (*ApplyUnsigned)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
178   int (*ApplyUnoriented)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
179   int (*ApplyAtPointsInElement)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
180   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
181   int (*GetAtPointsElementOffset)(CeedElemRestriction, CeedInt, CeedSize *);
182   int (*GetOffsets)(CeedElemRestriction, CeedMemType, const CeedInt **);
183   int (*GetOrientations)(CeedElemRestriction, CeedMemType, const bool **);
184   int (*GetCurlOrientations)(CeedElemRestriction, CeedMemType, const CeedInt8 **);
185   int (*Destroy)(CeedElemRestriction);
186   CeedInt  num_elem;    /* number of elements */
187   CeedInt  elem_size;   /* number of nodes per element */
188   CeedInt  num_points;  /* number of points, for points restriction */
189   CeedInt  num_comp;    /* number of components */
190   CeedInt  comp_stride; /* Component stride for L-vector ordering */
191   CeedSize l_size;      /* size of the L-vector, can be used for checking for correct vector sizes */
192   CeedSize e_size;      /* minimum size of the E-vector, can be used for checking for correct vector sizes */
193   CeedInt  block_size;  /* number of elements in a batch */
194   CeedInt  num_block;   /* number of blocks of elements */
195   CeedInt *strides;     /* strides between [nodes, components, elements] */
196   CeedInt  l_layout[3]; /* L-vector layout [nodes, components, elements] */
197   CeedInt  e_layout[3]; /* E-vector layout [nodes, components, elements] */
198   CeedRestrictionType
199            rstr_type;   /* initialized in element restriction constructor for default, oriented, curl-oriented, or strided element restriction */
200   uint64_t num_readers; /* number of instances of offset read only access */
201   CeedInt  num_tabs;    /* Viewing offset */
202   void    *data;        /* place for the backend to store any data */
203 };
204 
205 struct CeedBasis_private {
206   CeedObject_private obj;
207   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector);
208   int (*ApplyAdd)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector);
209   int (*ApplyAtPoints)(CeedBasis, CeedInt, const CeedInt *, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector, CeedVector);
210   int (*ApplyAddAtPoints)(CeedBasis, CeedInt, const CeedInt *, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector, CeedVector);
211   int (*Destroy)(CeedBasis);
212   bool               is_tensor_basis; /* flag for tensor basis */
213   CeedInt            dim;             /* topological dimension */
214   CeedElemTopology   topo;            /* element topology */
215   CeedInt            num_comp;        /* number of field components (1 for scalar fields) */
216   CeedInt            P_1d;            /* number of nodes in one dimension */
217   CeedInt            Q_1d;            /* number of quadrature points in one dimension */
218   CeedInt            P;               /* total number of nodes */
219   CeedInt            Q;               /* total number of quadrature points */
220   CeedFESpace        fe_space;        /* initialized in basis constructor with 1, 2, 3 for H^1, H(div), and H(curl) FE space */
221   CeedTensorContract contract;        /* tensor contraction object */
222   CeedScalar        *q_ref_1d;        /* array of length Q1d holding the locations of quadrature points on the 1D reference element [-1, 1] */
223   CeedScalar        *q_weight_1d;     /* array of length Q1d holding the quadrature weights on the reference element */
224   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
225                          quadrature points */
226   CeedScalar *interp_1d; /* row-major matrix of shape [Q1d, P1d] expressing the values of nodal basis functions at quadrature points */
227   CeedScalar *grad;      /* row-major matrix of shape [dim * Q, P] matrix expressing derivatives of nodal basis functions at quadrature points */
228   CeedScalar *grad_1d;   /* row-major matrix of shape [Q1d, P1d] matrix expressing derivatives of nodal basis functions at quadrature points */
229   CeedScalar *div; /* row-major matrix of shape [Q, P] expressing the divergence of basis functions at quadrature points for H(div) discretizations */
230   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
231                        quadrature points for H(curl) discretizations */
232   CeedVector  vec_chebyshev;
233   CeedBasis   basis_chebyshev; /* basis interpolating from nodes to Chebyshev polynomial coefficients */
234   CeedInt     num_tabs;        /* Viewing offset */
235   void       *data;            /* place for the backend to store any data */
236 };
237 
238 struct CeedTensorContract_private {
239   CeedObject_private obj;
240   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt, const CeedScalar *restrict, CeedTransposeMode, const CeedInt,
241                const CeedScalar *restrict, CeedScalar *restrict);
242   int (*Destroy)(CeedTensorContract);
243   void *data;
244 };
245 
246 struct CeedQFunctionField_private {
247   const char  *field_name;
248   CeedInt      size;
249   CeedEvalMode eval_mode;
250 };
251 
252 struct CeedQFunction_private {
253   CeedObject_private obj;
254   int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *);
255   int (*SetCUDAUserFunction)(CeedQFunction, void *);
256   int (*SetHIPUserFunction)(CeedQFunction, void *);
257   int (*Destroy)(CeedQFunction);
258   CeedInt              vec_length; /* Number of quadrature points must be padded to a multiple of vec_length */
259   CeedInt              num_tabs;   /* Viewing offset */
260   CeedQFunctionField  *input_fields;
261   CeedQFunctionField  *output_fields;
262   CeedInt              num_input_fields, num_output_fields;
263   CeedQFunctionUser    function;
264   CeedInt              user_flop_estimate;
265   const char          *user_source;
266   const char          *source_path;
267   const char          *kernel_name;
268   const char          *gallery_name;
269   bool                 is_gallery;
270   bool                 is_identity;
271   bool                 is_fortran;
272   bool                 is_immutable;
273   bool                 is_context_writable;
274   CeedQFunctionContext ctx;  /* user context for function */
275   void                *data; /* place for the backend to store any data */
276 };
277 
278 struct CeedQFunctionContext_private {
279   CeedObject_private obj;
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   CeedObject_private obj;
356   CeedOperator       op_fallback, op_fallback_parent;
357   int (*LinearAssembleQFunction)(CeedOperator, CeedVector *, CeedElemRestriction *, CeedRequest *);
358   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *);
359   int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *);
360   int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *);
361   int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
362   int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *);
363   int (*LinearAssembleSymbolic)(CeedOperator, CeedSize *, CeedInt **, CeedInt **);
364   int (*LinearAssemble)(CeedOperator, CeedVector);
365   int (*LinearAssembleSingle)(CeedOperator, CeedInt, CeedVector);
366   int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *);
367   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
368   int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
369   int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
370   int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
371   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector, CeedVector, CeedRequest *);
372   int (*Destroy)(CeedOperator);
373   CeedOperatorField        *input_fields;
374   CeedOperatorField        *output_fields;
375   CeedSize                  input_size, output_size;
376   CeedInt                   num_tabs;   /* Viewing offset */
377   CeedInt                   num_elem;   /* Number of elements */
378   CeedInt                   num_qpts;   /* Number of quadrature points over all elements */
379   CeedInt                   num_fields; /* Number of fields that have been set */
380   CeedQFunction             qf;
381   CeedQFunction             dqf;
382   CeedQFunction             dqfT;
383   const char               *name;
384   bool                      is_immutable;
385   bool                      is_interface_setup;
386   bool                      is_backend_setup;
387   bool                      is_composite;
388   bool                      is_at_points;
389   bool                      has_restriction;
390   CeedQFunctionAssemblyData qf_assembled;
391   CeedOperatorAssemblyData  op_assembled;
392   CeedOperator             *sub_operators;
393   CeedInt                   num_suboperators;
394   void                     *data;
395   CeedInt                   num_context_labels;
396   CeedInt                   max_context_labels;
397   CeedContextFieldLabel    *context_labels;
398   CeedElemRestriction       rstr_points, first_points_rstr;
399   CeedVector                point_coords;
400 };
401