13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3d7b241e6Sjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5d7b241e6Sjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7d7b241e6Sjeremylt 8d7b241e6Sjeremylt #define _POSIX_C_SOURCE 200112 93d576824SJeremy L Thompson #include <ceed-impl.h> 10*2b730f8bSJeremy L Thompson #include <ceed/backend.h> 11*2b730f8bSJeremy L Thompson #include <ceed/ceed.h> 12aedaa0e5Sjeremylt #include <limits.h> 13d7b241e6Sjeremylt #include <stdarg.h> 146e79d475Sjeremylt #include <stddef.h> 15d7b241e6Sjeremylt #include <stdio.h> 16d7b241e6Sjeremylt #include <stdlib.h> 17d7b241e6Sjeremylt #include <string.h> 18d7b241e6Sjeremylt 19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP 20d7b241e6Sjeremylt static CeedRequest ceed_request_immediate; 21d7b241e6Sjeremylt static CeedRequest ceed_request_ordered; 22d7b241e6Sjeremylt 23d7b241e6Sjeremylt static struct { 24d7b241e6Sjeremylt char prefix[CEED_MAX_RESOURCE_LEN]; 25d7b241e6Sjeremylt int (*init)(const char *resource, Ceed f); 26d7b241e6Sjeremylt unsigned int priority; 27d7b241e6Sjeremylt } backends[32]; 28d7b241e6Sjeremylt static size_t num_backends; 29fe2413ffSjeremylt 306e79d475Sjeremylt #define CEED_FTABLE_ENTRY(class, method) \ 316e79d475Sjeremylt { #class #method, offsetof(struct class##_private, method) } 32d7b241e6Sjeremylt /// @endcond 33d7b241e6Sjeremylt 34d7b241e6Sjeremylt /// @file 35d7b241e6Sjeremylt /// Implementation of core components of Ceed library 367a982d89SJeremy L. Thompson 377a982d89SJeremy L. Thompson /// @addtogroup CeedUser 38d7b241e6Sjeremylt /// @{ 39d7b241e6Sjeremylt 40dfdf5a53Sjeremylt /** 41dfdf5a53Sjeremylt @brief Request immediate completion 42dfdf5a53Sjeremylt 43dfdf5a53Sjeremylt This predefined constant is passed as the \ref CeedRequest argument to 44dfdf5a53Sjeremylt interfaces when the caller wishes for the operation to be performed 45dfdf5a53Sjeremylt immediately. The code 46dfdf5a53Sjeremylt 47dfdf5a53Sjeremylt @code 48dfdf5a53Sjeremylt CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE); 49dfdf5a53Sjeremylt @endcode 50dfdf5a53Sjeremylt 51dfdf5a53Sjeremylt is semantically equivalent to 52dfdf5a53Sjeremylt 53dfdf5a53Sjeremylt @code 54dfdf5a53Sjeremylt CeedRequest request; 55dfdf5a53Sjeremylt CeedOperatorApply(op, ..., &request); 56dfdf5a53Sjeremylt CeedRequestWait(&request); 57dfdf5a53Sjeremylt @endcode 58dfdf5a53Sjeremylt 59dfdf5a53Sjeremylt @sa CEED_REQUEST_ORDERED 60dfdf5a53Sjeremylt **/ 61d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate; 62d7b241e6Sjeremylt 63d7b241e6Sjeremylt /** 64b11c1e72Sjeremylt @brief Request ordered completion 65d7b241e6Sjeremylt 66d7b241e6Sjeremylt This predefined constant is passed as the \ref CeedRequest argument to 67d7b241e6Sjeremylt interfaces when the caller wishes for the operation to be completed in the 68d7b241e6Sjeremylt order that it is submitted to the device. It is typically used in a construct 69d7b241e6Sjeremylt such as 70d7b241e6Sjeremylt 71d7b241e6Sjeremylt @code 72d7b241e6Sjeremylt CeedRequest request; 73d7b241e6Sjeremylt CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED); 74d7b241e6Sjeremylt CeedOperatorApply(op2, ..., &request); 75d7b241e6Sjeremylt // other optional work 768b2d6f4aSMatthew Knepley CeedRequestWait(&request); 77d7b241e6Sjeremylt @endcode 78d7b241e6Sjeremylt 79d7b241e6Sjeremylt which allows the sequence to complete asynchronously but does not start 80d7b241e6Sjeremylt `op2` until `op1` has completed. 81d7b241e6Sjeremylt 82288c0443SJeremy L Thompson @todo The current implementation is overly strict, offering equivalent 834cc79fe7SJed Brown semantics to @ref CEED_REQUEST_IMMEDIATE. 84d7b241e6Sjeremylt 85d7b241e6Sjeremylt @sa CEED_REQUEST_IMMEDIATE 86d7b241e6Sjeremylt */ 87d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered; 88d7b241e6Sjeremylt 89b11c1e72Sjeremylt /** 907a982d89SJeremy L. Thompson @brief Wait for a CeedRequest to complete. 91dfdf5a53Sjeremylt 927a982d89SJeremy L. Thompson Calling CeedRequestWait on a NULL request is a no-op. 937a982d89SJeremy L. Thompson 947a982d89SJeremy L. Thompson @param req Address of CeedRequest to wait for; zeroed on completion. 957a982d89SJeremy L. Thompson 967a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 977a982d89SJeremy L. Thompson 987a982d89SJeremy L. Thompson @ref User 99b11c1e72Sjeremylt **/ 1007a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) { 101*2b730f8bSJeremy L Thompson if (!*req) return CEED_ERROR_SUCCESS; 102*2b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_UNSUPPORTED, "CeedRequestWait not implemented"); 103683faae0SJed Brown } 1047a982d89SJeremy L. Thompson 1057a982d89SJeremy L. Thompson /// @} 1067a982d89SJeremy L. Thompson 1077a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1087a982d89SJeremy L. Thompson /// Ceed Library Internal Functions 1097a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1107a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper 1117a982d89SJeremy L. Thompson /// @{ 112d7b241e6Sjeremylt 1136a406739SJeremy L Thompson /** 1146a406739SJeremy L Thompson @brief Register a Ceed backend internally. 1156a406739SJeremy L Thompson Note: Backends should call `CeedRegister` instead. 1166a406739SJeremy L Thompson 1176a406739SJeremy L Thompson @param prefix Prefix of resources for this backend to respond to. For 1186a406739SJeremy L Thompson example, the reference backend responds to "/cpu/self". 1196a406739SJeremy L Thompson @param init Initialization function called by CeedInit() when the backend 1206a406739SJeremy L Thompson is selected to drive the requested resource. 1216a406739SJeremy L Thompson @param priority Integer priority. Lower values are preferred in case the 1226a406739SJeremy L Thompson resource requested by CeedInit() has non-unique best prefix 1236a406739SJeremy L Thompson match. 1246a406739SJeremy L Thompson 1256a406739SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1266a406739SJeremy L Thompson 1276a406739SJeremy L Thompson @ref Developer 1286a406739SJeremy L Thompson **/ 129*2b730f8bSJeremy L Thompson int CeedRegisterImpl(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) { 1306a406739SJeremy L Thompson if (num_backends >= sizeof(backends) / sizeof(backends[0])) 1316a406739SJeremy L Thompson // LCOV_EXCL_START 1326a406739SJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "Too many backends"); 1336a406739SJeremy L Thompson // LCOV_EXCL_STOP 1346a406739SJeremy L Thompson 1356a406739SJeremy L Thompson strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN); 1366a406739SJeremy L Thompson backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN - 1] = 0; 1376a406739SJeremy L Thompson backends[num_backends].init = init; 1386a406739SJeremy L Thompson backends[num_backends].priority = priority; 1396a406739SJeremy L Thompson num_backends++; 1406a406739SJeremy L Thompson return CEED_ERROR_SUCCESS; 1416a406739SJeremy L Thompson } 1426a406739SJeremy L Thompson 1437a982d89SJeremy L. Thompson /// @} 144d7b241e6Sjeremylt 1457a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1467a982d89SJeremy L. Thompson /// Ceed Backend API 1477a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1487a982d89SJeremy L. Thompson /// @addtogroup CeedBackend 1497a982d89SJeremy L. Thompson /// @{ 150d7b241e6Sjeremylt 151b11c1e72Sjeremylt /** 1523f21f6b1SJeremy L Thompson @brief Return value of CEED_DEBUG environment variable 15360f9e2d6SJeremy L Thompson 15460f9e2d6SJeremy L Thompson @param ceed Ceed context 15560f9e2d6SJeremy L Thompson 1563f21f6b1SJeremy L Thompson @return boolean value: true - debugging mode enabled 1573f21f6b1SJeremy L Thompson false - debugging mode disabled 15860f9e2d6SJeremy L Thompson 15960f9e2d6SJeremy L Thompson @ref Backend 16060f9e2d6SJeremy L Thompson **/ 161fc6bbcedSJeremy L Thompson // LCOV_EXCL_START 162*2b730f8bSJeremy L Thompson bool CeedDebugFlag(const Ceed ceed) { return ceed->is_debug; } 163fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP 16460f9e2d6SJeremy L Thompson 16560f9e2d6SJeremy L Thompson /** 1663f21f6b1SJeremy L Thompson @brief Return value of CEED_DEBUG environment variable 16760f9e2d6SJeremy L Thompson 1683f21f6b1SJeremy L Thompson @return boolean value: true - debugging mode enabled 1693f21f6b1SJeremy L Thompson false - debugging mode disabled 1703f21f6b1SJeremy L Thompson 1713f21f6b1SJeremy L Thompson @ref Backend 1723f21f6b1SJeremy L Thompson **/ 1733f21f6b1SJeremy L Thompson // LCOV_EXCL_START 174*2b730f8bSJeremy L Thompson bool CeedDebugFlagEnv(void) { return !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG"); } 1753f21f6b1SJeremy L Thompson // LCOV_EXCL_STOP 1763f21f6b1SJeremy L Thompson 1773f21f6b1SJeremy L Thompson /** 1783f21f6b1SJeremy L Thompson @brief Print debugging information in color 1793f21f6b1SJeremy L Thompson 18060f9e2d6SJeremy L Thompson @param color Color to print 18160f9e2d6SJeremy L Thompson @param format Printing format 18260f9e2d6SJeremy L Thompson 18360f9e2d6SJeremy L Thompson @ref Backend 18460f9e2d6SJeremy L Thompson **/ 185fc6bbcedSJeremy L Thompson // LCOV_EXCL_START 1863f21f6b1SJeremy L Thompson void CeedDebugImpl256(const unsigned char color, const char *format, ...) { 18760f9e2d6SJeremy L Thompson va_list args; 18860f9e2d6SJeremy L Thompson va_start(args, format); 18960f9e2d6SJeremy L Thompson fflush(stdout); 190*2b730f8bSJeremy L Thompson if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[38;5;%dm", color); 19160f9e2d6SJeremy L Thompson vfprintf(stdout, format, args); 192*2b730f8bSJeremy L Thompson if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[m"); 19360f9e2d6SJeremy L Thompson fprintf(stdout, "\n"); 19460f9e2d6SJeremy L Thompson fflush(stdout); 19560f9e2d6SJeremy L Thompson va_end(args); 19660f9e2d6SJeremy L Thompson } 197fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP 19860f9e2d6SJeremy L Thompson 19960f9e2d6SJeremy L Thompson /** 200b11c1e72Sjeremylt @brief Allocate an array on the host; use CeedMalloc() 201b11c1e72Sjeremylt 202b11c1e72Sjeremylt Memory usage can be tracked by the library. This ensures sufficient 203b11c1e72Sjeremylt alignment for vectorization and should be used for large allocations. 204b11c1e72Sjeremylt 205b11c1e72Sjeremylt @param n Number of units to allocate 206b11c1e72Sjeremylt @param unit Size of each unit 207b11c1e72Sjeremylt @param p Address of pointer to hold the result. 208b11c1e72Sjeremylt 209b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 210b11c1e72Sjeremylt 211b11c1e72Sjeremylt @sa CeedFree() 212dfdf5a53Sjeremylt 2137a982d89SJeremy L. Thompson @ref Backend 214b11c1e72Sjeremylt **/ 215d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) { 216d7b241e6Sjeremylt int ierr = posix_memalign((void **)p, CEED_ALIGN, n * unit); 217*2b730f8bSJeremy L Thompson if (ierr) { 218c042f62fSJeremy L Thompson // LCOV_EXCL_START 219*2b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "posix_memalign failed to allocate %zd members of size %zd\n", n, unit); 220c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 221*2b730f8bSJeremy L Thompson } 222e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 223d7b241e6Sjeremylt } 224d7b241e6Sjeremylt 225b11c1e72Sjeremylt /** 226b11c1e72Sjeremylt @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc() 227b11c1e72Sjeremylt 228b11c1e72Sjeremylt Memory usage can be tracked by the library. 229b11c1e72Sjeremylt 230b11c1e72Sjeremylt @param n Number of units to allocate 231b11c1e72Sjeremylt @param unit Size of each unit 232b11c1e72Sjeremylt @param p Address of pointer to hold the result. 233b11c1e72Sjeremylt 234b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 235b11c1e72Sjeremylt 236b11c1e72Sjeremylt @sa CeedFree() 237dfdf5a53Sjeremylt 2387a982d89SJeremy L. Thompson @ref Backend 239b11c1e72Sjeremylt **/ 240d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) { 241d7b241e6Sjeremylt *(void **)p = calloc(n, unit); 242*2b730f8bSJeremy L Thompson if (n && unit && !*(void **)p) { 243c042f62fSJeremy L Thompson // LCOV_EXCL_START 244*2b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "calloc failed to allocate %zd members of size %zd\n", n, unit); 245c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 246*2b730f8bSJeremy L Thompson } 247e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 248d7b241e6Sjeremylt } 249d7b241e6Sjeremylt 250b11c1e72Sjeremylt /** 251b11c1e72Sjeremylt @brief Reallocate an array on the host; use CeedRealloc() 252b11c1e72Sjeremylt 253b11c1e72Sjeremylt Memory usage can be tracked by the library. 254b11c1e72Sjeremylt 255b11c1e72Sjeremylt @param n Number of units to allocate 256b11c1e72Sjeremylt @param unit Size of each unit 257b11c1e72Sjeremylt @param p Address of pointer to hold the result. 258b11c1e72Sjeremylt 259b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 260b11c1e72Sjeremylt 261b11c1e72Sjeremylt @sa CeedFree() 262dfdf5a53Sjeremylt 2637a982d89SJeremy L. Thompson @ref Backend 264b11c1e72Sjeremylt **/ 265d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) { 266d7b241e6Sjeremylt *(void **)p = realloc(*(void **)p, n * unit); 267*2b730f8bSJeremy L Thompson if (n && unit && !*(void **)p) { 268c042f62fSJeremy L Thompson // LCOV_EXCL_START 269*2b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "realloc failed to allocate %zd members of size %zd\n", n, unit); 270c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 271*2b730f8bSJeremy L Thompson } 272e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 273d7b241e6Sjeremylt } 274d7b241e6Sjeremylt 275f7e22acaSJeremy L Thompson /** 276f7e22acaSJeremy L Thompson @brief Allocate a cleared string buffer on the host 277f7e22acaSJeremy L Thompson 278f7e22acaSJeremy L Thompson Memory usage can be tracked by the library. 279f7e22acaSJeremy L Thompson 280f7e22acaSJeremy L Thompson @param source Pointer to string to be copied 281f7e22acaSJeremy L Thompson @param copy Pointer to variable to hold newly allocated string copy 282f7e22acaSJeremy L Thompson 283f7e22acaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 284f7e22acaSJeremy L Thompson 285f7e22acaSJeremy L Thompson @sa CeedFree() 286f7e22acaSJeremy L Thompson 287f7e22acaSJeremy L Thompson @ref Backend 288f7e22acaSJeremy L Thompson **/ 289f7e22acaSJeremy L Thompson int CeedStringAllocCopy(const char *source, char **copy) { 290f7e22acaSJeremy L Thompson size_t len = strlen(source); 291*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(len + 1, copy)); 292d602d780SJeremy L Thompson memcpy(*copy, source, len); 293f7e22acaSJeremy L Thompson return CEED_ERROR_SUCCESS; 294f7e22acaSJeremy L Thompson } 295f7e22acaSJeremy L Thompson 29634138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc() 29734138859Sjeremylt 29834138859Sjeremylt @param p address of pointer to memory. This argument is of type void* to 29934138859Sjeremylt avoid needing a cast, but is the address of the pointer (which is 30034138859Sjeremylt zeroed) rather than the pointer. 30134138859Sjeremylt **/ 302d7b241e6Sjeremylt int CeedFree(void *p) { 303d7b241e6Sjeremylt free(*(void **)p); 304d7b241e6Sjeremylt *(void **)p = NULL; 305e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 306d7b241e6Sjeremylt } 307d7b241e6Sjeremylt 308d7b241e6Sjeremylt /** 3097a982d89SJeremy L. Thompson @brief Register a Ceed backend 310d7b241e6Sjeremylt 3117a982d89SJeremy L. Thompson @param prefix Prefix of resources for this backend to respond to. For 3127a982d89SJeremy L. Thompson example, the reference backend responds to "/cpu/self". 3137a982d89SJeremy L. Thompson @param init Initialization function called by CeedInit() when the backend 3147a982d89SJeremy L. Thompson is selected to drive the requested resource. 3157a982d89SJeremy L. Thompson @param priority Integer priority. Lower values are preferred in case the 3167a982d89SJeremy L. Thompson resource requested by CeedInit() has non-unique best prefix 3177a982d89SJeremy L. Thompson match. 318b11c1e72Sjeremylt 319b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 320dfdf5a53Sjeremylt 3217a982d89SJeremy L. Thompson @ref Backend 322b11c1e72Sjeremylt **/ 323*2b730f8bSJeremy L Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) { 32410243053SJeremy L Thompson CeedDebugEnv("Backend Register: %s", prefix); 3256a406739SJeremy L Thompson CeedRegisterImpl(prefix, init, priority); 326e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 327d7b241e6Sjeremylt } 328d7b241e6Sjeremylt 329b11c1e72Sjeremylt /** 33060f9e2d6SJeremy L Thompson @brief Return debugging status flag 33160f9e2d6SJeremy L Thompson 33260f9e2d6SJeremy L Thompson @param ceed Ceed context to get debugging flag 333d1d35e2fSjeremylt @param is_debug Variable to store debugging flag 33460f9e2d6SJeremy L Thompson 33560f9e2d6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 33660f9e2d6SJeremy L Thompson 337d1d35e2fSjeremylt @ref Backend 33860f9e2d6SJeremy L Thompson **/ 339d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) { 3403f21f6b1SJeremy L Thompson *is_debug = ceed->is_debug; 341e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 34260f9e2d6SJeremy L Thompson } 34360f9e2d6SJeremy L Thompson 34460f9e2d6SJeremy L Thompson /** 3457a982d89SJeremy L. Thompson @brief Retrieve a parent Ceed context 3467a982d89SJeremy L. Thompson 3477a982d89SJeremy L. Thompson @param ceed Ceed context to retrieve parent of 3487a982d89SJeremy L. Thompson @param[out] parent Address to save the parent to 3497a982d89SJeremy L. Thompson 3507a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3517a982d89SJeremy L. Thompson 3527a982d89SJeremy L. Thompson @ref Backend 3537a982d89SJeremy L. Thompson **/ 3547a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) { 3557a982d89SJeremy L. Thompson if (ceed->parent) { 356*2b730f8bSJeremy L Thompson CeedCall(CeedGetParent(ceed->parent, parent)); 357e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3587a982d89SJeremy L. Thompson } 3597a982d89SJeremy L. Thompson *parent = ceed; 360e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3617a982d89SJeremy L. Thompson } 3627a982d89SJeremy L. Thompson 3637a982d89SJeremy L. Thompson /** 3647a982d89SJeremy L. Thompson @brief Retrieve a delegate Ceed context 3657a982d89SJeremy L. Thompson 3667a982d89SJeremy L. Thompson @param ceed Ceed context to retrieve delegate of 3677a982d89SJeremy L. Thompson @param[out] delegate Address to save the delegate to 3687a982d89SJeremy L. Thompson 3697a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3707a982d89SJeremy L. Thompson 3717a982d89SJeremy L. Thompson @ref Backend 3727a982d89SJeremy L. Thompson **/ 3737a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) { 3747a982d89SJeremy L. Thompson *delegate = ceed->delegate; 375e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3767a982d89SJeremy L. Thompson } 3777a982d89SJeremy L. Thompson 3787a982d89SJeremy L. Thompson /** 3797a982d89SJeremy L. Thompson @brief Set a delegate Ceed context 3807a982d89SJeremy L. Thompson 3817a982d89SJeremy L. Thompson This function allows a Ceed context to set a delegate Ceed context. All 3827a982d89SJeremy L. Thompson backend implementations default to the delegate Ceed context, unless 3837a982d89SJeremy L. Thompson overridden. 3847a982d89SJeremy L. Thompson 3857a982d89SJeremy L. Thompson @param ceed Ceed context to set delegate of 3867a982d89SJeremy L. Thompson @param[out] delegate Address to set the delegate to 3877a982d89SJeremy L. Thompson 3887a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3897a982d89SJeremy L. Thompson 3907a982d89SJeremy L. Thompson @ref Backend 3917a982d89SJeremy L. Thompson **/ 3927a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) { 3937a982d89SJeremy L. Thompson ceed->delegate = delegate; 3947a982d89SJeremy L. Thompson delegate->parent = ceed; 395e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3967a982d89SJeremy L. Thompson } 3977a982d89SJeremy L. Thompson 3987a982d89SJeremy L. Thompson /** 3997a982d89SJeremy L. Thompson @brief Retrieve a delegate Ceed context for a specific object type 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 403d1d35e2fSjeremylt @param[in] obj_name Name of the object type to retrieve delegate for 4047a982d89SJeremy L. Thompson 4057a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4067a982d89SJeremy L. Thompson 4077a982d89SJeremy L. Thompson @ref Backend 4087a982d89SJeremy L. Thompson **/ 409d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) { 4107a982d89SJeremy L. Thompson // Check for object delegate 411*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) { 412d1d35e2fSjeremylt if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) { 413d1d35e2fSjeremylt *delegate = ceed->obj_delegates->delegate; 414e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4157a982d89SJeremy L. Thompson } 416*2b730f8bSJeremy L Thompson } 4177a982d89SJeremy L. Thompson 4187a982d89SJeremy L. Thompson // Use default delegate if no object delegate 419*2b730f8bSJeremy L Thompson CeedCall(CeedGetDelegate(ceed, delegate)); 420e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4217a982d89SJeremy L. Thompson } 4227a982d89SJeremy L. Thompson 4237a982d89SJeremy L. Thompson /** 4247a982d89SJeremy L. Thompson @brief Set a delegate Ceed context for a specific object type 4257a982d89SJeremy L. Thompson 4267a982d89SJeremy L. Thompson This function allows a Ceed context to set a delegate Ceed context for a 4277a982d89SJeremy L. Thompson given type of Ceed object. All backend implementations default to the 4287a982d89SJeremy L. Thompson delegate Ceed context for this object. For example, 4297a982d89SJeremy L. Thompson CeedSetObjectDelegate(ceed, refceed, "Basis") 4307a982d89SJeremy L. Thompson uses refceed implementations for all CeedBasis backend functions. 4317a982d89SJeremy L. Thompson 4327a982d89SJeremy L. Thompson @param ceed Ceed context to set delegate of 4337a982d89SJeremy L. Thompson @param[out] delegate Address to set the delegate to 434d1d35e2fSjeremylt @param[in] obj_name Name of the object type to set delegate for 4357a982d89SJeremy L. Thompson 4367a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4377a982d89SJeremy L. Thompson 4387a982d89SJeremy L. Thompson @ref Backend 4397a982d89SJeremy L. Thompson **/ 440d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) { 441d1d35e2fSjeremylt CeedInt count = ceed->obj_delegate_count; 4427a982d89SJeremy L. Thompson 4437a982d89SJeremy L. Thompson // Malloc or Realloc 4447a982d89SJeremy L. Thompson if (count) { 445*2b730f8bSJeremy L Thompson CeedCall(CeedRealloc(count + 1, &ceed->obj_delegates)); 4467a982d89SJeremy L. Thompson } else { 447*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &ceed->obj_delegates)); 4487a982d89SJeremy L. Thompson } 449d1d35e2fSjeremylt ceed->obj_delegate_count++; 4507a982d89SJeremy L. Thompson 4517a982d89SJeremy L. Thompson // Set object delegate 452d1d35e2fSjeremylt ceed->obj_delegates[count].delegate = delegate; 453*2b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(obj_name, &ceed->obj_delegates[count].obj_name)); 4547a982d89SJeremy L. Thompson 4557a982d89SJeremy L. Thompson // Set delegate parent 4567a982d89SJeremy L. Thompson delegate->parent = ceed; 457e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4587a982d89SJeremy L. Thompson } 4597a982d89SJeremy L. Thompson 4607a982d89SJeremy L. Thompson /** 4617a982d89SJeremy L. Thompson @brief Get the fallback resource for CeedOperators 4627a982d89SJeremy L. Thompson 4637a982d89SJeremy L. Thompson @param ceed Ceed context 4647a982d89SJeremy L. Thompson @param[out] resource Variable to store fallback resource 4657a982d89SJeremy L. Thompson 4667a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4677a982d89SJeremy L. Thompson 4687a982d89SJeremy L. Thompson @ref Backend 4697a982d89SJeremy L. Thompson **/ 4707a982d89SJeremy L. Thompson 4717a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) { 472d1d35e2fSjeremylt *resource = (const char *)ceed->op_fallback_resource; 473e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4747a982d89SJeremy L. Thompson } 4757a982d89SJeremy L. Thompson 4767a982d89SJeremy L. Thompson /** 4778687e1d4SJeremy L Thompson @brief Get the fallback Ceed for CeedOperators 4788687e1d4SJeremy L Thompson 4798687e1d4SJeremy L Thompson @param ceed Ceed context 4808687e1d4SJeremy L Thompson @param[out] fallback_ceed Variable to store fallback Ceed 4818687e1d4SJeremy L Thompson 4828687e1d4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4838687e1d4SJeremy L Thompson 4848687e1d4SJeremy L Thompson @ref Backend 4858687e1d4SJeremy L Thompson **/ 4868687e1d4SJeremy L Thompson 4878687e1d4SJeremy L Thompson int CeedGetOperatorFallbackCeed(Ceed ceed, Ceed *fallback_ceed) { 488d04bbc78SJeremy L Thompson if (ceed->has_valid_op_fallback_resource) { 489d04bbc78SJeremy L Thompson CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); 490*2b730f8bSJeremy L Thompson CeedDebug(ceed, "Getting fallback from %s to %s\n", ceed->resource, ceed->op_fallback_resource); 491d04bbc78SJeremy L Thompson } 4928687e1d4SJeremy L Thompson 493d04bbc78SJeremy L Thompson // Create fallback Ceed if uninitalized 494d04bbc78SJeremy L Thompson if (!ceed->op_fallback_ceed && ceed->has_valid_op_fallback_resource) { 49513f886e9SJeremy L Thompson CeedDebug(ceed, "Creating fallback Ceed"); 496d04bbc78SJeremy L Thompson 4978687e1d4SJeremy L Thompson Ceed fallback_ceed; 498d04bbc78SJeremy L Thompson const char *fallback_resource; 499d04bbc78SJeremy L Thompson 500*2b730f8bSJeremy L Thompson CeedCall(CeedGetOperatorFallbackResource(ceed, &fallback_resource)); 501*2b730f8bSJeremy L Thompson CeedCall(CeedInit(fallback_resource, &fallback_ceed)); 5028687e1d4SJeremy L Thompson fallback_ceed->op_fallback_parent = ceed; 5038687e1d4SJeremy L Thompson fallback_ceed->Error = ceed->Error; 5048687e1d4SJeremy L Thompson ceed->op_fallback_ceed = fallback_ceed; 5058687e1d4SJeremy L Thompson } 5068687e1d4SJeremy L Thompson *fallback_ceed = ceed->op_fallback_ceed; 5078687e1d4SJeremy L Thompson 5088687e1d4SJeremy L Thompson return CEED_ERROR_SUCCESS; 5098687e1d4SJeremy L Thompson } 5108687e1d4SJeremy L Thompson 5118687e1d4SJeremy L Thompson /** 5127a982d89SJeremy L. Thompson @brief Set the fallback resource for CeedOperators. The current resource, if 5137a982d89SJeremy L. Thompson any, is freed by calling this function. This string is freed upon the 5147a982d89SJeremy L. Thompson destruction of the Ceed context. 5157a982d89SJeremy L. Thompson 5167a982d89SJeremy L. Thompson @param[out] ceed Ceed context 5177a982d89SJeremy L. Thompson @param resource Fallback resource to set 5187a982d89SJeremy L. Thompson 5197a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5207a982d89SJeremy L. Thompson 5217a982d89SJeremy L. Thompson @ref Backend 5227a982d89SJeremy L. Thompson **/ 5237a982d89SJeremy L. Thompson 5247a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) { 5257a982d89SJeremy L. Thompson // Free old 526*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&ceed->op_fallback_resource)); 5277a982d89SJeremy L. Thompson 5287a982d89SJeremy L. Thompson // Set new 529*2b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(resource, (char **)&ceed->op_fallback_resource)); 530d04bbc78SJeremy L Thompson 531d04bbc78SJeremy L Thompson // Check validity 532*2b730f8bSJeremy L Thompson ceed->has_valid_op_fallback_resource = ceed->op_fallback_resource && ceed->resource && strcmp(ceed->op_fallback_resource, ceed->resource); 533d04bbc78SJeremy L Thompson 534e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5357a982d89SJeremy L. Thompson } 5367a982d89SJeremy L. Thompson 5377a982d89SJeremy L. Thompson /** 5387a982d89SJeremy L. Thompson @brief Get the parent Ceed context associated with a fallback Ceed context 5397a982d89SJeremy L. Thompson for a CeedOperator 5407a982d89SJeremy L. Thompson 5417a982d89SJeremy L. Thompson @param ceed Ceed context 5427a982d89SJeremy L. Thompson @param[out] parent Variable to store parent Ceed context 5437a982d89SJeremy L. Thompson 5447a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5457a982d89SJeremy L. Thompson 5467a982d89SJeremy L. Thompson @ref Backend 5477a982d89SJeremy L. Thompson **/ 5487a982d89SJeremy L. Thompson 5497a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) { 550d1d35e2fSjeremylt *parent = ceed->op_fallback_parent; 551e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5527a982d89SJeremy L. Thompson } 5537a982d89SJeremy L. Thompson 5547a982d89SJeremy L. Thompson /** 5559525855cSJeremy L Thompson @brief Flag Ceed context as deterministic 5569525855cSJeremy L Thompson 5579525855cSJeremy L Thompson @param ceed Ceed to flag as deterministic 55896b902e2Sjeremylt @param[out] is_deterministic Deterministic status to set 5599525855cSJeremy L Thompson 5609525855cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5619525855cSJeremy L Thompson 5629525855cSJeremy L Thompson @ref Backend 5639525855cSJeremy L Thompson **/ 5649525855cSJeremy L Thompson 565d1d35e2fSjeremylt int CeedSetDeterministic(Ceed ceed, bool is_deterministic) { 566d1d35e2fSjeremylt ceed->is_deterministic = is_deterministic; 567e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5689525855cSJeremy L Thompson } 5699525855cSJeremy L Thompson 5709525855cSJeremy L Thompson /** 5717a982d89SJeremy L. Thompson @brief Set a backend function 5727a982d89SJeremy L. Thompson 5737a982d89SJeremy L. Thompson This function is used for a backend to set the function associated with 5747a982d89SJeremy L. Thompson the Ceed objects. For example, 5757a982d89SJeremy L. Thompson CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate) 5767a982d89SJeremy L. Thompson sets the backend implementation of 'CeedVectorCreate' and 5777a982d89SJeremy L. Thompson CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply) 5787a982d89SJeremy L. Thompson sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed' 5797a982d89SJeremy L. Thompson is not required for the object type ("Basis" vs "CeedBasis"). 5807a982d89SJeremy L. Thompson 5817a982d89SJeremy L. Thompson @param ceed Ceed context for error handling 5827a982d89SJeremy L. Thompson @param type Type of Ceed object to set function for 5837a982d89SJeremy L. Thompson @param[out] object Ceed object to set function for 584d1d35e2fSjeremylt @param func_name Name of function to set 5857a982d89SJeremy L. Thompson @param f Function to set 5867a982d89SJeremy L. Thompson 5877a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5887a982d89SJeremy L. Thompson 5897a982d89SJeremy L. Thompson @ref Backend 5907a982d89SJeremy L. Thompson **/ 591*2b730f8bSJeremy L Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object, const char *func_name, int (*f)()) { 592d1d35e2fSjeremylt char lookup_name[CEED_MAX_RESOURCE_LEN + 1] = ""; 5937a982d89SJeremy L. Thompson 5947a982d89SJeremy L. Thompson // Build lookup name 595*2b730f8bSJeremy L Thompson if (strcmp(type, "Ceed")) strncat(lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN); 596d1d35e2fSjeremylt strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN); 597d1d35e2fSjeremylt strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN); 5987a982d89SJeremy L. Thompson 5997a982d89SJeremy L. Thompson // Find and use offset 600*2b730f8bSJeremy L Thompson for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++) { 601d1d35e2fSjeremylt if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) { 602d1d35e2fSjeremylt size_t offset = ceed->f_offsets[i].offset; 6037a982d89SJeremy L. Thompson int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD* 6047a982d89SJeremy L. Thompson *fpointer = f; 605e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6067a982d89SJeremy L. Thompson } 607*2b730f8bSJeremy L Thompson } 6087a982d89SJeremy L. Thompson 6097a982d89SJeremy L. Thompson // LCOV_EXCL_START 610*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Requested function '%s' was not found for CEED object '%s'", func_name, type); 6117a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 6127a982d89SJeremy L. Thompson } 6137a982d89SJeremy L. Thompson 6147a982d89SJeremy L. Thompson /** 6157a982d89SJeremy L. Thompson @brief Retrieve backend data for a Ceed context 6167a982d89SJeremy L. Thompson 6177a982d89SJeremy L. Thompson @param ceed Ceed context to retrieve data of 6187a982d89SJeremy L. Thompson @param[out] data Address to save data to 6197a982d89SJeremy L. Thompson 6207a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6217a982d89SJeremy L. Thompson 6227a982d89SJeremy L. Thompson @ref Backend 6237a982d89SJeremy L. Thompson **/ 624777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) { 625777ff853SJeremy L Thompson *(void **)data = ceed->data; 626e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6277a982d89SJeremy L. Thompson } 6287a982d89SJeremy L. Thompson 6297a982d89SJeremy L. Thompson /** 6307a982d89SJeremy L. Thompson @brief Set backend data for a Ceed context 6317a982d89SJeremy L. Thompson 6327a982d89SJeremy L. Thompson @param ceed Ceed context to set data of 6337a982d89SJeremy L. Thompson @param data Address of data to set 6347a982d89SJeremy L. Thompson 6357a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6367a982d89SJeremy L. Thompson 6377a982d89SJeremy L. Thompson @ref Backend 6387a982d89SJeremy L. Thompson **/ 639777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) { 640777ff853SJeremy L Thompson ceed->data = data; 641e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6427a982d89SJeremy L. Thompson } 6437a982d89SJeremy L. Thompson 64434359f16Sjeremylt /** 64534359f16Sjeremylt @brief Increment the reference counter for a Ceed context 64634359f16Sjeremylt 64734359f16Sjeremylt @param ceed Ceed context to increment the reference counter 64834359f16Sjeremylt 64934359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 65034359f16Sjeremylt 65134359f16Sjeremylt @ref Backend 65234359f16Sjeremylt **/ 6539560d06aSjeremylt int CeedReference(Ceed ceed) { 65434359f16Sjeremylt ceed->ref_count++; 65534359f16Sjeremylt return CEED_ERROR_SUCCESS; 65634359f16Sjeremylt } 65734359f16Sjeremylt 6587a982d89SJeremy L. Thompson /// @} 6597a982d89SJeremy L. Thompson 6607a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6617a982d89SJeremy L. Thompson /// Ceed Public API 6627a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6637a982d89SJeremy L. Thompson /// @addtogroup CeedUser 6647a982d89SJeremy L. Thompson /// @{ 6657a982d89SJeremy L. Thompson 6667a982d89SJeremy L. Thompson /** 66792ee7d1cSjeremylt @brief Get the list of available resource names for Ceed contexts 6689ff86846Sjeremylt Note: The caller is responsible for `free()`ing the resources and priorities arrays, 6699ff86846Sjeremylt but should not `free()` the contents of the resources array. 67022e44211Sjeremylt 67192ee7d1cSjeremylt @param[out] n Number of available resources 67292ee7d1cSjeremylt @param[out] resources List of available resource names 67322e44211Sjeremylt @param[out] priorities Resource name prioritization values, lower is better 67422e44211Sjeremylt 67522e44211Sjeremylt @return An error code: 0 - success, otherwise - failure 67622e44211Sjeremylt 67722e44211Sjeremylt @ref User 67822e44211Sjeremylt **/ 67922e44211Sjeremylt // LCOV_EXCL_START 680*2b730f8bSJeremy L Thompson int CeedRegistryGetList(size_t *n, char ***const resources, CeedInt **priorities) { 681d0c91ce9Sjeremylt *n = 0; 6829ff86846Sjeremylt *resources = malloc(num_backends * sizeof(**resources)); 683*2b730f8bSJeremy L Thompson if (!resources) return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure"); 6849ff86846Sjeremylt if (priorities) { 6859ff86846Sjeremylt *priorities = malloc(num_backends * sizeof(**priorities)); 686*2b730f8bSJeremy L Thompson if (!priorities) return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure"); 6879ff86846Sjeremylt } 68822e44211Sjeremylt for (size_t i = 0; i < num_backends; i++) { 689d0c91ce9Sjeremylt // Only report compiled backends 690d0c91ce9Sjeremylt if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) { 69122e44211Sjeremylt *resources[i] = backends[i].prefix; 6929ff86846Sjeremylt if (priorities) *priorities[i] = backends[i].priority; 693d0c91ce9Sjeremylt *n += 1; 694d0c91ce9Sjeremylt } 695d0c91ce9Sjeremylt } 696*2b730f8bSJeremy L Thompson if (*n == 0) { 69778464608Sjeremylt // LCOV_EXCL_START 69878464608Sjeremylt return CeedError(NULL, CEED_ERROR_MAJOR, "No backends installed"); 69978464608Sjeremylt // LCOV_EXCL_STOP 700*2b730f8bSJeremy L Thompson } 701d0c91ce9Sjeremylt *resources = realloc(*resources, *n * sizeof(**resources)); 702*2b730f8bSJeremy L Thompson if (!resources) return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure"); 703d0c91ce9Sjeremylt if (priorities) { 704d0c91ce9Sjeremylt *priorities = realloc(*priorities, *n * sizeof(**priorities)); 705*2b730f8bSJeremy L Thompson if (!priorities) return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure"); 70622e44211Sjeremylt } 70722e44211Sjeremylt return CEED_ERROR_SUCCESS; 70845f1e315Sjeremylt } 70922e44211Sjeremylt // LCOV_EXCL_STOP 71022e44211Sjeremylt 71122e44211Sjeremylt /** 712d79b80ecSjeremylt @brief Initialize a \ref Ceed context to use the specified resource. 71322e44211Sjeremylt Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self") 71422e44211Sjeremylt will result in CeedInt printing the current libCEED version number 71592ee7d1cSjeremylt and a list of current available backend resources to stderr. 716b11c1e72Sjeremylt 717b11c1e72Sjeremylt @param resource Resource to use, e.g., "/cpu/self" 718b11c1e72Sjeremylt @param ceed The library context 719b11c1e72Sjeremylt @sa CeedRegister() CeedDestroy() 720b11c1e72Sjeremylt 721b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 722dfdf5a53Sjeremylt 7237a982d89SJeremy L. Thompson @ref User 724b11c1e72Sjeremylt **/ 725d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) { 726*2b730f8bSJeremy L Thompson size_t match_len = 0, match_index = UINT_MAX, match_priority = CEED_MAX_BACKEND_PRIORITY, priority; 727d7b241e6Sjeremylt 728fe2413ffSjeremylt // Find matching backend 729*2b730f8bSJeremy L Thompson if (!resource) { 73013873f79Sjeremylt // LCOV_EXCL_START 731e15f9bd0SJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided"); 73213873f79Sjeremylt // LCOV_EXCL_STOP 733*2b730f8bSJeremy L Thompson } 734*2b730f8bSJeremy L Thompson CeedCall(CeedRegisterAll()); 73513873f79Sjeremylt 73622e44211Sjeremylt // Check for help request 73722e44211Sjeremylt const char *help_prefix = "help"; 738*2b730f8bSJeremy L Thompson size_t match_help = 0; 739*2b730f8bSJeremy L Thompson while (match_help < 4 && resource[match_help] == help_prefix[match_help]) match_help++; 74022e44211Sjeremylt if (match_help == 4) { 741*2b730f8bSJeremy L Thompson fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR, CEED_VERSION_MINOR, CEED_VERSION_PATCH, 74222e44211Sjeremylt CEED_VERSION_RELEASE ? "" : "+development"); 74392ee7d1cSjeremylt fprintf(stderr, "Available backend resources:\n"); 74422e44211Sjeremylt for (size_t i = 0; i < num_backends; i++) { 745d0c91ce9Sjeremylt // Only report compiled backends 746*2b730f8bSJeremy L Thompson if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) fprintf(stderr, " %s\n", backends[i].prefix); 74722e44211Sjeremylt } 74822e44211Sjeremylt fflush(stderr); 74922e44211Sjeremylt match_help = 5; // Delineating character expected 75022e44211Sjeremylt } else { 75122e44211Sjeremylt match_help = 0; 75222e44211Sjeremylt } 75322e44211Sjeremylt 7549c9a0587SLeila Ghaffari // Find best match, computed as number of matching characters 7559c9a0587SLeila Ghaffari // from requested resource stem 756*2b730f8bSJeremy L Thompson size_t stem_length = 0; 757*2b730f8bSJeremy L Thompson while (resource[stem_length + match_help] && resource[stem_length + match_help] != ':') stem_length++; 758d7b241e6Sjeremylt for (size_t i = 0; i < num_backends; i++) { 759*2b730f8bSJeremy L Thompson size_t n = 0; 760d7b241e6Sjeremylt const char *prefix = backends[i].prefix; 761*2b730f8bSJeremy L Thompson while (prefix[n] && prefix[n] == resource[n + match_help]) n++; 762d7b241e6Sjeremylt priority = backends[i].priority; 763d1d35e2fSjeremylt if (n > match_len || (n == match_len && match_priority > priority)) { 764d1d35e2fSjeremylt match_len = n; 765d1d35e2fSjeremylt match_priority = priority; 766f7e22acaSJeremy L Thompson match_index = i; 767d7b241e6Sjeremylt } 768d7b241e6Sjeremylt } 7699c9a0587SLeila Ghaffari // Using Levenshtein distance to find closest match 7709c9a0587SLeila Ghaffari if (match_len <= 1 || match_len != stem_length) { 771203015caSLeila Ghaffari // LCOV_EXCL_START 7729c9a0587SLeila Ghaffari size_t lev_dis = UINT_MAX; 773f7e22acaSJeremy L Thompson size_t lev_index = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY; 7749c9a0587SLeila Ghaffari for (size_t i = 0; i < num_backends; i++) { 7759c9a0587SLeila Ghaffari const char *prefix = backends[i].prefix; 7769c9a0587SLeila Ghaffari size_t prefix_length = strlen(backends[i].prefix); 7779c9a0587SLeila Ghaffari size_t min_len = (prefix_length < stem_length) ? prefix_length : stem_length; 778092904ddSLeila Ghaffari size_t column[min_len + 1]; 779092904ddSLeila Ghaffari for (size_t j = 0; j <= min_len; j++) column[j] = j; 7809c9a0587SLeila Ghaffari for (size_t j = 1; j <= min_len; j++) { 7819c9a0587SLeila Ghaffari column[0] = j; 7829c9a0587SLeila Ghaffari for (size_t k = 1, last_diag = j - 1; k <= min_len; k++) { 783092904ddSLeila Ghaffari size_t old_diag = column[k]; 7849c9a0587SLeila Ghaffari size_t min_1 = (column[k] < column[k - 1]) ? column[k] + 1 : column[k - 1] + 1; 7859c9a0587SLeila Ghaffari size_t min_2 = last_diag + (resource[k - 1] == prefix[j - 1] ? 0 : 1); 7869c9a0587SLeila Ghaffari column[k] = (min_1 < min_2) ? min_1 : min_2; 7879c9a0587SLeila Ghaffari last_diag = old_diag; 7889c9a0587SLeila Ghaffari } 7899c9a0587SLeila Ghaffari } 7909c9a0587SLeila Ghaffari size_t n = column[min_len]; 7919c9a0587SLeila Ghaffari priority = backends[i].priority; 792*2b730f8bSJeremy L Thompson if (n < lev_dis || (n == lev_dis && lev_priority > priority)) { 7939c9a0587SLeila Ghaffari lev_dis = n; 7949c9a0587SLeila Ghaffari lev_priority = priority; 795f7e22acaSJeremy L Thompson lev_index = i; 7969c9a0587SLeila Ghaffari } 7979c9a0587SLeila Ghaffari } 798f7e22acaSJeremy L Thompson const char *prefix_lev = backends[lev_index].prefix; 799*2b730f8bSJeremy L Thompson size_t lev_length = 0; 800*2b730f8bSJeremy L Thompson while (prefix_lev[lev_length] && prefix_lev[lev_length] != '\0') lev_length++; 8019c9a0587SLeila Ghaffari size_t m = (lev_length < stem_length) ? lev_length : stem_length; 8029c9a0587SLeila Ghaffari if (lev_dis + 1 >= m) { 803*2b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s", resource); 8049c9a0587SLeila Ghaffari } else { 805*2b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, 806*2b730f8bSJeremy L Thompson "No suitable backend: %s\n" 807*2b730f8bSJeremy L Thompson "Closest match: %s", 808*2b730f8bSJeremy L Thompson resource, backends[lev_index].prefix); 8092bbc7fe8Sjeremylt } 810203015caSLeila Ghaffari // LCOV_EXCL_STOP 8119c9a0587SLeila Ghaffari } 812fe2413ffSjeremylt 813fe2413ffSjeremylt // Setup Ceed 814*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, ceed)); 815*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &(*ceed)->jit_source_roots)); 816bc81ce41Sjeremylt const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 817*2b730f8bSJeremy L Thompson if (!ceed_error_handler) ceed_error_handler = "abort"; 818*2b730f8bSJeremy L Thompson if (!strcmp(ceed_error_handler, "exit")) (*ceed)->Error = CeedErrorExit; 819*2b730f8bSJeremy L Thompson else if (!strcmp(ceed_error_handler, "store")) (*ceed)->Error = CeedErrorStore; 820*2b730f8bSJeremy L Thompson else (*ceed)->Error = CeedErrorAbort; 821d1d35e2fSjeremylt memcpy((*ceed)->err_msg, "No error message stored", 24); 822d1d35e2fSjeremylt (*ceed)->ref_count = 1; 823d7b241e6Sjeremylt (*ceed)->data = NULL; 824fe2413ffSjeremylt 825fe2413ffSjeremylt // Set lookup table 826d1d35e2fSjeremylt FOffset f_offsets[] = { 8276e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, Error), 8286e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType), 8296e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, Destroy), 830f8902d9eSjeremylt CEED_FTABLE_ENTRY(Ceed, VectorCreate), 8316e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate), 832fc0567d9Srezgarshakeri CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateOriented), 8336e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked), 8346e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1), 8356e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, BasisCreateH1), 83650c301a5SRezgar Shakeri CEED_FTABLE_ENTRY(Ceed, BasisCreateHdiv), 8376e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, TensorContractCreate), 8386e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, QFunctionCreate), 839777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate), 8406e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, OperatorCreate), 8416e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate), 8429c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, HasValidArray), 8439c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, HasBorrowedArrayOfType), 8446e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, SetArray), 8456a6c615bSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, TakeArray), 8466e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, SetValue), 847f48ed27dSnbeams CEED_FTABLE_ENTRY(CeedVector, SyncArray), 8486e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, GetArray), 8496e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, GetArrayRead), 8509c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, GetArrayWrite), 8516e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, RestoreArray), 8526e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead), 853547d9b97Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Norm), 854e0dd3b27Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Scale), 8550f7fd0f8Sjeremylt CEED_FTABLE_ENTRY(CeedVector, AXPY), 8560f7fd0f8Sjeremylt CEED_FTABLE_ENTRY(CeedVector, PointwiseMult), 857d99fa3c5SJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, Reciprocal), 8586e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Destroy), 8596e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, Apply), 8606e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock), 861bd33150aSjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets), 8626e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy), 8636e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedBasis, Apply), 8646e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedBasis, Destroy), 8656e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedTensorContract, Apply), 8666e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedTensorContract, Destroy), 8676e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, Apply), 8688c84ac63Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction), 8698c84ac63Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction), 8706e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, Destroy), 8719c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, HasValidData), 8729c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, HasBorrowedDataOfType), 873777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData), 874891038deSjeremylt CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData), 875777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData), 87628bfd0b7SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, GetDataRead), 877777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData), 87828bfd0b7SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreDataRead), 8792e64a2b9SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, DataDestroy), 880777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy), 88180ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction), 88270a7ffb3SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate), 88380ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal), 8849e9210b8SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal), 88580ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal), 8869e9210b8SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal), 887e2f04181SAndrew T. Barker CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic), 888e2f04181SAndrew T. Barker CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble), 889cefa2673SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSingle), 890713f43c3Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse), 8916e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, Apply), 892250756a7Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite), 893cae8b89aSjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd), 894250756a7Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite), 8956e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian), 8966e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, Destroy), 8976e79d475Sjeremylt {NULL, 0} // End of lookup table - used in SetBackendFunction loop 8981dfeef1dSjeremylt }; 899fe2413ffSjeremylt 900*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets)); 901d1d35e2fSjeremylt memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets)); 902fe2413ffSjeremylt 9035107b09fSJeremy L Thompson // Set fallback for advanced CeedOperator functions 904e2f04181SAndrew T. Barker const char fallbackresource[] = ""; 905*2b730f8bSJeremy L Thompson CeedCall(CeedSetOperatorFallbackResource(*ceed, fallbackresource)); 9065107b09fSJeremy L Thompson 90760f9e2d6SJeremy L Thompson // Record env variables CEED_DEBUG or DBG 908*2b730f8bSJeremy L Thompson (*ceed)->is_debug = !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG"); 90960f9e2d6SJeremy L Thompson 91022e44211Sjeremylt // Copy resource prefix, if backend setup successful 911*2b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(backends[match_index].prefix, (char **)&(*ceed)->resource)); 912ee5a26f2SJeremy L Thompson 913ee5a26f2SJeremy L Thompson // Set default JiT source root 9146155f12fSJeremy L Thompson // Note: there will always be the default root for every Ceed 9156155f12fSJeremy L Thompson // but all additional paths are added to the top-most parent 916*2b730f8bSJeremy L Thompson CeedCall(CeedAddJitSourceRoot(*ceed, (char *)CeedJitSourceRootDefault)); 917ee5a26f2SJeremy L Thompson 918d04bbc78SJeremy L Thompson // Backend specific setup 919*2b730f8bSJeremy L Thompson CeedCall(backends[match_index].init(&resource[match_help], *ceed)); 920d04bbc78SJeremy L Thompson 921e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 922d7b241e6Sjeremylt } 923d7b241e6Sjeremylt 924d7b241e6Sjeremylt /** 9259560d06aSjeremylt @brief Copy the pointer to a Ceed context. Both pointers should 9269560d06aSjeremylt be destroyed with `CeedDestroy()`; 9279560d06aSjeremylt Note: If `*ceed_copy` is non-NULL, then it is assumed that 9289560d06aSjeremylt `*ceed_copy` is a pointer to a Ceed context. This Ceed 9299560d06aSjeremylt context will be destroyed if `*ceed_copy` is the only 9309560d06aSjeremylt reference to this Ceed context. 9319560d06aSjeremylt 9329560d06aSjeremylt @param ceed Ceed context to copy reference to 9339560d06aSjeremylt @param[out] ceed_copy Variable to store copied reference 9349560d06aSjeremylt 9359560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 9369560d06aSjeremylt 9379560d06aSjeremylt @ref User 9389560d06aSjeremylt **/ 9399560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) { 940*2b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 941*2b730f8bSJeremy L Thompson CeedCall(CeedDestroy(ceed_copy)); 9429560d06aSjeremylt *ceed_copy = ceed; 9439560d06aSjeremylt return CEED_ERROR_SUCCESS; 9449560d06aSjeremylt } 9459560d06aSjeremylt 9469560d06aSjeremylt /** 9477a982d89SJeremy L. Thompson @brief Get the full resource name for a Ceed context 9482f86a920SJeremy L Thompson 9497a982d89SJeremy L. Thompson @param ceed Ceed context to get resource name of 9507a982d89SJeremy L. Thompson @param[out] resource Variable to store resource name 9512f86a920SJeremy L Thompson 9522f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9532f86a920SJeremy L Thompson 9547a982d89SJeremy L. Thompson @ref User 9555107b09fSJeremy L Thompson **/ 9567a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) { 9577a982d89SJeremy L. Thompson *resource = (const char *)ceed->resource; 958e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9595107b09fSJeremy L Thompson } 9605107b09fSJeremy L Thompson 9615107b09fSJeremy L Thompson /** 962d79b80ecSjeremylt @brief Return Ceed context preferred memory type 963c907536fSjeremylt 964d79b80ecSjeremylt @param ceed Ceed context to get preferred memory type of 965d1d35e2fSjeremylt @param[out] mem_type Address to save preferred memory type to 966c907536fSjeremylt 967c907536fSjeremylt @return An error code: 0 - success, otherwise - failure 968c907536fSjeremylt 9697a982d89SJeremy L. Thompson @ref User 970c907536fSjeremylt **/ 971d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) { 972c907536fSjeremylt if (ceed->GetPreferredMemType) { 973*2b730f8bSJeremy L Thompson CeedCall(ceed->GetPreferredMemType(mem_type)); 974c907536fSjeremylt } else { 975c263cd57Sjeremylt Ceed delegate; 976*2b730f8bSJeremy L Thompson CeedCall(CeedGetDelegate(ceed, &delegate)); 977c263cd57Sjeremylt 978c263cd57Sjeremylt if (delegate) { 979*2b730f8bSJeremy L Thompson CeedCall(CeedGetPreferredMemType(delegate, mem_type)); 980c263cd57Sjeremylt } else { 981d1d35e2fSjeremylt *mem_type = CEED_MEM_HOST; 982c907536fSjeremylt } 983c263cd57Sjeremylt } 984e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 985c907536fSjeremylt } 986c907536fSjeremylt 987c907536fSjeremylt /** 9889525855cSJeremy L Thompson @brief Get deterministic status of Ceed 9899525855cSJeremy L Thompson 9909525855cSJeremy L Thompson @param[in] ceed Ceed 991d1d35e2fSjeremylt @param[out] is_deterministic Variable to store deterministic status 9929525855cSJeremy L Thompson 9939525855cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9949525855cSJeremy L Thompson 9959525855cSJeremy L Thompson @ref User 9969525855cSJeremy L Thompson **/ 997d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) { 998d1d35e2fSjeremylt *is_deterministic = ceed->is_deterministic; 999e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10009525855cSJeremy L Thompson } 10019525855cSJeremy L Thompson 10029525855cSJeremy L Thompson /** 1003ee5a26f2SJeremy L Thompson @brief Set additional JiT source root for Ceed 1004ee5a26f2SJeremy L Thompson 1005ee5a26f2SJeremy L Thompson @param[in] ceed Ceed 1006ee5a26f2SJeremy L Thompson @param[in] jit_source_root Absolute path to additional JiT source directory 1007ee5a26f2SJeremy L Thompson 1008ee5a26f2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1009ee5a26f2SJeremy L Thompson 1010ee5a26f2SJeremy L Thompson @ref User 1011ee5a26f2SJeremy L Thompson **/ 1012ee5a26f2SJeremy L Thompson int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root) { 10136155f12fSJeremy L Thompson Ceed ceed_parent; 1014ee5a26f2SJeremy L Thompson 1015*2b730f8bSJeremy L Thompson CeedCall(CeedGetParent(ceed, &ceed_parent)); 10166155f12fSJeremy L Thompson 10176155f12fSJeremy L Thompson CeedInt index = ceed_parent->num_jit_source_roots; 1018ee5a26f2SJeremy L Thompson size_t path_length = strlen(jit_source_root); 1019*2b730f8bSJeremy L Thompson CeedCall(CeedRealloc(index + 1, &ceed_parent->jit_source_roots)); 1020*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(path_length + 1, &ceed_parent->jit_source_roots[index])); 1021d602d780SJeremy L Thompson memcpy(ceed_parent->jit_source_roots[index], jit_source_root, path_length); 10226155f12fSJeremy L Thompson ceed_parent->num_jit_source_roots++; 1023ee5a26f2SJeremy L Thompson 1024ee5a26f2SJeremy L Thompson return CEED_ERROR_SUCCESS; 1025ee5a26f2SJeremy L Thompson } 1026ee5a26f2SJeremy L Thompson 1027ee5a26f2SJeremy L Thompson /** 10280a0da059Sjeremylt @brief View a Ceed 10290a0da059Sjeremylt 10300a0da059Sjeremylt @param[in] ceed Ceed to view 10310a0da059Sjeremylt @param[in] stream Filestream to write to 10320a0da059Sjeremylt 10330a0da059Sjeremylt @return An error code: 0 - success, otherwise - failure 10340a0da059Sjeremylt 10350a0da059Sjeremylt @ref User 10360a0da059Sjeremylt **/ 10370a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) { 1038d1d35e2fSjeremylt CeedMemType mem_type; 10390a0da059Sjeremylt 1040*2b730f8bSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); 10410a0da059Sjeremylt 1042*2b730f8bSJeremy L Thompson fprintf(stream, 1043*2b730f8bSJeremy L Thompson "Ceed\n" 10440a0da059Sjeremylt " Ceed Resource: %s\n" 10450a0da059Sjeremylt " Preferred MemType: %s\n", 1046d1d35e2fSjeremylt ceed->resource, CeedMemTypes[mem_type]); 1047e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10480a0da059Sjeremylt } 10490a0da059Sjeremylt 10500a0da059Sjeremylt /** 1051b11c1e72Sjeremylt @brief Destroy a Ceed context 1052d7b241e6Sjeremylt 1053d7b241e6Sjeremylt @param ceed Address of Ceed context to destroy 1054b11c1e72Sjeremylt 1055b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1056dfdf5a53Sjeremylt 10577a982d89SJeremy L. Thompson @ref User 1058b11c1e72Sjeremylt **/ 1059d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) { 1060d1d35e2fSjeremylt if (!*ceed || --(*ceed)->ref_count > 0) return CEED_ERROR_SUCCESS; 1061*2b730f8bSJeremy L Thompson if ((*ceed)->delegate) CeedCall(CeedDestroy(&(*ceed)->delegate)); 10620ace9bf2Sjeremylt 1063d1d35e2fSjeremylt if ((*ceed)->obj_delegate_count > 0) { 106492ae7e47SJeremy L Thompson for (CeedInt i = 0; i < (*ceed)->obj_delegate_count; i++) { 1065*2b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&((*ceed)->obj_delegates[i].delegate))); 1066*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->obj_delegates[i].obj_name)); 1067aefd8378Sjeremylt } 1068*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->obj_delegates)); 1069aefd8378Sjeremylt } 10700ace9bf2Sjeremylt 1071*2b730f8bSJeremy L Thompson if ((*ceed)->Destroy) CeedCall((*ceed)->Destroy(*ceed)); 10720ace9bf2Sjeremylt 107392ae7e47SJeremy L Thompson for (CeedInt i = 0; i < (*ceed)->num_jit_source_roots; i++) { 1074*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->jit_source_roots[i])); 1075032e71eaSJeremy L Thompson } 1076*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->jit_source_roots)); 1077032e71eaSJeremy L Thompson 1078*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->f_offsets)); 1079*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->resource)); 1080*2b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*ceed)->op_fallback_ceed)); 1081*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->op_fallback_resource)); 1082*2b730f8bSJeremy L Thompson CeedCall(CeedFree(ceed)); 1083e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1084d7b241e6Sjeremylt } 1085d7b241e6Sjeremylt 1086f9982c62SWill Pazner // LCOV_EXCL_START 1087f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) { 1088*2b730f8bSJeremy L Thompson if (ceed->parent) return CeedErrorFormat(ceed->parent, format, args); 1089*2b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedErrorFormat(ceed->op_fallback_parent, format, args); 109078464608Sjeremylt // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized 109178464608Sjeremylt vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args); // NOLINT 1092d1d35e2fSjeremylt return ceed->err_msg; 1093f9982c62SWill Pazner } 1094f9982c62SWill Pazner // LCOV_EXCL_STOP 1095f9982c62SWill Pazner 10967a982d89SJeremy L. Thompson /** 10977a982d89SJeremy L. Thompson @brief Error handling implementation; use \ref CeedError instead. 10987a982d89SJeremy L. Thompson 10997a982d89SJeremy L. Thompson @ref Developer 11007a982d89SJeremy L. Thompson **/ 1101*2b730f8bSJeremy L Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func, int ecode, const char *format, ...) { 11027a982d89SJeremy L. Thompson va_list args; 1103d1d35e2fSjeremylt int ret_val; 11047a982d89SJeremy L. Thompson va_start(args, format); 11057a982d89SJeremy L. Thompson if (ceed) { 1106d1d35e2fSjeremylt ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args); 11077a982d89SJeremy L. Thompson } else { 1108b0d62198Sjeremylt // LCOV_EXCL_START 1109477729cfSJeremy L Thompson const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 1110*2b730f8bSJeremy L Thompson if (!ceed_error_handler) ceed_error_handler = "abort"; 1111*2b730f8bSJeremy L Thompson if (!strcmp(ceed_error_handler, "return")) ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args); 1112477729cfSJeremy L Thompson else 1113477729cfSJeremy L Thompson // This function will not return 1114d1d35e2fSjeremylt ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args); 11157a982d89SJeremy L. Thompson } 11167a982d89SJeremy L. Thompson va_end(args); 1117d1d35e2fSjeremylt return ret_val; 1118b0d62198Sjeremylt // LCOV_EXCL_STOP 11197a982d89SJeremy L. Thompson } 11207a982d89SJeremy L. Thompson 1121477729cfSJeremy L Thompson /** 1122477729cfSJeremy L Thompson @brief Error handler that returns without printing anything. 1123477729cfSJeremy L Thompson 1124477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1125477729cfSJeremy L Thompson 1126477729cfSJeremy L Thompson @ref Developer 1127477729cfSJeremy L Thompson **/ 1128477729cfSJeremy L Thompson // LCOV_EXCL_START 1129*2b730f8bSJeremy L Thompson int CeedErrorReturn(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 1130d1d35e2fSjeremylt return err_code; 1131477729cfSJeremy L Thompson } 1132477729cfSJeremy L Thompson // LCOV_EXCL_STOP 1133477729cfSJeremy L Thompson 1134477729cfSJeremy L Thompson /** 1135477729cfSJeremy L Thompson @brief Error handler that stores the error message for future use and returns 1136477729cfSJeremy L Thompson the error. 1137477729cfSJeremy L Thompson 1138477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1139477729cfSJeremy L Thompson 1140477729cfSJeremy L Thompson @ref Developer 1141477729cfSJeremy L Thompson **/ 1142477729cfSJeremy L Thompson // LCOV_EXCL_START 1143*2b730f8bSJeremy L Thompson int CeedErrorStore(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 1144*2b730f8bSJeremy L Thompson if (ceed->parent) return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format, args); 1145*2b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func, err_code, format, args); 1146477729cfSJeremy L Thompson 1147477729cfSJeremy L Thompson // Build message 1148990fdeb6SJeremy L Thompson int len; 1149*2b730f8bSJeremy L Thompson len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ", filename, line_no, func); 115078464608Sjeremylt // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized 115178464608Sjeremylt // *INDENT-OFF* 115278464608Sjeremylt vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args); // NOLINT 115378464608Sjeremylt // *INDENT-ON* 1154d1d35e2fSjeremylt return err_code; 1155477729cfSJeremy L Thompson } 1156477729cfSJeremy L Thompson // LCOV_EXCL_STOP 1157477729cfSJeremy L Thompson 1158477729cfSJeremy L Thompson /** 1159477729cfSJeremy L Thompson @brief Error handler that prints to stderr and aborts 1160477729cfSJeremy L Thompson 1161477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1162477729cfSJeremy L Thompson 1163477729cfSJeremy L Thompson @ref Developer 1164477729cfSJeremy L Thompson **/ 1165477729cfSJeremy L Thompson // LCOV_EXCL_START 1166*2b730f8bSJeremy L Thompson int CeedErrorAbort(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 1167d1d35e2fSjeremylt fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func); 1168f9982c62SWill Pazner vfprintf(stderr, format, *args); 1169477729cfSJeremy L Thompson fprintf(stderr, "\n"); 1170477729cfSJeremy L Thompson abort(); 1171d1d35e2fSjeremylt return err_code; 1172477729cfSJeremy L Thompson } 1173477729cfSJeremy L Thompson // LCOV_EXCL_STOP 1174477729cfSJeremy L Thompson 1175477729cfSJeremy L Thompson /** 1176477729cfSJeremy L Thompson @brief Error handler that prints to stderr and exits 1177477729cfSJeremy L Thompson 1178477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1179477729cfSJeremy L Thompson 1180477729cfSJeremy L Thompson In contrast to CeedErrorAbort(), this exits without a signal, so atexit() 1181477729cfSJeremy L Thompson handlers (e.g., as used by gcov) are run. 1182477729cfSJeremy L Thompson 1183477729cfSJeremy L Thompson @ref Developer 1184477729cfSJeremy L Thompson **/ 1185*2b730f8bSJeremy L Thompson int CeedErrorExit(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 1186d1d35e2fSjeremylt fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func); 118778464608Sjeremylt // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized 118878464608Sjeremylt vfprintf(stderr, format, *args); // NOLINT 1189477729cfSJeremy L Thompson fprintf(stderr, "\n"); 1190d1d35e2fSjeremylt exit(err_code); 1191d1d35e2fSjeremylt return err_code; 1192477729cfSJeremy L Thompson } 1193477729cfSJeremy L Thompson 1194477729cfSJeremy L Thompson /** 1195477729cfSJeremy L Thompson @brief Set error handler 1196477729cfSJeremy L Thompson 1197477729cfSJeremy L Thompson A default error handler is set in CeedInit(). Use this function to change 1198477729cfSJeremy L Thompson the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined 1199477729cfSJeremy L Thompson error handler. 1200477729cfSJeremy L Thompson 1201477729cfSJeremy L Thompson @ref Developer 1202477729cfSJeremy L Thompson **/ 1203d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) { 1204d1d35e2fSjeremylt ceed->Error = handler; 1205d1d35e2fSjeremylt if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler); 1206*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler); 1207e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1208477729cfSJeremy L Thompson } 1209477729cfSJeremy L Thompson 1210477729cfSJeremy L Thompson /** 1211477729cfSJeremy L Thompson @brief Get error message 1212477729cfSJeremy L Thompson 1213477729cfSJeremy L Thompson The error message is only stored when using the error handler 1214477729cfSJeremy L Thompson CeedErrorStore() 1215477729cfSJeremy L Thompson 1216477729cfSJeremy L Thompson @param[in] ceed Ceed contex to retrieve error message 1217d1d35e2fSjeremylt @param[out] err_msg Char pointer to hold error message 1218477729cfSJeremy L Thompson 1219477729cfSJeremy L Thompson @ref Developer 1220477729cfSJeremy L Thompson **/ 1221d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) { 1222*2b730f8bSJeremy L Thompson if (ceed->parent) return CeedGetErrorMessage(ceed->parent, err_msg); 1223*2b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg); 1224d1d35e2fSjeremylt *err_msg = ceed->err_msg; 1225e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1226477729cfSJeremy L Thompson } 1227477729cfSJeremy L Thompson 1228477729cfSJeremy L Thompson /** 1229477729cfSJeremy L Thompson @brief Restore error message 1230477729cfSJeremy L Thompson 1231477729cfSJeremy L Thompson The error message is only stored when using the error handler 1232477729cfSJeremy L Thompson CeedErrorStore() 1233477729cfSJeremy L Thompson 1234477729cfSJeremy L Thompson @param[in] ceed Ceed contex to restore error message 1235d1d35e2fSjeremylt @param[out] err_msg Char pointer that holds error message 1236477729cfSJeremy L Thompson 1237477729cfSJeremy L Thompson @ref Developer 1238477729cfSJeremy L Thompson **/ 1239d1d35e2fSjeremylt int CeedResetErrorMessage(Ceed ceed, const char **err_msg) { 1240*2b730f8bSJeremy L Thompson if (ceed->parent) return CeedResetErrorMessage(ceed->parent, err_msg); 1241*2b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg); 1242d1d35e2fSjeremylt *err_msg = NULL; 1243d1d35e2fSjeremylt memcpy(ceed->err_msg, "No error message stored", 24); 1244e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1245477729cfSJeremy L Thompson } 1246477729cfSJeremy L Thompson 12471070991dSJed Brown /** 12481070991dSJed Brown @brief Get libCEED library version info 12491070991dSJed Brown 12501070991dSJed Brown libCEED version numbers have the form major.minor.patch. Non-release versions 12511070991dSJed Brown may contain unstable interfaces. 12521070991dSJed Brown 12531070991dSJed Brown @param[out] major Major version of the library 12541070991dSJed Brown @param[out] minor Minor version of the library 12551070991dSJed Brown @param[out] patch Patch (subminor) version of the library 12561070991dSJed Brown @param[out] release True for releases; false for development branches. 12571070991dSJed Brown 12581070991dSJed Brown The caller may pass NULL for any arguments that are not needed. 12591070991dSJed Brown 12601070991dSJed Brown @sa CEED_VERSION_GE() 12611070991dSJed Brown 12621070991dSJed Brown @ref Developer 12631070991dSJed Brown */ 12641070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) { 12651070991dSJed Brown if (major) *major = CEED_VERSION_MAJOR; 12661070991dSJed Brown if (minor) *minor = CEED_VERSION_MINOR; 12671070991dSJed Brown if (patch) *patch = CEED_VERSION_PATCH; 12681070991dSJed Brown if (release) *release = CEED_VERSION_RELEASE; 12691070991dSJed Brown return 0; 12701070991dSJed Brown } 12711070991dSJed Brown 127280a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) { 127380a9ef05SNatalie Beams *scalar_type = CEED_SCALAR_TYPE; 127480a9ef05SNatalie Beams return 0; 127580a9ef05SNatalie Beams } 127680a9ef05SNatalie Beams 1277d7b241e6Sjeremylt /// @} 1278