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-impl.h> 18 #include <ceed-backend.h> 19 #include <string.h> 20 #include <limits.h> 21 22 /// @cond DOXYGEN_SKIP 23 static struct CeedQFunction_private ceed_qfunction_none; 24 /// @endcond 25 26 /// @cond DOXYGEN_SKIP 27 static struct { 28 char name[CEED_MAX_RESOURCE_LEN]; 29 char source[CEED_MAX_RESOURCE_LEN]; 30 CeedInt vlength; 31 CeedQFunctionUser f; 32 int (*init)(Ceed ceed, const char *name, CeedQFunction qf); 33 } qfunctions[1024]; 34 static size_t num_qfunctions; 35 /// @endcond 36 37 /// @file 38 /// Implementation of public CeedQFunction interfaces 39 /// 40 /// @addtogroup CeedQFunction 41 /// @{ 42 43 /** 44 @brief Create a CeedQFunction for evaluating interior (volumetric) terms. 45 46 @param ceed A Ceed object where the CeedQFunction will be created 47 @param vlength Vector length. Caller must ensure that number of quadrature 48 points is a multiple of vlength. 49 @param f Function pointer to evaluate action at quadrature points. 50 See \ref CeedQFunctionUser. 51 @param source Absolute path to source of QFunction, 52 "\abs_path\file.h:function_name" 53 @param[out] qf Address of the variable where the newly created 54 CeedQFunction will be stored 55 56 @return An error code: 0 - success, otherwise - failure 57 58 See \ref CeedQFunctionUser for details on the call-back function @a f's 59 arguments. 60 61 @ref Basic 62 **/ 63 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f, 64 const char *source, CeedQFunction *qf) { 65 int ierr; 66 char *source_copy; 67 68 if (!ceed->QFunctionCreate) { 69 Ceed delegate; 70 ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr); 71 72 if (!delegate) 73 // LCOV_EXCL_START 74 return CeedError(ceed, 1, "Backend does not support QFunctionCreate"); 75 // LCOV_EXCL_STOP 76 77 ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf); 78 CeedChk(ierr); 79 return 0; 80 } 81 82 ierr = CeedCalloc(1,qf); CeedChk(ierr); 83 (*qf)->ceed = ceed; 84 ceed->refcount++; 85 (*qf)->refcount = 1; 86 (*qf)->vlength = vlength; 87 (*qf)->identity = 0; 88 (*qf)->function = f; 89 size_t slen = strlen(source) + 1; 90 ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr); 91 memcpy(source_copy, source, slen); 92 (*qf)->sourcepath = source_copy; 93 ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr); 94 ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr); 95 ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr); 96 return 0; 97 } 98 99 /** 100 @brief Register a gallery QFunction 101 102 @param name Name for this backend to respond to 103 @param source Absolute path to source of QFunction, 104 "\path\CEED_DIR\gallery\folder\file.h:function_name" 105 @param vlength Vector length. Caller must ensure that number of quadrature 106 points is a multiple of vlength. 107 @param f Function pointer to evaluate action at quadrature points. 108 See \ref CeedQFunctionUser. 109 @param init Initialization function called by CeedQFunctionInit() when the 110 QFunction is selected. 111 112 @return An error code: 0 - success, otherwise - failure 113 114 @ref Advanced 115 **/ 116 int CeedQFunctionRegister(const char *name, const char *source, 117 CeedInt vlength, CeedQFunctionUser f, 118 int (*init)(Ceed, const char *, CeedQFunction)) { 119 if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0])) 120 // LCOV_EXCL_START 121 return CeedError(NULL, 1, "Too many gallery QFunctions"); 122 // LCOV_EXCL_STOP 123 124 strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN); 125 qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0; 126 strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN); 127 qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0; 128 qfunctions[num_qfunctions].vlength = vlength; 129 qfunctions[num_qfunctions].f = f; 130 qfunctions[num_qfunctions].init = init; 131 num_qfunctions++; 132 return 0; 133 } 134 135 /** 136 @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name. 137 138 @param ceed A Ceed object where the CeedQFunction will be created 139 @param name Name of QFunction to use from gallery 140 @param[out] qf Address of the variable where the newly created 141 CeedQFunction will be stored 142 143 @return An error code: 0 - success, otherwise - failure 144 145 @ref Basic 146 **/ 147 int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, 148 CeedQFunction *qf) { 149 int ierr; 150 size_t matchlen = 0, matchidx = UINT_MAX; 151 152 // Find matching backend 153 if (!name) return CeedError(NULL, 1, "No QFunction name provided"); 154 for (size_t i=0; i<num_qfunctions; i++) { 155 size_t n; 156 const char *currname = qfunctions[i].name; 157 for (n = 0; currname[n] && currname[n] == name[n]; n++) {} 158 if (n > matchlen) { 159 matchlen = n; 160 matchidx = i; 161 } 162 } 163 if (!matchlen) 164 // LCOV_EXCL_START 165 return CeedError(NULL, 1, "No suitable gallery QFunction"); 166 // LCOV_EXCL_STOP 167 168 // Create QFunction 169 ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength, 170 qfunctions[matchidx].f, 171 qfunctions[matchidx].source, qf); 172 CeedChk(ierr); 173 174 // QFunction specific setup 175 ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr); 176 177 return 0; 178 } 179 180 /** 181 @brief Create an identity CeedQFunction. Inputs are written into outputs in 182 the order given. This is useful for CeedOperators that can be 183 represented with only the action of a CeedRestriction and CeedBasis, 184 such as restriction and prolongation operators for p-multigrid. 185 Backends may optimize CeedOperators with this CeedQFunction to avoid 186 the copy of input data to output fields by using the same memory 187 location for both. 188 189 @param ceed A Ceed object where the CeedQFunction will be created 190 @param[in] size Size of the qfunction fields 191 @param[in] inmode CeedEvalMode for input to CeedQFunction 192 @param[in] outmode CeedEvalMode for output to CeedQFunction 193 @param[out] qf Address of the variable where the newly created 194 CeedQFunction will be stored 195 196 @return An error code: 0 - success, otherwise - failure 197 198 @ref Basic 199 **/ 200 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode, 201 CeedEvalMode outmode, CeedQFunction *qf) { 202 int ierr; 203 204 if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE) 205 // LCOV_EXCL_START 206 return CeedError(ceed, 1, "CEED_EVAL_NONE for a both the input and " 207 "output does not make sense with an identity QFunction"); 208 // LCOV_EXCL_STOP 209 210 ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr); 211 ierr = CeedQFunctionAddInput(*qf, "input", 1, inmode); CeedChk(ierr); 212 ierr = CeedQFunctionAddOutput(*qf, "output", 1, outmode); CeedChk(ierr); 213 214 (*qf)->identity = 1; 215 if (size > 1) { 216 CeedInt *ctx; 217 ierr = CeedCalloc(1, &ctx); CeedChk(ierr); 218 ctx[0] = size; 219 ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr); 220 (*qf)->inputfields[0]->size = size; 221 (*qf)->outputfields[0]->size = size; 222 } 223 224 return 0; 225 } 226 227 /** 228 @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output 229 230 @param f CeedQFunctionField 231 @param fieldname Name of QFunction field 232 @param size Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or 233 1 for CEED_EVAL_NONE, CEED_EVAL_INTERP, and CEED_EVAL_WEIGHT) 234 @param emode \ref CEED_EVAL_NONE to use values directly, 235 \ref CEED_EVAL_INTERP to use interpolated values, 236 \ref CEED_EVAL_GRAD to use gradients, 237 \ref CEED_EVAL_WEIGHT to use quadrature weights. 238 239 @return An error code: 0 - success, otherwise - failure 240 241 @ref Developer 242 **/ 243 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname, 244 CeedInt size, CeedEvalMode emode) { 245 size_t len = strlen(fieldname); 246 char *tmp; 247 int ierr; 248 ierr = CeedCalloc(1,f); CeedChk(ierr); 249 250 ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 251 memcpy(tmp, fieldname, len+1); 252 (*f)->fieldname = tmp; 253 (*f)->size = size; 254 (*f)->emode = emode; 255 return 0; 256 } 257 258 /** 259 @brief Add a CeedQFunction input 260 261 @param qf CeedQFunction 262 @param fieldname Name of QFunction field 263 @param size Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or 264 1 for CEED_EVAL_NONE and CEED_EVAL_INTERP) 265 @param emode \ref CEED_EVAL_NONE to use values directly, 266 \ref CEED_EVAL_INTERP to use interpolated values, 267 \ref CEED_EVAL_GRAD to use gradients. 268 269 @return An error code: 0 - success, otherwise - failure 270 271 @ref Basic 272 **/ 273 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size, 274 CeedEvalMode emode) { 275 int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields], 276 fieldname, size, emode); 277 CeedChk(ierr); 278 qf->numinputfields++; 279 return 0; 280 } 281 282 /** 283 @brief Add a CeedQFunction output 284 285 @param qf CeedQFunction 286 @param fieldname Name of QFunction field 287 @param size Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or 288 1 for CEED_EVAL_NONE and CEED_EVAL_INTERP) 289 @param emode \ref CEED_EVAL_NONE to use values directly, 290 \ref CEED_EVAL_INTERP to use interpolated values, 291 \ref CEED_EVAL_GRAD to use gradients. 292 293 @return An error code: 0 - success, otherwise - failure 294 295 @ref Basic 296 **/ 297 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname, 298 CeedInt size, CeedEvalMode emode) { 299 if (emode == CEED_EVAL_WEIGHT) 300 // LCOV_EXCL_START 301 return CeedError(qf->ceed, 1, "Cannot create QFunction output with " 302 "CEED_EVAL_WEIGHT"); 303 // LCOV_EXCL_STOP 304 int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields], 305 fieldname, size, emode); 306 CeedChk(ierr); 307 qf->numoutputfields++; 308 return 0; 309 } 310 311 /** 312 @brief Get the Ceed associated with a CeedQFunction 313 314 @param qf CeedQFunction 315 @param[out] ceed Variable to store Ceed 316 317 @return An error code: 0 - success, otherwise - failure 318 319 @ref Advanced 320 **/ 321 322 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 323 *ceed = qf->ceed; 324 return 0; 325 } 326 327 /** 328 @brief Get the vector length of a CeedQFunction 329 330 @param qf CeedQFunction 331 @param[out] vlength Variable to store vector length 332 333 @return An error code: 0 - success, otherwise - failure 334 335 @ref Advanced 336 **/ 337 338 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) { 339 *vlength = qf->vlength; 340 return 0; 341 } 342 343 /** 344 @brief Get the number of inputs and outputs to a CeedQFunction 345 346 @param qf CeedQFunction 347 @param[out] numinput Variable to store number of input fields 348 @param[out] numoutput Variable to store number of output fields 349 350 @return An error code: 0 - success, otherwise - failure 351 352 @ref Advanced 353 **/ 354 355 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput, 356 CeedInt *numoutput) { 357 if (numinput) *numinput = qf->numinputfields; 358 if (numoutput) *numoutput = qf->numoutputfields; 359 return 0; 360 } 361 362 /** 363 @brief Get the source path string for a CeedQFunction 364 365 @param qf CeedQFunction 366 @param[out] source Variable to store source path string 367 368 @return An error code: 0 - success, otherwise - failure 369 370 @ref Advanced 371 **/ 372 373 int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) { 374 *source = (char *) qf->sourcepath; 375 return 0; 376 } 377 378 /** 379 @brief Get the User Function for a CeedQFunction 380 381 @param qf CeedQFunction 382 @param[out] f Variable to store user function 383 384 @return An error code: 0 - success, otherwise - failure 385 386 @ref Advanced 387 **/ 388 389 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { 390 *f = qf->function; 391 return 0; 392 } 393 394 /** 395 @brief Get global context size for a CeedQFunction 396 397 @param qf CeedQFunction 398 @param[out] ctxsize Variable to store size of context data values 399 400 @return An error code: 0 - success, otherwise - failure 401 402 @ref Advanced 403 **/ 404 405 int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) { 406 if (qf->fortranstatus) { 407 fContext *fctx = qf->ctx; 408 *ctxsize = fctx->innerctxsize; 409 } else { 410 *ctxsize = qf->ctxsize; 411 } 412 return 0; 413 } 414 415 /** 416 @brief Get global context for a CeedQFunction 417 418 @param qf CeedQFunction 419 @param[out] ctx Variable to store context data values 420 421 @return An error code: 0 - success, otherwise - failure 422 423 @ref Advanced 424 **/ 425 426 int CeedQFunctionGetContext(CeedQFunction qf, void **ctx) { 427 *ctx = qf->ctx; 428 return 0; 429 } 430 431 /** 432 @brief Determine if Fortran interface was used 433 434 @param qf CeedQFunction 435 @param[out] fortranstatus Variable to store Fortran status 436 437 @return An error code: 0 - success, otherwise - failure 438 439 @ref Advanced 440 **/ 441 442 int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) { 443 *fortranstatus = qf->fortranstatus; 444 return 0; 445 } 446 447 /** 448 @brief Determine if QFunction is identity 449 450 @param qf CeedQFunction 451 @param[out] identity Variable to store identity status 452 453 @return An error code: 0 - success, otherwise - failure 454 455 @ref Advanced 456 **/ 457 458 int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) { 459 *identity = qf->identity; 460 return 0; 461 } 462 463 /** 464 @brief Get true user context for a CeedQFunction 465 466 @param qf CeedQFunction 467 @param[out] ctx Variable to store context data values 468 469 @return An error code: 0 - success, otherwise - failure 470 471 @ref Advanced 472 **/ 473 474 int CeedQFunctionGetInnerContext(CeedQFunction qf, void **ctx) { 475 if (qf->fortranstatus) { 476 fContext *fctx = qf->ctx; 477 *ctx = fctx->innerctx; 478 } else { 479 *ctx = qf->ctx; 480 } 481 482 483 return 0; 484 } 485 486 /** 487 @brief Get backend data of a CeedQFunction 488 489 @param qf CeedQFunction 490 @param[out] data Variable to store data 491 492 @return An error code: 0 - success, otherwise - failure 493 494 @ref Advanced 495 **/ 496 497 int CeedQFunctionGetData(CeedQFunction qf, void **data) { 498 *data = qf->data; 499 return 0; 500 } 501 502 /** 503 @brief Set backend data of a CeedQFunction 504 505 @param[out] qf CeedQFunction 506 @param data Data to set 507 508 @return An error code: 0 - success, otherwise - failure 509 510 @ref Advanced 511 **/ 512 513 int CeedQFunctionSetData(CeedQFunction qf, void **data) { 514 qf->data = *data; 515 return 0; 516 } 517 518 /** 519 @brief Set global context for a CeedQFunction 520 521 @param qf CeedQFunction 522 @param ctx Context data to set 523 @param ctxsize Size of context data values 524 525 @return An error code: 0 - success, otherwise - failure 526 527 @ref Basic 528 **/ 529 int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) { 530 qf->ctx = ctx; 531 qf->ctxsize = ctxsize; 532 return 0; 533 } 534 535 /** 536 @brief Apply the action of a CeedQFunction 537 538 @param qf CeedQFunction 539 @param Q Number of quadrature points 540 @param[in] u Array of input data arrays 541 @param[out] v Array of output data arrays 542 543 @return An error code: 0 - success, otherwise - failure 544 545 @ref Advanced 546 **/ 547 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 548 CeedVector *u, CeedVector *v) { 549 int ierr; 550 if (!qf->Apply) 551 // LCOV_EXCL_START 552 return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply"); 553 // LCOV_EXCL_STOP 554 if (Q % qf->vlength) 555 // LCOV_EXCL_START 556 return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a " 557 "multiple of %d", Q, qf->vlength); 558 // LCOV_EXCL_STOP 559 ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr); 560 return 0; 561 } 562 563 /** 564 @brief Get the CeedQFunctionFields of a CeedQFunction 565 566 @param qf CeedQFunction 567 @param[out] inputfields Variable to store inputfields 568 @param[out] outputfields Variable to store outputfields 569 570 @return An error code: 0 - success, otherwise - failure 571 572 @ref Advanced 573 **/ 574 575 int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields, 576 CeedQFunctionField **outputfields) { 577 if (inputfields) 578 *inputfields = qf->inputfields; 579 if (outputfields) 580 *outputfields = qf->outputfields; 581 return 0; 582 } 583 584 /** 585 @brief Get the name of a CeedQFunctionField 586 587 @param qffield CeedQFunctionField 588 @param[out] fieldname Variable to store the field name 589 590 @return An error code: 0 - success, otherwise - failure 591 592 @ref Advanced 593 **/ 594 595 int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) { 596 *fieldname = (char *)qffield->fieldname; 597 return 0; 598 } 599 600 /** 601 @brief Get the number of components of a CeedQFunctionField 602 603 @param qffield CeedQFunctionField 604 @param[out] size Variable to store the size of the field 605 606 @return An error code: 0 - success, otherwise - failure 607 608 @ref Advanced 609 **/ 610 611 int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) { 612 *size = qffield->size; 613 return 0; 614 } 615 616 /** 617 @brief Get the CeedEvalMode of a CeedQFunctionField 618 619 @param qffield CeedQFunctionField 620 @param[out] emode Variable to store the field evaluation mode 621 622 @return An error code: 0 - success, otherwise - failure 623 624 @ref Advanced 625 **/ 626 627 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield, 628 CeedEvalMode *emode) { 629 *emode = qffield->emode; 630 return 0; 631 } 632 633 /** 634 @brief Destroy a CeedQFunction 635 636 @param qf CeedQFunction to destroy 637 638 @return An error code: 0 - success, otherwise - failure 639 640 @ref Basic 641 **/ 642 int CeedQFunctionDestroy(CeedQFunction *qf) { 643 int ierr; 644 645 if (!*qf || --(*qf)->refcount > 0) 646 return 0; 647 // Backend destroy 648 if ((*qf)->Destroy) { 649 ierr = (*qf)->Destroy(*qf); CeedChk(ierr); 650 } 651 // Free fields 652 for (int i=0; i<(*qf)->numinputfields; i++) { 653 ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr); 654 ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr); 655 } 656 for (int i=0; i<(*qf)->numoutputfields; i++) { 657 ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr); 658 ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr); 659 } 660 ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr); 661 ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr); 662 // Free ctx if identity 663 if ((*qf)->identity) { 664 ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr); 665 } 666 667 ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr); 668 ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr); 669 ierr = CeedFree(qf); CeedChk(ierr); 670 return 0; 671 } 672 673 /// @cond DOXYGEN_SKIP 674 // Indicate that no QFunction is provided by the user 675 CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none; 676 /// @endcond 677 /// @} 678