1 // Copyright (c) 2017-2024, 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 #pragma once 11 12 #include <ceed.h> 13 #include <ceed/backend.h> 14 #include <stdbool.h> 15 16 CEED_INTERN const char *CeedJitSourceRootDefault; 17 18 /** @defgroup CeedUser Public API for Ceed 19 @ingroup Ceed 20 */ 21 /** @defgroup CeedBackend Backend API for Ceed 22 @ingroup Ceed 23 */ 24 /** @defgroup CeedDeveloper Internal library functions for Ceed 25 @ingroup Ceed 26 */ 27 /** @defgroup CeedVectorUser Public API for CeedVector 28 @ingroup CeedVector 29 */ 30 /** @defgroup CeedVectorBackend Backend API for CeedVector 31 @ingroup CeedVector 32 */ 33 /** @defgroup CeedVectorDeveloper Internal library functions for CeedVector 34 @ingroup CeedVector 35 */ 36 /** @defgroup CeedElemRestrictionUser Public API for CeedElemRestriction 37 @ingroup CeedElemRestriction 38 */ 39 /** @defgroup CeedElemRestrictionBackend Backend API for CeedElemRestriction 40 @ingroup CeedElemRestriction 41 */ 42 /** @defgroup CeedElemRestrictionDeveloper Internal library functions for CeedElemRestriction 43 @ingroup CeedElemRestriction 44 */ 45 /** @defgroup CeedBasisUser Public API for CeedBasis 46 @ingroup CeedBasis 47 */ 48 /** @defgroup CeedBasisBackend Backend API for CeedBasis 49 @ingroup CeedBasis 50 */ 51 /** @defgroup CeedBasisDeveloper Internal library functions for CeedBasis 52 @ingroup CeedBasis 53 */ 54 /** @defgroup CeedQFunctionUser Public API for CeedQFunction 55 @ingroup CeedQFunction 56 */ 57 /** @defgroup CeedQFunctionBackend Backend API for CeedQFunction 58 @ingroup CeedQFunction 59 */ 60 /** @defgroup CeedQFunctionDeveloper Internal library functions for CeedQFunction 61 @ingroup CeedQFunction 62 */ 63 /** @defgroup CeedOperatorUser Public API for CeedOperator 64 @ingroup CeedOperator 65 */ 66 /** @defgroup CeedOperatorBackend Backend API for CeedOperator 67 @ingroup CeedOperator 68 */ 69 /** @defgroup CeedOperatorDeveloper Internal library functions for CeedOperator 70 @ingroup CeedOperator 71 */ 72 73 // Lookup table field for backend functions 74 typedef struct { 75 const char *func_name; 76 size_t offset; 77 } FOffset; 78 79 // Lookup table field for object delegates 80 typedef struct { 81 char *obj_name; 82 Ceed delegate; 83 } ObjDelegate; 84 85 // Work vector tracking 86 typedef struct CeedWorkVectors_private *CeedWorkVectors; 87 struct CeedWorkVectors_private { 88 CeedInt num_vecs, max_vecs; 89 bool *is_in_use; 90 CeedVector *vecs; 91 }; 92 93 struct Ceed_private { 94 const char *resource; 95 Ceed delegate; 96 Ceed parent; 97 ObjDelegate *obj_delegates; 98 int obj_delegate_count; 99 Ceed op_fallback_ceed, op_fallback_parent; 100 const char *op_fallback_resource; 101 char **jit_source_roots; 102 CeedInt num_jit_source_roots, max_jit_source_roots; 103 char **jit_defines; 104 CeedInt num_jit_defines, max_jit_defines; 105 int (*Error)(Ceed, const char *, int, const char *, int, const char *, va_list *); 106 int (*SetStream)(Ceed, void *); 107 int (*GetPreferredMemType)(CeedMemType *); 108 int (*Destroy)(Ceed); 109 int (*VectorCreate)(CeedSize, CeedVector); 110 int (*ElemRestrictionCreate)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction); 111 int (*ElemRestrictionCreateAtPoints)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction); 112 int (*ElemRestrictionCreateBlocked)(CeedMemType, CeedCopyMode, const CeedInt *, const bool *, const CeedInt8 *, CeedElemRestriction); 113 int (*BasisCreateTensorH1)(CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, CeedBasis); 114 int (*BasisCreateH1)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, 115 CeedBasis); 116 int (*BasisCreateHdiv)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, 117 CeedBasis); 118 int (*BasisCreateHcurl)(CeedElemTopology, CeedInt, CeedInt, CeedInt, const CeedScalar *, const CeedScalar *, const CeedScalar *, const CeedScalar *, 119 CeedBasis); 120 int (*TensorContractCreate)(CeedTensorContract); 121 int (*QFunctionCreate)(CeedQFunction); 122 int (*QFunctionContextCreate)(CeedQFunctionContext); 123 int (*OperatorCreate)(CeedOperator); 124 int (*OperatorCreateAtPoints)(CeedOperator); 125 int (*CompositeOperatorCreate)(CeedOperator); 126 int ref_count; 127 void *data; 128 bool is_debug; 129 bool has_valid_op_fallback_resource; 130 bool is_deterministic; 131 char err_msg[CEED_MAX_RESOURCE_LEN]; 132 FOffset *f_offsets; 133 CeedWorkVectors work_vectors; 134 }; 135 136 struct CeedVector_private { 137 Ceed ceed; 138 int (*HasValidArray)(CeedVector, bool *); 139 int (*HasBorrowedArrayOfType)(CeedVector, CeedMemType, bool *); 140 int (*CopyStrided)(CeedVector, CeedSize, CeedSize, CeedVector); 141 int (*SetArray)(CeedVector, CeedMemType, CeedCopyMode, CeedScalar *); 142 int (*SetValue)(CeedVector, CeedScalar); 143 int (*SetValueStrided)(CeedVector, CeedSize, CeedSize, CeedScalar); 144 int (*SyncArray)(CeedVector, CeedMemType); 145 int (*TakeArray)(CeedVector, CeedMemType, CeedScalar **); 146 int (*GetArray)(CeedVector, CeedMemType, CeedScalar **); 147 int (*GetArrayRead)(CeedVector, CeedMemType, const CeedScalar **); 148 int (*GetArrayWrite)(CeedVector, CeedMemType, CeedScalar **); 149 int (*RestoreArray)(CeedVector); 150 int (*RestoreArrayRead)(CeedVector); 151 int (*Norm)(CeedVector, CeedNormType, CeedScalar *); 152 int (*Scale)(CeedVector, CeedScalar); 153 int (*AXPY)(CeedVector, CeedScalar, CeedVector); 154 int (*AXPBY)(CeedVector, CeedScalar, CeedScalar, CeedVector); 155 int (*PointwiseMult)(CeedVector, CeedVector, CeedVector); 156 int (*Reciprocal)(CeedVector); 157 int (*Destroy)(CeedVector); 158 int ref_count; 159 CeedSize length; 160 uint64_t state; 161 uint64_t num_readers; 162 void *data; 163 }; 164 165 struct CeedElemRestriction_private { 166 Ceed ceed; 167 CeedElemRestriction rstr_base; 168 int (*Apply)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *); 169 int (*ApplyUnsigned)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *); 170 int (*ApplyUnoriented)(CeedElemRestriction, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *); 171 int (*ApplyAtPointsInElement)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *); 172 int (*ApplyBlock)(CeedElemRestriction, CeedInt, CeedTransposeMode, CeedVector, CeedVector, CeedRequest *); 173 int (*GetAtPointsElementOffset)(CeedElemRestriction, CeedInt, CeedSize *); 174 int (*GetOffsets)(CeedElemRestriction, CeedMemType, const CeedInt **); 175 int (*GetOrientations)(CeedElemRestriction, CeedMemType, const bool **); 176 int (*GetCurlOrientations)(CeedElemRestriction, CeedMemType, const CeedInt8 **); 177 int (*Destroy)(CeedElemRestriction); 178 int ref_count; 179 CeedInt num_elem; /* number of elements */ 180 CeedInt elem_size; /* number of nodes per element */ 181 CeedInt num_points; /* number of points, for points restriction */ 182 CeedInt num_comp; /* number of components */ 183 CeedInt comp_stride; /* Component stride for L-vector ordering */ 184 CeedSize l_size; /* size of the L-vector, can be used for checking for correct vector sizes */ 185 CeedSize e_size; /* minimum size of the E-vector, can be used for checking for correct vector sizes */ 186 CeedInt block_size; /* number of elements in a batch */ 187 CeedInt num_block; /* number of blocks of elements */ 188 CeedInt *strides; /* strides between [nodes, components, elements] */ 189 CeedInt l_layout[3]; /* L-vector layout [nodes, components, elements] */ 190 CeedInt e_layout[3]; /* E-vector layout [nodes, components, elements] */ 191 CeedRestrictionType 192 rstr_type; /* initialized in element restriction constructor for default, oriented, curl-oriented, or strided element restriction */ 193 uint64_t num_readers; /* number of instances of offset read only access */ 194 void *data; /* place for the backend to store any data */ 195 }; 196 197 struct CeedBasis_private { 198 Ceed ceed; 199 int (*Apply)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector); 200 int (*ApplyAdd)(CeedBasis, CeedInt, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector); 201 int (*ApplyAtPoints)(CeedBasis, CeedInt, const CeedInt *, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector, CeedVector); 202 int (*ApplyAddAtPoints)(CeedBasis, CeedInt, const CeedInt *, CeedTransposeMode, CeedEvalMode, CeedVector, CeedVector, CeedVector); 203 int (*Destroy)(CeedBasis); 204 int ref_count; 205 bool is_tensor_basis; /* flag for tensor basis */ 206 CeedInt dim; /* topological dimension */ 207 CeedElemTopology topo; /* element topology */ 208 CeedInt num_comp; /* number of field components (1 for scalar fields) */ 209 CeedInt P_1d; /* number of nodes in one dimension */ 210 CeedInt Q_1d; /* number of quadrature points in one dimension */ 211 CeedInt P; /* total number of nodes */ 212 CeedInt Q; /* total number of quadrature points */ 213 CeedFESpace fe_space; /* initialized in basis constructor with 1, 2, 3 for H^1, H(div), and H(curl) FE space */ 214 CeedTensorContract contract; /* tensor contraction object */ 215 CeedScalar *q_ref_1d; /* array of length Q1d holding the locations of quadrature points on the 1D reference element [-1, 1] */ 216 CeedScalar *q_weight_1d; /* array of length Q1d holding the quadrature weights on the reference element */ 217 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 218 quadrature points */ 219 CeedScalar *interp_1d; /* row-major matrix of shape [Q1d, P1d] expressing the values of nodal basis functions at quadrature points */ 220 CeedScalar *grad; /* row-major matrix of shape [dim * Q, P] matrix expressing derivatives of nodal basis functions at quadrature points */ 221 CeedScalar *grad_1d; /* row-major matrix of shape [Q1d, P1d] matrix expressing derivatives of nodal basis functions at quadrature points */ 222 CeedScalar *div; /* row-major matrix of shape [Q, P] expressing the divergence of basis functions at quadrature points for H(div) discretizations */ 223 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 224 quadrature points for H(curl) discretizations */ 225 CeedVector vec_chebyshev; 226 CeedBasis basis_chebyshev; /* basis interpolating from nodes to Chebyshev polynomial coefficients */ 227 void *data; /* place for the backend to store any data */ 228 }; 229 230 struct CeedTensorContract_private { 231 Ceed ceed; 232 int (*Apply)(CeedTensorContract, CeedInt, CeedInt, CeedInt, CeedInt, const CeedScalar *restrict, CeedTransposeMode, const CeedInt, 233 const CeedScalar *restrict, CeedScalar *restrict); 234 int (*Destroy)(CeedTensorContract); 235 int ref_count; 236 void *data; 237 }; 238 239 struct CeedQFunctionField_private { 240 const char *field_name; 241 CeedInt size; 242 CeedEvalMode eval_mode; 243 }; 244 245 struct CeedQFunction_private { 246 Ceed ceed; 247 int (*Apply)(CeedQFunction, CeedInt, CeedVector *, CeedVector *); 248 int (*SetCUDAUserFunction)(CeedQFunction, void *); 249 int (*SetHIPUserFunction)(CeedQFunction, void *); 250 int (*Destroy)(CeedQFunction); 251 int ref_count; 252 CeedInt vec_length; /* Number of quadrature points must be padded to a multiple of vec_length */ 253 CeedQFunctionField *input_fields; 254 CeedQFunctionField *output_fields; 255 CeedInt num_input_fields, num_output_fields; 256 CeedQFunctionUser function; 257 CeedInt user_flop_estimate; 258 const char *user_source; 259 const char *source_path; 260 const char *kernel_name; 261 const char *gallery_name; 262 bool is_gallery; 263 bool is_identity; 264 bool is_fortran; 265 bool is_immutable; 266 bool is_context_writable; 267 CeedQFunctionContext ctx; /* user context for function */ 268 void *data; /* place for the backend to store any data */ 269 }; 270 271 struct CeedQFunctionContext_private { 272 Ceed ceed; 273 int ref_count; 274 int (*HasValidData)(CeedQFunctionContext, bool *); 275 int (*HasBorrowedDataOfType)(CeedQFunctionContext, CeedMemType, bool *); 276 int (*SetData)(CeedQFunctionContext, CeedMemType, CeedCopyMode, void *); 277 int (*TakeData)(CeedQFunctionContext, CeedMemType, void *); 278 int (*GetData)(CeedQFunctionContext, CeedMemType, void *); 279 int (*GetDataRead)(CeedQFunctionContext, CeedMemType, void *); 280 int (*RestoreData)(CeedQFunctionContext); 281 int (*RestoreDataRead)(CeedQFunctionContext); 282 int (*DataDestroy)(CeedQFunctionContext); 283 int (*Destroy)(CeedQFunctionContext); 284 CeedQFunctionContextDataDestroyUser data_destroy_function; 285 CeedMemType data_destroy_mem_type; 286 CeedInt num_fields; 287 CeedInt max_fields; 288 CeedContextFieldLabel *field_labels; 289 uint64_t state; 290 uint64_t num_readers; 291 size_t ctx_size; 292 void *data; 293 }; 294 295 /// Struct to handle the context data to use the Fortran QFunction stub 296 /// @ingroup CeedQFunction 297 struct CeedFortranContext_private { 298 CeedQFunctionContext inner_ctx; 299 void (*f)(void *ctx, int *nq, const CeedScalar *u, const CeedScalar *u1, const CeedScalar *u2, const CeedScalar *u3, const CeedScalar *u4, 300 const CeedScalar *u5, const CeedScalar *u6, const CeedScalar *u7, const CeedScalar *u8, const CeedScalar *u9, const CeedScalar *u10, 301 const CeedScalar *u11, const CeedScalar *u12, const CeedScalar *u13, const CeedScalar *u14, const CeedScalar *u15, CeedScalar *v, 302 CeedScalar *v1, CeedScalar *v2, CeedScalar *v3, CeedScalar *v4, CeedScalar *v5, CeedScalar *v6, CeedScalar *v7, CeedScalar *v8, 303 CeedScalar *v9, CeedScalar *v10, CeedScalar *v11, CeedScalar *v12, CeedScalar *v13, CeedScalar *v14, CeedScalar *v15, int *err); 304 }; 305 typedef struct CeedFortranContext_private *CeedFortranContext; 306 307 struct CeedContextFieldLabel_private { 308 const char *name; 309 const char *description; 310 CeedContextFieldType type; 311 size_t size; 312 size_t num_values; 313 size_t offset; 314 CeedInt num_sub_labels; 315 CeedContextFieldLabel *sub_labels; 316 bool from_op; 317 }; 318 319 struct CeedOperatorField_private { 320 CeedElemRestriction elem_rstr; /* Restriction from L-vector */ 321 CeedBasis basis; /* Basis or CEED_BASIS_NONE for collocated fields */ 322 CeedVector vec; /* State vector for passive fields or CEED_VECTOR_NONE for no vector */ 323 const char *field_name; /* matching QFunction field name */ 324 }; 325 326 struct CeedQFunctionAssemblyData_private { 327 Ceed ceed; 328 int ref_count; 329 bool is_setup; 330 bool reuse_data; 331 bool needs_data_update; 332 CeedVector vec; 333 CeedElemRestriction rstr; 334 }; 335 336 struct CeedOperatorAssemblyData_private { 337 Ceed ceed; 338 CeedInt num_active_bases_in, num_active_bases_out; 339 CeedBasis *active_bases_in, *active_bases_out; 340 CeedElemRestriction *active_elem_rstrs_in, *active_elem_rstrs_out; 341 CeedInt *num_eval_modes_in, *num_eval_modes_out; 342 CeedEvalMode **eval_modes_in, **eval_modes_out; 343 CeedScalar **assembled_bases_in, **assembled_bases_out; 344 CeedSize **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components; 345 }; 346 347 struct CeedOperator_private { 348 Ceed ceed; 349 CeedOperator op_fallback, op_fallback_parent; 350 int ref_count; 351 int (*LinearAssembleQFunction)(CeedOperator, CeedVector *, CeedElemRestriction *, CeedRequest *); 352 int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *); 353 int (*LinearAssembleDiagonal)(CeedOperator, CeedVector, CeedRequest *); 354 int (*LinearAssembleAddDiagonal)(CeedOperator, CeedVector, CeedRequest *); 355 int (*LinearAssemblePointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *); 356 int (*LinearAssembleAddPointBlockDiagonal)(CeedOperator, CeedVector, CeedRequest *); 357 int (*LinearAssembleSymbolic)(CeedOperator, CeedSize *, CeedInt **, CeedInt **); 358 int (*LinearAssemble)(CeedOperator, CeedVector); 359 int (*LinearAssembleSingle)(CeedOperator, CeedInt, CeedVector); 360 int (*CreateFDMElementInverse)(CeedOperator, CeedOperator *, CeedRequest *); 361 int (*Apply)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 362 int (*ApplyComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 363 int (*ApplyAdd)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 364 int (*ApplyAddComposite)(CeedOperator, CeedVector, CeedVector, CeedRequest *); 365 int (*ApplyJacobian)(CeedOperator, CeedVector, CeedVector, CeedVector, 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 is_at_points; 382 bool has_restriction; 383 CeedQFunctionAssemblyData qf_assembled; 384 CeedOperatorAssemblyData op_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 CeedElemRestriction rstr_points, first_points_rstr; 392 CeedVector point_coords; 393 }; 394