xref: /libCEED/include/ceed-impl.h (revision 5107b09fcb4710dffb8bf2363d6d7d4be3d24cc9)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 /// @file
18 /// Private header for frontend components of libCEED
19 #ifndef _ceed_impl_h
20 #define _ceed_impl_h
21 
22 #include <ceed.h>
23 #include <ceed-backend.h>
24 #include <stdbool.h>
25 
26 #define CEED_INTERN CEED_EXTERN __attribute__((visibility ("hidden")))
27 
28 #define CEED_MAX_RESOURCE_LEN 1024
29 #define CEED_ALIGN 64
30 
31 #define CEED_COMPOSITE_MAX 16
32 
33 // Lookup table field for backend functions
34 typedef struct {
35   const char *fname;
36   size_t offset;
37 } foffset;
38 
39 // Lookup table field for object delegates
40 typedef struct {
41   char *objname;
42   Ceed delegate;
43 } objdelegate;
44 
45 struct Ceed_private {
46   const char *resource;
47   Ceed delegate;
48   Ceed parent;
49   objdelegate *objdelegates;
50   int objdelegatecount;
51   Ceed opfallbackceed, opfallbackparent;
52   const char *opfallbackresource;
53   int (*Error)(Ceed, const char *, int, const char *, int, const char *,
54                va_list);
55   int (*GetPreferredMemType)(CeedMemType *);
56   int (*Destroy)(Ceed);
57   int (*VectorCreate)(CeedInt, CeedVector);
58   int (*ElemRestrictionCreate)(CeedMemType, CeedCopyMode,
59                                const CeedInt *, CeedElemRestriction);
60   int (*ElemRestrictionCreateBlocked)(CeedMemType, CeedCopyMode,
61                                       const CeedInt *, CeedElemRestriction);
62   int (*BasisCreateTensorH1)(CeedInt, CeedInt, CeedInt, const CeedScalar *,
63                              const CeedScalar *, const CeedScalar *,
64                              const CeedScalar *, CeedBasis);
65   int (*BasisCreateH1)(CeedElemTopology, CeedInt, CeedInt, CeedInt,
66                        const CeedScalar *,
67                        const CeedScalar *, const CeedScalar *,
68                        const CeedScalar *, CeedBasis);
69   int (*TensorContractCreate)(CeedBasis, CeedTensorContract);
70   int (*QFunctionCreate)(CeedQFunction);
71   int (*OperatorCreate)(CeedOperator);
72   int (*CompositeOperatorCreate)(CeedOperator);
73   int refcount;
74   void *data;
75   foffset *foffsets;
76 };
77 
78 struct CeedVector_private {
79   Ceed ceed;
80   int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *);
81   int (*SetValue)(CeedVector, CeedScalar);
82   int (*SyncArray)(CeedVector, CeedMemType);
83   int (*GetArray)(CeedVector, CeedMemType, CeedScalar **);
84   int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **);
85   int (*RestoreArray)(CeedVector);
86   int (*RestoreArrayRead)(CeedVector);
87   int (*Destroy)(CeedVector);
88   int refcount;
89   CeedInt length;
90   uint64_t state;
91   uint64_t numreaders;
92   void *data;
93 };
94 
95 struct CeedElemRestriction_private {
96   Ceed ceed;
97   int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedTransposeMode,
98                CeedVector, CeedVector, CeedRequest *);
99   int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode,
100                     CeedTransposeMode, CeedVector, CeedVector, CeedRequest *);
101   int (*Destroy)(CeedElemRestriction);
102   int refcount;
103   CeedInt nelem;    /* number of elements */
104   CeedInt elemsize; /* number of nodes per element */
105   CeedInt nnodes;   /* size of the L-vector, can be used for checking for
106                       correct vector sizes */
107   CeedInt ncomp;    /* number of components */
108   CeedInt blksize;  /* number of elements in a batch */
109   CeedInt nblk;     /* number of blocks of elements */
110   void *data;       /* place for the backend to store any data */
111 };
112 
113 struct CeedBasis_private {
114   Ceed ceed;
115   int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode,
116                CeedVector, CeedVector);
117   int (*Destroy)(CeedBasis);
118   int refcount;
119   bool tensorbasis;      /* flag for tensor basis */
120   CeedInt dim;           /* topological dimension */
121   CeedInt ncomp;         /* number of field components (1 for scalar fields) */
122   CeedInt P1d;           /* number of nodes in one dimension */
123   CeedInt Q1d;           /* number of quadrature points in one dimension */
124   CeedInt P;             /* total number of nodes */
125   CeedInt Q;             /* total number of quadrature points */
126   CeedScalar *qref1d;    /* Array of length Q1d holding the locations of
127                             quadrature points on the 1D reference element [-1, 1] */
128   CeedScalar *qweight1d; /* array of length Q1d holding the quadrature weights on
129                             the reference element */
130   CeedScalar
131   *interp1d;  /* row-major matrix of shape [Q1d, P1d] expressing the values of
132                             nodal basis functions at quadrature points */
133   CeedScalar
134   *grad1d;    /* row-major matrix of shape [Q1d, P1d] matrix expressing derivatives of
135                             nodal basis functions at quadrature points */
136   CeedTensorContract contract; /* tensor contraction object */
137   void *data;            /* place for the backend to store any data */
138 };
139 
140 struct CeedTensorContract_private {
141   Ceed ceed;
142   int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt,
143                const CeedScalar *restrict, CeedTransposeMode, const CeedInt,
144                const CeedScalar *restrict, CeedScalar *restrict);
145   int (*Destroy)(CeedTensorContract);
146   int refcount;
147   void *data;
148 };
149 
150 struct CeedQFunctionField_private {
151   const char *fieldname;
152   CeedInt size;
153   CeedEvalMode emode;
154 };
155 
156 struct CeedQFunction_private {
157   Ceed ceed;
158   int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *);
159   int (*Destroy)(CeedQFunction);
160   int refcount;
161   CeedInt vlength;    // Number of quadrature points must be padded to a multiple of vlength
162   CeedQFunctionField *inputfields;
163   CeedQFunctionField *outputfields;
164   CeedInt numinputfields, numoutputfields;
165   CeedQFunctionUser function;
166   const char *sourcepath;
167   const char *qfname;
168   bool fortranstatus;
169   bool identity;
170   void *ctx;      /* user context for function */
171   size_t ctxsize; /* size of user context; may be used to copy to a device */
172   void *data;     /* backend data */
173 };
174 
175 /// Struct to handle the context data to use the Fortran QFunction stub
176 /// @ingroup CeedQFunction
177 typedef struct {
178   CeedScalar *innerctx;
179   size_t innerctxsize;
180   void (*f)(void *ctx, int *nq,
181             const CeedScalar *u,const CeedScalar *u1,
182             const CeedScalar *u2,const CeedScalar *u3,
183             const CeedScalar *u4,const CeedScalar *u5,
184             const CeedScalar *u6,const CeedScalar *u7,
185             const CeedScalar *u8,const CeedScalar *u9,
186             const CeedScalar *u10,const CeedScalar *u11,
187             const CeedScalar *u12,const CeedScalar *u13,
188             const CeedScalar *u14,const CeedScalar *u15,
189             CeedScalar *v,CeedScalar *v1,CeedScalar *v2,
190             CeedScalar *v3,CeedScalar *v4,CeedScalar *v5,
191             CeedScalar *v6,CeedScalar *v7,CeedScalar *v8,
192             CeedScalar *v9, CeedScalar *v10,CeedScalar *v11,
193             CeedScalar *v12,CeedScalar *v13,CeedScalar *v14,
194             CeedScalar *v15, int *err);
195 } fContext;
196 
197 struct CeedOperatorField_private {
198   CeedElemRestriction Erestrict; /// Restriction from L-vector or NULL if identity
199   CeedTransposeMode lmode;       /// Transpose mode for lvector ordering
200   CeedBasis basis;               /// Basis or NULL for collocated fields
201   CeedVector
202   vec;                /// State vector for passive fields, NULL for active fields
203 };
204 
205 struct CeedOperator_private {
206   Ceed ceed;
207   CeedOperator opfallback;
208   CeedQFunction qffallback;
209   int refcount;
210   int (*AssembleLinearQFunction)(CeedOperator, CeedVector *,
211                                  CeedElemRestriction *, CeedRequest *);
212   int (*AssembleLinearDiagonal)(CeedOperator, CeedVector *, CeedRequest *);
213   int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *);
214   int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector,
215                        CeedVector, CeedRequest *);
216   int (*Destroy)(CeedOperator);
217   CeedOperatorField *inputfields;
218   CeedOperatorField *outputfields;
219   CeedInt numelements; /// Number of elements
220   CeedInt numqpoints;  /// Number of quadrature points over all elements
221   CeedInt nfields;     /// Number of fields that have been set
222   CeedQFunction qf;
223   CeedQFunction dqf;
224   CeedQFunction dqfT;
225   bool setupdone;
226   bool composite;
227   bool hasrestriction;
228   CeedOperator *suboperators;
229   CeedInt numsub;
230   void *data;
231 };
232 
233 CEED_INTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int,
234                                 const char *, va_list);
235 CEED_INTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int,
236                                const char *, va_list);
237 CEED_INTERN int CeedErrorExit(Ceed, const char *, int, const char *, int,
238                               const char *, va_list);
239 CEED_INTERN int CeedSetErrorHandler(Ceed ceed,
240                                     int (eh)(Ceed, const char *, int,
241                                         const char *, int, const char *,
242                                         va_list));
243 
244 #endif
245