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