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