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