1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3 // reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 #include <ceed/ceed.h> 18 #include <ceed/backend.h> 19 #include <ceed/jit-tools.h> 20 #include <ceed-impl.h> 21 #include <limits.h> 22 #include <stdbool.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 /// @file 28 /// Implementation of public CeedQFunction interfaces 29 30 /// @cond DOXYGEN_SKIP 31 static struct CeedQFunction_private ceed_qfunction_none; 32 /// @endcond 33 34 /// @addtogroup CeedQFunctionUser 35 /// @{ 36 37 // Indicate that no QFunction is provided by the user 38 const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none; 39 40 /// @} 41 42 /// @cond DOXYGEN_SKIP 43 static struct { 44 char name[CEED_MAX_RESOURCE_LEN]; 45 char source[CEED_MAX_RESOURCE_LEN]; 46 CeedInt vec_length; 47 CeedQFunctionUser f; 48 int (*init)(Ceed ceed, const char *name, CeedQFunction qf); 49 } gallery_qfunctions[1024]; 50 static size_t num_qfunctions; 51 /// @endcond 52 53 /// ---------------------------------------------------------------------------- 54 /// CeedQFunction Library Internal Functions 55 /// ---------------------------------------------------------------------------- 56 /// @addtogroup CeedQFunctionDeveloper 57 /// @{ 58 59 /** 60 @brief Register a gallery QFunction 61 62 @param name Name for this backend to respond to 63 @param source Absolute path to source of QFunction, 64 "\path\CEED_DIR\gallery\folder\file.h:function_name" 65 @param vec_length Vector length. Caller must ensure that number of quadrature 66 points is a multiple of vec_length. 67 @param f Function pointer to evaluate action at quadrature points. 68 See \ref CeedQFunctionUser. 69 @param init Initialization function called by CeedQFunctionInit() when the 70 QFunction is selected. 71 72 @return An error code: 0 - success, otherwise - failure 73 74 @ref Developer 75 **/ 76 int CeedQFunctionRegister(const char *name, const char *source, 77 CeedInt vec_length, CeedQFunctionUser f, 78 int (*init)(Ceed, const char *, CeedQFunction)) { 79 if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof( 80 gallery_qfunctions[0])) 81 // LCOV_EXCL_START 82 return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions"); 83 // LCOV_EXCL_STOP 84 85 CeedDebugEnv("Gallery Register: %s", name); 86 87 strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN); 88 gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0; 89 strncpy(gallery_qfunctions[num_qfunctions].source, source, 90 CEED_MAX_RESOURCE_LEN); 91 gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0; 92 gallery_qfunctions[num_qfunctions].vec_length = vec_length; 93 gallery_qfunctions[num_qfunctions].f = f; 94 gallery_qfunctions[num_qfunctions].init = init; 95 num_qfunctions++; 96 return CEED_ERROR_SUCCESS; 97 } 98 99 /** 100 @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output 101 102 @param f CeedQFunctionField 103 @param field_name Name of QFunction field 104 @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 105 (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT 106 @param eval_mode \ref CEED_EVAL_NONE to use values directly, 107 \ref CEED_EVAL_INTERP to use interpolated values, 108 \ref CEED_EVAL_GRAD to use gradients, 109 \ref CEED_EVAL_WEIGHT to use quadrature weights. 110 111 @return An error code: 0 - success, otherwise - failure 112 113 @ref Developer 114 **/ 115 static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, 116 CeedInt size, CeedEvalMode eval_mode) { 117 int ierr; 118 119 ierr = CeedCalloc(1, f); CeedChk(ierr); 120 ierr = CeedStringAllocCopy(field_name, (char **)&(*f)->field_name); 121 CeedChk(ierr); 122 (*f)->size = size; 123 (*f)->eval_mode = eval_mode; 124 return CEED_ERROR_SUCCESS; 125 } 126 127 /** 128 @brief View a field of a CeedQFunction 129 130 @param[in] field QFunction field to view 131 @param[in] field_number Number of field being viewed 132 @param[in] in true for input field, false for output 133 @param[in] stream Stream to view to, e.g., stdout 134 135 @return An error code: 0 - success, otherwise - failure 136 137 @ref Utility 138 **/ 139 static int CeedQFunctionFieldView(CeedQFunctionField field, 140 CeedInt field_number, 141 bool in, FILE *stream) { 142 int ierr; 143 const char *inout = in ? "Input" : "Output"; 144 char *field_name; 145 ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr); 146 CeedInt size; 147 ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr); 148 CeedEvalMode eval_mode; 149 ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr); 150 fprintf(stream, " %s Field [%d]:\n" 151 " Name: \"%s\"\n" 152 " Size: %d\n" 153 " EvalMode: \"%s\"\n", 154 inout, field_number, field_name, size, CeedEvalModes[eval_mode]); 155 return CEED_ERROR_SUCCESS; 156 } 157 158 /** 159 @brief Set flag to determine if Fortran interface is used 160 161 @param qf CeedQFunction 162 @param status Boolean value to set as Fortran status 163 164 @return An error code: 0 - success, otherwise - failure 165 166 @ref Backend 167 **/ 168 int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) { 169 qf->is_fortran = status; 170 return CEED_ERROR_SUCCESS; 171 } 172 173 /// @} 174 175 /// ---------------------------------------------------------------------------- 176 /// CeedQFunction Backend API 177 /// ---------------------------------------------------------------------------- 178 /// @addtogroup CeedQFunctionBackend 179 /// @{ 180 181 /** 182 @brief Get the vector length of a CeedQFunction 183 184 @param qf CeedQFunction 185 @param[out] vec_length Variable to store vector length 186 187 @return An error code: 0 - success, otherwise - failure 188 189 @ref Backend 190 **/ 191 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) { 192 *vec_length = qf->vec_length; 193 return CEED_ERROR_SUCCESS; 194 } 195 196 /** 197 @brief Get the number of inputs and outputs to a CeedQFunction 198 199 @param qf CeedQFunction 200 @param[out] num_input Variable to store number of input fields 201 @param[out] num_output Variable to store number of output fields 202 203 @return An error code: 0 - success, otherwise - failure 204 205 @ref Backend 206 **/ 207 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, 208 CeedInt *num_output) { 209 if (num_input) *num_input = qf->num_input_fields; 210 if (num_output) *num_output = qf->num_output_fields; 211 return CEED_ERROR_SUCCESS; 212 } 213 214 /** 215 @brief Get the name of the user function for a CeedQFunction 216 217 @param qf CeedQFunction 218 @param[out] kernel_name Variable to store source path string 219 220 @return An error code: 0 - success, otherwise - failure 221 222 @ref Backend 223 **/ 224 int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) { 225 *kernel_name = (char *) qf->kernel_name; 226 return CEED_ERROR_SUCCESS; 227 } 228 229 /** 230 @brief Get the source path string for a CeedQFunction 231 232 @param qf CeedQFunction 233 @param[out] source_path Variable to store source path string 234 235 @return An error code: 0 - success, otherwise - failure 236 237 @ref Backend 238 **/ 239 int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) { 240 *source_path = (char *) qf->source_path; 241 return CEED_ERROR_SUCCESS; 242 } 243 244 /** 245 @brief Initalize and load QFunction source file into string buffer, including 246 full text of local files in place of `#include "local.h"`. 247 The `buffer` is set to `NULL` if there is no QFunction source file. 248 Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 249 250 @param qf CeedQFunction 251 @param[out] source_buffer String buffer for source file contents 252 253 @return An error code: 0 - success, otherwise - failure 254 255 @ref Backend 256 **/ 257 int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) { 258 int ierr; 259 char *source_path; 260 261 ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr); 262 *source_buffer = NULL; 263 if (source_path) { 264 ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer); 265 CeedChk(ierr); 266 } 267 268 return CEED_ERROR_SUCCESS; 269 } 270 271 /** 272 @brief Get the User Function for a CeedQFunction 273 274 @param qf CeedQFunction 275 @param[out] f Variable to store user function 276 277 @return An error code: 0 - success, otherwise - failure 278 279 @ref Backend 280 **/ 281 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { 282 *f = qf->function; 283 return CEED_ERROR_SUCCESS; 284 } 285 286 /** 287 @brief Get global context for a CeedQFunction. 288 Note: For QFunctions from the Fortran interface, this 289 function will return the Fortran context 290 CeedQFunctionContext. 291 292 @param qf CeedQFunction 293 @param[out] ctx Variable to store CeedQFunctionContext 294 295 @return An error code: 0 - success, otherwise - failure 296 297 @ref Backend 298 **/ 299 int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 300 *ctx = qf->ctx; 301 return CEED_ERROR_SUCCESS; 302 } 303 304 /** 305 @brief Get true user context for a CeedQFunction 306 Note: For all QFunctions this function will return the user 307 CeedQFunctionContext and not interface context 308 CeedQFunctionContext, if any such object exists. 309 310 @param qf CeedQFunction 311 @param[out] ctx Variable to store CeedQFunctionContext 312 313 @return An error code: 0 - success, otherwise - failure 314 @ref Backend 315 **/ 316 int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 317 int ierr; 318 if (qf->is_fortran) { 319 CeedFortranContext fortran_ctx = NULL; 320 ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx); 321 CeedChk(ierr); 322 *ctx = fortran_ctx->inner_ctx; 323 ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx); 324 CeedChk(ierr); 325 } else { 326 *ctx = qf->ctx; 327 } 328 return CEED_ERROR_SUCCESS; 329 } 330 331 /** 332 @brief Determine if QFunction is identity 333 334 @param qf CeedQFunction 335 @param[out] is_identity Variable to store identity status 336 337 @return An error code: 0 - success, otherwise - failure 338 339 @ref Backend 340 **/ 341 int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) { 342 *is_identity = qf->is_identity; 343 return CEED_ERROR_SUCCESS; 344 } 345 346 /** 347 @brief Get backend data of a CeedQFunction 348 349 @param qf CeedQFunction 350 @param[out] data Variable to store data 351 352 @return An error code: 0 - success, otherwise - failure 353 354 @ref Backend 355 **/ 356 int CeedQFunctionGetData(CeedQFunction qf, void *data) { 357 *(void **)data = qf->data; 358 return CEED_ERROR_SUCCESS; 359 } 360 361 /** 362 @brief Set backend data of a CeedQFunction 363 364 @param[out] qf CeedQFunction 365 @param data Data to set 366 367 @return An error code: 0 - success, otherwise - failure 368 369 @ref Backend 370 **/ 371 int CeedQFunctionSetData(CeedQFunction qf, void *data) { 372 qf->data = data; 373 return CEED_ERROR_SUCCESS; 374 } 375 376 /** 377 @brief Increment the reference counter for a CeedQFunction 378 379 @param qf CeedQFunction to increment the reference counter 380 381 @return An error code: 0 - success, otherwise - failure 382 383 @ref Backend 384 **/ 385 int CeedQFunctionReference(CeedQFunction qf) { 386 qf->ref_count++; 387 return CEED_ERROR_SUCCESS; 388 } 389 390 /// @} 391 392 /// ---------------------------------------------------------------------------- 393 /// CeedQFunction Public API 394 /// ---------------------------------------------------------------------------- 395 /// @addtogroup CeedQFunctionUser 396 /// @{ 397 398 /** 399 @brief Create a CeedQFunction for evaluating interior (volumetric) terms. 400 401 @param ceed A Ceed object where the CeedQFunction will be created 402 @param vec_length Vector length. Caller must ensure that number of quadrature 403 points is a multiple of vec_length. 404 @param f Function pointer to evaluate action at quadrature points. 405 See \ref CeedQFunctionUser. 406 @param source Absolute path to source of QFunction, 407 "\abs_path\file.h:function_name". 408 For support across all backends, this source must only 409 contain constructs supported by C99, C++11, and CUDA. 410 @param[out] qf Address of the variable where the newly created 411 CeedQFunction will be stored 412 413 @return An error code: 0 - success, otherwise - failure 414 415 See \ref CeedQFunctionUser for details on the call-back function @a f's 416 arguments. 417 418 @ref User 419 **/ 420 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, 421 CeedQFunctionUser f, 422 const char *source, CeedQFunction *qf) { 423 int ierr; 424 char *source_copy, *kernel_name_copy; 425 426 if (!ceed->QFunctionCreate) { 427 Ceed delegate; 428 ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr); 429 430 if (!delegate) 431 // LCOV_EXCL_START 432 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 433 "Backend does not support QFunctionCreate"); 434 // LCOV_EXCL_STOP 435 436 ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf); 437 CeedChk(ierr); 438 return CEED_ERROR_SUCCESS; 439 } 440 441 if (strlen(source) && !strrchr(source, ':'))\ 442 // LCOV_EXCL_START 443 return CeedError(ceed, CEED_ERROR_INCOMPLETE, 444 "Provided path to source does not include function name. " 445 "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", 446 source); 447 // LCOV_EXCL_STOP 448 449 ierr = CeedCalloc(1, qf); CeedChk(ierr); 450 (*qf)->ceed = ceed; 451 ierr = CeedReference(ceed); CeedChk(ierr); 452 (*qf)->ref_count = 1; 453 (*qf)->vec_length = vec_length; 454 (*qf)->is_identity = false; 455 (*qf)->function = f; 456 if (strlen(source)) { 457 const char *kernel_name = strrchr(source, ':') + 1; 458 size_t kernel_name_len = strlen(kernel_name); 459 ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr); 460 strncpy(kernel_name_copy, kernel_name, kernel_name_len); 461 (*qf)->kernel_name = kernel_name_copy; 462 463 size_t source_len = strlen(source) - kernel_name_len - 1; 464 ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr); 465 strncpy(source_copy, source, source_len); 466 (*qf)->source_path = source_copy; 467 } 468 ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr); 469 ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr); 470 ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr); 471 return CEED_ERROR_SUCCESS; 472 } 473 474 /** 475 @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name. 476 477 @param ceed A Ceed object where the CeedQFunction will be created 478 @param name Name of QFunction to use from gallery 479 @param[out] qf Address of the variable where the newly created 480 CeedQFunction will be stored 481 482 @return An error code: 0 - success, otherwise - failure 483 484 @ref User 485 **/ 486 int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, 487 CeedQFunction *qf) { 488 int ierr; 489 size_t match_len = 0, match_index = UINT_MAX; 490 491 ierr = CeedQFunctionRegisterAll(); CeedChk(ierr); 492 // Find matching backend 493 if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, 494 "No QFunction name provided"); 495 for (size_t i=0; i<num_qfunctions; i++) { 496 size_t n; 497 const char *curr_name = gallery_qfunctions[i].name; 498 for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {} 499 if (n > match_len) { 500 match_len = n; 501 match_index = i; 502 } 503 } 504 if (!match_len) 505 // LCOV_EXCL_START 506 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction"); 507 // LCOV_EXCL_STOP 508 509 // Create QFunction 510 ierr = CeedQFunctionCreateInterior(ceed, 511 gallery_qfunctions[match_index].vec_length, 512 gallery_qfunctions[match_index].f, 513 gallery_qfunctions[match_index].source, qf); 514 CeedChk(ierr); 515 516 // QFunction specific setup 517 ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr); 518 519 // Copy name 520 ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr); 521 (*qf)->is_gallery = true; 522 return CEED_ERROR_SUCCESS; 523 } 524 525 /** 526 @brief Create an identity CeedQFunction. Inputs are written into outputs in 527 the order given. This is useful for CeedOperators that can be 528 represented with only the action of a CeedRestriction and CeedBasis, 529 such as restriction and prolongation operators for p-multigrid. 530 Backends may optimize CeedOperators with this CeedQFunction to avoid 531 the copy of input data to output fields by using the same memory 532 location for both. 533 534 @param ceed A Ceed object where the CeedQFunction will be created 535 @param[in] size Size of the QFunction fields 536 @param[in] in_mode CeedEvalMode for input to CeedQFunction 537 @param[in] out_mode CeedEvalMode for output to CeedQFunction 538 @param[out] qf Address of the variable where the newly created 539 CeedQFunction will be stored 540 541 @return An error code: 0 - success, otherwise - failure 542 543 @ref User 544 **/ 545 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, 546 CeedEvalMode out_mode, CeedQFunction *qf) { 547 int ierr; 548 549 ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr); 550 ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr); 551 ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr); 552 553 (*qf)->is_identity = true; 554 555 CeedQFunctionContext ctx; 556 ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr); 557 ierr = CeedQFunctionContextSetInt32(ctx, "size", size); CeedChk(ierr); 558 559 return CEED_ERROR_SUCCESS; 560 } 561 562 /** 563 @brief Copy the pointer to a CeedQFunction. Both pointers should 564 be destroyed with `CeedQFunctionDestroy()`; 565 Note: If `*qf_copy` is non-NULL, then it is assumed that 566 `*qf_copy` is a pointer to a CeedQFunction. This 567 CeedQFunction will be destroyed if `*qf_copy` is the only 568 reference to this CeedQFunction. 569 570 @param qf CeedQFunction to copy reference to 571 @param[out] qf_copy Variable to store copied reference 572 573 @return An error code: 0 - success, otherwise - failure 574 575 @ref User 576 **/ 577 int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) { 578 int ierr; 579 580 ierr = CeedQFunctionReference(qf); CeedChk(ierr); 581 ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr); 582 *qf_copy = qf; 583 return CEED_ERROR_SUCCESS; 584 } 585 586 /** 587 @brief Add a CeedQFunction input 588 589 @param qf CeedQFunction 590 @param field_name Name of QFunction field 591 @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 592 (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 593 @param eval_mode \ref CEED_EVAL_NONE to use values directly, 594 \ref CEED_EVAL_INTERP to use interpolated values, 595 \ref CEED_EVAL_GRAD to use gradients. 596 597 @return An error code: 0 - success, otherwise - failure 598 599 @ref User 600 **/ 601 int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, 602 CeedInt size, 603 CeedEvalMode eval_mode) { 604 if (qf->is_immutable) 605 // LCOV_EXCL_START 606 return CeedError(qf->ceed, CEED_ERROR_MAJOR, 607 "QFunction cannot be changed after set as immutable"); 608 // LCOV_EXCL_STOP 609 if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1)) 610 // LCOV_EXCL_START 611 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 612 "CEED_EVAL_WEIGHT should have size 1"); 613 // LCOV_EXCL_STOP 614 int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], 615 field_name, size, eval_mode); 616 CeedChk(ierr); 617 qf->num_input_fields++; 618 return CEED_ERROR_SUCCESS; 619 } 620 621 /** 622 @brief Add a CeedQFunction output 623 624 @param qf CeedQFunction 625 @param field_name Name of QFunction field 626 @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 627 (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 628 @param eval_mode \ref CEED_EVAL_NONE to use values directly, 629 \ref CEED_EVAL_INTERP to use interpolated values, 630 \ref CEED_EVAL_GRAD to use gradients. 631 632 @return An error code: 0 - success, otherwise - failure 633 634 @ref User 635 **/ 636 int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, 637 CeedInt size, CeedEvalMode eval_mode) { 638 if (qf->is_immutable) 639 // LCOV_EXCL_START 640 return CeedError(qf->ceed, CEED_ERROR_MAJOR, 641 "QFunction cannot be changed after set as immutable"); 642 // LCOV_EXCL_STOP 643 if (eval_mode == CEED_EVAL_WEIGHT) 644 // LCOV_EXCL_START 645 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 646 "Cannot create QFunction output with " 647 "CEED_EVAL_WEIGHT"); 648 // LCOV_EXCL_STOP 649 int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], 650 field_name, size, eval_mode); 651 CeedChk(ierr); 652 qf->num_output_fields++; 653 return CEED_ERROR_SUCCESS; 654 } 655 656 /** 657 @brief Get the CeedQFunctionFields of a CeedQFunction 658 659 Note: Calling this function asserts that setup is complete 660 and sets the CeedQFunction as immutable. 661 662 @param qf CeedQFunction 663 @param[out] num_input_fields Variable to store number of input fields 664 @param[out] input_fields Variable to store input fields 665 @param[out] num_output_fields Variable to store number of output fields 666 @param[out] output_fields Variable to store output fields 667 668 @return An error code: 0 - success, otherwise - failure 669 670 @ref Advanced 671 **/ 672 int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, 673 CeedQFunctionField **input_fields, 674 CeedInt *num_output_fields, 675 CeedQFunctionField **output_fields) { 676 qf->is_immutable = true; 677 if (num_input_fields) *num_input_fields = qf->num_input_fields; 678 if (input_fields) *input_fields = qf->input_fields; 679 if (num_output_fields) *num_output_fields = qf->num_output_fields; 680 if (output_fields) *output_fields = qf->output_fields; 681 return CEED_ERROR_SUCCESS; 682 } 683 684 /** 685 @brief Get the name of a CeedQFunctionField 686 687 @param qf_field CeedQFunctionField 688 @param[out] field_name Variable to store the field name 689 690 @return An error code: 0 - success, otherwise - failure 691 692 @ref Advanced 693 **/ 694 int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) { 695 *field_name = (char *)qf_field->field_name; 696 return CEED_ERROR_SUCCESS; 697 } 698 699 /** 700 @brief Get the number of components of a CeedQFunctionField 701 702 @param qf_field CeedQFunctionField 703 @param[out] size Variable to store the size of the field 704 705 @return An error code: 0 - success, otherwise - failure 706 707 @ref Advanced 708 **/ 709 int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) { 710 *size = qf_field->size; 711 return CEED_ERROR_SUCCESS; 712 } 713 714 /** 715 @brief Get the CeedEvalMode of a CeedQFunctionField 716 717 @param qf_field CeedQFunctionField 718 @param[out] eval_mode Variable to store the field evaluation mode 719 720 @return An error code: 0 - success, otherwise - failure 721 722 @ref Advanced 723 **/ 724 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, 725 CeedEvalMode *eval_mode) { 726 *eval_mode = qf_field->eval_mode; 727 return CEED_ERROR_SUCCESS; 728 } 729 730 /** 731 @brief Set global context for a CeedQFunction 732 733 @param qf CeedQFunction 734 @param ctx Context data to set 735 736 @return An error code: 0 - success, otherwise - failure 737 738 @ref User 739 **/ 740 int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) { 741 int ierr; 742 qf->ctx = ctx; 743 ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr); 744 return CEED_ERROR_SUCCESS; 745 } 746 747 /** 748 @brief View a CeedQFunction 749 750 @param[in] qf CeedQFunction to view 751 @param[in] stream Stream to write; typically stdout/stderr or a file 752 753 @return Error code: 0 - success, otherwise - failure 754 755 @ref User 756 **/ 757 int CeedQFunctionView(CeedQFunction qf, FILE *stream) { 758 int ierr; 759 760 fprintf(stream, "%sCeedQFunction %s\n", 761 qf->is_gallery ? "Gallery " : "User ", 762 qf->is_gallery ? qf->gallery_name : qf->kernel_name); 763 764 fprintf(stream, " %d Input Field%s:\n", qf->num_input_fields, 765 qf->num_input_fields>1 ? "s" : ""); 766 for (CeedInt i=0; i<qf->num_input_fields; i++) { 767 ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream); 768 CeedChk(ierr); 769 } 770 771 fprintf(stream, " %d Output Field%s:\n", qf->num_output_fields, 772 qf->num_output_fields>1 ? "s" : ""); 773 for (CeedInt i=0; i<qf->num_output_fields; i++) { 774 ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream); 775 CeedChk(ierr); 776 } 777 return CEED_ERROR_SUCCESS; 778 } 779 780 /** 781 @brief Get the Ceed associated with a CeedQFunction 782 783 @param qf CeedQFunction 784 @param[out] ceed Variable to store Ceed 785 786 @return An error code: 0 - success, otherwise - failure 787 788 @ref Advanced 789 **/ 790 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 791 *ceed = qf->ceed; 792 return CEED_ERROR_SUCCESS; 793 } 794 795 /** 796 @brief Apply the action of a CeedQFunction 797 798 Note: Calling this function asserts that setup is complete 799 and sets the CeedQFunction as immutable. 800 801 @param qf CeedQFunction 802 @param Q Number of quadrature points 803 @param[in] u Array of input CeedVectors 804 @param[out] v Array of output CeedVectors 805 806 @return An error code: 0 - success, otherwise - failure 807 808 @ref User 809 **/ 810 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 811 CeedVector *u, CeedVector *v) { 812 int ierr; 813 if (!qf->Apply) 814 // LCOV_EXCL_START 815 return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, 816 "Backend does not support QFunctionApply"); 817 // LCOV_EXCL_STOP 818 if (Q % qf->vec_length) 819 // LCOV_EXCL_START 820 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 821 "Number of quadrature points %d must be a " 822 "multiple of %d", Q, qf->vec_length); 823 // LCOV_EXCL_STOP 824 qf->is_immutable = true; 825 ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr); 826 return CEED_ERROR_SUCCESS; 827 } 828 829 /** 830 @brief Destroy a CeedQFunction 831 832 @param qf CeedQFunction to destroy 833 834 @return An error code: 0 - success, otherwise - failure 835 836 @ref User 837 **/ 838 int CeedQFunctionDestroy(CeedQFunction *qf) { 839 int ierr; 840 841 if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS; 842 // Backend destroy 843 if ((*qf)->Destroy) { 844 ierr = (*qf)->Destroy(*qf); CeedChk(ierr); 845 } 846 // Free fields 847 for (int i=0; i<(*qf)->num_input_fields; i++) { 848 ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr); 849 ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr); 850 } 851 for (int i=0; i<(*qf)->num_output_fields; i++) { 852 ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr); 853 ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr); 854 } 855 ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr); 856 ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr); 857 858 // User context data object 859 ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr); 860 861 ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr); 862 ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr); 863 ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr); 864 ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr); 865 ierr = CeedFree(qf); CeedChk(ierr); 866 return CEED_ERROR_SUCCESS; 867 } 868 869 /// @} 870