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