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 *user_source; 256 const char *source_path; 257 const char *kernel_name; 258 const char *gallery_name; 259 bool is_gallery; 260 bool is_identity; 261 bool is_fortran; 262 bool is_immutable; 263 bool is_context_writable; 264 CeedQFunctionContext ctx; /* user context for function */ 265 void *data; /* place for the backend to store any data */ 266 }; 267 268 struct CeedQFunctionContext_private { 269 Ceed ceed; 270 int ref_count; 271 int (*HasValidData)(CeedQFunctionContext, bool *); 272 int (*HasBorrowedDataOfType)(CeedQFunctionContext, CeedMemType, bool *); 273 int (*SetData)(CeedQFunctionContext, CeedMemType, CeedCopyMode, void *); 274 int (*TakeData)(CeedQFunctionContext, CeedMemType, void *); 275 int (*GetData)(CeedQFunctionContext, CeedMemType, void *); 276 int (*GetDataRead)(CeedQFunctionContext, CeedMemType, void *); 277 int (*RestoreData)(CeedQFunctionContext); 278 int (*RestoreDataRead)(CeedQFunctionContext); 279 int (*DataDestroy)(CeedQFunctionContext); 280 int (*Destroy)(CeedQFunctionContext); 281 CeedQFunctionContextDataDestroyUser data_destroy_function; 282 CeedMemType data_destroy_mem_type; 283 CeedInt num_fields; 284 CeedInt max_fields; 285 CeedContextFieldLabel *field_labels; 286 uint64_t state; 287 uint64_t num_readers; 288 size_t ctx_size; 289 void *data; 290 }; 291 292 /// Struct to handle the context data to use the Fortran QFunction stub 293 /// @ingroup CeedQFunction 294 struct CeedFortranContext_private { 295 CeedQFunctionContext inner_ctx; 296 void (*f)(void *ctx, int *nq, 297 const CeedScalar *u,const CeedScalar *u1, 298 const CeedScalar *u2,const CeedScalar *u3, 299 const CeedScalar *u4,const CeedScalar *u5, 300 const CeedScalar *u6,const CeedScalar *u7, 301 const CeedScalar *u8,const CeedScalar *u9, 302 const CeedScalar *u10,const CeedScalar *u11, 303 const CeedScalar *u12,const CeedScalar *u13, 304 const CeedScalar *u14,const CeedScalar *u15, 305 CeedScalar *v,CeedScalar *v1,CeedScalar *v2, 306 CeedScalar *v3,CeedScalar *v4,CeedScalar *v5, 307 CeedScalar *v6,CeedScalar *v7,CeedScalar *v8, 308 CeedScalar *v9, CeedScalar *v10,CeedScalar *v11, 309 CeedScalar *v12,CeedScalar *v13,CeedScalar *v14, 310 CeedScalar *v15, int *err); 311 }; 312 typedef struct CeedFortranContext_private *CeedFortranContext; 313 314 struct CeedContextFieldLabel_private { 315 const char *name; 316 const char *description; 317 CeedContextFieldType type; 318 size_t size; 319 size_t num_values; 320 size_t offset; 321 CeedInt num_sub_labels; 322 CeedContextFieldLabel *sub_labels; 323 }; 324 325 struct CeedOperatorField_private { 326 CeedElemRestriction elem_restr; /* Restriction from L-vector */ 327 CeedBasis basis; /* Basis or CEED_BASIS_COLLOCATED for 328 collocated fields */ 329 CeedVector vec; /* State vector for passive fields or 330 CEED_VECTOR_NONE for no vector */ 331 const char *field_name; /* matching QFunction field name */ 332 }; 333 334 struct CeedQFunctionAssemblyData_private { 335 Ceed ceed; 336 int ref_count; 337 bool is_setup; 338 bool reuse_data; 339 bool needs_data_update; 340 CeedVector vec; 341 CeedElemRestriction rstr; 342 }; 343 344 struct CeedOperatorAssemblyData_private { 345 Ceed ceed; 346 CeedInt num_eval_mode_in, num_eval_mode_out; 347 CeedEvalMode *eval_mode_in, *eval_mode_out; 348 CeedScalar *B_in, *B_out; 349 CeedBasis basis_in, basis_out; 350 }; 351 352 struct CeedOperator_private { 353 Ceed ceed; 354 CeedOperator op_fallback; 355 int ref_count; 356 int (*LinearAssembleQFunction)(CeedOperator, CeedVector *, 357 CeedElemRestriction *, CeedRequest *); 358 int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, 359 CeedElemRestriction, CeedRequest *); 360 int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *); 361 int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *); 362 int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector, 363 CeedRequest *); 364 int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector, 365 CeedRequest *); 366 int (*LinearAssembleSymbolic)(CeedOperator, CeedSize *, CeedInt **, 367 CeedInt **); 368 int (*LinearAssemble)(CeedOperator, CeedVector); 369 int (*LinearAssembleSingle)(CeedOperator, CeedInt, CeedVector); 370 int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *); 371 int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 372 int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 373 int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 374 int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 375 int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector, 376 CeedVector, CeedRequest *); 377 int (*Destroy)(CeedOperator); 378 CeedOperatorField *input_fields; 379 CeedOperatorField *output_fields; 380 CeedSize input_size, output_size; 381 CeedInt num_elem; /* Number of elements */ 382 CeedInt num_qpts; /* Number of quadrature points over all elements */ 383 CeedInt num_fields; /* Number of fields that have been set */ 384 CeedQFunction qf; 385 CeedQFunction dqf; 386 CeedQFunction dqfT; 387 const char *name; 388 bool is_immutable; 389 bool is_interface_setup; 390 bool is_backend_setup; 391 bool is_composite; 392 bool has_restriction; 393 CeedQFunctionAssemblyData qf_assembled; 394 CeedOperatorAssemblyData op_assembled; 395 CeedOperator *sub_operators; 396 CeedInt num_suboperators; 397 void *data; 398 CeedInt num_context_labels; 399 CeedInt max_context_labels; 400 CeedContextFieldLabel *context_labels; 401 }; 402 403 CEED_INTERN int CeedOperatorGetFallback(CeedOperator op, 404 CeedOperator *op_fallback); 405 406 #endif 407