1 // Copyright (c) 2017-2025, 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 #include <ceed-impl.h> 9 #include <ceed.h> 10 #include <ceed/backend.h> 11 #include <ceed/jit-tools.h> 12 #include <limits.h> 13 #include <stdbool.h> 14 #include <stdio.h> 15 #include <string.h> 16 17 /// @file 18 /// Implementation of public CeedQFunction interfaces 19 20 /// @cond DOXYGEN_SKIP 21 static struct CeedQFunction_private ceed_qfunction_none; 22 /// @endcond 23 24 /// @addtogroup CeedQFunctionUser 25 /// @{ 26 27 // Indicate that no `CeedQFunction` is provided by the user 28 const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none; 29 30 /// @} 31 32 /// @cond DOXYGEN_SKIP 33 static struct { 34 char name[CEED_MAX_RESOURCE_LEN]; 35 char source[CEED_MAX_RESOURCE_LEN]; 36 CeedInt vec_length; 37 CeedQFunctionUser f; 38 int (*init)(Ceed ceed, const char *name, CeedQFunction qf); 39 } gallery_qfunctions[1024]; 40 static size_t num_qfunctions; 41 /// @endcond 42 43 /// ---------------------------------------------------------------------------- 44 /// CeedQFunction Library Internal Functions 45 /// ---------------------------------------------------------------------------- 46 /// @addtogroup CeedQFunctionDeveloper 47 /// @{ 48 49 /** 50 @brief Register a gallery `CeedQFunction` 51 52 @param[in] name Name for this backend to respond to 53 @param[in] source Absolute path to source of `CeedQFunction`, "\path\CEED_DIR\gallery\folder\file.h:function_name" 54 @param[in] vec_length Vector length. 55 Caller must ensure that number of quadrature points is a multiple of `vec_length`. 56 @param[in] f Function pointer to evaluate action at quadrature points. 57 See `CeedQFunctionUser`. 58 @param[in] init Initialization function called by @ref CeedQFunctionCreateInteriorByName() when the `CeedQFunction` is selected. 59 60 @return An error code: 0 - success, otherwise - failure 61 62 @ref Developer 63 **/ 64 int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f, 65 int (*init)(Ceed, const char *, CeedQFunction)) { 66 const char *relative_file_path; 67 int ierr = 0; 68 69 CeedDebugEnv("Gallery Register: %s", name); 70 CeedCall(CeedGetJitRelativePath(source, &relative_file_path)); 71 CeedPragmaCritical(CeedQFunctionRegister) { 72 if (num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) { 73 strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN); 74 gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0; 75 strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN); 76 gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0; 77 gallery_qfunctions[num_qfunctions].vec_length = vec_length; 78 gallery_qfunctions[num_qfunctions].f = f; 79 gallery_qfunctions[num_qfunctions].init = init; 80 num_qfunctions++; 81 } else { 82 ierr = 1; 83 } 84 } 85 CeedCheck(ierr == 0, NULL, CEED_ERROR_MAJOR, "Too many gallery CeedQFunctions"); 86 return CEED_ERROR_SUCCESS; 87 } 88 89 /** 90 @brief Set a `CeedQFunction` field, used by @ref CeedQFunctionAddInput() and @ref CeedQFunctionAddOutput() 91 92 @param[out] f `CeedQFunctionField` 93 @param[in] field_name Name of `CeedQFunction` field 94 @param[in] size Size of `CeedQFunction` field, (`num_comp * 1`) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT, (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space, (`num_comp * dim`) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` and `curl_dim = dim` for @ref CEED_EVAL_CURL. 95 @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 96 @ref CEED_EVAL_WEIGHT to use quadrature weights, 97 @ref CEED_EVAL_INTERP to use interpolated values, 98 @ref CEED_EVAL_GRAD to use gradients, 99 @ref CEED_EVAL_DIV to use divergence, 100 @ref CEED_EVAL_CURL to use curl 101 102 @return An error code: 0 - success, otherwise - failure 103 104 @ref Developer 105 **/ 106 static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) { 107 CeedCall(CeedCalloc(1, f)); 108 CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name)); 109 (*f)->size = size; 110 (*f)->eval_mode = eval_mode; 111 return CEED_ERROR_SUCCESS; 112 } 113 114 /** 115 @brief View a field of a `CeedQFunction` 116 117 @param[in] field `CeedQFunction` field to view 118 @param[in] field_number Number of field being viewed 119 @param[in] in true for input field, false for output 120 @param[in] stream Stream to view to, e.g., `stdout` 121 122 @return An error code: 0 - success, otherwise - failure 123 124 @ref Utility 125 **/ 126 static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) { 127 const char *inout = in ? "Input" : "Output"; 128 const char *field_name; 129 CeedInt size; 130 CeedEvalMode eval_mode; 131 132 CeedCall(CeedQFunctionFieldGetData(field, &field_name, &size, &eval_mode)); 133 fprintf(stream, 134 " %s field %" CeedInt_FMT 135 ":\n" 136 " Name: \"%s\"\n" 137 " Size: %" CeedInt_FMT 138 "\n" 139 " EvalMode: \"%s\"\n", 140 inout, field_number, field_name, size, CeedEvalModes[eval_mode]); 141 return CEED_ERROR_SUCCESS; 142 } 143 144 /** 145 @brief Set flag to determine if Fortran interface is used 146 147 @param[in,out] qf CeedQFunction 148 @param[in] status Boolean value to set as Fortran status 149 150 @return An error code: 0 - success, otherwise - failure 151 152 @ref Backend 153 **/ 154 int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) { 155 qf->is_fortran = status; 156 return CEED_ERROR_SUCCESS; 157 } 158 159 /// @} 160 161 /// ---------------------------------------------------------------------------- 162 /// CeedQFunction Backend API 163 /// ---------------------------------------------------------------------------- 164 /// @addtogroup CeedQFunctionBackend 165 /// @{ 166 167 /** 168 @brief Get the vector length of a `CeedQFunction` 169 170 @param[in] qf `CeedQFunction` 171 @param[out] vec_length Variable to store vector length 172 173 @return An error code: 0 - success, otherwise - failure 174 175 @ref Backend 176 **/ 177 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) { 178 *vec_length = qf->vec_length; 179 return CEED_ERROR_SUCCESS; 180 } 181 182 /** 183 @brief Get the number of inputs and outputs to a `CeedQFunction` 184 185 @param[in] qf `CeedQFunction` 186 @param[out] num_input Variable to store number of input fields 187 @param[out] num_output Variable to store number of output fields 188 189 @return An error code: 0 - success, otherwise - failure 190 191 @ref Backend 192 **/ 193 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) { 194 if (num_input) *num_input = qf->num_input_fields; 195 if (num_output) *num_output = qf->num_output_fields; 196 return CEED_ERROR_SUCCESS; 197 } 198 199 /** 200 @brief Get the name of the `CeedQFunction`. 201 Use the `name` if created via @ref `CeedQFunctionCreateInteriorByName`, otherwise return the kernel name via @ref `CeedQFunctionGetKernelName`. 202 203 @param[in] qf `CeedQFunction` 204 @param[out] kernel_name Variable to store `CeedQFunction` name 205 206 @return An error code: 0 - success, otherwise - failure 207 208 @ref Backend 209 **/ 210 int CeedQFunctionGetName(CeedQFunction qf, const char **name) { 211 if (qf->is_gallery) { 212 *name = qf->gallery_name; 213 } else { 214 CeedCall(CeedQFunctionGetKernelName(qf, name)); 215 } 216 return CEED_ERROR_SUCCESS; 217 } 218 219 /** 220 @brief Get the name of the user function for a `CeedQFunction` 221 222 @param[in] qf `CeedQFunction` 223 @param[out] kernel_name Variable to store string holding kernel name 224 225 @return An error code: 0 - success, otherwise - failure 226 227 @ref Backend 228 **/ 229 int CeedQFunctionGetKernelName(CeedQFunction qf, const char **kernel_name) { 230 if (!qf->kernel_name) { 231 char *kernel_name_copy; 232 233 if (qf->user_source) { 234 const char *kernel_name = strrchr(qf->user_source, ':') + 1; 235 size_t kernel_name_len = strlen(kernel_name); 236 237 CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy)); 238 memcpy(kernel_name_copy, kernel_name, kernel_name_len); 239 } else { 240 CeedCall(CeedCalloc(1, &kernel_name_copy)); 241 } 242 qf->kernel_name = kernel_name_copy; 243 } 244 245 *kernel_name = qf->kernel_name; 246 return CEED_ERROR_SUCCESS; 247 } 248 249 /** 250 @brief Get the source path string for a `CeedQFunction` 251 252 @param[in] qf `CeedQFunction` 253 @param[out] source_path Variable to store source path string 254 255 @return An error code: 0 - success, otherwise - failure 256 257 @ref Backend 258 **/ 259 int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) { 260 if (!qf->source_path && qf->user_source) { 261 Ceed ceed; 262 bool is_absolute_path; 263 char *source_path_copy; 264 const char *absolute_path; 265 const char *kernel_name = strrchr(qf->user_source, ':') + 1; 266 size_t kernel_name_len = strlen(kernel_name); 267 268 CeedCall(CeedQFunctionGetCeed(qf, &ceed)); 269 CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path)); 270 if (is_absolute_path) { 271 absolute_path = (char *)qf->user_source; 272 } else { 273 CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path)); 274 } 275 CeedCall(CeedDestroy(&ceed)); 276 277 size_t source_len = strlen(absolute_path) - kernel_name_len - 1; 278 279 CeedCall(CeedCalloc(source_len + 1, &source_path_copy)); 280 memcpy(source_path_copy, absolute_path, source_len); 281 qf->source_path = source_path_copy; 282 283 if (!is_absolute_path) CeedCall(CeedFree(&absolute_path)); 284 } 285 286 *source_path = (char *)qf->source_path; 287 return CEED_ERROR_SUCCESS; 288 } 289 290 /** 291 @brief Initialize and load `CeedQFunction` source file into string buffer, including full text of local files in place of `#include "local.h"`. 292 293 The `buffer` is set to `NULL` if there is no `CeedQFunction` source file. 294 295 Note: This function may as well return a mutable buffer, but all current uses 296 do not modify it. (This is just a downside of `const` semantics with output 297 arguments instead of returns.) 298 299 Note: Caller is responsible for freeing the string buffer with @ref CeedFree(). 300 301 @param[in] qf `CeedQFunction` 302 @param[out] source_buffer String buffer for source file contents 303 304 @return An error code: 0 - success, otherwise - failure 305 306 @ref Backend 307 **/ 308 int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer) { 309 const char *source_path; 310 311 CeedCall(CeedQFunctionGetSourcePath(qf, &source_path)); 312 *source_buffer = NULL; 313 if (source_path) { 314 Ceed ceed; 315 char *buffer = NULL; 316 317 CeedCall(CeedQFunctionGetCeed(qf, &ceed)); 318 CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer)); 319 CeedCall(CeedDestroy(&ceed)); 320 *source_buffer = buffer; 321 } 322 return CEED_ERROR_SUCCESS; 323 } 324 325 /** 326 @brief Get the User Function for a `CeedQFunction` 327 328 @param[in] qf `CeedQFunction` 329 @param[out] f Variable to store user function 330 331 @return An error code: 0 - success, otherwise - failure 332 333 @ref Backend 334 **/ 335 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { 336 *f = qf->function; 337 return CEED_ERROR_SUCCESS; 338 } 339 340 /** 341 @brief Get global context for a `CeedQFunction`. 342 343 Note: For `CeedQFunction` from the Fortran interface, this function will return the Fortran context `CeedQFunctionContext`. 344 345 @param[in] qf CeedQFunction 346 @param[out] ctx Variable to store CeedQFunctionContext 347 348 @return An error code: 0 - success, otherwise - failure 349 350 @ref Backend 351 **/ 352 int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 353 *ctx = NULL; 354 if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx)); 355 return CEED_ERROR_SUCCESS; 356 } 357 358 /** 359 @brief Get context data of a `CeedQFunction` 360 361 @param[in] qf `CeedQFunction` 362 @param[in] mem_type Memory type on which to access the data. 363 If the backend uses a different memory type, this will perform a copy. 364 @param[out] data Data on memory type mem_type 365 366 @return An error code: 0 - success, otherwise - failure 367 368 @ref Backend 369 **/ 370 int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) { 371 bool is_writable; 372 CeedQFunctionContext ctx; 373 374 CeedCall(CeedQFunctionGetContext(qf, &ctx)); 375 if (ctx) { 376 CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable)); 377 if (is_writable) { 378 CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data)); 379 } else { 380 CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data)); 381 } 382 } else { 383 *(void **)data = NULL; 384 } 385 CeedCall(CeedQFunctionContextDestroy(&ctx)); 386 return CEED_ERROR_SUCCESS; 387 } 388 389 /** 390 @brief Restore context data of a `CeedQFunction` 391 392 @param[in] qf `CeedQFunction` 393 @param[in,out] data Data to restore 394 395 @return An error code: 0 - success, otherwise - failure 396 397 @ref Backend 398 **/ 399 int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) { 400 bool is_writable; 401 CeedQFunctionContext ctx; 402 403 CeedCall(CeedQFunctionGetContext(qf, &ctx)); 404 if (ctx) { 405 CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable)); 406 if (is_writable) { 407 CeedCall(CeedQFunctionContextRestoreData(ctx, data)); 408 } else { 409 CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data)); 410 } 411 } 412 CeedCall(CeedQFunctionContextDestroy(&ctx)); 413 return CEED_ERROR_SUCCESS; 414 } 415 416 /** 417 @brief Get true user context for a `CeedQFunction` 418 419 Note: For all `CeedQFunction` this function will return the user `CeedQFunctionContext` and not interface context `CeedQFunctionContext`, if any such object exists. 420 421 @param[in] qf `CeedQFunction` 422 @param[out] ctx Variable to store `CeedQFunctionContext` 423 424 @return An error code: 0 - success, otherwise - failure 425 @ref Backend 426 **/ 427 int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 428 CeedQFunctionContext qf_ctx; 429 430 CeedCall(CeedQFunctionGetContext(qf, &qf_ctx)); 431 if (qf->is_fortran) { 432 CeedFortranContext fortran_ctx = NULL; 433 434 CeedCall(CeedQFunctionContextGetData(qf_ctx, CEED_MEM_HOST, &fortran_ctx)); 435 *ctx = fortran_ctx->inner_ctx; 436 CeedCall(CeedQFunctionContextRestoreData(qf_ctx, &fortran_ctx)); 437 } else { 438 *ctx = qf_ctx; 439 } 440 CeedCall(CeedQFunctionContextDestroy(&qf_ctx)); 441 return CEED_ERROR_SUCCESS; 442 } 443 444 /** 445 @brief Get inner context data of a `CeedQFunction` 446 447 @param[in] qf `CeedQFunction` 448 @param[in] mem_type Memory type on which to access the data. 449 If the backend uses a different memory type, this will perform a copy. 450 @param[out] data Data on memory type mem_type 451 452 @return An error code: 0 - success, otherwise - failure 453 454 @ref Backend 455 **/ 456 int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) { 457 bool is_writable; 458 CeedQFunctionContext ctx; 459 460 CeedCall(CeedQFunctionGetInnerContext(qf, &ctx)); 461 if (ctx) { 462 CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable)); 463 if (is_writable) { 464 CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data)); 465 } else { 466 CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data)); 467 } 468 } else { 469 *(void **)data = NULL; 470 } 471 return CEED_ERROR_SUCCESS; 472 } 473 474 /** 475 @brief Restore inner context data of a `CeedQFunction` 476 477 @param[in] qf `CeedQFunction` 478 @param[in,out] data Data to restore 479 480 @return An error code: 0 - success, otherwise - failure 481 482 @ref Backend 483 **/ 484 int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) { 485 bool is_writable; 486 CeedQFunctionContext ctx; 487 488 CeedCall(CeedQFunctionGetInnerContext(qf, &ctx)); 489 if (ctx) { 490 CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable)); 491 if (is_writable) { 492 CeedCall(CeedQFunctionContextRestoreData(ctx, data)); 493 } else { 494 CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data)); 495 } 496 } 497 *(void **)data = NULL; 498 return CEED_ERROR_SUCCESS; 499 } 500 501 /** 502 @brief Determine if `CeedQFunction` is identity 503 504 @param[in] qf `CeedQFunction` 505 @param[out] is_identity Variable to store identity status 506 507 @return An error code: 0 - success, otherwise - failure 508 509 @ref Backend 510 **/ 511 int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) { 512 *is_identity = qf->is_identity; 513 return CEED_ERROR_SUCCESS; 514 } 515 516 /** 517 @brief Determine if `CeedQFunctionContext` is writable 518 519 @param[in] qf `CeedQFunction` 520 @param[out] is_writable Variable to store context writeable status 521 522 @return An error code: 0 - success, otherwise - failure 523 524 @ref Backend 525 **/ 526 int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) { 527 *is_writable = qf->is_context_writable; 528 return CEED_ERROR_SUCCESS; 529 } 530 531 /** 532 @brief Get backend data of a `CeedQFunction` 533 534 @param[in] qf `CeedQFunction` 535 @param[out] data Variable to store data 536 537 @return An error code: 0 - success, otherwise - failure 538 539 @ref Backend 540 **/ 541 int CeedQFunctionGetData(CeedQFunction qf, void *data) { 542 *(void **)data = qf->data; 543 return CEED_ERROR_SUCCESS; 544 } 545 546 /** 547 @brief Set backend data of a `CeedQFunction` 548 549 @param[in,out] qf `CeedQFunction` 550 @param[in] data Data to set 551 552 @return An error code: 0 - success, otherwise - failure 553 554 @ref Backend 555 **/ 556 int CeedQFunctionSetData(CeedQFunction qf, void *data) { 557 qf->data = data; 558 return CEED_ERROR_SUCCESS; 559 } 560 561 /** 562 @brief Get a boolean value indicating if the `CeedQFunction` is immutable 563 564 @param[in] qf `CeedOperator` 565 @param[out] is_immutable Variable to store immutability status 566 567 @return An error code: 0 - success, otherwise - failure 568 569 @ref Backend 570 **/ 571 int CeedQFunctionIsImmutable(CeedQFunction qf, bool *is_immutable) { 572 *is_immutable = qf->is_immutable; 573 return CEED_ERROR_SUCCESS; 574 } 575 576 /** 577 @brief Set the immutable flag of a `CeedQFunction` to `true` 578 579 @param[in,out] qf `CeedQFunction` 580 581 @return An error code: 0 - success, otherwise - failure 582 583 @ref Backend 584 **/ 585 int CeedQFunctionSetImmutable(CeedQFunction qf) { 586 qf->is_immutable = true; 587 return CEED_ERROR_SUCCESS; 588 } 589 590 /** 591 @brief Increment the reference counter for a `CeedQFunction` 592 593 @param[in,out] qf `CeedQFunction` to increment the reference counter 594 595 @return An error code: 0 - success, otherwise - failure 596 597 @ref Backend 598 **/ 599 int CeedQFunctionReference(CeedQFunction qf) { 600 qf->ref_count++; 601 return CEED_ERROR_SUCCESS; 602 } 603 604 /** 605 @brief Estimate number of FLOPs per quadrature required to apply `CeedQFunction` 606 607 @param[in] qf `CeedQFunction` to estimate FLOPs for 608 @param[out] flops Address of variable to hold FLOPs estimate 609 610 @ref Backend 611 **/ 612 int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) { 613 *flops = qf->user_flop_estimate; 614 return CEED_ERROR_SUCCESS; 615 } 616 617 /// @} 618 619 /// ---------------------------------------------------------------------------- 620 /// CeedQFunction Public API 621 /// ---------------------------------------------------------------------------- 622 /// @addtogroup CeedQFunctionUser 623 /// @{ 624 625 /** 626 @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms 627 628 @param[in] ceed `Ceed` object used to create the `CeedQFunction` 629 @param[in] vec_length Vector length. 630 Caller must ensure that number of quadrature points is a multiple of `vec_length`. 631 @param[in] f Function pointer to evaluate action at quadrature points. 632 See `CeedQFunctionUser`. 633 @param[in] source Absolute path to source of `CeedQFunctionUser`, "\abs_path\file.h:function_name". 634 The entire source file must only contain constructs supported by all targeted backends (i.e. CUDA for `/gpu/cuda`, OpenCL/SYCL for `/gpu/sycl`, etc.). 635 The entire contents of this file and all locally included files are used during JiT compilation for GPU backends. 636 The header `ceed/types.h` is preferred over `ceed.h` or `ceed/ceed.h` for `CeedQFunction` source files. 637 The macro `CEED_RUNNING_JIT_PASS` is set during JiT and can be used to guard include statements that JiT compilers cannot use, such as `math.h` or `std*.h`. 638 All source files must be at the provided filepath at runtime for JiT to function. 639 @param[out] qf Address of the variable where the newly created `CeedQFunction` will be stored 640 641 @return An error code: 0 - success, otherwise - failure 642 643 See \ref CeedQFunctionUser for details on the call-back function `f` arguments. 644 645 @ref User 646 **/ 647 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) { 648 char *user_source_copy; 649 650 if (!ceed->QFunctionCreate) { 651 Ceed delegate; 652 653 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction")); 654 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionCreateInterior"); 655 CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf)); 656 CeedCall(CeedDestroy(&delegate)); 657 return CEED_ERROR_SUCCESS; 658 } 659 660 CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE, 661 "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source); 662 663 CeedCall(CeedCalloc(1, qf)); 664 CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed)); 665 (*qf)->ref_count = 1; 666 (*qf)->vec_length = vec_length; 667 (*qf)->is_identity = false; 668 (*qf)->is_context_writable = true; 669 (*qf)->function = f; 670 (*qf)->user_flop_estimate = -1; 671 if (strlen(source)) { 672 size_t user_source_len = strlen(source); 673 674 CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy)); 675 memcpy(user_source_copy, source, user_source_len); 676 (*qf)->user_source = user_source_copy; 677 } 678 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields)); 679 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields)); 680 CeedCall(ceed->QFunctionCreate(*qf)); 681 return CEED_ERROR_SUCCESS; 682 } 683 684 /** 685 @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms by name 686 687 @param[in] ceed `Ceed` object used to create the `CeedQFunction` 688 @param[in] name Name of `CeedQFunction` to use from gallery 689 @param[out] qf Address of the variable where the newly created `CeedQFunction` will be stored 690 691 @return An error code: 0 - success, otherwise - failure 692 693 @ref User 694 **/ 695 int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) { 696 size_t match_len = 0, match_index = UINT_MAX; 697 698 CeedCall(CeedQFunctionRegisterAll()); 699 // Find matching backend 700 CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No CeedQFunction name provided"); 701 for (size_t i = 0; i < num_qfunctions; i++) { 702 size_t n; 703 const char *curr_name = gallery_qfunctions[i].name; 704 for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) { 705 } 706 if (n > match_len) { 707 match_len = n; 708 match_index = i; 709 } 710 } 711 CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery CeedQFunction"); 712 713 // Create QFunction 714 CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f, 715 gallery_qfunctions[match_index].source, qf)); 716 717 // QFunction specific setup 718 CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf)); 719 720 // Copy name 721 CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name)); 722 (*qf)->is_gallery = true; 723 return CEED_ERROR_SUCCESS; 724 } 725 726 /** 727 @brief Create an identity `CeedQFunction`. 728 729 Inputs are written into outputs in the order given. 730 This is useful for `CeedOperator that can be represented with only the action of a `CeedElemRestriction` and `CeedBasis`, such as restriction and prolongation operators for p-multigrid. 731 Backends may optimize `CeedOperator` with this `CeedQFunction` to avoid the copy of input data to output fields by using the same memory location for both. 732 733 @param[in] ceed `Ceed` object used to create the `CeedQFunction` 734 @param[in] size Size of the `CeedQFunction` fields 735 @param[in] in_mode @ref CeedEvalMode for input to `CeedQFunction` 736 @param[in] out_mode @ref CeedEvalMode for output to `CeedQFunction` 737 @param[out] qf Address of the variable where the newly created `CeedQFunction` will be stored 738 739 @return An error code: 0 - success, otherwise - failure 740 741 @ref User 742 **/ 743 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) { 744 CeedQFunctionContext ctx; 745 CeedContextFieldLabel size_label; 746 747 CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf)); 748 CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode)); 749 CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode)); 750 751 (*qf)->is_identity = true; 752 753 CeedCall(CeedQFunctionGetContext(*qf, &ctx)); 754 CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label)); 755 CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size)); 756 CeedCall(CeedQFunctionContextDestroy(&ctx)); 757 return CEED_ERROR_SUCCESS; 758 } 759 760 /** 761 @brief Copy the pointer to a `CeedQFunction`. 762 763 Both pointers should be destroyed with @ref CeedQFunctionDestroy(). 764 765 Note: If the value of `*qf_copy` passed to this function is non-NULL, then it is assumed that `*qf_copy` is a pointer to a `CeedQFunction`. 766 This `CeedQFunction` will be destroyed if `*qf_copy` is the only reference to this `CeedQFunction`. 767 768 @param[in] qf `CeedQFunction` to copy reference to 769 @param[out] qf_copy Variable to store copied reference 770 771 @return An error code: 0 - success, otherwise - failure 772 773 @ref User 774 **/ 775 int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) { 776 CeedCall(CeedQFunctionReference(qf)); 777 CeedCall(CeedQFunctionDestroy(qf_copy)); 778 *qf_copy = qf; 779 return CEED_ERROR_SUCCESS; 780 } 781 782 /** 783 @brief Add a `CeedQFunction` input 784 785 @param[in,out] qf `CeedQFunction` 786 @param[in] field_name Name of `CeedQFunction` field 787 @param[in] size Size of `CeedQFunction` field, 788 (`num_comp * 1`) for @ref CEED_EVAL_NONE, 789 (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space, 790 (`num_comp * dim`) for @ref CEED_EVAL_GRAD, 791 (`num_comp * 1`) for @ref CEED_EVAL_DIV, and 792 (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL. 793 @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 794 @ref CEED_EVAL_INTERP to use interpolated values, 795 @ref CEED_EVAL_GRAD to use gradients, 796 @ref CEED_EVAL_DIV to use divergence, 797 @ref CEED_EVAL_CURL to use curl 798 799 Note: In the user `CeedQFunctionUser`, the `in` argument list the fields in the order given by the calls to `CeedQFunctionAddInput`. 800 801 @return An error code: 0 - success, otherwise - failure 802 803 @ref User 804 **/ 805 int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) { 806 bool is_immutable; 807 808 CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable)); 809 CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable"); 810 CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1"); 811 for (CeedInt i = 0; i < qf->num_input_fields; i++) { 812 CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, 813 "CeedQFunction field names must be unique. Duplicate name: %s", field_name); 814 } 815 for (CeedInt i = 0; i < qf->num_output_fields; i++) { 816 CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, 817 "CeedQFunction field names must be unique. Duplicate name: %s", field_name); 818 } 819 CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode)); 820 qf->num_input_fields++; 821 return CEED_ERROR_SUCCESS; 822 } 823 824 /** 825 @brief Add a `CeedQFunction` output 826 827 @param[in,out] qf `CeedQFunction` 828 @param[in] field_name Name of `CeedQFunction` field 829 @param[in] size Size of `CeedQFunction` field, 830 (`num_comp * 1`) for @ref CEED_EVAL_NONE, 831 (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space, 832 (`num_comp * dim`) for @ref CEED_EVAL_GRAD, 833 (`num_comp * 1`) for @ref CEED_EVAL_DIV, and 834 (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL. 835 @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 836 @ref CEED_EVAL_INTERP to use interpolated values, 837 @ref CEED_EVAL_GRAD to use gradients, 838 @ref CEED_EVAL_DIV to use divergence, 839 @ref CEED_EVAL_CURL to use curl. 840 841 Note: In the user `CeedQFunctionUser`, the `out` argument list the fields in the order given by the calls to `CeedQFunctionAddOutput`. 842 843 @return An error code: 0 - success, otherwise - failure 844 845 @ref User 846 **/ 847 int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) { 848 bool is_immutable; 849 850 CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable)); 851 CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable"); 852 CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, 853 "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT"); 854 for (CeedInt i = 0; i < qf->num_input_fields; i++) { 855 CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, 856 "CeedQFunction field names must be unique"); 857 } 858 for (CeedInt i = 0; i < qf->num_output_fields; i++) { 859 CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, 860 "CeedQFunction field names must be unique"); 861 } 862 CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode)); 863 qf->num_output_fields++; 864 return CEED_ERROR_SUCCESS; 865 } 866 867 /** 868 @brief Get the `CeedQFunctionField` of a `CeedQFunction` 869 870 Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable. 871 872 @param[in] qf `CeedQFunction` 873 @param[out] num_input_fields Variable to store number of input fields 874 @param[out] input_fields Variable to store input fields 875 @param[out] num_output_fields Variable to store number of output fields 876 @param[out] output_fields Variable to store output fields 877 878 @return An error code: 0 - success, otherwise - failure 879 880 @ref Advanced 881 **/ 882 int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields, 883 CeedQFunctionField **output_fields) { 884 CeedCall(CeedQFunctionSetImmutable(qf)); 885 if (num_input_fields) *num_input_fields = qf->num_input_fields; 886 if (input_fields) *input_fields = qf->input_fields; 887 if (num_output_fields) *num_output_fields = qf->num_output_fields; 888 if (output_fields) *output_fields = qf->output_fields; 889 return CEED_ERROR_SUCCESS; 890 } 891 892 /** 893 @brief Get the name of a `CeedQFunctionField` 894 895 @param[in] qf_field `CeedQFunctionField` 896 @param[out] field_name Variable to store the field name 897 898 @return An error code: 0 - success, otherwise - failure 899 900 @ref Advanced 901 **/ 902 int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, const char **field_name) { 903 *field_name = qf_field->field_name; 904 return CEED_ERROR_SUCCESS; 905 } 906 907 /** 908 @brief Get the number of components of a `CeedQFunctionField` 909 910 @param[in] qf_field `CeedQFunctionField` 911 @param[out] size Variable to store the size of the field 912 913 @return An error code: 0 - success, otherwise - failure 914 915 @ref Advanced 916 **/ 917 int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) { 918 *size = qf_field->size; 919 return CEED_ERROR_SUCCESS; 920 } 921 922 /** 923 @brief Get the @ref CeedEvalMode of a `CeedQFunctionField` 924 925 @param[in] qf_field `CeedQFunctionField` 926 @param[out] eval_mode Variable to store the field evaluation mode 927 928 @return An error code: 0 - success, otherwise - failure 929 930 @ref Advanced 931 **/ 932 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) { 933 *eval_mode = qf_field->eval_mode; 934 return CEED_ERROR_SUCCESS; 935 } 936 937 /** 938 @brief Get the data of a `CeedQFunctionField`. 939 940 Any arguments set as `NULL` are ignored. 941 942 @param[in] qf_field `CeedQFunctionField` 943 @param[out] field_name Variable to store the field name 944 @param[out] size Variable to store the size of the field 945 @param[out] eval_mode Variable to store the field evaluation mode 946 947 @return An error code: 0 - success, otherwise - failure 948 949 @ref Advanced 950 **/ 951 int CeedQFunctionFieldGetData(CeedQFunctionField qf_field, const char **field_name, CeedInt *size, CeedEvalMode *eval_mode) { 952 if (field_name) CeedCall(CeedQFunctionFieldGetName(qf_field, field_name)); 953 if (size) CeedCall(CeedQFunctionFieldGetSize(qf_field, size)); 954 if (eval_mode) CeedCall(CeedQFunctionFieldGetEvalMode(qf_field, eval_mode)); 955 return CEED_ERROR_SUCCESS; 956 } 957 958 /** 959 @brief Set global context for a `CeedQFunction` 960 961 @param[in,out] qf `CeedQFunction` 962 @param[in] ctx Context data to set 963 964 @return An error code: 0 - success, otherwise - failure 965 966 @ref User 967 **/ 968 int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) { 969 CeedCall(CeedQFunctionContextDestroy(&qf->ctx)); 970 qf->ctx = ctx; 971 if (ctx) CeedCall(CeedQFunctionContextReference(ctx)); 972 return CEED_ERROR_SUCCESS; 973 } 974 975 /** 976 @brief Set writability of `CeedQFunctionContext` when calling the `CeedQFunctionUser`. 977 978 The default value is `is_writable == true`. 979 980 Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the `CeedQFunctionContext` and requires memory synchronization after calling @ref CeedQFunctionApply(). 981 982 Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the `CeedQFunctionContext`. 983 Violating this assertion may lead to inconsistent data. 984 985 Setting `is_writable == false` may offer a performance improvement on GPU backends. 986 987 @param[in,out] qf `CeedQFunction` 988 @param[in] is_writable Boolean flag for writability status 989 990 @return An error code: 0 - success, otherwise - failure 991 992 @ref User 993 **/ 994 int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) { 995 qf->is_context_writable = is_writable; 996 return CEED_ERROR_SUCCESS; 997 } 998 999 /** 1000 @brief Set estimated number of FLOPs per quadrature required to apply `CeedQFunction` 1001 1002 @param[in] qf `CeedQFunction` to estimate FLOPs for 1003 @param[out] flops FLOPs per quadrature point estimate 1004 1005 @ref Backend 1006 **/ 1007 int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) { 1008 CeedCheck(flops >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate"); 1009 qf->user_flop_estimate = flops; 1010 return CEED_ERROR_SUCCESS; 1011 } 1012 1013 /** 1014 @brief View a `CeedQFunction` 1015 1016 @param[in] qf `CeedQFunction` to view 1017 @param[in] stream Stream to write; typically `stdout` or a file 1018 1019 @return Error code: 0 - success, otherwise - failure 1020 1021 @ref User 1022 **/ 1023 int CeedQFunctionView(CeedQFunction qf, FILE *stream) { 1024 const char *name; 1025 1026 CeedCall(CeedQFunctionGetName(qf, &name)); 1027 fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", name); 1028 1029 fprintf(stream, " %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : ""); 1030 for (CeedInt i = 0; i < qf->num_input_fields; i++) { 1031 CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream)); 1032 } 1033 1034 fprintf(stream, " %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : ""); 1035 for (CeedInt i = 0; i < qf->num_output_fields; i++) { 1036 CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream)); 1037 } 1038 return CEED_ERROR_SUCCESS; 1039 } 1040 1041 /** 1042 @brief Get the `Ceed` associated with a `CeedQFunction` 1043 1044 @param[in] qf `CeedQFunction` 1045 @param[out] ceed Variable to store`Ceed` 1046 1047 @return An error code: 0 - success, otherwise - failure 1048 1049 @ref Advanced 1050 **/ 1051 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 1052 *ceed = NULL; 1053 CeedCall(CeedReferenceCopy(CeedQFunctionReturnCeed(qf), ceed)); 1054 return CEED_ERROR_SUCCESS; 1055 } 1056 1057 /** 1058 @brief Return the `Ceed` associated with a `CeedQFunction` 1059 1060 @param[in] qf `CeedQFunction` 1061 1062 @return `Ceed` associated with the `qf` 1063 1064 @ref Advanced 1065 **/ 1066 Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return qf->ceed; } 1067 1068 /** 1069 @brief Apply the action of a `CeedQFunction` 1070 1071 Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable. 1072 1073 @param[in] qf `CeedQFunction` 1074 @param[in] Q Number of quadrature points 1075 @param[in] u Array of input `CeedVector` 1076 @param[out] v Array of output `CeedVector` 1077 1078 @return An error code: 0 - success, otherwise - failure 1079 1080 @ref User 1081 **/ 1082 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) { 1083 CeedInt vec_length; 1084 1085 CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply"); 1086 CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length)); 1087 CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, 1088 "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length); 1089 CeedCall(CeedQFunctionSetImmutable(qf)); 1090 CeedCall(qf->Apply(qf, Q, u, v)); 1091 return CEED_ERROR_SUCCESS; 1092 } 1093 1094 /** 1095 @brief Destroy a `CeedQFunction` 1096 1097 @param[in,out] qf `CeedQFunction` to destroy 1098 1099 @return An error code: 0 - success, otherwise - failure 1100 1101 @ref User 1102 **/ 1103 int CeedQFunctionDestroy(CeedQFunction *qf) { 1104 if (!*qf || --(*qf)->ref_count > 0) { 1105 *qf = NULL; 1106 return CEED_ERROR_SUCCESS; 1107 } 1108 // Backend destroy 1109 if ((*qf)->Destroy) { 1110 CeedCall((*qf)->Destroy(*qf)); 1111 } 1112 // Free fields 1113 for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) { 1114 CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name)); 1115 CeedCall(CeedFree(&(*qf)->input_fields[i])); 1116 } 1117 for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) { 1118 CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name)); 1119 CeedCall(CeedFree(&(*qf)->output_fields[i])); 1120 } 1121 CeedCall(CeedFree(&(*qf)->input_fields)); 1122 CeedCall(CeedFree(&(*qf)->output_fields)); 1123 1124 // User context data object 1125 CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx)); 1126 1127 CeedCall(CeedFree(&(*qf)->user_source)); 1128 CeedCall(CeedFree(&(*qf)->source_path)); 1129 CeedCall(CeedFree(&(*qf)->gallery_name)); 1130 CeedCall(CeedFree(&(*qf)->kernel_name)); 1131 CeedCall(CeedDestroy(&(*qf)->ceed)); 1132 CeedCall(CeedFree(qf)); 1133 return CEED_ERROR_SUCCESS; 1134 } 1135 1136 /// @} 1137