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