xref: /libCEED/rust/libceed-sys/c-src/include/ceed-impl.h (revision ca94c3ddc8f82b7d93a79f9e4812e99b8be840ff)
1 // Copyright (c) 2017-2022, 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 #ifndef CEED_IMPL_H
11 #define CEED_IMPL_H
12 
13 #include <ceed.h>
14 #include <ceed/backend.h>
15 #include <stdbool.h>
16 
17 CEED_INTERN const char *CeedJitSourceRootDefault;
18 
19 /** @defgroup CeedUser Public API for Ceed
20     @ingroup Ceed
21 */
22 /** @defgroup CeedBackend Backend API for Ceed
23     @ingroup Ceed
24 */
25 /** @defgroup CeedDeveloper Internal library functions for Ceed
26     @ingroup Ceed
27 */
28 /** @defgroup CeedVectorUser Public API for CeedVector
29     @ingroup CeedVector
30 */
31 /** @defgroup CeedVectorBackend Backend API for CeedVector
32     @ingroup CeedVector
33 */
34 /** @defgroup CeedVectorDeveloper Internal library functions for CeedVector
35     @ingroup CeedVector
36 */
37 /** @defgroup CeedElemRestrictionUser Public API for CeedElemRestriction
38     @ingroup CeedElemRestriction
39 */
40 /** @defgroup CeedElemRestrictionBackend Backend API for CeedElemRestriction
41     @ingroup CeedElemRestriction
42 */
43 /** @defgroup CeedElemRestrictionDeveloper Internal library functions for CeedElemRestriction
44     @ingroup CeedElemRestriction
45 */
46 /** @defgroup CeedBasisUser Public API for CeedBasis
47     @ingroup CeedBasis
48 */
49 /** @defgroup CeedBasisBackend Backend API for CeedBasis
50     @ingroup CeedBasis
51 */
52 /** @defgroup CeedBasisDeveloper Internal library functions for CeedBasis
53     @ingroup CeedBasis
54 */
55 /** @defgroup CeedQFunctionUser Public API for CeedQFunction
56     @ingroup CeedQFunction
57 */
58 /** @defgroup CeedQFunctionBackend Backend API for CeedQFunction
59     @ingroup CeedQFunction
60 */
61 /** @defgroup CeedQFunctionDeveloper Internal library functions for CeedQFunction
62     @ingroup CeedQFunction
63 */
64 /** @defgroup CeedOperatorUser Public API for CeedOperator
65     @ingroup CeedOperator
66 */
67 /** @defgroup CeedOperatorBackend Backend API for CeedOperator
68     @ingroup CeedOperator
69 */
70 /** @defgroup CeedOperatorDeveloper Internal library functions for CeedOperator
71     @ingroup CeedOperator
72 */
73 
74 // Lookup table field for backend functions
75 typedef struct {
76   const char *func_name;
77   size_t      offset;
78 } FOffset;
79 
80 // Lookup table field for object delegates
81 typedef struct {
82   char *obj_name;
83   Ceed  delegate;
84 } ObjDelegate;
85 
86 struct Ceed_private {
87   const char  *resource;
88   Ceed         delegate;
89   Ceed         parent;
90   ObjDelegate *obj_delegates;
91   int          obj_delegate_count;
92   Ceed         op_fallback_ceed, op_fallback_parent;
93   const char  *op_fallback_resource;
94   char       **jit_source_roots;
95   CeedInt      num_jit_source_roots;
96   int (*Error)(Ceed, const char *, int, const char *, int, const char *, va_list *);
97   int (*SetStream)(Ceed, void *);
98   int (*GetPreferredMemType)(CeedMemType *);
99   int (*Destroy)(Ceed);
100   int (*VectorCreate)(CeedSize, CeedVector);
101   int (*ElemRestrictionCreate)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
102   int (*ElemRestrictionCreateAtPoints)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
103   int (*ElemRestrictionCreateBlocked)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction);
104   int (*BasisCreateTensorH1)(CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, CeedBasis);
105   int (*BasisCreateH1)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
106                        CeedBasis);
107   int (*BasisCreateHdiv)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
108                          CeedBasis);
109   int (*BasisCreateHcurl)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *,
110                           CeedBasis);
111   int (*TensorContractCreate)(CeedTensorContract);
112   int (*QFunctionCreate)(CeedQFunction);
113   int (*QFunctionContextCreate)(CeedQFunctionContext);
114   int (*OperatorCreate)(CeedOperator);
115   int (*OperatorCreateAtPoints)(CeedOperator);
116   int (*CompositeOperatorCreate)(CeedOperator);
117   int      ref_count;
118   void    *data;
119   bool     is_debug;
120   bool     has_valid_op_fallback_resource;
121   bool     is_deterministic;
122   char     err_msg[CEED_MAX_RESOURCE_LEN];
123   FOffset *f_offsets;
124 };
125 
126 struct CeedVector_private {
127   Ceed ceed;
128   int (*HasValidArray)(CeedVector, bool *);
129   int (*HasBorrowedArrayOfType)(CeedVector, CeedMemType, bool *);
130   int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *);
131   int (*SetValue)(CeedVector, CeedScalar);
132   int (*SyncArray)(CeedVector, CeedMemType);
133   int (*TakeArray)(CeedVector, CeedMemType, CeedScalar **);
134   int (*GetArray)(CeedVector, CeedMemType, CeedScalar **);
135   int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **);
136   int (*GetArrayWrite)(CeedVector, CeedMemType, CeedScalar **);
137   int (*RestoreArray)(CeedVector);
138   int (*RestoreArrayRead)(CeedVector);
139   int (*Norm)(CeedVector, CeedNormType, CeedScalar *);
140   int (*Scale)(CeedVector, CeedScalar);
141   int (*AXPY)(CeedVector, CeedScalar, CeedVector);
142   int (*AXPBY)(CeedVector, CeedScalar, CeedScalar, CeedVector);
143   int (*PointwiseMult)(CeedVector, CeedVector, CeedVector);
144   int (*Reciprocal)(CeedVector);
145   int (*Destroy)(CeedVector);
146   int      ref_count;
147   CeedSize length;
148   uint64_t state;
149   uint64_t num_readers;
150   void    *data;
151 };
152 
153 struct CeedElemRestriction_private {
154   Ceed                ceed;
155   CeedElemRestriction rstr_base;
156   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
157   int (*ApplyUnsigned)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
158   int (*ApplyUnoriented)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
159   int (*ApplyAtPointsInElement)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
160   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
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  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 
379 #endif  // CEED_IMPL_H
380