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 #define _POSIX_C_SOURCE 200112 18 #include <ceed-impl.h> 19 #include <ceed-backend.h> 20 #include <limits.h> 21 #include <stdarg.h> 22 #include <stddef.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 /// @cond DOXYGEN_SKIP 28 static CeedRequest ceed_request_immediate; 29 static CeedRequest ceed_request_ordered; 30 31 static struct { 32 char prefix[CEED_MAX_RESOURCE_LEN]; 33 int (*init)(const char *resource, Ceed f); 34 unsigned int priority; 35 } backends[32]; 36 static size_t num_backends; 37 38 #define CEED_FTABLE_ENTRY(class, method) \ 39 {#class #method, offsetof(struct class ##_private, method)} 40 /// @endcond 41 42 /// @file 43 /// Implementation of core components of Ceed library 44 45 /// @addtogroup CeedUser 46 /// @{ 47 48 /** 49 @brief Request immediate completion 50 51 This predefined constant is passed as the \ref CeedRequest argument to 52 interfaces when the caller wishes for the operation to be performed 53 immediately. The code 54 55 @code 56 CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE); 57 @endcode 58 59 is semantically equivalent to 60 61 @code 62 CeedRequest request; 63 CeedOperatorApply(op, ..., &request); 64 CeedRequestWait(&request); 65 @endcode 66 67 @sa CEED_REQUEST_ORDERED 68 **/ 69 CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate; 70 71 /** 72 @brief Request ordered completion 73 74 This predefined constant is passed as the \ref CeedRequest argument to 75 interfaces when the caller wishes for the operation to be completed in the 76 order that it is submitted to the device. It is typically used in a construct 77 such as 78 79 @code 80 CeedRequest request; 81 CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED); 82 CeedOperatorApply(op2, ..., &request); 83 // other optional work 84 CeedWait(&request); 85 @endcode 86 87 which allows the sequence to complete asynchronously but does not start 88 `op2` until `op1` has completed. 89 90 @todo The current implementation is overly strict, offering equivalent 91 semantics to @ref CEED_REQUEST_IMMEDIATE. 92 93 @sa CEED_REQUEST_IMMEDIATE 94 */ 95 CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered; 96 97 /** 98 @brief Wait for a CeedRequest to complete. 99 100 Calling CeedRequestWait on a NULL request is a no-op. 101 102 @param req Address of CeedRequest to wait for; zeroed on completion. 103 104 @return An error code: 0 - success, otherwise - failure 105 106 @ref User 107 **/ 108 int CeedRequestWait(CeedRequest *req) { 109 if (!*req) 110 return 0; 111 return CeedError(NULL, 2, "CeedRequestWait not implemented"); 112 } 113 114 /// @} 115 116 /// ---------------------------------------------------------------------------- 117 /// Ceed Library Internal Functions 118 /// ---------------------------------------------------------------------------- 119 /// @addtogroup CeedDeveloper 120 /// @{ 121 122 /// @} 123 124 /// ---------------------------------------------------------------------------- 125 /// Ceed Backend API 126 /// ---------------------------------------------------------------------------- 127 /// @addtogroup CeedBackend 128 /// @{ 129 130 /** 131 @brief Print Ceed debugging information 132 133 @param ceed Ceed context 134 @param format Printing format 135 136 @return None 137 138 @ref Backend 139 **/ 140 // LCOV_EXCL_START 141 void CeedDebugImpl(const Ceed ceed, const char *format,...) { 142 if (!ceed->debug) return; 143 va_list args; 144 va_start(args, format); 145 CeedDebugImpl256(ceed, 0, format, args); 146 va_end(args); 147 } 148 // LCOV_EXCL_STOP 149 150 /** 151 @brief Print Ceed debugging information in color 152 153 @param ceed Ceed context 154 @param color Color to print 155 @param format Printing format 156 157 @return None 158 159 @ref Backend 160 **/ 161 // LCOV_EXCL_START 162 void CeedDebugImpl256(const Ceed ceed, const unsigned char color, 163 const char *format,...) { 164 if (!ceed->debug) return; 165 va_list args; 166 va_start(args, format); 167 fflush(stdout); 168 fprintf(stdout, "\033[38;5;%dm", color); 169 vfprintf(stdout, format, args); 170 fprintf(stdout, "\033[m"); 171 fprintf(stdout, "\n"); 172 fflush(stdout); 173 va_end(args); 174 } 175 // LCOV_EXCL_STOP 176 177 /** 178 @brief Allocate an array on the host; use CeedMalloc() 179 180 Memory usage can be tracked by the library. This ensures sufficient 181 alignment for vectorization and should be used for large allocations. 182 183 @param n Number of units to allocate 184 @param unit Size of each unit 185 @param p Address of pointer to hold the result. 186 187 @return An error code: 0 - success, otherwise - failure 188 189 @sa CeedFree() 190 191 @ref Backend 192 **/ 193 int CeedMallocArray(size_t n, size_t unit, void *p) { 194 int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit); 195 if (ierr) 196 // LCOV_EXCL_START 197 return CeedError(NULL, ierr, "posix_memalign failed to allocate %zd " 198 "members of size %zd\n", n, unit); 199 // LCOV_EXCL_STOP 200 201 return 0; 202 } 203 204 /** 205 @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc() 206 207 Memory usage can be tracked by the library. 208 209 @param n Number of units to allocate 210 @param unit Size of each unit 211 @param p Address of pointer to hold the result. 212 213 @return An error code: 0 - success, otherwise - failure 214 215 @sa CeedFree() 216 217 @ref Backend 218 **/ 219 int CeedCallocArray(size_t n, size_t unit, void *p) { 220 *(void **)p = calloc(n, unit); 221 if (n && unit && !*(void **)p) 222 // LCOV_EXCL_START 223 return CeedError(NULL, 1, "calloc failed to allocate %zd members of size " 224 "%zd\n", n, unit); 225 // LCOV_EXCL_STOP 226 227 return 0; 228 } 229 230 /** 231 @brief Reallocate an array on the host; use CeedRealloc() 232 233 Memory usage can be tracked by the library. 234 235 @param n Number of units to allocate 236 @param unit Size of each unit 237 @param p Address of pointer to hold the result. 238 239 @return An error code: 0 - success, otherwise - failure 240 241 @sa CeedFree() 242 243 @ref Backend 244 **/ 245 int CeedReallocArray(size_t n, size_t unit, void *p) { 246 *(void **)p = realloc(*(void **)p, n*unit); 247 if (n && unit && !*(void **)p) 248 // LCOV_EXCL_START 249 return CeedError(NULL, 1, "realloc failed to allocate %zd members of size " 250 "%zd\n", n, unit); 251 // LCOV_EXCL_STOP 252 253 return 0; 254 } 255 256 /** Free memory allocated using CeedMalloc() or CeedCalloc() 257 258 @param p address of pointer to memory. This argument is of type void* to 259 avoid needing a cast, but is the address of the pointer (which is 260 zeroed) rather than the pointer. 261 **/ 262 int CeedFree(void *p) { 263 free(*(void **)p); 264 *(void **)p = NULL; 265 return 0; 266 } 267 268 /** 269 @brief Register a Ceed backend 270 271 @param prefix Prefix of resources for this backend to respond to. For 272 example, the reference backend responds to "/cpu/self". 273 @param init Initialization function called by CeedInit() when the backend 274 is selected to drive the requested resource. 275 @param priority Integer priority. Lower values are preferred in case the 276 resource requested by CeedInit() has non-unique best prefix 277 match. 278 279 @return An error code: 0 - success, otherwise - failure 280 281 @ref Backend 282 **/ 283 int CeedRegister(const char *prefix, int (*init)(const char *, Ceed), 284 unsigned int priority) { 285 if (num_backends >= sizeof(backends) / sizeof(backends[0])) 286 // LCOV_EXCL_START 287 return CeedError(NULL, 1, "Too many backends"); 288 // LCOV_EXCL_STOP 289 290 strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN); 291 backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0; 292 backends[num_backends].init = init; 293 backends[num_backends].priority = priority; 294 num_backends++; 295 return 0; 296 } 297 298 /** 299 @brief Return debugging status flag 300 301 @param ceed Ceed context to get debugging flag 302 @param isDebug Variable to store debugging flag 303 304 @return An error code: 0 - success, otherwise - failure 305 306 @ref Bcakend 307 **/ 308 int CeedIsDebug(Ceed ceed, bool *isDebug) { 309 *isDebug = ceed->debug; 310 return 0; 311 } 312 313 /** 314 @brief Retrieve a parent Ceed context 315 316 @param ceed Ceed context to retrieve parent of 317 @param[out] parent Address to save the parent to 318 319 @return An error code: 0 - success, otherwise - failure 320 321 @ref Backend 322 **/ 323 int CeedGetParent(Ceed ceed, Ceed *parent) { 324 int ierr; 325 if (ceed->parent) { 326 ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr); 327 return 0; 328 } 329 *parent = ceed; 330 return 0; 331 } 332 333 /** 334 @brief Retrieve a delegate Ceed context 335 336 @param ceed Ceed context to retrieve delegate of 337 @param[out] delegate Address to save the delegate to 338 339 @return An error code: 0 - success, otherwise - failure 340 341 @ref Backend 342 **/ 343 int CeedGetDelegate(Ceed ceed, Ceed *delegate) { 344 *delegate = ceed->delegate; 345 return 0; 346 } 347 348 /** 349 @brief Set a delegate Ceed context 350 351 This function allows a Ceed context to set a delegate Ceed context. All 352 backend implementations default to the delegate Ceed context, unless 353 overridden. 354 355 @param ceed Ceed context to set delegate of 356 @param[out] delegate Address to set the delegate to 357 358 @return An error code: 0 - success, otherwise - failure 359 360 @ref Backend 361 **/ 362 int CeedSetDelegate(Ceed ceed, Ceed delegate) { 363 ceed->delegate = delegate; 364 delegate->parent = ceed; 365 return 0; 366 } 367 368 /** 369 @brief Retrieve a delegate Ceed context for a specific object type 370 371 @param ceed Ceed context to retrieve delegate of 372 @param[out] delegate Address to save the delegate to 373 @param[in] objname Name of the object type to retrieve delegate for 374 375 @return An error code: 0 - success, otherwise - failure 376 377 @ref Backend 378 **/ 379 int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *objname) { 380 CeedInt ierr; 381 382 // Check for object delegate 383 for (CeedInt i=0; i<ceed->objdelegatecount; i++) 384 if (!strcmp(objname, ceed->objdelegates->objname)) { 385 *delegate = ceed->objdelegates->delegate; 386 return 0; 387 } 388 389 // Use default delegate if no object delegate 390 ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr); 391 392 return 0; 393 } 394 395 /** 396 @brief Set a delegate Ceed context for a specific object type 397 398 This function allows a Ceed context to set a delegate Ceed context for a 399 given type of Ceed object. All backend implementations default to the 400 delegate Ceed context for this object. For example, 401 CeedSetObjectDelegate(ceed, refceed, "Basis") 402 uses refceed implementations for all CeedBasis backend functions. 403 404 @param ceed Ceed context to set delegate of 405 @param[out] delegate Address to set the delegate to 406 @param[in] objname Name of the object type to set delegate for 407 408 @return An error code: 0 - success, otherwise - failure 409 410 @ref Backend 411 **/ 412 int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *objname) { 413 CeedInt ierr; 414 CeedInt count = ceed->objdelegatecount; 415 416 // Malloc or Realloc 417 if (count) { 418 ierr = CeedRealloc(count+1, &ceed->objdelegates); CeedChk(ierr); 419 } else { 420 ierr = CeedCalloc(1, &ceed->objdelegates); CeedChk(ierr); 421 } 422 ceed->objdelegatecount++; 423 424 // Set object delegate 425 ceed->objdelegates[count].delegate = delegate; 426 size_t slen = strlen(objname) + 1; 427 ierr = CeedMalloc(slen, &ceed->objdelegates[count].objname); CeedChk(ierr); 428 memcpy(ceed->objdelegates[count].objname, objname, slen); 429 430 // Set delegate parent 431 delegate->parent = ceed; 432 433 return 0; 434 } 435 436 /** 437 @brief Get the fallback resource for CeedOperators 438 439 @param ceed Ceed context 440 @param[out] resource Variable to store fallback resource 441 442 @return An error code: 0 - success, otherwise - failure 443 444 @ref Backend 445 **/ 446 447 int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) { 448 *resource = (const char *)ceed->opfallbackresource; 449 return 0; 450 } 451 452 /** 453 @brief Set the fallback resource for CeedOperators. The current resource, if 454 any, is freed by calling this function. This string is freed upon the 455 destruction of the Ceed context. 456 457 @param[out] ceed Ceed context 458 @param resource Fallback resource to set 459 460 @return An error code: 0 - success, otherwise - failure 461 462 @ref Backend 463 **/ 464 465 int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) { 466 int ierr; 467 468 // Free old 469 ierr = CeedFree(&ceed->opfallbackresource); CeedChk(ierr); 470 471 // Set new 472 size_t len = strlen(resource); 473 char *tmp; 474 ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 475 memcpy(tmp, resource, len+1); 476 ceed->opfallbackresource = tmp; 477 478 return 0; 479 } 480 481 /** 482 @brief Get the parent Ceed context associated with a fallback Ceed context 483 for a CeedOperator 484 485 @param ceed Ceed context 486 @param[out] parent Variable to store parent Ceed context 487 488 @return An error code: 0 - success, otherwise - failure 489 490 @ref Backend 491 **/ 492 493 int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) { 494 *parent = ceed->opfallbackparent; 495 return 0; 496 } 497 498 /** 499 @brief Flag Ceed context as deterministic 500 501 @param ceed Ceed to flag as deterministic 502 503 @return An error code: 0 - success, otherwise - failure 504 505 @ref Backend 506 **/ 507 508 int CeedSetDeterministic(Ceed ceed, bool isDeterministic) { 509 ceed->isDeterministic = isDeterministic; 510 return 0; 511 } 512 513 /** 514 @brief Set a backend function 515 516 This function is used for a backend to set the function associated with 517 the Ceed objects. For example, 518 CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate) 519 sets the backend implementation of 'CeedVectorCreate' and 520 CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply) 521 sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed' 522 is not required for the object type ("Basis" vs "CeedBasis"). 523 524 @param ceed Ceed context for error handling 525 @param type Type of Ceed object to set function for 526 @param[out] object Ceed object to set function for 527 @param fname Name of function to set 528 @param f Function to set 529 530 @return An error code: 0 - success, otherwise - failure 531 532 @ref Backend 533 **/ 534 int CeedSetBackendFunction(Ceed ceed, const char *type, void *object, 535 const char *fname, int (*f)()) { 536 char lookupname[CEED_MAX_RESOURCE_LEN+1] = ""; 537 538 // Build lookup name 539 if (strcmp(type, "Ceed")) 540 strncat (lookupname, "Ceed", CEED_MAX_RESOURCE_LEN); 541 strncat(lookupname, type, CEED_MAX_RESOURCE_LEN); 542 strncat(lookupname, fname, CEED_MAX_RESOURCE_LEN); 543 544 // Find and use offset 545 for (CeedInt i = 0; ceed->foffsets[i].fname; i++) 546 if (!strcmp(ceed->foffsets[i].fname, lookupname)) { 547 size_t offset = ceed->foffsets[i].offset; 548 int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD* 549 *fpointer = f; 550 return 0; 551 } 552 553 // LCOV_EXCL_START 554 return CeedError(ceed, 1, "Requested function '%s' was not found for CEED " 555 "object '%s'", fname, type); 556 // LCOV_EXCL_STOP 557 } 558 559 /** 560 @brief Retrieve backend data for a Ceed context 561 562 @param ceed Ceed context to retrieve data of 563 @param[out] data Address to save data to 564 565 @return An error code: 0 - success, otherwise - failure 566 567 @ref Backend 568 **/ 569 int CeedGetData(Ceed ceed, void **data) { 570 *data = ceed->data; 571 return 0; 572 } 573 574 /** 575 @brief Set backend data for a Ceed context 576 577 @param ceed Ceed context to set data of 578 @param data Address of data to set 579 580 @return An error code: 0 - success, otherwise - failure 581 582 @ref Backend 583 **/ 584 int CeedSetData(Ceed ceed, void **data) { 585 ceed->data = *data; 586 return 0; 587 } 588 589 /// @} 590 591 /// ---------------------------------------------------------------------------- 592 /// Ceed Public API 593 /// ---------------------------------------------------------------------------- 594 /// @addtogroup CeedUser 595 /// @{ 596 597 /** 598 @brief Initialize a \ref Ceed context to use the specified resource. 599 600 @param resource Resource to use, e.g., "/cpu/self" 601 @param ceed The library context 602 @sa CeedRegister() CeedDestroy() 603 604 @return An error code: 0 - success, otherwise - failure 605 606 @ref User 607 **/ 608 int CeedInit(const char *resource, Ceed *ceed) { 609 int ierr; 610 size_t matchlen = 0, matchidx = UINT_MAX, matchpriority = UINT_MAX, priority; 611 612 // Find matching backend 613 if (!resource) 614 // LCOV_EXCL_START 615 return CeedError(NULL, 1, "No resource provided"); 616 // LCOV_EXCL_STOP 617 618 for (size_t i=0; i<num_backends; i++) { 619 size_t n; 620 const char *prefix = backends[i].prefix; 621 for (n = 0; prefix[n] && prefix[n] == resource[n]; n++) {} 622 priority = backends[i].priority; 623 if (n > matchlen || (n == matchlen && matchpriority > priority)) { 624 matchlen = n; 625 matchpriority = priority; 626 matchidx = i; 627 } 628 } 629 if (!matchlen) 630 // LCOV_EXCL_START 631 return CeedError(NULL, 1, "No suitable backend: %s", resource); 632 // LCOV_EXCL_STOP 633 634 // Setup Ceed 635 ierr = CeedCalloc(1, ceed); CeedChk(ierr); 636 const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 637 if (!ceed_error_handler) 638 ceed_error_handler = "abort"; 639 if (!strcmp(ceed_error_handler, "exit")) 640 (*ceed)->Error = CeedErrorExit; 641 else if (!strcmp(ceed_error_handler, "store")) 642 (*ceed)->Error = CeedErrorStore; 643 else 644 (*ceed)->Error = CeedErrorAbort; 645 memcpy((*ceed)->errmsg, "No error message stored", 24); 646 (*ceed)->refcount = 1; 647 (*ceed)->data = NULL; 648 649 // Set lookup table 650 foffset foffsets[] = { 651 CEED_FTABLE_ENTRY(Ceed, Error), 652 CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType), 653 CEED_FTABLE_ENTRY(Ceed, Destroy), 654 CEED_FTABLE_ENTRY(Ceed, VectorCreate), 655 CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate), 656 CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked), 657 CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1), 658 CEED_FTABLE_ENTRY(Ceed, BasisCreateH1), 659 CEED_FTABLE_ENTRY(Ceed, TensorContractCreate), 660 CEED_FTABLE_ENTRY(Ceed, QFunctionCreate), 661 CEED_FTABLE_ENTRY(Ceed, OperatorCreate), 662 CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate), 663 CEED_FTABLE_ENTRY(CeedVector, SetArray), 664 CEED_FTABLE_ENTRY(CeedVector, TakeArray), 665 CEED_FTABLE_ENTRY(CeedVector, SetValue), 666 CEED_FTABLE_ENTRY(CeedVector, GetArray), 667 CEED_FTABLE_ENTRY(CeedVector, GetArrayRead), 668 CEED_FTABLE_ENTRY(CeedVector, RestoreArray), 669 CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead), 670 CEED_FTABLE_ENTRY(CeedVector, Norm), 671 CEED_FTABLE_ENTRY(CeedVector, Reciprocal), 672 CEED_FTABLE_ENTRY(CeedVector, Destroy), 673 CEED_FTABLE_ENTRY(CeedElemRestriction, Apply), 674 CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock), 675 CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets), 676 CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy), 677 CEED_FTABLE_ENTRY(CeedBasis, Apply), 678 CEED_FTABLE_ENTRY(CeedBasis, Destroy), 679 CEED_FTABLE_ENTRY(CeedTensorContract, Apply), 680 CEED_FTABLE_ENTRY(CeedTensorContract, Destroy), 681 CEED_FTABLE_ENTRY(CeedQFunction, Apply), 682 CEED_FTABLE_ENTRY(CeedQFunction, Destroy), 683 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction), 684 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal), 685 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal), 686 CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal), 687 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal), 688 CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse), 689 CEED_FTABLE_ENTRY(CeedOperator, Apply), 690 CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite), 691 CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd), 692 CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite), 693 CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian), 694 CEED_FTABLE_ENTRY(CeedOperator, Destroy), 695 {NULL, 0} // End of lookup table - used in SetBackendFunction loop 696 }; 697 698 ierr = CeedCalloc(sizeof(foffsets), &(*ceed)->foffsets); CeedChk(ierr); 699 memcpy((*ceed)->foffsets, foffsets, sizeof(foffsets)); 700 701 // Set fallback for advanced CeedOperator functions 702 const char fallbackresource[] = "/cpu/self/ref/serial"; 703 ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource); 704 CeedChk(ierr); 705 706 // Record env variables CEED_DEBUG or DBG 707 (*ceed)->debug = !!getenv("CEED_DEBUG") || !!getenv("DBG"); 708 709 // Backend specific setup 710 ierr = backends[matchidx].init(resource, *ceed); CeedChk(ierr); 711 712 // Copy resource prefix, if backend setup sucessful 713 size_t len = strlen(backends[matchidx].prefix); 714 char *tmp; 715 ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 716 memcpy(tmp, backends[matchidx].prefix, len+1); 717 (*ceed)->resource = tmp; 718 719 return 0; 720 } 721 722 /** 723 @brief Get the full resource name for a Ceed context 724 725 @param ceed Ceed context to get resource name of 726 @param[out] resource Variable to store resource name 727 728 @return An error code: 0 - success, otherwise - failure 729 730 @ref User 731 **/ 732 733 int CeedGetResource(Ceed ceed, const char **resource) { 734 *resource = (const char *)ceed->resource; 735 return 0; 736 } 737 738 /** 739 @brief Return Ceed context preferred memory type 740 741 @param ceed Ceed context to get preferred memory type of 742 @param[out] type Address to save preferred memory type to 743 744 @return An error code: 0 - success, otherwise - failure 745 746 @ref User 747 **/ 748 int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type) { 749 int ierr; 750 751 if (ceed->GetPreferredMemType) { 752 ierr = ceed->GetPreferredMemType(type); CeedChk(ierr); 753 } else { 754 Ceed delegate; 755 ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr); 756 757 if (delegate) { 758 ierr = CeedGetPreferredMemType(delegate, type); CeedChk(ierr); 759 } else { 760 *type = CEED_MEM_HOST; 761 } 762 } 763 764 return 0; 765 } 766 767 /** 768 @brief Get deterministic status of Ceed 769 770 @param[in] ceed Ceed 771 @param[out] isDeterministic Variable to store deterministic status 772 773 @return An error code: 0 - success, otherwise - failure 774 775 @ref User 776 **/ 777 int CeedIsDeterministic(Ceed ceed, bool *isDeterministic) { 778 *isDeterministic = ceed->isDeterministic; 779 return 0; 780 } 781 782 /** 783 @brief View a Ceed 784 785 @param[in] ceed Ceed to view 786 @param[in] stream Filestream to write to 787 788 @return An error code: 0 - success, otherwise - failure 789 790 @ref User 791 **/ 792 int CeedView(Ceed ceed, FILE *stream) { 793 int ierr; 794 CeedMemType memtype; 795 796 ierr = CeedGetPreferredMemType(ceed, &memtype); CeedChk(ierr); 797 798 fprintf(stream, "Ceed\n" 799 " Ceed Resource: %s\n" 800 " Preferred MemType: %s\n", 801 ceed->resource, CeedMemTypes[memtype]); 802 803 return 0; 804 } 805 806 /** 807 @brief Destroy a Ceed context 808 809 @param ceed Address of Ceed context to destroy 810 811 @return An error code: 0 - success, otherwise - failure 812 813 @ref User 814 **/ 815 int CeedDestroy(Ceed *ceed) { 816 int ierr; 817 if (!*ceed || --(*ceed)->refcount > 0) return 0; 818 if ((*ceed)->delegate) { 819 ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr); 820 } 821 822 if ((*ceed)->objdelegatecount > 0) { 823 for (int i=0; i<(*ceed)->objdelegatecount; i++) { 824 ierr = CeedDestroy(&((*ceed)->objdelegates[i].delegate)); CeedChk(ierr); 825 ierr = CeedFree(&(*ceed)->objdelegates[i].objname); CeedChk(ierr); 826 } 827 ierr = CeedFree(&(*ceed)->objdelegates); CeedChk(ierr); 828 } 829 830 if ((*ceed)->Destroy) { 831 ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr); 832 } 833 834 ierr = CeedFree(&(*ceed)->foffsets); CeedChk(ierr); 835 ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr); 836 ierr = CeedDestroy(&(*ceed)->opfallbackceed); CeedChk(ierr); 837 ierr = CeedFree(&(*ceed)->opfallbackresource); CeedChk(ierr); 838 ierr = CeedFree(ceed); CeedChk(ierr); 839 return 0; 840 } 841 842 /** 843 @brief Error handling implementation; use \ref CeedError instead. 844 845 @ref Developer 846 **/ 847 int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func, 848 int ecode, const char *format, ...) { 849 va_list args; 850 int retval; 851 va_start(args, format); 852 if (ceed) { 853 retval = ceed->Error(ceed, filename, lineno, func, ecode, format, args); 854 } else { 855 // LCOV_EXCL_START 856 const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 857 if (!ceed_error_handler) 858 ceed_error_handler = "abort"; 859 if (!strcmp(ceed_error_handler, "return")) 860 retval = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, args); 861 else 862 // This function will not return 863 retval = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, args); 864 } 865 va_end(args); 866 return retval; 867 // LCOV_EXCL_STOP 868 } 869 870 /** 871 @brief Error handler that returns without printing anything. 872 873 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 874 875 @ref Developer 876 **/ 877 // LCOV_EXCL_START 878 int CeedErrorReturn(Ceed ceed, const char *filename, int lineno, 879 const char *func, int ecode, const char *format, 880 va_list args) { 881 return ecode; 882 } 883 // LCOV_EXCL_STOP 884 885 /** 886 @brief Error handler that stores the error message for future use and returns 887 the error. 888 889 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 890 891 @ref Developer 892 **/ 893 // LCOV_EXCL_START 894 int CeedErrorStore(Ceed ceed, const char *filename, int lineno, 895 const char *func, int ecode, const char *format, 896 va_list args) { 897 if (ceed->parent) 898 CeedErrorStore(ceed->parent, filename, lineno, func, ecode, format, args); 899 900 // Build message 901 CeedInt len; 902 len = snprintf(ceed->errmsg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ", 903 filename, lineno, func); 904 vsnprintf(ceed->errmsg + len, CEED_MAX_RESOURCE_LEN - len, format, args); 905 return ecode; 906 } 907 // LCOV_EXCL_STOP 908 909 /** 910 @brief Error handler that prints to stderr and aborts 911 912 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 913 914 @ref Developer 915 **/ 916 // LCOV_EXCL_START 917 int CeedErrorAbort(Ceed ceed, const char *filename, int lineno, 918 const char *func, int ecode, const char *format, 919 va_list args) { 920 fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func); 921 vfprintf(stderr, format, args); 922 fprintf(stderr, "\n"); 923 abort(); 924 return ecode; 925 } 926 // LCOV_EXCL_STOP 927 928 /** 929 @brief Error handler that prints to stderr and exits 930 931 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 932 933 In contrast to CeedErrorAbort(), this exits without a signal, so atexit() 934 handlers (e.g., as used by gcov) are run. 935 936 @ref Developer 937 **/ 938 int CeedErrorExit(Ceed ceed, const char *filename, int lineno, const char *func, 939 int ecode, const char *format, va_list args) { 940 fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func); 941 vfprintf(stderr, format, args); 942 fprintf(stderr, "\n"); 943 exit(ecode); 944 return ecode; 945 } 946 947 /** 948 @brief Set error handler 949 950 A default error handler is set in CeedInit(). Use this function to change 951 the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined 952 error handler. 953 954 @ref Developer 955 **/ 956 int CeedSetErrorHandler(Ceed ceed, 957 int (*eh)(Ceed, const char *, int, const char *, 958 int, const char *, va_list)) { 959 ceed->Error = eh; 960 if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, eh); 961 for (int i=0; i<ceed->objdelegatecount; i++) 962 CeedSetErrorHandler(ceed->objdelegates[i].delegate, eh); 963 return 0; 964 } 965 966 /** 967 @brief Get error message 968 969 The error message is only stored when using the error handler 970 CeedErrorStore() 971 972 @param[in] ceed Ceed contex to retrieve error message 973 @param[out] errmsg Char pointer to hold error message 974 975 @ref Developer 976 **/ 977 int CeedGetErrorMessage(Ceed ceed, const char **errmsg) { 978 *errmsg = ceed->errmsg; 979 return 0; 980 } 981 982 /** 983 @brief Restore error message 984 985 The error message is only stored when using the error handler 986 CeedErrorStore() 987 988 @param[in] ceed Ceed contex to restore error message 989 @param[out] errmsg Char pointer that holds error message 990 991 @ref Developer 992 **/ 993 int CeedResetErrorMessage(Ceed ceed, const char **errmsg) { 994 *errmsg = NULL; 995 memcpy(ceed->errmsg, "No error message stored", 24); 996 return 0; 997 } 998 999 /// @} 1000