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