1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details. 4d7b241e6Sjeremylt // 5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software 6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral 7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and 8d7b241e6Sjeremylt // source code availability see http://github.com/ceed. 9d7b241e6Sjeremylt // 10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office 12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for 13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including 14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early 15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative. 16d7b241e6Sjeremylt 17d7b241e6Sjeremylt #define _POSIX_C_SOURCE 200112 18d7b241e6Sjeremylt #include <ceed-impl.h> 19d863ab9bSjeremylt #include <ceed-backend.h> 20aedaa0e5Sjeremylt #include <limits.h> 21d7b241e6Sjeremylt #include <stdarg.h> 226e79d475Sjeremylt #include <stddef.h> 23d7b241e6Sjeremylt #include <stdio.h> 24d7b241e6Sjeremylt #include <stdlib.h> 25d7b241e6Sjeremylt #include <string.h> 26d7b241e6Sjeremylt 27d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP 28d7b241e6Sjeremylt static CeedRequest ceed_request_immediate; 29d7b241e6Sjeremylt static CeedRequest ceed_request_ordered; 30d7b241e6Sjeremylt 31d7b241e6Sjeremylt static struct { 32d7b241e6Sjeremylt char prefix[CEED_MAX_RESOURCE_LEN]; 33d7b241e6Sjeremylt int (*init)(const char *resource, Ceed f); 34d7b241e6Sjeremylt unsigned int priority; 35d7b241e6Sjeremylt } backends[32]; 36d7b241e6Sjeremylt static size_t num_backends; 37fe2413ffSjeremylt 386e79d475Sjeremylt #define CEED_FTABLE_ENTRY(class, method) \ 396e79d475Sjeremylt {#class #method, offsetof(struct class ##_private, method)} 40d7b241e6Sjeremylt /// @endcond 41d7b241e6Sjeremylt 42d7b241e6Sjeremylt /// @file 43d7b241e6Sjeremylt /// Implementation of core components of Ceed library 447a982d89SJeremy L. Thompson 457a982d89SJeremy L. Thompson /// @addtogroup CeedUser 46d7b241e6Sjeremylt /// @{ 47d7b241e6Sjeremylt 48dfdf5a53Sjeremylt /** 49dfdf5a53Sjeremylt @brief Request immediate completion 50dfdf5a53Sjeremylt 51dfdf5a53Sjeremylt This predefined constant is passed as the \ref CeedRequest argument to 52dfdf5a53Sjeremylt interfaces when the caller wishes for the operation to be performed 53dfdf5a53Sjeremylt immediately. The code 54dfdf5a53Sjeremylt 55dfdf5a53Sjeremylt @code 56dfdf5a53Sjeremylt CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE); 57dfdf5a53Sjeremylt @endcode 58dfdf5a53Sjeremylt 59dfdf5a53Sjeremylt is semantically equivalent to 60dfdf5a53Sjeremylt 61dfdf5a53Sjeremylt @code 62dfdf5a53Sjeremylt CeedRequest request; 63dfdf5a53Sjeremylt CeedOperatorApply(op, ..., &request); 64dfdf5a53Sjeremylt CeedRequestWait(&request); 65dfdf5a53Sjeremylt @endcode 66dfdf5a53Sjeremylt 67dfdf5a53Sjeremylt @sa CEED_REQUEST_ORDERED 68dfdf5a53Sjeremylt **/ 69d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate; 70d7b241e6Sjeremylt 71d7b241e6Sjeremylt /** 72b11c1e72Sjeremylt @brief Request ordered completion 73d7b241e6Sjeremylt 74d7b241e6Sjeremylt This predefined constant is passed as the \ref CeedRequest argument to 75d7b241e6Sjeremylt interfaces when the caller wishes for the operation to be completed in the 76d7b241e6Sjeremylt order that it is submitted to the device. It is typically used in a construct 77d7b241e6Sjeremylt such as 78d7b241e6Sjeremylt 79d7b241e6Sjeremylt @code 80d7b241e6Sjeremylt CeedRequest request; 81d7b241e6Sjeremylt CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED); 82d7b241e6Sjeremylt CeedOperatorApply(op2, ..., &request); 83d7b241e6Sjeremylt // other optional work 84d7b241e6Sjeremylt CeedWait(&request); 85d7b241e6Sjeremylt @endcode 86d7b241e6Sjeremylt 87d7b241e6Sjeremylt which allows the sequence to complete asynchronously but does not start 88d7b241e6Sjeremylt `op2` until `op1` has completed. 89d7b241e6Sjeremylt 90288c0443SJeremy L Thompson @todo The current implementation is overly strict, offering equivalent 914cc79fe7SJed Brown semantics to @ref CEED_REQUEST_IMMEDIATE. 92d7b241e6Sjeremylt 93d7b241e6Sjeremylt @sa CEED_REQUEST_IMMEDIATE 94d7b241e6Sjeremylt */ 95d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered; 96d7b241e6Sjeremylt 97b11c1e72Sjeremylt /** 987a982d89SJeremy L. Thompson @brief Wait for a CeedRequest to complete. 99dfdf5a53Sjeremylt 1007a982d89SJeremy L. Thompson Calling CeedRequestWait on a NULL request is a no-op. 1017a982d89SJeremy L. Thompson 1027a982d89SJeremy L. Thompson @param req Address of CeedRequest to wait for; zeroed on completion. 1037a982d89SJeremy L. Thompson 1047a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1057a982d89SJeremy L. Thompson 1067a982d89SJeremy L. Thompson @ref User 107b11c1e72Sjeremylt **/ 1087a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) { 1097a982d89SJeremy L. Thompson if (!*req) 1107a982d89SJeremy L. Thompson return 0; 1117a982d89SJeremy L. Thompson return CeedError(NULL, 2, "CeedRequestWait not implemented"); 112683faae0SJed Brown } 1137a982d89SJeremy L. Thompson 1147a982d89SJeremy L. Thompson /// @} 1157a982d89SJeremy L. Thompson 1167a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1177a982d89SJeremy L. Thompson /// Ceed Library Internal Functions 1187a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1197a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper 1207a982d89SJeremy L. Thompson /// @{ 121d7b241e6Sjeremylt 122b11c1e72Sjeremylt /** 123b11c1e72Sjeremylt @brief Error handler that returns without printing anything. 124b11c1e72Sjeremylt 125b11c1e72Sjeremylt Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 126dfdf5a53Sjeremylt 127dfdf5a53Sjeremylt @ref Developer 128b11c1e72Sjeremylt **/ 12913873f79Sjeremylt // LCOV_EXCL_START 130d7b241e6Sjeremylt int CeedErrorReturn(Ceed ceed, const char *filename, int lineno, 131d7b241e6Sjeremylt const char *func, int ecode, const char *format, 132d7b241e6Sjeremylt va_list args) { 133d7b241e6Sjeremylt return ecode; 134d7b241e6Sjeremylt } 13513873f79Sjeremylt // LCOV_EXCL_STOP 136d7b241e6Sjeremylt 137b11c1e72Sjeremylt /** 138b11c1e72Sjeremylt @brief Error handler that prints to stderr and aborts 139b11c1e72Sjeremylt 140b11c1e72Sjeremylt Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 141dfdf5a53Sjeremylt 142dfdf5a53Sjeremylt @ref Developer 143b11c1e72Sjeremylt **/ 14413873f79Sjeremylt // LCOV_EXCL_START 145d7b241e6Sjeremylt int CeedErrorAbort(Ceed ceed, const char *filename, int lineno, 1461d102b48SJeremy L Thompson const char *func, int ecode, const char *format, 1471d102b48SJeremy L Thompson va_list args) { 148d7b241e6Sjeremylt fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func); 149d7b241e6Sjeremylt vfprintf(stderr, format, args); 150d7b241e6Sjeremylt fprintf(stderr, "\n"); 151d7b241e6Sjeremylt abort(); 152d7b241e6Sjeremylt return ecode; 153d7b241e6Sjeremylt } 15413873f79Sjeremylt // LCOV_EXCL_STOP 155d7b241e6Sjeremylt 156b11c1e72Sjeremylt /** 15756e866f4SJed Brown @brief Error handler that prints to stderr and exits 15856e866f4SJed Brown 15956e866f4SJed Brown Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 16056e866f4SJed Brown 16156e866f4SJed Brown In contrast to CeedErrorAbort(), this exits without a signal, so atexit() 16256e866f4SJed Brown handlers (e.g., as used by gcov) are run. 16356e866f4SJed Brown 16456e866f4SJed Brown @ref Developer 16556e866f4SJed Brown **/ 166692c2638Sjeremylt int CeedErrorExit(Ceed ceed, const char *filename, int lineno, const char *func, 167692c2638Sjeremylt int ecode, const char *format, va_list args) { 16856e866f4SJed Brown fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func); 16956e866f4SJed Brown vfprintf(stderr, format, args); 17056e866f4SJed Brown fprintf(stderr, "\n"); 17156e866f4SJed Brown exit(ecode); 17256e866f4SJed Brown return ecode; 17356e866f4SJed Brown } 17456e866f4SJed Brown 17556e866f4SJed Brown /** 176dfdf5a53Sjeremylt @brief Set error handler 177b11c1e72Sjeremylt 178b11c1e72Sjeremylt A default error handler is set in CeedInit(). Use this function to change 179b11c1e72Sjeremylt the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined 180b11c1e72Sjeremylt error handler. 181dfdf5a53Sjeremylt 182dfdf5a53Sjeremylt @ref Developer 183b11c1e72Sjeremylt **/ 184d7b241e6Sjeremylt int CeedSetErrorHandler(Ceed ceed, 1857a982d89SJeremy L. Thompson int (*eh)(Ceed, const char *, int, const char *, 186d7b241e6Sjeremylt int, const char *, va_list)) { 187d7b241e6Sjeremylt ceed->Error = eh; 188d7b241e6Sjeremylt return 0; 189d7b241e6Sjeremylt } 190d7b241e6Sjeremylt 1917a982d89SJeremy L. Thompson /// @} 192d7b241e6Sjeremylt 1937a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1947a982d89SJeremy L. Thompson /// Ceed Backend API 1957a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1967a982d89SJeremy L. Thompson /// @addtogroup CeedBackend 1977a982d89SJeremy L. Thompson /// @{ 198d7b241e6Sjeremylt 199b11c1e72Sjeremylt /** 200*60f9e2d6SJeremy L Thompson @brief Print Ceed debugging information 201*60f9e2d6SJeremy L Thompson 202*60f9e2d6SJeremy L Thompson @param ceed Ceed context 203*60f9e2d6SJeremy L Thompson @param format Printing format 204*60f9e2d6SJeremy L Thompson 205*60f9e2d6SJeremy L Thompson @return None 206*60f9e2d6SJeremy L Thompson 207*60f9e2d6SJeremy L Thompson @ref Backend 208*60f9e2d6SJeremy L Thompson **/ 209*60f9e2d6SJeremy L Thompson void CeedDebugImpl(const Ceed ceed, const char *format,...) { 210*60f9e2d6SJeremy L Thompson if (!ceed->debug) return; 211*60f9e2d6SJeremy L Thompson va_list args; 212*60f9e2d6SJeremy L Thompson va_start(args, format); 213*60f9e2d6SJeremy L Thompson CeedDebugImpl256(ceed, 0, format, args); 214*60f9e2d6SJeremy L Thompson va_end(args); 215*60f9e2d6SJeremy L Thompson } 216*60f9e2d6SJeremy L Thompson 217*60f9e2d6SJeremy L Thompson /** 218*60f9e2d6SJeremy L Thompson @brief Print Ceed debugging information in color 219*60f9e2d6SJeremy L Thompson 220*60f9e2d6SJeremy L Thompson @param ceed Ceed context 221*60f9e2d6SJeremy L Thompson @param color Color to print 222*60f9e2d6SJeremy L Thompson @param format Printing format 223*60f9e2d6SJeremy L Thompson 224*60f9e2d6SJeremy L Thompson @return None 225*60f9e2d6SJeremy L Thompson 226*60f9e2d6SJeremy L Thompson @ref Backend 227*60f9e2d6SJeremy L Thompson **/ 228*60f9e2d6SJeremy L Thompson void CeedDebugImpl256(const Ceed ceed, const unsigned char color, 229*60f9e2d6SJeremy L Thompson const char *format,...) { 230*60f9e2d6SJeremy L Thompson if (!ceed->debug) return; 231*60f9e2d6SJeremy L Thompson va_list args; 232*60f9e2d6SJeremy L Thompson va_start(args, format); 233*60f9e2d6SJeremy L Thompson fflush(stdout); 234*60f9e2d6SJeremy L Thompson fprintf(stdout, "\033[38;5;%dm", color); 235*60f9e2d6SJeremy L Thompson vfprintf(stdout, format, args); 236*60f9e2d6SJeremy L Thompson fprintf(stdout, "\033[m"); 237*60f9e2d6SJeremy L Thompson fprintf(stdout, "\n"); 238*60f9e2d6SJeremy L Thompson fflush(stdout); 239*60f9e2d6SJeremy L Thompson va_end(args); 240*60f9e2d6SJeremy L Thompson } 241*60f9e2d6SJeremy L Thompson 242*60f9e2d6SJeremy L Thompson /** 243b11c1e72Sjeremylt @brief Allocate an array on the host; use CeedMalloc() 244b11c1e72Sjeremylt 245b11c1e72Sjeremylt Memory usage can be tracked by the library. This ensures sufficient 246b11c1e72Sjeremylt alignment for vectorization and should be used for large allocations. 247b11c1e72Sjeremylt 248b11c1e72Sjeremylt @param n Number of units to allocate 249b11c1e72Sjeremylt @param unit Size of each unit 250b11c1e72Sjeremylt @param p Address of pointer to hold the result. 251b11c1e72Sjeremylt 252b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 253b11c1e72Sjeremylt 254b11c1e72Sjeremylt @sa CeedFree() 255dfdf5a53Sjeremylt 2567a982d89SJeremy L. Thompson @ref Backend 257b11c1e72Sjeremylt **/ 258d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) { 259d7b241e6Sjeremylt int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit); 260d7b241e6Sjeremylt if (ierr) 261c042f62fSJeremy L Thompson // LCOV_EXCL_START 2621d102b48SJeremy L Thompson return CeedError(NULL, ierr, "posix_memalign failed to allocate %zd " 2631d102b48SJeremy L Thompson "members of size %zd\n", n, unit); 264c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 265c042f62fSJeremy L Thompson 266d7b241e6Sjeremylt return 0; 267d7b241e6Sjeremylt } 268d7b241e6Sjeremylt 269b11c1e72Sjeremylt /** 270b11c1e72Sjeremylt @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc() 271b11c1e72Sjeremylt 272b11c1e72Sjeremylt Memory usage can be tracked by the library. 273b11c1e72Sjeremylt 274b11c1e72Sjeremylt @param n Number of units to allocate 275b11c1e72Sjeremylt @param unit Size of each unit 276b11c1e72Sjeremylt @param p Address of pointer to hold the result. 277b11c1e72Sjeremylt 278b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 279b11c1e72Sjeremylt 280b11c1e72Sjeremylt @sa CeedFree() 281dfdf5a53Sjeremylt 2827a982d89SJeremy L. Thompson @ref Backend 283b11c1e72Sjeremylt **/ 284d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) { 285d7b241e6Sjeremylt *(void **)p = calloc(n, unit); 286d7b241e6Sjeremylt if (n && unit && !*(void **)p) 287c042f62fSJeremy L Thompson // LCOV_EXCL_START 2881d102b48SJeremy L Thompson return CeedError(NULL, 1, "calloc failed to allocate %zd members of size " 2891d102b48SJeremy L Thompson "%zd\n", n, unit); 290c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 291c042f62fSJeremy L Thompson 292d7b241e6Sjeremylt return 0; 293d7b241e6Sjeremylt } 294d7b241e6Sjeremylt 295b11c1e72Sjeremylt /** 296b11c1e72Sjeremylt @brief Reallocate an array on the host; use CeedRealloc() 297b11c1e72Sjeremylt 298b11c1e72Sjeremylt Memory usage can be tracked by the library. 299b11c1e72Sjeremylt 300b11c1e72Sjeremylt @param n Number of units to allocate 301b11c1e72Sjeremylt @param unit Size of each unit 302b11c1e72Sjeremylt @param p Address of pointer to hold the result. 303b11c1e72Sjeremylt 304b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 305b11c1e72Sjeremylt 306b11c1e72Sjeremylt @sa CeedFree() 307dfdf5a53Sjeremylt 3087a982d89SJeremy L. Thompson @ref Backend 309b11c1e72Sjeremylt **/ 310d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) { 311d7b241e6Sjeremylt *(void **)p = realloc(*(void **)p, n*unit); 312d7b241e6Sjeremylt if (n && unit && !*(void **)p) 313c042f62fSJeremy L Thompson // LCOV_EXCL_START 3141d102b48SJeremy L Thompson return CeedError(NULL, 1, "realloc failed to allocate %zd members of size " 3151d102b48SJeremy L Thompson "%zd\n", n, unit); 316c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 317c042f62fSJeremy L Thompson 318d7b241e6Sjeremylt return 0; 319d7b241e6Sjeremylt } 320d7b241e6Sjeremylt 32134138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc() 32234138859Sjeremylt 32334138859Sjeremylt @param p address of pointer to memory. This argument is of type void* to 32434138859Sjeremylt avoid needing a cast, but is the address of the pointer (which is 32534138859Sjeremylt zeroed) rather than the pointer. 32634138859Sjeremylt **/ 327d7b241e6Sjeremylt int CeedFree(void *p) { 328d7b241e6Sjeremylt free(*(void **)p); 329d7b241e6Sjeremylt *(void **)p = NULL; 330d7b241e6Sjeremylt return 0; 331d7b241e6Sjeremylt } 332d7b241e6Sjeremylt 333d7b241e6Sjeremylt /** 3347a982d89SJeremy L. Thompson @brief Register a Ceed backend 335d7b241e6Sjeremylt 3367a982d89SJeremy L. Thompson @param prefix Prefix of resources for this backend to respond to. For 3377a982d89SJeremy L. Thompson example, the reference backend responds to "/cpu/self". 3387a982d89SJeremy L. Thompson @param init Initialization function called by CeedInit() when the backend 3397a982d89SJeremy L. Thompson is selected to drive the requested resource. 3407a982d89SJeremy L. Thompson @param priority Integer priority. Lower values are preferred in case the 3417a982d89SJeremy L. Thompson resource requested by CeedInit() has non-unique best prefix 3427a982d89SJeremy L. Thompson match. 343b11c1e72Sjeremylt 344b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 345dfdf5a53Sjeremylt 3467a982d89SJeremy L. Thompson @ref Backend 347b11c1e72Sjeremylt **/ 3487a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed), 3497a982d89SJeremy L. Thompson unsigned int priority) { 3507a982d89SJeremy L. Thompson if (num_backends >= sizeof(backends) / sizeof(backends[0])) 3517a982d89SJeremy L. Thompson // LCOV_EXCL_START 3527a982d89SJeremy L. Thompson return CeedError(NULL, 1, "Too many backends"); 3537a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 3547a982d89SJeremy L. Thompson 3557a982d89SJeremy L. Thompson strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN); 3567a982d89SJeremy L. Thompson backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0; 3577a982d89SJeremy L. Thompson backends[num_backends].init = init; 3587a982d89SJeremy L. Thompson backends[num_backends].priority = priority; 3597a982d89SJeremy L. Thompson num_backends++; 3601d102b48SJeremy L Thompson return 0; 361d7b241e6Sjeremylt } 362d7b241e6Sjeremylt 363b11c1e72Sjeremylt /** 364*60f9e2d6SJeremy L Thompson @brief Return debugging status flag 365*60f9e2d6SJeremy L Thompson 366*60f9e2d6SJeremy L Thompson @param ceed Ceed context to get debugging flag 367*60f9e2d6SJeremy L Thompson @param isDebug Variable to store debugging flag 368*60f9e2d6SJeremy L Thompson 369*60f9e2d6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 370*60f9e2d6SJeremy L Thompson 371*60f9e2d6SJeremy L Thompson @ref Bcakend 372*60f9e2d6SJeremy L Thompson **/ 373*60f9e2d6SJeremy L Thompson int CeedIsDebug(Ceed ceed, bool *isDebug) { 374*60f9e2d6SJeremy L Thompson *isDebug = ceed->debug; 375*60f9e2d6SJeremy L Thompson return 0; 376*60f9e2d6SJeremy L Thompson } 377*60f9e2d6SJeremy L Thompson 378*60f9e2d6SJeremy L Thompson /** 3797a982d89SJeremy L. Thompson @brief Retrieve a parent Ceed context 3807a982d89SJeremy L. Thompson 3817a982d89SJeremy L. Thompson @param ceed Ceed context to retrieve parent of 3827a982d89SJeremy L. Thompson @param[out] parent Address to save the parent to 3837a982d89SJeremy L. Thompson 3847a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3857a982d89SJeremy L. Thompson 3867a982d89SJeremy L. Thompson @ref Backend 3877a982d89SJeremy L. Thompson **/ 3887a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) { 3897a982d89SJeremy L. Thompson int ierr; 3907a982d89SJeremy L. Thompson if (ceed->parent) { 3917a982d89SJeremy L. Thompson ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr); 3927a982d89SJeremy L. Thompson return 0; 3937a982d89SJeremy L. Thompson } 3947a982d89SJeremy L. Thompson *parent = ceed; 3957a982d89SJeremy L. Thompson return 0; 3967a982d89SJeremy L. Thompson } 3977a982d89SJeremy L. Thompson 3987a982d89SJeremy L. Thompson /** 3997a982d89SJeremy L. Thompson @brief Retrieve a delegate Ceed context 4007a982d89SJeremy L. Thompson 4017a982d89SJeremy L. Thompson @param ceed Ceed context to retrieve delegate of 4027a982d89SJeremy L. Thompson @param[out] delegate Address to save the delegate to 4037a982d89SJeremy L. Thompson 4047a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4057a982d89SJeremy L. Thompson 4067a982d89SJeremy L. Thompson @ref Backend 4077a982d89SJeremy L. Thompson **/ 4087a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) { 4097a982d89SJeremy L. Thompson *delegate = ceed->delegate; 4107a982d89SJeremy L. Thompson return 0; 4117a982d89SJeremy L. Thompson } 4127a982d89SJeremy L. Thompson 4137a982d89SJeremy L. Thompson /** 4147a982d89SJeremy L. Thompson @brief Set a delegate Ceed context 4157a982d89SJeremy L. Thompson 4167a982d89SJeremy L. Thompson This function allows a Ceed context to set a delegate Ceed context. All 4177a982d89SJeremy L. Thompson backend implementations default to the delegate Ceed context, unless 4187a982d89SJeremy L. Thompson overridden. 4197a982d89SJeremy L. Thompson 4207a982d89SJeremy L. Thompson @param ceed Ceed context to set delegate of 4217a982d89SJeremy L. Thompson @param[out] delegate Address to set the delegate to 4227a982d89SJeremy L. Thompson 4237a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4247a982d89SJeremy L. Thompson 4257a982d89SJeremy L. Thompson @ref Backend 4267a982d89SJeremy L. Thompson **/ 4277a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) { 4287a982d89SJeremy L. Thompson ceed->delegate = delegate; 4297a982d89SJeremy L. Thompson delegate->parent = ceed; 4307a982d89SJeremy L. Thompson return 0; 4317a982d89SJeremy L. Thompson } 4327a982d89SJeremy L. Thompson 4337a982d89SJeremy L. Thompson /** 4347a982d89SJeremy L. Thompson @brief Retrieve a delegate Ceed context for a specific object type 4357a982d89SJeremy L. Thompson 4367a982d89SJeremy L. Thompson @param ceed Ceed context to retrieve delegate of 4377a982d89SJeremy L. Thompson @param[out] delegate Address to save the delegate to 4387a982d89SJeremy L. Thompson @param[in] objname Name of the object type to retrieve delegate for 4397a982d89SJeremy L. Thompson 4407a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4417a982d89SJeremy L. Thompson 4427a982d89SJeremy L. Thompson @ref Backend 4437a982d89SJeremy L. Thompson **/ 4447a982d89SJeremy L. Thompson int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *objname) { 4457a982d89SJeremy L. Thompson CeedInt ierr; 4467a982d89SJeremy L. Thompson 4477a982d89SJeremy L. Thompson // Check for object delegate 4487a982d89SJeremy L. Thompson for (CeedInt i=0; i<ceed->objdelegatecount; i++) 4497a982d89SJeremy L. Thompson if (!strcmp(objname, ceed->objdelegates->objname)) { 4507a982d89SJeremy L. Thompson *delegate = ceed->objdelegates->delegate; 4517a982d89SJeremy L. Thompson return 0; 4527a982d89SJeremy L. Thompson } 4537a982d89SJeremy L. Thompson 4547a982d89SJeremy L. Thompson // Use default delegate if no object delegate 4557a982d89SJeremy L. Thompson ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr); 4567a982d89SJeremy L. Thompson 4577a982d89SJeremy L. Thompson return 0; 4587a982d89SJeremy L. Thompson } 4597a982d89SJeremy L. Thompson 4607a982d89SJeremy L. Thompson /** 4617a982d89SJeremy L. Thompson @brief Set a delegate Ceed context for a specific object type 4627a982d89SJeremy L. Thompson 4637a982d89SJeremy L. Thompson This function allows a Ceed context to set a delegate Ceed context for a 4647a982d89SJeremy L. Thompson given type of Ceed object. All backend implementations default to the 4657a982d89SJeremy L. Thompson delegate Ceed context for this object. For example, 4667a982d89SJeremy L. Thompson CeedSetObjectDelegate(ceed, refceed, "Basis") 4677a982d89SJeremy L. Thompson uses refceed implementations for all CeedBasis backend functions. 4687a982d89SJeremy L. Thompson 4697a982d89SJeremy L. Thompson @param ceed Ceed context to set delegate of 4707a982d89SJeremy L. Thompson @param[out] delegate Address to set the delegate to 4717a982d89SJeremy L. Thompson @param[in] objname Name of the object type to set delegate for 4727a982d89SJeremy L. Thompson 4737a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4747a982d89SJeremy L. Thompson 4757a982d89SJeremy L. Thompson @ref Backend 4767a982d89SJeremy L. Thompson **/ 4777a982d89SJeremy L. Thompson int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *objname) { 4787a982d89SJeremy L. Thompson CeedInt ierr; 4797a982d89SJeremy L. Thompson CeedInt count = ceed->objdelegatecount; 4807a982d89SJeremy L. Thompson 4817a982d89SJeremy L. Thompson // Malloc or Realloc 4827a982d89SJeremy L. Thompson if (count) { 4837a982d89SJeremy L. Thompson ierr = CeedRealloc(count+1, &ceed->objdelegates); CeedChk(ierr); 4847a982d89SJeremy L. Thompson } else { 4857a982d89SJeremy L. Thompson ierr = CeedCalloc(1, &ceed->objdelegates); CeedChk(ierr); 4867a982d89SJeremy L. Thompson } 4877a982d89SJeremy L. Thompson ceed->objdelegatecount++; 4887a982d89SJeremy L. Thompson 4897a982d89SJeremy L. Thompson // Set object delegate 4907a982d89SJeremy L. Thompson ceed->objdelegates[count].delegate = delegate; 4917a982d89SJeremy L. Thompson size_t slen = strlen(objname) + 1; 4927a982d89SJeremy L. Thompson ierr = CeedMalloc(slen, &ceed->objdelegates[count].objname); CeedChk(ierr); 4937a982d89SJeremy L. Thompson memcpy(ceed->objdelegates[count].objname, objname, slen); 4947a982d89SJeremy L. Thompson 4957a982d89SJeremy L. Thompson // Set delegate parent 4967a982d89SJeremy L. Thompson delegate->parent = ceed; 4977a982d89SJeremy L. Thompson 4987a982d89SJeremy L. Thompson return 0; 4997a982d89SJeremy L. Thompson } 5007a982d89SJeremy L. Thompson 5017a982d89SJeremy L. Thompson /** 5027a982d89SJeremy L. Thompson @brief Get the fallback resource for CeedOperators 5037a982d89SJeremy L. Thompson 5047a982d89SJeremy L. Thompson @param ceed Ceed context 5057a982d89SJeremy L. Thompson @param[out] resource Variable to store fallback resource 5067a982d89SJeremy L. Thompson 5077a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5087a982d89SJeremy L. Thompson 5097a982d89SJeremy L. Thompson @ref Backend 5107a982d89SJeremy L. Thompson **/ 5117a982d89SJeremy L. Thompson 5127a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) { 5137a982d89SJeremy L. Thompson *resource = (const char *)ceed->opfallbackresource; 5147a982d89SJeremy L. Thompson return 0; 5157a982d89SJeremy L. Thompson } 5167a982d89SJeremy L. Thompson 5177a982d89SJeremy L. Thompson /** 5187a982d89SJeremy L. Thompson @brief Set the fallback resource for CeedOperators. The current resource, if 5197a982d89SJeremy L. Thompson any, is freed by calling this function. This string is freed upon the 5207a982d89SJeremy L. Thompson destruction of the Ceed context. 5217a982d89SJeremy L. Thompson 5227a982d89SJeremy L. Thompson @param[out] ceed Ceed context 5237a982d89SJeremy L. Thompson @param resource Fallback resource to set 5247a982d89SJeremy L. Thompson 5257a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5267a982d89SJeremy L. Thompson 5277a982d89SJeremy L. Thompson @ref Backend 5287a982d89SJeremy L. Thompson **/ 5297a982d89SJeremy L. Thompson 5307a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) { 5317a982d89SJeremy L. Thompson int ierr; 5327a982d89SJeremy L. Thompson 5337a982d89SJeremy L. Thompson // Free old 5347a982d89SJeremy L. Thompson ierr = CeedFree(&ceed->opfallbackresource); CeedChk(ierr); 5357a982d89SJeremy L. Thompson 5367a982d89SJeremy L. Thompson // Set new 5377a982d89SJeremy L. Thompson size_t len = strlen(resource); 5387a982d89SJeremy L. Thompson char *tmp; 5397a982d89SJeremy L. Thompson ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 5407a982d89SJeremy L. Thompson memcpy(tmp, resource, len+1); 5417a982d89SJeremy L. Thompson ceed->opfallbackresource = tmp; 5427a982d89SJeremy L. Thompson 5437a982d89SJeremy L. Thompson return 0; 5447a982d89SJeremy L. Thompson } 5457a982d89SJeremy L. Thompson 5467a982d89SJeremy L. Thompson /** 5477a982d89SJeremy L. Thompson @brief Get the parent Ceed context associated with a fallback Ceed context 5487a982d89SJeremy L. Thompson for a CeedOperator 5497a982d89SJeremy L. Thompson 5507a982d89SJeremy L. Thompson @param ceed Ceed context 5517a982d89SJeremy L. Thompson @param[out] parent Variable to store parent Ceed context 5527a982d89SJeremy L. Thompson 5537a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5547a982d89SJeremy L. Thompson 5557a982d89SJeremy L. Thompson @ref Backend 5567a982d89SJeremy L. Thompson **/ 5577a982d89SJeremy L. Thompson 5587a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) { 5597a982d89SJeremy L. Thompson *parent = ceed->opfallbackparent; 5607a982d89SJeremy L. Thompson return 0; 5617a982d89SJeremy L. Thompson } 5627a982d89SJeremy L. Thompson 5637a982d89SJeremy L. Thompson /** 5649525855cSJeremy L Thompson @brief Flag Ceed context as deterministic 5659525855cSJeremy L Thompson 5669525855cSJeremy L Thompson @param ceed Ceed to flag as deterministic 5679525855cSJeremy L Thompson 5689525855cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5699525855cSJeremy L Thompson 5709525855cSJeremy L Thompson @ref Backend 5719525855cSJeremy L Thompson **/ 5729525855cSJeremy L Thompson 5739525855cSJeremy L Thompson int CeedSetDeterministic(Ceed ceed, bool isDeterministic) { 5749525855cSJeremy L Thompson ceed->isDeterministic = isDeterministic; 5759525855cSJeremy L Thompson return 0; 5769525855cSJeremy L Thompson } 5779525855cSJeremy L Thompson 5789525855cSJeremy L Thompson /** 5797a982d89SJeremy L. Thompson @brief Set a backend function 5807a982d89SJeremy L. Thompson 5817a982d89SJeremy L. Thompson This function is used for a backend to set the function associated with 5827a982d89SJeremy L. Thompson the Ceed objects. For example, 5837a982d89SJeremy L. Thompson CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate) 5847a982d89SJeremy L. Thompson sets the backend implementation of 'CeedVectorCreate' and 5857a982d89SJeremy L. Thompson CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply) 5867a982d89SJeremy L. Thompson sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed' 5877a982d89SJeremy L. Thompson is not required for the object type ("Basis" vs "CeedBasis"). 5887a982d89SJeremy L. Thompson 5897a982d89SJeremy L. Thompson @param ceed Ceed context for error handling 5907a982d89SJeremy L. Thompson @param type Type of Ceed object to set function for 5917a982d89SJeremy L. Thompson @param[out] object Ceed object to set function for 5927a982d89SJeremy L. Thompson @param fname Name of function to set 5937a982d89SJeremy L. Thompson @param f Function to set 5947a982d89SJeremy L. Thompson 5957a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5967a982d89SJeremy L. Thompson 5977a982d89SJeremy L. Thompson @ref Backend 5987a982d89SJeremy L. Thompson **/ 5997a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object, 6007a982d89SJeremy L. Thompson const char *fname, int (*f)()) { 6017a982d89SJeremy L. Thompson char lookupname[CEED_MAX_RESOURCE_LEN+1] = ""; 6027a982d89SJeremy L. Thompson 6037a982d89SJeremy L. Thompson // Build lookup name 6047a982d89SJeremy L. Thompson if (strcmp(type, "Ceed")) 6057a982d89SJeremy L. Thompson strncat (lookupname, "Ceed", CEED_MAX_RESOURCE_LEN); 6067a982d89SJeremy L. Thompson strncat(lookupname, type, CEED_MAX_RESOURCE_LEN); 6077a982d89SJeremy L. Thompson strncat(lookupname, fname, CEED_MAX_RESOURCE_LEN); 6087a982d89SJeremy L. Thompson 6097a982d89SJeremy L. Thompson // Find and use offset 6107a982d89SJeremy L. Thompson for (CeedInt i = 0; ceed->foffsets[i].fname; i++) 6117a982d89SJeremy L. Thompson if (!strcmp(ceed->foffsets[i].fname, lookupname)) { 6127a982d89SJeremy L. Thompson size_t offset = ceed->foffsets[i].offset; 6137a982d89SJeremy L. Thompson int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD* 6147a982d89SJeremy L. Thompson *fpointer = f; 6157a982d89SJeremy L. Thompson return 0; 6167a982d89SJeremy L. Thompson } 6177a982d89SJeremy L. Thompson 6187a982d89SJeremy L. Thompson // LCOV_EXCL_START 6197a982d89SJeremy L. Thompson return CeedError(ceed, 1, "Requested function '%s' was not found for CEED " 6207a982d89SJeremy L. Thompson "object '%s'", fname, type); 6217a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 6227a982d89SJeremy L. Thompson } 6237a982d89SJeremy L. Thompson 6247a982d89SJeremy L. Thompson /** 6257a982d89SJeremy L. Thompson @brief Retrieve backend data for a Ceed context 6267a982d89SJeremy L. Thompson 6277a982d89SJeremy L. Thompson @param ceed Ceed context to retrieve data of 6287a982d89SJeremy L. Thompson @param[out] data Address to save data to 6297a982d89SJeremy L. Thompson 6307a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6317a982d89SJeremy L. Thompson 6327a982d89SJeremy L. Thompson @ref Backend 6337a982d89SJeremy L. Thompson **/ 6347a982d89SJeremy L. Thompson int CeedGetData(Ceed ceed, void **data) { 6357a982d89SJeremy L. Thompson *data = ceed->data; 6367a982d89SJeremy L. Thompson return 0; 6377a982d89SJeremy L. Thompson } 6387a982d89SJeremy L. Thompson 6397a982d89SJeremy L. Thompson /** 6407a982d89SJeremy L. Thompson @brief Set backend data for a Ceed context 6417a982d89SJeremy L. Thompson 6427a982d89SJeremy L. Thompson @param ceed Ceed context to set data of 6437a982d89SJeremy L. Thompson @param data Address of data to set 6447a982d89SJeremy L. Thompson 6457a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6467a982d89SJeremy L. Thompson 6477a982d89SJeremy L. Thompson @ref Backend 6487a982d89SJeremy L. Thompson **/ 6497a982d89SJeremy L. Thompson int CeedSetData(Ceed ceed, void **data) { 6507a982d89SJeremy L. Thompson ceed->data = *data; 6517a982d89SJeremy L. Thompson return 0; 6527a982d89SJeremy L. Thompson } 6537a982d89SJeremy L. Thompson 6547a982d89SJeremy L. Thompson /// @} 6557a982d89SJeremy L. Thompson 6567a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6577a982d89SJeremy L. Thompson /// Ceed Public API 6587a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6597a982d89SJeremy L. Thompson /// @addtogroup CeedUser 6607a982d89SJeremy L. Thompson /// @{ 6617a982d89SJeremy L. Thompson 6627a982d89SJeremy L. Thompson /** 663d79b80ecSjeremylt @brief Initialize a \ref Ceed context to use the specified resource. 664b11c1e72Sjeremylt 665b11c1e72Sjeremylt @param resource Resource to use, e.g., "/cpu/self" 666b11c1e72Sjeremylt @param ceed The library context 667b11c1e72Sjeremylt @sa CeedRegister() CeedDestroy() 668b11c1e72Sjeremylt 669b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 670dfdf5a53Sjeremylt 6717a982d89SJeremy L. Thompson @ref User 672b11c1e72Sjeremylt **/ 673d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) { 674d7b241e6Sjeremylt int ierr; 675aedaa0e5Sjeremylt size_t matchlen = 0, matchidx = UINT_MAX, matchpriority = UINT_MAX, priority; 676d7b241e6Sjeremylt 677fe2413ffSjeremylt // Find matching backend 6781d102b48SJeremy L Thompson if (!resource) 67913873f79Sjeremylt // LCOV_EXCL_START 6801d102b48SJeremy L Thompson return CeedError(NULL, 1, "No resource provided"); 68113873f79Sjeremylt // LCOV_EXCL_STOP 68213873f79Sjeremylt 683d7b241e6Sjeremylt for (size_t i=0; i<num_backends; i++) { 684d7b241e6Sjeremylt size_t n; 685d7b241e6Sjeremylt const char *prefix = backends[i].prefix; 686d7b241e6Sjeremylt for (n = 0; prefix[n] && prefix[n] == resource[n]; n++) {} 687d7b241e6Sjeremylt priority = backends[i].priority; 688d7b241e6Sjeremylt if (n > matchlen || (n == matchlen && matchpriority > priority)) { 689d7b241e6Sjeremylt matchlen = n; 690d7b241e6Sjeremylt matchpriority = priority; 691d7b241e6Sjeremylt matchidx = i; 692d7b241e6Sjeremylt } 693d7b241e6Sjeremylt } 6941d102b48SJeremy L Thompson if (!matchlen) 69513873f79Sjeremylt // LCOV_EXCL_START 6968079b1e4SJed Brown return CeedError(NULL, 1, "No suitable backend: %s", resource); 69713873f79Sjeremylt // LCOV_EXCL_STOP 698fe2413ffSjeremylt 699fe2413ffSjeremylt // Setup Ceed 700d7b241e6Sjeremylt ierr = CeedCalloc(1, ceed); CeedChk(ierr); 701bc81ce41Sjeremylt const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 7021d102b48SJeremy L Thompson if (!ceed_error_handler) 7031d102b48SJeremy L Thompson ceed_error_handler = "abort"; 704bc81ce41Sjeremylt if (!strcmp(ceed_error_handler, "exit")) 70556e866f4SJed Brown (*ceed)->Error = CeedErrorExit; 70656e866f4SJed Brown else 707d7b241e6Sjeremylt (*ceed)->Error = CeedErrorAbort; 708d7b241e6Sjeremylt (*ceed)->refcount = 1; 709d7b241e6Sjeremylt (*ceed)->data = NULL; 710fe2413ffSjeremylt 711fe2413ffSjeremylt // Set lookup table 7126e79d475Sjeremylt foffset foffsets[] = { 7136e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, Error), 7146e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType), 7156e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, Destroy), 716f8902d9eSjeremylt CEED_FTABLE_ENTRY(Ceed, VectorCreate), 7176e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate), 7186e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked), 7196e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1), 7206e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, BasisCreateH1), 7216e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, TensorContractCreate), 7226e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, QFunctionCreate), 7236e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, OperatorCreate), 7246e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate), 7256e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, SetArray), 7266e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, SetValue), 7276e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, GetArray), 7286e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, GetArrayRead), 7296e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, RestoreArray), 7306e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead), 731547d9b97Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Norm), 7326e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Destroy), 7336e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, Apply), 7346e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock), 735bd33150aSjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets), 7366e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy), 7376e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedBasis, Apply), 7386e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedBasis, Destroy), 7396e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedTensorContract, Apply), 7406e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedTensorContract, Destroy), 7416e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, Apply), 7426e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, Destroy), 74380ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction), 74480ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal), 7459e9210b8SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal), 74680ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal), 7479e9210b8SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal), 748713f43c3Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse), 7496e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, Apply), 750250756a7Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite), 751cae8b89aSjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd), 752250756a7Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite), 7536e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian), 7546e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, Destroy), 7556e79d475Sjeremylt {NULL, 0} // End of lookup table - used in SetBackendFunction loop 7561dfeef1dSjeremylt }; 757fe2413ffSjeremylt 7586e79d475Sjeremylt ierr = CeedCalloc(sizeof(foffsets), &(*ceed)->foffsets); CeedChk(ierr); 7596e79d475Sjeremylt memcpy((*ceed)->foffsets, foffsets, sizeof(foffsets)); 760fe2413ffSjeremylt 7615107b09fSJeremy L Thompson // Set fallback for advanced CeedOperator functions 7625107b09fSJeremy L Thompson const char fallbackresource[] = "/cpu/self/ref/serial"; 7635107b09fSJeremy L Thompson ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource); 7645107b09fSJeremy L Thompson CeedChk(ierr); 7655107b09fSJeremy L Thompson 766*60f9e2d6SJeremy L Thompson // Record env variables CEED_DEBUG or DBG 767*60f9e2d6SJeremy L Thompson (*ceed)->debug = !!getenv("CEED_DEBUG") || !!getenv("DBG"); 768*60f9e2d6SJeremy L Thompson 769fe2413ffSjeremylt // Backend specific setup 770d7b241e6Sjeremylt ierr = backends[matchidx].init(resource, *ceed); CeedChk(ierr); 771fe2413ffSjeremylt 772e07206deSjeremylt // Copy resource prefix, if backend setup sucessful 773e07206deSjeremylt size_t len = strlen(backends[matchidx].prefix); 774e07206deSjeremylt char *tmp; 775e07206deSjeremylt ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 776e07206deSjeremylt memcpy(tmp, backends[matchidx].prefix, len+1); 777e07206deSjeremylt (*ceed)->resource = tmp; 778e07206deSjeremylt 779d7b241e6Sjeremylt return 0; 780d7b241e6Sjeremylt } 781d7b241e6Sjeremylt 782d7b241e6Sjeremylt /** 7837a982d89SJeremy L. Thompson @brief Get the full resource name for a Ceed context 7842f86a920SJeremy L Thompson 7857a982d89SJeremy L. Thompson @param ceed Ceed context to get resource name of 7867a982d89SJeremy L. Thompson @param[out] resource Variable to store resource name 7872f86a920SJeremy L Thompson 7882f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 7892f86a920SJeremy L Thompson 7907a982d89SJeremy L. Thompson @ref User 7915107b09fSJeremy L Thompson **/ 7925107b09fSJeremy L Thompson 7937a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) { 7947a982d89SJeremy L. Thompson *resource = (const char *)ceed->resource; 7955107b09fSJeremy L Thompson return 0; 7965107b09fSJeremy L Thompson } 7975107b09fSJeremy L Thompson 7985107b09fSJeremy L Thompson /** 799d79b80ecSjeremylt @brief Return Ceed context preferred memory type 800c907536fSjeremylt 801d79b80ecSjeremylt @param ceed Ceed context to get preferred memory type of 802288c0443SJeremy L Thompson @param[out] type Address to save preferred memory type to 803c907536fSjeremylt 804c907536fSjeremylt @return An error code: 0 - success, otherwise - failure 805c907536fSjeremylt 8067a982d89SJeremy L. Thompson @ref User 807c907536fSjeremylt **/ 808c907536fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type) { 809c907536fSjeremylt int ierr; 810c263cd57Sjeremylt 811c907536fSjeremylt if (ceed->GetPreferredMemType) { 812c907536fSjeremylt ierr = ceed->GetPreferredMemType(type); CeedChk(ierr); 813c907536fSjeremylt } else { 814c263cd57Sjeremylt Ceed delegate; 815c263cd57Sjeremylt ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr); 816c263cd57Sjeremylt 817c263cd57Sjeremylt if (delegate) { 818c263cd57Sjeremylt ierr = CeedGetPreferredMemType(delegate, type); CeedChk(ierr); 819c263cd57Sjeremylt } else { 820c907536fSjeremylt *type = CEED_MEM_HOST; 821c907536fSjeremylt } 822c263cd57Sjeremylt } 823c907536fSjeremylt 824c907536fSjeremylt return 0; 825c907536fSjeremylt } 826c907536fSjeremylt 827c907536fSjeremylt /** 8289525855cSJeremy L Thompson @brief Get deterministic status of Ceed 8299525855cSJeremy L Thompson 8309525855cSJeremy L Thompson @param[in] ceed Ceed 8319525855cSJeremy L Thompson @param[out] isDeterministic Variable to store deterministic status 8329525855cSJeremy L Thompson 8339525855cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 8349525855cSJeremy L Thompson 8359525855cSJeremy L Thompson @ref User 8369525855cSJeremy L Thompson **/ 8379525855cSJeremy L Thompson int CeedIsDeterministic(Ceed ceed, bool *isDeterministic) { 8389525855cSJeremy L Thompson *isDeterministic = ceed->isDeterministic; 8399525855cSJeremy L Thompson return 0; 8409525855cSJeremy L Thompson } 8419525855cSJeremy L Thompson 8429525855cSJeremy L Thompson /** 8430a0da059Sjeremylt @brief View a Ceed 8440a0da059Sjeremylt 8450a0da059Sjeremylt @param[in] ceed Ceed to view 8460a0da059Sjeremylt @param[in] stream Filestream to write to 8470a0da059Sjeremylt 8480a0da059Sjeremylt @return An error code: 0 - success, otherwise - failure 8490a0da059Sjeremylt 8500a0da059Sjeremylt @ref User 8510a0da059Sjeremylt **/ 8520a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) { 8530a0da059Sjeremylt int ierr; 8540a0da059Sjeremylt CeedMemType memtype; 8550a0da059Sjeremylt 8560a0da059Sjeremylt ierr = CeedGetPreferredMemType(ceed, &memtype); CeedChk(ierr); 8570a0da059Sjeremylt 8580a0da059Sjeremylt fprintf(stream, "Ceed\n" 8590a0da059Sjeremylt " Ceed Resource: %s\n" 8600a0da059Sjeremylt " Preferred MemType: %s\n", 8610a0da059Sjeremylt ceed->resource, CeedMemTypes[memtype]); 8620a0da059Sjeremylt 8630a0da059Sjeremylt return 0; 8640a0da059Sjeremylt } 8650a0da059Sjeremylt 8660a0da059Sjeremylt /** 867b11c1e72Sjeremylt @brief Destroy a Ceed context 868d7b241e6Sjeremylt 869d7b241e6Sjeremylt @param ceed Address of Ceed context to destroy 870b11c1e72Sjeremylt 871b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 872dfdf5a53Sjeremylt 8737a982d89SJeremy L. Thompson @ref User 874b11c1e72Sjeremylt **/ 875d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) { 876d7b241e6Sjeremylt int ierr; 8771d102b48SJeremy L Thompson if (!*ceed || --(*ceed)->refcount > 0) 8781d102b48SJeremy L Thompson return 0; 8790ace9bf2Sjeremylt 8805fe0d4faSjeremylt if ((*ceed)->delegate) { 8815fe0d4faSjeremylt ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr); 8825fe0d4faSjeremylt } 8830ace9bf2Sjeremylt 884aefd8378Sjeremylt if ((*ceed)->objdelegatecount > 0) { 885aefd8378Sjeremylt for (int i=0; i<(*ceed)->objdelegatecount; i++) { 886aefd8378Sjeremylt ierr = CeedDestroy(&((*ceed)->objdelegates[i].delegate)); CeedChk(ierr); 887aefd8378Sjeremylt ierr = CeedFree(&(*ceed)->objdelegates[i].objname); CeedChk(ierr); 888aefd8378Sjeremylt } 889aefd8378Sjeremylt ierr = CeedFree(&(*ceed)->objdelegates); CeedChk(ierr); 890aefd8378Sjeremylt } 8910ace9bf2Sjeremylt 892d7b241e6Sjeremylt if ((*ceed)->Destroy) { 893d7b241e6Sjeremylt ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr); 894d7b241e6Sjeremylt } 8950ace9bf2Sjeremylt 8966e79d475Sjeremylt ierr = CeedFree(&(*ceed)->foffsets); CeedChk(ierr); 897e07206deSjeremylt ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr); 8985107b09fSJeremy L Thompson ierr = CeedDestroy(&(*ceed)->opfallbackceed); CeedChk(ierr); 8995107b09fSJeremy L Thompson ierr = CeedFree(&(*ceed)->opfallbackresource); CeedChk(ierr); 900d7b241e6Sjeremylt ierr = CeedFree(ceed); CeedChk(ierr); 901d7b241e6Sjeremylt return 0; 902d7b241e6Sjeremylt } 903d7b241e6Sjeremylt 9047a982d89SJeremy L. Thompson /** 9057a982d89SJeremy L. Thompson @brief Error handling implementation; use \ref CeedError instead. 9067a982d89SJeremy L. Thompson 9077a982d89SJeremy L. Thompson @ref Developer 9087a982d89SJeremy L. Thompson **/ 9097a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func, 9107a982d89SJeremy L. Thompson int ecode, const char *format, ...) { 9117a982d89SJeremy L. Thompson va_list args; 9127a982d89SJeremy L. Thompson int retval; 9137a982d89SJeremy L. Thompson va_start(args, format); 9147a982d89SJeremy L. Thompson if (ceed) { 9157a982d89SJeremy L. Thompson retval = ceed->Error(ceed, filename, lineno, func, ecode, format, args); 9167a982d89SJeremy L. Thompson } else { 9177a982d89SJeremy L. Thompson // This function doesn't actually return 918b0d62198Sjeremylt // LCOV_EXCL_START 9197a982d89SJeremy L. Thompson retval = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, args); 9207a982d89SJeremy L. Thompson } 9217a982d89SJeremy L. Thompson va_end(args); 9227a982d89SJeremy L. Thompson return retval; 923b0d62198Sjeremylt // LCOV_EXCL_STOP 9247a982d89SJeremy L. Thompson } 9257a982d89SJeremy L. Thompson 926d7b241e6Sjeremylt /// @} 927