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