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