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, Destroy), 672 CEED_FTABLE_ENTRY(CeedElemRestriction, Apply), 673 CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock), 674 CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets), 675 CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy), 676 CEED_FTABLE_ENTRY(CeedBasis, Apply), 677 CEED_FTABLE_ENTRY(CeedBasis, Destroy), 678 CEED_FTABLE_ENTRY(CeedTensorContract, Apply), 679 CEED_FTABLE_ENTRY(CeedTensorContract, Destroy), 680 CEED_FTABLE_ENTRY(CeedQFunction, Apply), 681 CEED_FTABLE_ENTRY(CeedQFunction, Destroy), 682 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction), 683 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal), 684 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal), 685 CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal), 686 CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal), 687 CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse), 688 CEED_FTABLE_ENTRY(CeedOperator, Apply), 689 CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite), 690 CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd), 691 CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite), 692 CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian), 693 CEED_FTABLE_ENTRY(CeedOperator, Destroy), 694 {NULL, 0} // End of lookup table - used in SetBackendFunction loop 695 }; 696 697 ierr = CeedCalloc(sizeof(foffsets), &(*ceed)->foffsets); CeedChk(ierr); 698 memcpy((*ceed)->foffsets, foffsets, sizeof(foffsets)); 699 700 // Set fallback for advanced CeedOperator functions 701 const char fallbackresource[] = "/cpu/self/ref/serial"; 702 ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource); 703 CeedChk(ierr); 704 705 // Record env variables CEED_DEBUG or DBG 706 (*ceed)->debug = !!getenv("CEED_DEBUG") || !!getenv("DBG"); 707 708 // Backend specific setup 709 ierr = backends[matchidx].init(resource, *ceed); CeedChk(ierr); 710 711 // Copy resource prefix, if backend setup sucessful 712 size_t len = strlen(backends[matchidx].prefix); 713 char *tmp; 714 ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 715 memcpy(tmp, backends[matchidx].prefix, len+1); 716 (*ceed)->resource = tmp; 717 718 return 0; 719 } 720 721 /** 722 @brief Get the full resource name for a Ceed context 723 724 @param ceed Ceed context to get resource name of 725 @param[out] resource Variable to store resource name 726 727 @return An error code: 0 - success, otherwise - failure 728 729 @ref User 730 **/ 731 732 int CeedGetResource(Ceed ceed, const char **resource) { 733 *resource = (const char *)ceed->resource; 734 return 0; 735 } 736 737 /** 738 @brief Return Ceed context preferred memory type 739 740 @param ceed Ceed context to get preferred memory type of 741 @param[out] type Address to save preferred memory type to 742 743 @return An error code: 0 - success, otherwise - failure 744 745 @ref User 746 **/ 747 int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type) { 748 int ierr; 749 750 if (ceed->GetPreferredMemType) { 751 ierr = ceed->GetPreferredMemType(type); CeedChk(ierr); 752 } else { 753 Ceed delegate; 754 ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr); 755 756 if (delegate) { 757 ierr = CeedGetPreferredMemType(delegate, type); CeedChk(ierr); 758 } else { 759 *type = CEED_MEM_HOST; 760 } 761 } 762 763 return 0; 764 } 765 766 /** 767 @brief Get deterministic status of Ceed 768 769 @param[in] ceed Ceed 770 @param[out] isDeterministic Variable to store deterministic status 771 772 @return An error code: 0 - success, otherwise - failure 773 774 @ref User 775 **/ 776 int CeedIsDeterministic(Ceed ceed, bool *isDeterministic) { 777 *isDeterministic = ceed->isDeterministic; 778 return 0; 779 } 780 781 /** 782 @brief View a Ceed 783 784 @param[in] ceed Ceed to view 785 @param[in] stream Filestream to write to 786 787 @return An error code: 0 - success, otherwise - failure 788 789 @ref User 790 **/ 791 int CeedView(Ceed ceed, FILE *stream) { 792 int ierr; 793 CeedMemType memtype; 794 795 ierr = CeedGetPreferredMemType(ceed, &memtype); CeedChk(ierr); 796 797 fprintf(stream, "Ceed\n" 798 " Ceed Resource: %s\n" 799 " Preferred MemType: %s\n", 800 ceed->resource, CeedMemTypes[memtype]); 801 802 return 0; 803 } 804 805 /** 806 @brief Destroy a Ceed context 807 808 @param ceed Address of Ceed context to destroy 809 810 @return An error code: 0 - success, otherwise - failure 811 812 @ref User 813 **/ 814 int CeedDestroy(Ceed *ceed) { 815 int ierr; 816 if (!*ceed || --(*ceed)->refcount > 0) 817 return 0; 818 819 if ((*ceed)->delegate) { 820 ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr); 821 } 822 823 if ((*ceed)->objdelegatecount > 0) { 824 for (int i=0; i<(*ceed)->objdelegatecount; i++) { 825 ierr = CeedDestroy(&((*ceed)->objdelegates[i].delegate)); CeedChk(ierr); 826 ierr = CeedFree(&(*ceed)->objdelegates[i].objname); CeedChk(ierr); 827 } 828 ierr = CeedFree(&(*ceed)->objdelegates); CeedChk(ierr); 829 } 830 831 if ((*ceed)->Destroy) { 832 ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr); 833 } 834 835 ierr = CeedFree(&(*ceed)->foffsets); CeedChk(ierr); 836 ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr); 837 ierr = CeedDestroy(&(*ceed)->opfallbackceed); CeedChk(ierr); 838 ierr = CeedFree(&(*ceed)->opfallbackresource); CeedChk(ierr); 839 ierr = CeedFree(ceed); CeedChk(ierr); 840 return 0; 841 } 842 843 /** 844 @brief Error handling implementation; use \ref CeedError instead. 845 846 @ref Developer 847 **/ 848 int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func, 849 int ecode, const char *format, ...) { 850 va_list args; 851 int retval; 852 va_start(args, format); 853 if (ceed) { 854 retval = ceed->Error(ceed, filename, lineno, func, ecode, format, args); 855 } else { 856 // LCOV_EXCL_START 857 const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 858 if (!ceed_error_handler) 859 ceed_error_handler = "abort"; 860 if (!strcmp(ceed_error_handler, "return")) 861 retval = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, args); 862 else 863 // This function will not return 864 retval = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, args); 865 } 866 va_end(args); 867 return retval; 868 // LCOV_EXCL_STOP 869 } 870 871 /** 872 @brief Error handler that returns without printing anything. 873 874 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 875 876 @ref Developer 877 **/ 878 // LCOV_EXCL_START 879 int CeedErrorReturn(Ceed ceed, const char *filename, int lineno, 880 const char *func, int ecode, const char *format, 881 va_list args) { 882 return ecode; 883 } 884 // LCOV_EXCL_STOP 885 886 /** 887 @brief Error handler that stores the error message for future use and returns 888 the error. 889 890 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 891 892 @ref Developer 893 **/ 894 // LCOV_EXCL_START 895 int CeedErrorStore(Ceed ceed, const char *filename, int lineno, 896 const char *func, int ecode, const char *format, 897 va_list args) { 898 if (ceed->parent) 899 CeedErrorStore(ceed->parent, filename, lineno, func, ecode, format, args); 900 901 // Build message 902 CeedInt len; 903 len = snprintf(ceed->errmsg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ", 904 filename, lineno, func); 905 vsnprintf(ceed->errmsg + len, CEED_MAX_RESOURCE_LEN - len, format, args); 906 return ecode; 907 } 908 // LCOV_EXCL_STOP 909 910 /** 911 @brief Error handler that prints to stderr and aborts 912 913 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 914 915 @ref Developer 916 **/ 917 // LCOV_EXCL_START 918 int CeedErrorAbort(Ceed ceed, const char *filename, int lineno, 919 const char *func, int ecode, const char *format, 920 va_list args) { 921 fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func); 922 vfprintf(stderr, format, args); 923 fprintf(stderr, "\n"); 924 abort(); 925 return ecode; 926 } 927 // LCOV_EXCL_STOP 928 929 /** 930 @brief Error handler that prints to stderr and exits 931 932 Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 933 934 In contrast to CeedErrorAbort(), this exits without a signal, so atexit() 935 handlers (e.g., as used by gcov) are run. 936 937 @ref Developer 938 **/ 939 int CeedErrorExit(Ceed ceed, const char *filename, int lineno, const char *func, 940 int ecode, const char *format, va_list args) { 941 fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func); 942 vfprintf(stderr, format, args); 943 fprintf(stderr, "\n"); 944 exit(ecode); 945 return ecode; 946 } 947 948 /** 949 @brief Set error handler 950 951 A default error handler is set in CeedInit(). Use this function to change 952 the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined 953 error handler. 954 955 @ref Developer 956 **/ 957 int CeedSetErrorHandler(Ceed ceed, 958 int (*eh)(Ceed, const char *, int, const char *, 959 int, const char *, va_list)) { 960 ceed->Error = eh; 961 if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, eh); 962 for (int i=0; i<ceed->objdelegatecount; i++) 963 CeedSetErrorHandler(ceed->objdelegates[i].delegate, eh); 964 return 0; 965 } 966 967 /** 968 @brief Get error message 969 970 The error message is only stored when using the error handler 971 CeedErrorStore() 972 973 @param[in] ceed Ceed contex to retrieve error message 974 @param[out] errmsg Char pointer to hold error message 975 976 @ref Developer 977 **/ 978 int CeedGetErrorMessage(Ceed ceed, const char **errmsg) { 979 *errmsg = ceed->errmsg; 980 return 0; 981 } 982 983 /** 984 @brief Restore error message 985 986 The error message is only stored when using the error handler 987 CeedErrorStore() 988 989 @param[in] ceed Ceed contex to restore error message 990 @param[out] errmsg Char pointer that holds error message 991 992 @ref Developer 993 **/ 994 int CeedResetErrorMessage(Ceed ceed, const char **errmsg) { 995 *errmsg = NULL; 996 memcpy(ceed->errmsg, "No error message stored", 24); 997 return 0; 998 } 999 1000 /// @} 1001