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