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> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 112b730f8bSJeremy 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 43*ea61e9acSJeremy L Thompson This predefined constant is passed as the \ref CeedRequest argument to interfaces when the caller wishes for the operation to be performed 44dfdf5a53Sjeremylt immediately. The code 45dfdf5a53Sjeremylt 46dfdf5a53Sjeremylt @code 47dfdf5a53Sjeremylt CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE); 48dfdf5a53Sjeremylt @endcode 49dfdf5a53Sjeremylt 50dfdf5a53Sjeremylt is semantically equivalent to 51dfdf5a53Sjeremylt 52dfdf5a53Sjeremylt @code 53dfdf5a53Sjeremylt CeedRequest request; 54dfdf5a53Sjeremylt CeedOperatorApply(op, ..., &request); 55dfdf5a53Sjeremylt CeedRequestWait(&request); 56dfdf5a53Sjeremylt @endcode 57dfdf5a53Sjeremylt 58dfdf5a53Sjeremylt @sa CEED_REQUEST_ORDERED 59dfdf5a53Sjeremylt **/ 60d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate; 61d7b241e6Sjeremylt 62d7b241e6Sjeremylt /** 63b11c1e72Sjeremylt @brief Request ordered completion 64d7b241e6Sjeremylt 65*ea61e9acSJeremy L Thompson This predefined constant is passed as the \ref CeedRequest argument to interfaces when the caller wishes for the operation to be completed in the 66*ea61e9acSJeremy L Thompson order that it is submitted to the device. It is typically used in a construct such as 67d7b241e6Sjeremylt 68d7b241e6Sjeremylt @code 69d7b241e6Sjeremylt CeedRequest request; 70d7b241e6Sjeremylt CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED); 71d7b241e6Sjeremylt CeedOperatorApply(op2, ..., &request); 72d7b241e6Sjeremylt // other optional work 738b2d6f4aSMatthew Knepley CeedRequestWait(&request); 74d7b241e6Sjeremylt @endcode 75d7b241e6Sjeremylt 76*ea61e9acSJeremy L Thompson which allows the sequence to complete asynchronously but does not start `op2` until `op1` has completed. 77d7b241e6Sjeremylt 78*ea61e9acSJeremy L Thompson @todo The current implementation is overly strict, offering equivalent semantics to @ref CEED_REQUEST_IMMEDIATE. 79d7b241e6Sjeremylt 80d7b241e6Sjeremylt @sa CEED_REQUEST_IMMEDIATE 81d7b241e6Sjeremylt */ 82d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered; 83d7b241e6Sjeremylt 84b11c1e72Sjeremylt /** 857a982d89SJeremy L. Thompson @brief Wait for a CeedRequest to complete. 86dfdf5a53Sjeremylt 877a982d89SJeremy L. Thompson Calling CeedRequestWait on a NULL request is a no-op. 887a982d89SJeremy L. Thompson 897a982d89SJeremy L. Thompson @param req Address of CeedRequest to wait for; zeroed on completion. 907a982d89SJeremy L. Thompson 917a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 927a982d89SJeremy L. Thompson 937a982d89SJeremy L. Thompson @ref User 94b11c1e72Sjeremylt **/ 957a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) { 962b730f8bSJeremy L Thompson if (!*req) return CEED_ERROR_SUCCESS; 972b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_UNSUPPORTED, "CeedRequestWait not implemented"); 98683faae0SJed Brown } 997a982d89SJeremy L. Thompson 1007a982d89SJeremy L. Thompson /// @} 1017a982d89SJeremy L. Thompson 1027a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1037a982d89SJeremy L. Thompson /// Ceed Library Internal Functions 1047a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1057a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper 1067a982d89SJeremy L. Thompson /// @{ 107d7b241e6Sjeremylt 1086a406739SJeremy L Thompson /** 1096a406739SJeremy L Thompson @brief Register a Ceed backend internally. 1106a406739SJeremy L Thompson Note: Backends should call `CeedRegister` instead. 1116a406739SJeremy L Thompson 112*ea61e9acSJeremy L Thompson @param[in] prefix Prefix of resources for this backend to respond to. 113*ea61e9acSJeremy L Thompson For example, the reference backend responds to "/cpu/self". 114*ea61e9acSJeremy L Thompson @param[in] init Initialization function called by CeedInit() when the backend is selected to drive the requested resource. 115*ea61e9acSJeremy L Thompson @param[in] priority Integer priority. 116*ea61e9acSJeremy L Thompson Lower values are preferred in case the resource requested by CeedInit() has non-unique best prefix match. 1176a406739SJeremy L Thompson 1186a406739SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1196a406739SJeremy L Thompson 1206a406739SJeremy L Thompson @ref Developer 1216a406739SJeremy L Thompson **/ 1222b730f8bSJeremy L Thompson int CeedRegisterImpl(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) { 1236a406739SJeremy L Thompson if (num_backends >= sizeof(backends) / sizeof(backends[0])) 1246a406739SJeremy L Thompson // LCOV_EXCL_START 1256a406739SJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "Too many backends"); 1266a406739SJeremy L Thompson // LCOV_EXCL_STOP 1276a406739SJeremy L Thompson 1286a406739SJeremy L Thompson strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN); 1296a406739SJeremy L Thompson backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN - 1] = 0; 1306a406739SJeremy L Thompson backends[num_backends].init = init; 1316a406739SJeremy L Thompson backends[num_backends].priority = priority; 1326a406739SJeremy L Thompson num_backends++; 1336a406739SJeremy L Thompson return CEED_ERROR_SUCCESS; 1346a406739SJeremy L Thompson } 1356a406739SJeremy L Thompson 1367a982d89SJeremy L. Thompson /// @} 137d7b241e6Sjeremylt 1387a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1397a982d89SJeremy L. Thompson /// Ceed Backend API 1407a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1417a982d89SJeremy L. Thompson /// @addtogroup CeedBackend 1427a982d89SJeremy L. Thompson /// @{ 143d7b241e6Sjeremylt 144b11c1e72Sjeremylt /** 1453f21f6b1SJeremy L Thompson @brief Return value of CEED_DEBUG environment variable 14660f9e2d6SJeremy L Thompson 147*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context 14860f9e2d6SJeremy L Thompson 1493f21f6b1SJeremy L Thompson @return boolean value: true - debugging mode enabled 1503f21f6b1SJeremy L Thompson false - debugging mode disabled 15160f9e2d6SJeremy L Thompson 15260f9e2d6SJeremy L Thompson @ref Backend 15360f9e2d6SJeremy L Thompson **/ 154fc6bbcedSJeremy L Thompson // LCOV_EXCL_START 1552b730f8bSJeremy L Thompson bool CeedDebugFlag(const Ceed ceed) { return ceed->is_debug; } 156fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP 15760f9e2d6SJeremy L Thompson 15860f9e2d6SJeremy L Thompson /** 1593f21f6b1SJeremy L Thompson @brief Return value of CEED_DEBUG environment variable 16060f9e2d6SJeremy L Thompson 1613f21f6b1SJeremy L Thompson @return boolean value: true - debugging mode enabled 1623f21f6b1SJeremy L Thompson false - debugging mode disabled 1633f21f6b1SJeremy L Thompson 1643f21f6b1SJeremy L Thompson @ref Backend 1653f21f6b1SJeremy L Thompson **/ 1663f21f6b1SJeremy L Thompson // LCOV_EXCL_START 1672b730f8bSJeremy L Thompson bool CeedDebugFlagEnv(void) { return !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG"); } 1683f21f6b1SJeremy L Thompson // LCOV_EXCL_STOP 1693f21f6b1SJeremy L Thompson 1703f21f6b1SJeremy L Thompson /** 1713f21f6b1SJeremy L Thompson @brief Print debugging information in color 1723f21f6b1SJeremy L Thompson 17360f9e2d6SJeremy L Thompson @param color Color to print 17460f9e2d6SJeremy L Thompson @param format Printing format 17560f9e2d6SJeremy L Thompson 17660f9e2d6SJeremy L Thompson @ref Backend 17760f9e2d6SJeremy L Thompson **/ 178fc6bbcedSJeremy L Thompson // LCOV_EXCL_START 1793f21f6b1SJeremy L Thompson void CeedDebugImpl256(const unsigned char color, const char *format, ...) { 18060f9e2d6SJeremy L Thompson va_list args; 18160f9e2d6SJeremy L Thompson va_start(args, format); 18260f9e2d6SJeremy L Thompson fflush(stdout); 1832b730f8bSJeremy L Thompson if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[38;5;%dm", color); 18460f9e2d6SJeremy L Thompson vfprintf(stdout, format, args); 1852b730f8bSJeremy L Thompson if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[m"); 18660f9e2d6SJeremy L Thompson fprintf(stdout, "\n"); 18760f9e2d6SJeremy L Thompson fflush(stdout); 18860f9e2d6SJeremy L Thompson va_end(args); 18960f9e2d6SJeremy L Thompson } 190fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP 19160f9e2d6SJeremy L Thompson 19260f9e2d6SJeremy L Thompson /** 193b11c1e72Sjeremylt @brief Allocate an array on the host; use CeedMalloc() 194b11c1e72Sjeremylt 195*ea61e9acSJeremy L Thompson Memory usage can be tracked by the library. 196*ea61e9acSJeremy L Thompson This ensures sufficient alignment for vectorization and should be used for large allocations. 197b11c1e72Sjeremylt 198*ea61e9acSJeremy L Thompson @param[in] n Number of units to allocate 199*ea61e9acSJeremy L Thompson @param[in] unit Size of each unit 200*ea61e9acSJeremy L Thompson @param[out] p Address of pointer to hold the result. 201b11c1e72Sjeremylt 202b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 203b11c1e72Sjeremylt 204b11c1e72Sjeremylt @sa CeedFree() 205dfdf5a53Sjeremylt 2067a982d89SJeremy L. Thompson @ref Backend 207b11c1e72Sjeremylt **/ 208d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) { 209d7b241e6Sjeremylt int ierr = posix_memalign((void **)p, CEED_ALIGN, n * unit); 2102b730f8bSJeremy L Thompson if (ierr) { 211c042f62fSJeremy L Thompson // LCOV_EXCL_START 2122b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "posix_memalign failed to allocate %zd members of size %zd\n", n, unit); 213c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 2142b730f8bSJeremy L Thompson } 215e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 216d7b241e6Sjeremylt } 217d7b241e6Sjeremylt 218b11c1e72Sjeremylt /** 219b11c1e72Sjeremylt @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc() 220b11c1e72Sjeremylt 221b11c1e72Sjeremylt Memory usage can be tracked by the library. 222b11c1e72Sjeremylt 223*ea61e9acSJeremy L Thompson @param[in] n Number of units to allocate 224*ea61e9acSJeremy L Thompson @param[in] unit Size of each unit 225*ea61e9acSJeremy L Thompson @param[out] p Address of pointer to hold the result. 226b11c1e72Sjeremylt 227b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 228b11c1e72Sjeremylt 229b11c1e72Sjeremylt @sa CeedFree() 230dfdf5a53Sjeremylt 2317a982d89SJeremy L. Thompson @ref Backend 232b11c1e72Sjeremylt **/ 233d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) { 234d7b241e6Sjeremylt *(void **)p = calloc(n, unit); 2352b730f8bSJeremy L Thompson if (n && unit && !*(void **)p) { 236c042f62fSJeremy L Thompson // LCOV_EXCL_START 2372b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "calloc failed to allocate %zd members of size %zd\n", n, unit); 238c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 2392b730f8bSJeremy L Thompson } 240e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 241d7b241e6Sjeremylt } 242d7b241e6Sjeremylt 243b11c1e72Sjeremylt /** 244b11c1e72Sjeremylt @brief Reallocate an array on the host; use CeedRealloc() 245b11c1e72Sjeremylt 246b11c1e72Sjeremylt Memory usage can be tracked by the library. 247b11c1e72Sjeremylt 248*ea61e9acSJeremy L Thompson @param[in] n Number of units to allocate 249*ea61e9acSJeremy L Thompson @param[in] unit Size of each unit 250*ea61e9acSJeremy L Thompson @param[out] 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 CeedReallocArray(size_t n, size_t unit, void *p) { 259d7b241e6Sjeremylt *(void **)p = realloc(*(void **)p, n * unit); 2602b730f8bSJeremy L Thompson if (n && unit && !*(void **)p) { 261c042f62fSJeremy L Thompson // LCOV_EXCL_START 2622b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "realloc failed to allocate %zd members of size %zd\n", n, unit); 263c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 2642b730f8bSJeremy L Thompson } 265e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 266d7b241e6Sjeremylt } 267d7b241e6Sjeremylt 268f7e22acaSJeremy L Thompson /** 269f7e22acaSJeremy L Thompson @brief Allocate a cleared string buffer on the host 270f7e22acaSJeremy L Thompson 271f7e22acaSJeremy L Thompson Memory usage can be tracked by the library. 272f7e22acaSJeremy L Thompson 273*ea61e9acSJeremy L Thompson @param[in] source Pointer to string to be copied 274*ea61e9acSJeremy L Thompson @param[out] copy Pointer to variable to hold newly allocated string copy 275f7e22acaSJeremy L Thompson 276f7e22acaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 277f7e22acaSJeremy L Thompson 278f7e22acaSJeremy L Thompson @sa CeedFree() 279f7e22acaSJeremy L Thompson 280f7e22acaSJeremy L Thompson @ref Backend 281f7e22acaSJeremy L Thompson **/ 282f7e22acaSJeremy L Thompson int CeedStringAllocCopy(const char *source, char **copy) { 283f7e22acaSJeremy L Thompson size_t len = strlen(source); 2842b730f8bSJeremy L Thompson CeedCall(CeedCalloc(len + 1, copy)); 285d602d780SJeremy L Thompson memcpy(*copy, source, len); 286f7e22acaSJeremy L Thompson return CEED_ERROR_SUCCESS; 287f7e22acaSJeremy L Thompson } 288f7e22acaSJeremy L Thompson 28934138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc() 29034138859Sjeremylt 291*ea61e9acSJeremy L Thompson @param[in,out] p address of pointer to memory. 292*ea61e9acSJeremy L Thompson This argument is of type void* to avoid needing a cast, but is the address of the pointer (which is zeroed) rather than the 293*ea61e9acSJeremy L Thompson pointer. 29434138859Sjeremylt **/ 295d7b241e6Sjeremylt int CeedFree(void *p) { 296d7b241e6Sjeremylt free(*(void **)p); 297d7b241e6Sjeremylt *(void **)p = NULL; 298e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 299d7b241e6Sjeremylt } 300d7b241e6Sjeremylt 301d7b241e6Sjeremylt /** 3027a982d89SJeremy L. Thompson @brief Register a Ceed backend 303d7b241e6Sjeremylt 304*ea61e9acSJeremy L Thompson @param[in] prefix Prefix of resources for this backend to respond to. 305*ea61e9acSJeremy L Thompson For example, the reference backend responds to "/cpu/self". 306*ea61e9acSJeremy L Thompson @param[in] init Initialization function called by CeedInit() when the backend is selected to drive the requested resource. 307*ea61e9acSJeremy L Thompson @param[in] priority Integer priority. 308*ea61e9acSJeremy L Thompson Lower values are preferred in case the resource requested by CeedInit() has non-unique best prefix match. 309b11c1e72Sjeremylt 310b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 311dfdf5a53Sjeremylt 3127a982d89SJeremy L. Thompson @ref Backend 313b11c1e72Sjeremylt **/ 3142b730f8bSJeremy L Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) { 31510243053SJeremy L Thompson CeedDebugEnv("Backend Register: %s", prefix); 3166a406739SJeremy L Thompson CeedRegisterImpl(prefix, init, priority); 317e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 318d7b241e6Sjeremylt } 319d7b241e6Sjeremylt 320b11c1e72Sjeremylt /** 32160f9e2d6SJeremy L Thompson @brief Return debugging status flag 32260f9e2d6SJeremy L Thompson 323*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to get debugging flag 324*ea61e9acSJeremy L Thompson @param[out] is_debug Variable to store debugging flag 32560f9e2d6SJeremy L Thompson 32660f9e2d6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 32760f9e2d6SJeremy L Thompson 328d1d35e2fSjeremylt @ref Backend 32960f9e2d6SJeremy L Thompson **/ 330d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) { 3313f21f6b1SJeremy L Thompson *is_debug = ceed->is_debug; 332e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 33360f9e2d6SJeremy L Thompson } 33460f9e2d6SJeremy L Thompson 33560f9e2d6SJeremy L Thompson /** 3367a982d89SJeremy L. Thompson @brief Retrieve a parent Ceed context 3377a982d89SJeremy L. Thompson 338*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to retrieve parent of 3397a982d89SJeremy L. Thompson @param[out] parent Address to save the parent to 3407a982d89SJeremy L. Thompson 3417a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3427a982d89SJeremy L. Thompson 3437a982d89SJeremy L. Thompson @ref Backend 3447a982d89SJeremy L. Thompson **/ 3457a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) { 3467a982d89SJeremy L. Thompson if (ceed->parent) { 3472b730f8bSJeremy L Thompson CeedCall(CeedGetParent(ceed->parent, parent)); 348e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3497a982d89SJeremy L. Thompson } 3507a982d89SJeremy L. Thompson *parent = ceed; 351e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3527a982d89SJeremy L. Thompson } 3537a982d89SJeremy L. Thompson 3547a982d89SJeremy L. Thompson /** 3557a982d89SJeremy L. Thompson @brief Retrieve a delegate Ceed context 3567a982d89SJeremy L. Thompson 357*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to retrieve delegate of 3587a982d89SJeremy L. Thompson @param[out] delegate Address to save the delegate to 3597a982d89SJeremy L. Thompson 3607a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3617a982d89SJeremy L. Thompson 3627a982d89SJeremy L. Thompson @ref Backend 3637a982d89SJeremy L. Thompson **/ 3647a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) { 3657a982d89SJeremy L. Thompson *delegate = ceed->delegate; 366e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3677a982d89SJeremy L. Thompson } 3687a982d89SJeremy L. Thompson 3697a982d89SJeremy L. Thompson /** 3707a982d89SJeremy L. Thompson @brief Set a delegate Ceed context 3717a982d89SJeremy L. Thompson 372*ea61e9acSJeremy L Thompson This function allows a Ceed context to set a delegate Ceed context. 373*ea61e9acSJeremy L Thompson All backend implementations default to the delegate Ceed context, unless overridden. 3747a982d89SJeremy L. Thompson 375*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to set delegate of 3767a982d89SJeremy L. Thompson @param[out] delegate Address to set the delegate to 3777a982d89SJeremy L. Thompson 3787a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3797a982d89SJeremy L. Thompson 3807a982d89SJeremy L. Thompson @ref Backend 3817a982d89SJeremy L. Thompson **/ 3827a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) { 3837a982d89SJeremy L. Thompson ceed->delegate = delegate; 3847a982d89SJeremy L. Thompson delegate->parent = ceed; 385e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3867a982d89SJeremy L. Thompson } 3877a982d89SJeremy L. Thompson 3887a982d89SJeremy L. Thompson /** 3897a982d89SJeremy L. Thompson @brief Retrieve a delegate Ceed context for a specific object type 3907a982d89SJeremy L. Thompson 391*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to retrieve delegate of 3927a982d89SJeremy L. Thompson @param[out] delegate Address to save the delegate to 393d1d35e2fSjeremylt @param[in] obj_name Name of the object type to retrieve delegate for 3947a982d89SJeremy L. Thompson 3957a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3967a982d89SJeremy L. Thompson 3977a982d89SJeremy L. Thompson @ref Backend 3987a982d89SJeremy L. Thompson **/ 399d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) { 4007a982d89SJeremy L. Thompson // Check for object delegate 4012b730f8bSJeremy L Thompson for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) { 402d1d35e2fSjeremylt if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) { 403d1d35e2fSjeremylt *delegate = ceed->obj_delegates->delegate; 404e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4057a982d89SJeremy L. Thompson } 4062b730f8bSJeremy L Thompson } 4077a982d89SJeremy L. Thompson 4087a982d89SJeremy L. Thompson // Use default delegate if no object delegate 4092b730f8bSJeremy L Thompson CeedCall(CeedGetDelegate(ceed, delegate)); 410e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4117a982d89SJeremy L. Thompson } 4127a982d89SJeremy L. Thompson 4137a982d89SJeremy L. Thompson /** 4147a982d89SJeremy L. Thompson @brief Set a delegate Ceed context for a specific object type 4157a982d89SJeremy L. Thompson 416*ea61e9acSJeremy L Thompson This function allows a Ceed context to set a delegate Ceed context for a given type of Ceed object. 417*ea61e9acSJeremy L Thompson All backend implementations default to the delegate Ceed context for this object. 418*ea61e9acSJeremy L Thompson For example, CeedSetObjectDelegate(ceed, refceed, "Basis") uses refceed implementations for all CeedBasis backend functions. 4197a982d89SJeremy L. Thompson 420*ea61e9acSJeremy L Thompson @param[in,out] ceed Ceed context to set delegate of 4217a982d89SJeremy L. Thompson @param[out] delegate Address to set the delegate to 422d1d35e2fSjeremylt @param[in] obj_name Name of the object type to set delegate for 4237a982d89SJeremy L. Thompson 4247a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4257a982d89SJeremy L. Thompson 4267a982d89SJeremy L. Thompson @ref Backend 4277a982d89SJeremy L. Thompson **/ 428d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) { 429d1d35e2fSjeremylt CeedInt count = ceed->obj_delegate_count; 4307a982d89SJeremy L. Thompson 4317a982d89SJeremy L. Thompson // Malloc or Realloc 4327a982d89SJeremy L. Thompson if (count) { 4332b730f8bSJeremy L Thompson CeedCall(CeedRealloc(count + 1, &ceed->obj_delegates)); 4347a982d89SJeremy L. Thompson } else { 4352b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &ceed->obj_delegates)); 4367a982d89SJeremy L. Thompson } 437d1d35e2fSjeremylt ceed->obj_delegate_count++; 4387a982d89SJeremy L. Thompson 4397a982d89SJeremy L. Thompson // Set object delegate 440d1d35e2fSjeremylt ceed->obj_delegates[count].delegate = delegate; 4412b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(obj_name, &ceed->obj_delegates[count].obj_name)); 4427a982d89SJeremy L. Thompson 4437a982d89SJeremy L. Thompson // Set delegate parent 4447a982d89SJeremy L. Thompson delegate->parent = ceed; 445e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4467a982d89SJeremy L. Thompson } 4477a982d89SJeremy L. Thompson 4487a982d89SJeremy L. Thompson /** 4497a982d89SJeremy L. Thompson @brief Get the fallback resource for CeedOperators 4507a982d89SJeremy L. Thompson 451*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context 4527a982d89SJeremy L. Thompson @param[out] resource Variable to store fallback resource 4537a982d89SJeremy L. Thompson 4547a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4557a982d89SJeremy L. Thompson 4567a982d89SJeremy L. Thompson @ref Backend 4577a982d89SJeremy L. Thompson **/ 4587a982d89SJeremy L. Thompson 4597a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) { 460d1d35e2fSjeremylt *resource = (const char *)ceed->op_fallback_resource; 461e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4627a982d89SJeremy L. Thompson } 4637a982d89SJeremy L. Thompson 4647a982d89SJeremy L. Thompson /** 4658687e1d4SJeremy L Thompson @brief Get the fallback Ceed for CeedOperators 4668687e1d4SJeremy L Thompson 467*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context 4688687e1d4SJeremy L Thompson @param[out] fallback_ceed Variable to store fallback Ceed 4698687e1d4SJeremy L Thompson 4708687e1d4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4718687e1d4SJeremy L Thompson 4728687e1d4SJeremy L Thompson @ref Backend 4738687e1d4SJeremy L Thompson **/ 4748687e1d4SJeremy L Thompson 4758687e1d4SJeremy L Thompson int CeedGetOperatorFallbackCeed(Ceed ceed, Ceed *fallback_ceed) { 476d04bbc78SJeremy L Thompson if (ceed->has_valid_op_fallback_resource) { 477d04bbc78SJeremy L Thompson CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); 4782b730f8bSJeremy L Thompson CeedDebug(ceed, "Getting fallback from %s to %s\n", ceed->resource, ceed->op_fallback_resource); 479d04bbc78SJeremy L Thompson } 4808687e1d4SJeremy L Thompson 481d04bbc78SJeremy L Thompson // Create fallback Ceed if uninitalized 482d04bbc78SJeremy L Thompson if (!ceed->op_fallback_ceed && ceed->has_valid_op_fallback_resource) { 48313f886e9SJeremy L Thompson CeedDebug(ceed, "Creating fallback Ceed"); 484d04bbc78SJeremy L Thompson 4858687e1d4SJeremy L Thompson Ceed fallback_ceed; 486d04bbc78SJeremy L Thompson const char *fallback_resource; 487d04bbc78SJeremy L Thompson 4882b730f8bSJeremy L Thompson CeedCall(CeedGetOperatorFallbackResource(ceed, &fallback_resource)); 4892b730f8bSJeremy L Thompson CeedCall(CeedInit(fallback_resource, &fallback_ceed)); 4908687e1d4SJeremy L Thompson fallback_ceed->op_fallback_parent = ceed; 4918687e1d4SJeremy L Thompson fallback_ceed->Error = ceed->Error; 4928687e1d4SJeremy L Thompson ceed->op_fallback_ceed = fallback_ceed; 4938687e1d4SJeremy L Thompson } 4948687e1d4SJeremy L Thompson *fallback_ceed = ceed->op_fallback_ceed; 4958687e1d4SJeremy L Thompson 4968687e1d4SJeremy L Thompson return CEED_ERROR_SUCCESS; 4978687e1d4SJeremy L Thompson } 4988687e1d4SJeremy L Thompson 4998687e1d4SJeremy L Thompson /** 500*ea61e9acSJeremy L Thompson @brief Set the fallback resource for CeedOperators. 501*ea61e9acSJeremy L Thompson The current resource, if any, is freed by calling this function. 502*ea61e9acSJeremy L Thompson This string is freed upon the destruction of the Ceed context. 5037a982d89SJeremy L. Thompson 504*ea61e9acSJeremy L Thompson @param[in,out] ceed Ceed context 505*ea61e9acSJeremy L Thompson @param[in] resource Fallback resource to set 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 CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) { 5137a982d89SJeremy L. Thompson // Free old 5142b730f8bSJeremy L Thompson CeedCall(CeedFree(&ceed->op_fallback_resource)); 5157a982d89SJeremy L. Thompson 5167a982d89SJeremy L. Thompson // Set new 5172b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(resource, (char **)&ceed->op_fallback_resource)); 518d04bbc78SJeremy L Thompson 519d04bbc78SJeremy L Thompson // Check validity 5202b730f8bSJeremy L Thompson ceed->has_valid_op_fallback_resource = ceed->op_fallback_resource && ceed->resource && strcmp(ceed->op_fallback_resource, ceed->resource); 521d04bbc78SJeremy L Thompson 522e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5237a982d89SJeremy L. Thompson } 5247a982d89SJeremy L. Thompson 5257a982d89SJeremy L. Thompson /** 526*ea61e9acSJeremy L Thompson @brief Get the parent Ceed context associated with a fallback Ceed context for a CeedOperator 5277a982d89SJeremy L. Thompson 528*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context 5297a982d89SJeremy L. Thompson @param[out] parent Variable to store parent Ceed context 5307a982d89SJeremy L. Thompson 5317a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5327a982d89SJeremy L. Thompson 5337a982d89SJeremy L. Thompson @ref Backend 5347a982d89SJeremy L. Thompson **/ 5357a982d89SJeremy L. Thompson 5367a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) { 537d1d35e2fSjeremylt *parent = ceed->op_fallback_parent; 538e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5397a982d89SJeremy L. Thompson } 5407a982d89SJeremy L. Thompson 5417a982d89SJeremy L. Thompson /** 5429525855cSJeremy L Thompson @brief Flag Ceed context as deterministic 5439525855cSJeremy L Thompson 544*ea61e9acSJeremy L Thompson @param[in] ceed Ceed to flag as deterministic 54596b902e2Sjeremylt @param[out] is_deterministic Deterministic status to set 5469525855cSJeremy L Thompson 5479525855cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5489525855cSJeremy L Thompson 5499525855cSJeremy L Thompson @ref Backend 5509525855cSJeremy L Thompson **/ 5519525855cSJeremy L Thompson 552d1d35e2fSjeremylt int CeedSetDeterministic(Ceed ceed, bool is_deterministic) { 553d1d35e2fSjeremylt ceed->is_deterministic = is_deterministic; 554e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5559525855cSJeremy L Thompson } 5569525855cSJeremy L Thompson 5579525855cSJeremy L Thompson /** 5587a982d89SJeremy L. Thompson @brief Set a backend function 5597a982d89SJeremy L. Thompson 560*ea61e9acSJeremy L Thompson This function is used for a backend to set the function associated with the Ceed objects. 561*ea61e9acSJeremy L Thompson For example, CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate) sets the backend implementation of 'CeedVectorCreate' 562*ea61e9acSJeremy L Thompson and CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply) sets the backend implementation of 'CeedBasisApply'. Note, the prefix 563*ea61e9acSJeremy L Thompson 'Ceed' is not required for the object type ("Basis" vs "CeedBasis"). 5647a982d89SJeremy L. Thompson 565*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context for error handling 566*ea61e9acSJeremy L Thompson @param[in] type Type of Ceed object to set function for 5677a982d89SJeremy L. Thompson @param[out] object Ceed object to set function for 568*ea61e9acSJeremy L Thompson @param[in] func_name Name of function to set 569*ea61e9acSJeremy L Thompson @param[in] f Function to set 5707a982d89SJeremy L. Thompson 5717a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5727a982d89SJeremy L. Thompson 5737a982d89SJeremy L. Thompson @ref Backend 5747a982d89SJeremy L. Thompson **/ 5752b730f8bSJeremy L Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object, const char *func_name, int (*f)()) { 576d1d35e2fSjeremylt char lookup_name[CEED_MAX_RESOURCE_LEN + 1] = ""; 5777a982d89SJeremy L. Thompson 5787a982d89SJeremy L. Thompson // Build lookup name 5792b730f8bSJeremy L Thompson if (strcmp(type, "Ceed")) strncat(lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN); 580d1d35e2fSjeremylt strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN); 581d1d35e2fSjeremylt strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN); 5827a982d89SJeremy L. Thompson 5837a982d89SJeremy L. Thompson // Find and use offset 5842b730f8bSJeremy L Thompson for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++) { 585d1d35e2fSjeremylt if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) { 586d1d35e2fSjeremylt size_t offset = ceed->f_offsets[i].offset; 5877a982d89SJeremy L. Thompson int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD* 5887a982d89SJeremy L. Thompson *fpointer = f; 589e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5907a982d89SJeremy L. Thompson } 5912b730f8bSJeremy L Thompson } 5927a982d89SJeremy L. Thompson 5937a982d89SJeremy L. Thompson // LCOV_EXCL_START 5942b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Requested function '%s' was not found for CEED object '%s'", func_name, type); 5957a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 5967a982d89SJeremy L. Thompson } 5977a982d89SJeremy L. Thompson 5987a982d89SJeremy L. Thompson /** 5997a982d89SJeremy L. Thompson @brief Retrieve backend data for a Ceed context 6007a982d89SJeremy L. Thompson 601*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to retrieve data of 6027a982d89SJeremy L. Thompson @param[out] data Address to save data to 6037a982d89SJeremy L. Thompson 6047a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6057a982d89SJeremy L. Thompson 6067a982d89SJeremy L. Thompson @ref Backend 6077a982d89SJeremy L. Thompson **/ 608777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) { 609777ff853SJeremy L Thompson *(void **)data = ceed->data; 610e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6117a982d89SJeremy L. Thompson } 6127a982d89SJeremy L. Thompson 6137a982d89SJeremy L. Thompson /** 6147a982d89SJeremy L. Thompson @brief Set backend data for a Ceed context 6157a982d89SJeremy L. Thompson 616*ea61e9acSJeremy L Thompson @param[in,out] ceed Ceed context to set data of 617*ea61e9acSJeremy L Thompson @param[in] data Address of data to set 6187a982d89SJeremy L. Thompson 6197a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6207a982d89SJeremy L. Thompson 6217a982d89SJeremy L. Thompson @ref Backend 6227a982d89SJeremy L. Thompson **/ 623777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) { 624777ff853SJeremy L Thompson ceed->data = data; 625e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6267a982d89SJeremy L. Thompson } 6277a982d89SJeremy L. Thompson 62834359f16Sjeremylt /** 62934359f16Sjeremylt @brief Increment the reference counter for a Ceed context 63034359f16Sjeremylt 631*ea61e9acSJeremy L Thompson @param[in,out] ceed Ceed context to increment the reference counter 63234359f16Sjeremylt 63334359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 63434359f16Sjeremylt 63534359f16Sjeremylt @ref Backend 63634359f16Sjeremylt **/ 6379560d06aSjeremylt int CeedReference(Ceed ceed) { 63834359f16Sjeremylt ceed->ref_count++; 63934359f16Sjeremylt return CEED_ERROR_SUCCESS; 64034359f16Sjeremylt } 64134359f16Sjeremylt 6427a982d89SJeremy L. Thompson /// @} 6437a982d89SJeremy L. Thompson 6447a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6457a982d89SJeremy L. Thompson /// Ceed Public API 6467a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6477a982d89SJeremy L. Thompson /// @addtogroup CeedUser 6487a982d89SJeremy L. Thompson /// @{ 6497a982d89SJeremy L. Thompson 6507a982d89SJeremy L. Thompson /** 65192ee7d1cSjeremylt @brief Get the list of available resource names for Ceed contexts 652*ea61e9acSJeremy L Thompson Note: The caller is responsible for `free()`ing the resources and priorities arrays, but should not `free()` the contents of the resources 653*ea61e9acSJeremy L Thompson array. 65422e44211Sjeremylt 65592ee7d1cSjeremylt @param[out] n Number of available resources 65692ee7d1cSjeremylt @param[out] resources List of available resource names 65722e44211Sjeremylt @param[out] priorities Resource name prioritization values, lower is better 65822e44211Sjeremylt 65922e44211Sjeremylt @return An error code: 0 - success, otherwise - failure 66022e44211Sjeremylt 66122e44211Sjeremylt @ref User 66222e44211Sjeremylt **/ 66322e44211Sjeremylt // LCOV_EXCL_START 6642b730f8bSJeremy L Thompson int CeedRegistryGetList(size_t *n, char ***const resources, CeedInt **priorities) { 665d0c91ce9Sjeremylt *n = 0; 6669ff86846Sjeremylt *resources = malloc(num_backends * sizeof(**resources)); 6672b730f8bSJeremy L Thompson if (!resources) return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure"); 6689ff86846Sjeremylt if (priorities) { 6699ff86846Sjeremylt *priorities = malloc(num_backends * sizeof(**priorities)); 6702b730f8bSJeremy L Thompson if (!priorities) return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure"); 6719ff86846Sjeremylt } 67222e44211Sjeremylt for (size_t i = 0; i < num_backends; i++) { 673d0c91ce9Sjeremylt // Only report compiled backends 674d0c91ce9Sjeremylt if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) { 67522e44211Sjeremylt *resources[i] = backends[i].prefix; 6769ff86846Sjeremylt if (priorities) *priorities[i] = backends[i].priority; 677d0c91ce9Sjeremylt *n += 1; 678d0c91ce9Sjeremylt } 679d0c91ce9Sjeremylt } 6802b730f8bSJeremy L Thompson if (*n == 0) { 68178464608Sjeremylt // LCOV_EXCL_START 68278464608Sjeremylt return CeedError(NULL, CEED_ERROR_MAJOR, "No backends installed"); 68378464608Sjeremylt // LCOV_EXCL_STOP 6842b730f8bSJeremy L Thompson } 685d0c91ce9Sjeremylt *resources = realloc(*resources, *n * sizeof(**resources)); 6862b730f8bSJeremy L Thompson if (!resources) return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure"); 687d0c91ce9Sjeremylt if (priorities) { 688d0c91ce9Sjeremylt *priorities = realloc(*priorities, *n * sizeof(**priorities)); 6892b730f8bSJeremy L Thompson if (!priorities) return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure"); 69022e44211Sjeremylt } 69122e44211Sjeremylt return CEED_ERROR_SUCCESS; 69245f1e315Sjeremylt } 69322e44211Sjeremylt // LCOV_EXCL_STOP 69422e44211Sjeremylt 69522e44211Sjeremylt /** 696d79b80ecSjeremylt @brief Initialize a \ref Ceed context to use the specified resource. 697*ea61e9acSJeremy L Thompson Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self") will result in CeedInt printing the current libCEED version number and a 698*ea61e9acSJeremy L Thompson list of current available backend resources to stderr. 699b11c1e72Sjeremylt 700*ea61e9acSJeremy L Thompson @param[in] resource Resource to use, e.g., "/cpu/self" 701*ea61e9acSJeremy L Thompson @param[out] ceed The library context 702b11c1e72Sjeremylt @sa CeedRegister() CeedDestroy() 703b11c1e72Sjeremylt 704b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 705dfdf5a53Sjeremylt 7067a982d89SJeremy L. Thompson @ref User 707b11c1e72Sjeremylt **/ 708d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) { 7092b730f8bSJeremy L Thompson size_t match_len = 0, match_index = UINT_MAX, match_priority = CEED_MAX_BACKEND_PRIORITY, priority; 710d7b241e6Sjeremylt 711fe2413ffSjeremylt // Find matching backend 7122b730f8bSJeremy L Thompson if (!resource) { 71313873f79Sjeremylt // LCOV_EXCL_START 714e15f9bd0SJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided"); 71513873f79Sjeremylt // LCOV_EXCL_STOP 7162b730f8bSJeremy L Thompson } 7172b730f8bSJeremy L Thompson CeedCall(CeedRegisterAll()); 71813873f79Sjeremylt 71922e44211Sjeremylt // Check for help request 72022e44211Sjeremylt const char *help_prefix = "help"; 7212b730f8bSJeremy L Thompson size_t match_help = 0; 7222b730f8bSJeremy L Thompson while (match_help < 4 && resource[match_help] == help_prefix[match_help]) match_help++; 72322e44211Sjeremylt if (match_help == 4) { 7242b730f8bSJeremy L Thompson fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR, CEED_VERSION_MINOR, CEED_VERSION_PATCH, 72522e44211Sjeremylt CEED_VERSION_RELEASE ? "" : "+development"); 72692ee7d1cSjeremylt fprintf(stderr, "Available backend resources:\n"); 72722e44211Sjeremylt for (size_t i = 0; i < num_backends; i++) { 728d0c91ce9Sjeremylt // Only report compiled backends 7292b730f8bSJeremy L Thompson if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) fprintf(stderr, " %s\n", backends[i].prefix); 73022e44211Sjeremylt } 73122e44211Sjeremylt fflush(stderr); 73222e44211Sjeremylt match_help = 5; // Delineating character expected 73322e44211Sjeremylt } else { 73422e44211Sjeremylt match_help = 0; 73522e44211Sjeremylt } 73622e44211Sjeremylt 737*ea61e9acSJeremy L Thompson // Find best match, computed as number of matching characters from requested resource stem 7382b730f8bSJeremy L Thompson size_t stem_length = 0; 7392b730f8bSJeremy L Thompson while (resource[stem_length + match_help] && resource[stem_length + match_help] != ':') stem_length++; 740d7b241e6Sjeremylt for (size_t i = 0; i < num_backends; i++) { 7412b730f8bSJeremy L Thompson size_t n = 0; 742d7b241e6Sjeremylt const char *prefix = backends[i].prefix; 7432b730f8bSJeremy L Thompson while (prefix[n] && prefix[n] == resource[n + match_help]) n++; 744d7b241e6Sjeremylt priority = backends[i].priority; 745d1d35e2fSjeremylt if (n > match_len || (n == match_len && match_priority > priority)) { 746d1d35e2fSjeremylt match_len = n; 747d1d35e2fSjeremylt match_priority = priority; 748f7e22acaSJeremy L Thompson match_index = i; 749d7b241e6Sjeremylt } 750d7b241e6Sjeremylt } 7519c9a0587SLeila Ghaffari // Using Levenshtein distance to find closest match 7529c9a0587SLeila Ghaffari if (match_len <= 1 || match_len != stem_length) { 753203015caSLeila Ghaffari // LCOV_EXCL_START 7549c9a0587SLeila Ghaffari size_t lev_dis = UINT_MAX; 755f7e22acaSJeremy L Thompson size_t lev_index = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY; 7569c9a0587SLeila Ghaffari for (size_t i = 0; i < num_backends; i++) { 7579c9a0587SLeila Ghaffari const char *prefix = backends[i].prefix; 7589c9a0587SLeila Ghaffari size_t prefix_length = strlen(backends[i].prefix); 7599c9a0587SLeila Ghaffari size_t min_len = (prefix_length < stem_length) ? prefix_length : stem_length; 760092904ddSLeila Ghaffari size_t column[min_len + 1]; 761092904ddSLeila Ghaffari for (size_t j = 0; j <= min_len; j++) column[j] = j; 7629c9a0587SLeila Ghaffari for (size_t j = 1; j <= min_len; j++) { 7639c9a0587SLeila Ghaffari column[0] = j; 7649c9a0587SLeila Ghaffari for (size_t k = 1, last_diag = j - 1; k <= min_len; k++) { 765092904ddSLeila Ghaffari size_t old_diag = column[k]; 7669c9a0587SLeila Ghaffari size_t min_1 = (column[k] < column[k - 1]) ? column[k] + 1 : column[k - 1] + 1; 7679c9a0587SLeila Ghaffari size_t min_2 = last_diag + (resource[k - 1] == prefix[j - 1] ? 0 : 1); 7689c9a0587SLeila Ghaffari column[k] = (min_1 < min_2) ? min_1 : min_2; 7699c9a0587SLeila Ghaffari last_diag = old_diag; 7709c9a0587SLeila Ghaffari } 7719c9a0587SLeila Ghaffari } 7729c9a0587SLeila Ghaffari size_t n = column[min_len]; 7739c9a0587SLeila Ghaffari priority = backends[i].priority; 7742b730f8bSJeremy L Thompson if (n < lev_dis || (n == lev_dis && lev_priority > priority)) { 7759c9a0587SLeila Ghaffari lev_dis = n; 7769c9a0587SLeila Ghaffari lev_priority = priority; 777f7e22acaSJeremy L Thompson lev_index = i; 7789c9a0587SLeila Ghaffari } 7799c9a0587SLeila Ghaffari } 780f7e22acaSJeremy L Thompson const char *prefix_lev = backends[lev_index].prefix; 7812b730f8bSJeremy L Thompson size_t lev_length = 0; 7822b730f8bSJeremy L Thompson while (prefix_lev[lev_length] && prefix_lev[lev_length] != '\0') lev_length++; 7839c9a0587SLeila Ghaffari size_t m = (lev_length < stem_length) ? lev_length : stem_length; 7849c9a0587SLeila Ghaffari if (lev_dis + 1 >= m) { 7852b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s", resource); 7869c9a0587SLeila Ghaffari } else { 7872b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, 7882b730f8bSJeremy L Thompson "No suitable backend: %s\n" 7892b730f8bSJeremy L Thompson "Closest match: %s", 7902b730f8bSJeremy L Thompson resource, backends[lev_index].prefix); 7912bbc7fe8Sjeremylt } 792203015caSLeila Ghaffari // LCOV_EXCL_STOP 7939c9a0587SLeila Ghaffari } 794fe2413ffSjeremylt 795fe2413ffSjeremylt // Setup Ceed 7962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, ceed)); 7972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &(*ceed)->jit_source_roots)); 798bc81ce41Sjeremylt const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 7992b730f8bSJeremy L Thompson if (!ceed_error_handler) ceed_error_handler = "abort"; 8002b730f8bSJeremy L Thompson if (!strcmp(ceed_error_handler, "exit")) (*ceed)->Error = CeedErrorExit; 8012b730f8bSJeremy L Thompson else if (!strcmp(ceed_error_handler, "store")) (*ceed)->Error = CeedErrorStore; 8022b730f8bSJeremy L Thompson else (*ceed)->Error = CeedErrorAbort; 803d1d35e2fSjeremylt memcpy((*ceed)->err_msg, "No error message stored", 24); 804d1d35e2fSjeremylt (*ceed)->ref_count = 1; 805d7b241e6Sjeremylt (*ceed)->data = NULL; 806fe2413ffSjeremylt 807fe2413ffSjeremylt // Set lookup table 808d1d35e2fSjeremylt FOffset f_offsets[] = { 8096e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, Error), 8106e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType), 8116e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, Destroy), 812f8902d9eSjeremylt CEED_FTABLE_ENTRY(Ceed, VectorCreate), 8136e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate), 814fc0567d9Srezgarshakeri CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateOriented), 8156e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked), 8166e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1), 8176e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, BasisCreateH1), 81850c301a5SRezgar Shakeri CEED_FTABLE_ENTRY(Ceed, BasisCreateHdiv), 8196e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, TensorContractCreate), 8206e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, QFunctionCreate), 821777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate), 8226e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, OperatorCreate), 8236e79d475Sjeremylt CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate), 8249c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, HasValidArray), 8259c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, HasBorrowedArrayOfType), 8266e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, SetArray), 8276a6c615bSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, TakeArray), 8286e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, SetValue), 829f48ed27dSnbeams CEED_FTABLE_ENTRY(CeedVector, SyncArray), 8306e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, GetArray), 8316e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, GetArrayRead), 8329c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, GetArrayWrite), 8336e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, RestoreArray), 8346e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead), 835547d9b97Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Norm), 836e0dd3b27Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Scale), 8370f7fd0f8Sjeremylt CEED_FTABLE_ENTRY(CeedVector, AXPY), 8380f7fd0f8Sjeremylt CEED_FTABLE_ENTRY(CeedVector, PointwiseMult), 839d99fa3c5SJeremy L Thompson CEED_FTABLE_ENTRY(CeedVector, Reciprocal), 8406e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedVector, Destroy), 8416e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, Apply), 8426e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock), 843bd33150aSjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets), 8446e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy), 8456e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedBasis, Apply), 8466e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedBasis, Destroy), 8476e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedTensorContract, Apply), 8486e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedTensorContract, Destroy), 8496e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, Apply), 8508c84ac63Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction), 8518c84ac63Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction), 8526e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedQFunction, Destroy), 8539c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, HasValidData), 8549c774eddSJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, HasBorrowedDataOfType), 855777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData), 856891038deSjeremylt CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData), 857777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData), 85828bfd0b7SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, GetDataRead), 859777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData), 86028bfd0b7SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreDataRead), 8612e64a2b9SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, DataDestroy), 862777ff853SJeremy L Thompson CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy), 86380ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction), 86470a7ffb3SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate), 86580ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal), 8669e9210b8SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal), 86780ac2e43SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal), 8689e9210b8SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal), 869e2f04181SAndrew T. Barker CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic), 870e2f04181SAndrew T. Barker CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble), 871cefa2673SJeremy L Thompson CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSingle), 872713f43c3Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse), 8736e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, Apply), 874250756a7Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite), 875cae8b89aSjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd), 876250756a7Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite), 8776e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian), 8786e79d475Sjeremylt CEED_FTABLE_ENTRY(CeedOperator, Destroy), 8796e79d475Sjeremylt {NULL, 0} // End of lookup table - used in SetBackendFunction loop 8801dfeef1dSjeremylt }; 881fe2413ffSjeremylt 8822b730f8bSJeremy L Thompson CeedCall(CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets)); 883d1d35e2fSjeremylt memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets)); 884fe2413ffSjeremylt 8855107b09fSJeremy L Thompson // Set fallback for advanced CeedOperator functions 886e2f04181SAndrew T. Barker const char fallbackresource[] = ""; 8872b730f8bSJeremy L Thompson CeedCall(CeedSetOperatorFallbackResource(*ceed, fallbackresource)); 8885107b09fSJeremy L Thompson 88960f9e2d6SJeremy L Thompson // Record env variables CEED_DEBUG or DBG 8902b730f8bSJeremy L Thompson (*ceed)->is_debug = !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG"); 89160f9e2d6SJeremy L Thompson 89222e44211Sjeremylt // Copy resource prefix, if backend setup successful 8932b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(backends[match_index].prefix, (char **)&(*ceed)->resource)); 894ee5a26f2SJeremy L Thompson 895ee5a26f2SJeremy L Thompson // Set default JiT source root 896*ea61e9acSJeremy L Thompson // Note: there will always be the default root for every Ceed but all additional paths are added to the top-most parent 8972b730f8bSJeremy L Thompson CeedCall(CeedAddJitSourceRoot(*ceed, (char *)CeedJitSourceRootDefault)); 898ee5a26f2SJeremy L Thompson 899d04bbc78SJeremy L Thompson // Backend specific setup 9002b730f8bSJeremy L Thompson CeedCall(backends[match_index].init(&resource[match_help], *ceed)); 901d04bbc78SJeremy L Thompson 902e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 903d7b241e6Sjeremylt } 904d7b241e6Sjeremylt 905d7b241e6Sjeremylt /** 906*ea61e9acSJeremy L Thompson @brief Copy the pointer to a Ceed context. 907*ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedDestroy()`; 908*ea61e9acSJeremy L Thompson Note: If `*ceed_copy` is non-NULL, then it is assumed that `*ceed_copy` is a pointer to a Ceed context. 909*ea61e9acSJeremy L Thompson This Ceed context will be destroyed if `*ceed_copy` is the only reference to this Ceed context. 9109560d06aSjeremylt 911*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to copy reference to 912*ea61e9acSJeremy L Thompson @param[in,out] ceed_copy Variable to store copied reference 9139560d06aSjeremylt 9149560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 9159560d06aSjeremylt 9169560d06aSjeremylt @ref User 9179560d06aSjeremylt **/ 9189560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) { 9192b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 9202b730f8bSJeremy L Thompson CeedCall(CeedDestroy(ceed_copy)); 9219560d06aSjeremylt *ceed_copy = ceed; 9229560d06aSjeremylt return CEED_ERROR_SUCCESS; 9239560d06aSjeremylt } 9249560d06aSjeremylt 9259560d06aSjeremylt /** 9267a982d89SJeremy L. Thompson @brief Get the full resource name for a Ceed context 9272f86a920SJeremy L Thompson 928*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to get resource name of 9297a982d89SJeremy L. Thompson @param[out] resource Variable to store resource name 9302f86a920SJeremy L Thompson 9312f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9322f86a920SJeremy L Thompson 9337a982d89SJeremy L. Thompson @ref User 9345107b09fSJeremy L Thompson **/ 9357a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) { 9367a982d89SJeremy L. Thompson *resource = (const char *)ceed->resource; 937e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9385107b09fSJeremy L Thompson } 9395107b09fSJeremy L Thompson 9405107b09fSJeremy L Thompson /** 941d79b80ecSjeremylt @brief Return Ceed context preferred memory type 942c907536fSjeremylt 943*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to get preferred memory type of 944d1d35e2fSjeremylt @param[out] mem_type Address to save preferred memory type to 945c907536fSjeremylt 946c907536fSjeremylt @return An error code: 0 - success, otherwise - failure 947c907536fSjeremylt 9487a982d89SJeremy L. Thompson @ref User 949c907536fSjeremylt **/ 950d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) { 951c907536fSjeremylt if (ceed->GetPreferredMemType) { 9522b730f8bSJeremy L Thompson CeedCall(ceed->GetPreferredMemType(mem_type)); 953c907536fSjeremylt } else { 954c263cd57Sjeremylt Ceed delegate; 9552b730f8bSJeremy L Thompson CeedCall(CeedGetDelegate(ceed, &delegate)); 956c263cd57Sjeremylt 957c263cd57Sjeremylt if (delegate) { 9582b730f8bSJeremy L Thompson CeedCall(CeedGetPreferredMemType(delegate, mem_type)); 959c263cd57Sjeremylt } else { 960d1d35e2fSjeremylt *mem_type = CEED_MEM_HOST; 961c907536fSjeremylt } 962c263cd57Sjeremylt } 963e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 964c907536fSjeremylt } 965c907536fSjeremylt 966c907536fSjeremylt /** 9679525855cSJeremy L Thompson @brief Get deterministic status of Ceed 9689525855cSJeremy L Thompson 9699525855cSJeremy L Thompson @param[in] ceed Ceed 970d1d35e2fSjeremylt @param[out] is_deterministic Variable to store deterministic status 9719525855cSJeremy L Thompson 9729525855cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9739525855cSJeremy L Thompson 9749525855cSJeremy L Thompson @ref User 9759525855cSJeremy L Thompson **/ 976d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) { 977d1d35e2fSjeremylt *is_deterministic = ceed->is_deterministic; 978e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9799525855cSJeremy L Thompson } 9809525855cSJeremy L Thompson 9819525855cSJeremy L Thompson /** 982ee5a26f2SJeremy L Thompson @brief Set additional JiT source root for Ceed 983ee5a26f2SJeremy L Thompson 984*ea61e9acSJeremy L Thompson @param[in,out] ceed Ceed 985ee5a26f2SJeremy L Thompson @param[in] jit_source_root Absolute path to additional JiT source directory 986ee5a26f2SJeremy L Thompson 987ee5a26f2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 988ee5a26f2SJeremy L Thompson 989ee5a26f2SJeremy L Thompson @ref User 990ee5a26f2SJeremy L Thompson **/ 991ee5a26f2SJeremy L Thompson int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root) { 9926155f12fSJeremy L Thompson Ceed ceed_parent; 993ee5a26f2SJeremy L Thompson 9942b730f8bSJeremy L Thompson CeedCall(CeedGetParent(ceed, &ceed_parent)); 9956155f12fSJeremy L Thompson 9966155f12fSJeremy L Thompson CeedInt index = ceed_parent->num_jit_source_roots; 997ee5a26f2SJeremy L Thompson size_t path_length = strlen(jit_source_root); 9982b730f8bSJeremy L Thompson CeedCall(CeedRealloc(index + 1, &ceed_parent->jit_source_roots)); 9992b730f8bSJeremy L Thompson CeedCall(CeedCalloc(path_length + 1, &ceed_parent->jit_source_roots[index])); 1000d602d780SJeremy L Thompson memcpy(ceed_parent->jit_source_roots[index], jit_source_root, path_length); 10016155f12fSJeremy L Thompson ceed_parent->num_jit_source_roots++; 1002ee5a26f2SJeremy L Thompson 1003ee5a26f2SJeremy L Thompson return CEED_ERROR_SUCCESS; 1004ee5a26f2SJeremy L Thompson } 1005ee5a26f2SJeremy L Thompson 1006ee5a26f2SJeremy L Thompson /** 10070a0da059Sjeremylt @brief View a Ceed 10080a0da059Sjeremylt 10090a0da059Sjeremylt @param[in] ceed Ceed to view 10100a0da059Sjeremylt @param[in] stream Filestream to write to 10110a0da059Sjeremylt 10120a0da059Sjeremylt @return An error code: 0 - success, otherwise - failure 10130a0da059Sjeremylt 10140a0da059Sjeremylt @ref User 10150a0da059Sjeremylt **/ 10160a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) { 1017d1d35e2fSjeremylt CeedMemType mem_type; 10180a0da059Sjeremylt 10192b730f8bSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); 10200a0da059Sjeremylt 10212b730f8bSJeremy L Thompson fprintf(stream, 10222b730f8bSJeremy L Thompson "Ceed\n" 10230a0da059Sjeremylt " Ceed Resource: %s\n" 10240a0da059Sjeremylt " Preferred MemType: %s\n", 1025d1d35e2fSjeremylt ceed->resource, CeedMemTypes[mem_type]); 1026e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10270a0da059Sjeremylt } 10280a0da059Sjeremylt 10290a0da059Sjeremylt /** 1030b11c1e72Sjeremylt @brief Destroy a Ceed context 1031d7b241e6Sjeremylt 1032*ea61e9acSJeremy L Thompson @param[in,out] ceed Address of Ceed context to destroy 1033b11c1e72Sjeremylt 1034b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1035dfdf5a53Sjeremylt 10367a982d89SJeremy L. Thompson @ref User 1037b11c1e72Sjeremylt **/ 1038d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) { 1039d1d35e2fSjeremylt if (!*ceed || --(*ceed)->ref_count > 0) return CEED_ERROR_SUCCESS; 10402b730f8bSJeremy L Thompson if ((*ceed)->delegate) CeedCall(CeedDestroy(&(*ceed)->delegate)); 10410ace9bf2Sjeremylt 1042d1d35e2fSjeremylt if ((*ceed)->obj_delegate_count > 0) { 104392ae7e47SJeremy L Thompson for (CeedInt i = 0; i < (*ceed)->obj_delegate_count; i++) { 10442b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&((*ceed)->obj_delegates[i].delegate))); 10452b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->obj_delegates[i].obj_name)); 1046aefd8378Sjeremylt } 10472b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->obj_delegates)); 1048aefd8378Sjeremylt } 10490ace9bf2Sjeremylt 10502b730f8bSJeremy L Thompson if ((*ceed)->Destroy) CeedCall((*ceed)->Destroy(*ceed)); 10510ace9bf2Sjeremylt 105292ae7e47SJeremy L Thompson for (CeedInt i = 0; i < (*ceed)->num_jit_source_roots; i++) { 10532b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->jit_source_roots[i])); 1054032e71eaSJeremy L Thompson } 10552b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->jit_source_roots)); 1056032e71eaSJeremy L Thompson 10572b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->f_offsets)); 10582b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->resource)); 10592b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*ceed)->op_fallback_ceed)); 10602b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ceed)->op_fallback_resource)); 10612b730f8bSJeremy L Thompson CeedCall(CeedFree(ceed)); 1062e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1063d7b241e6Sjeremylt } 1064d7b241e6Sjeremylt 1065f9982c62SWill Pazner // LCOV_EXCL_START 1066f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) { 10672b730f8bSJeremy L Thompson if (ceed->parent) return CeedErrorFormat(ceed->parent, format, args); 10682b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedErrorFormat(ceed->op_fallback_parent, format, args); 106978464608Sjeremylt // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized 107078464608Sjeremylt vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args); // NOLINT 1071d1d35e2fSjeremylt return ceed->err_msg; 1072f9982c62SWill Pazner } 1073f9982c62SWill Pazner // LCOV_EXCL_STOP 1074f9982c62SWill Pazner 10757a982d89SJeremy L. Thompson /** 10767a982d89SJeremy L. Thompson @brief Error handling implementation; use \ref CeedError instead. 10777a982d89SJeremy L. Thompson 10787a982d89SJeremy L. Thompson @ref Developer 10797a982d89SJeremy L. Thompson **/ 10802b730f8bSJeremy L Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func, int ecode, const char *format, ...) { 10817a982d89SJeremy L. Thompson va_list args; 1082d1d35e2fSjeremylt int ret_val; 10837a982d89SJeremy L. Thompson va_start(args, format); 10847a982d89SJeremy L. Thompson if (ceed) { 1085d1d35e2fSjeremylt ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args); 10867a982d89SJeremy L. Thompson } else { 1087b0d62198Sjeremylt // LCOV_EXCL_START 1088477729cfSJeremy L Thompson const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER"); 10892b730f8bSJeremy L Thompson if (!ceed_error_handler) ceed_error_handler = "abort"; 10902b730f8bSJeremy L Thompson if (!strcmp(ceed_error_handler, "return")) ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args); 1091477729cfSJeremy L Thompson else 1092477729cfSJeremy L Thompson // This function will not return 1093d1d35e2fSjeremylt ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args); 10947a982d89SJeremy L. Thompson } 10957a982d89SJeremy L. Thompson va_end(args); 1096d1d35e2fSjeremylt return ret_val; 1097b0d62198Sjeremylt // LCOV_EXCL_STOP 10987a982d89SJeremy L. Thompson } 10997a982d89SJeremy L. Thompson 1100477729cfSJeremy L Thompson /** 1101477729cfSJeremy L Thompson @brief Error handler that returns without printing anything. 1102477729cfSJeremy L Thompson 1103477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1104477729cfSJeremy L Thompson 1105477729cfSJeremy L Thompson @ref Developer 1106477729cfSJeremy L Thompson **/ 1107477729cfSJeremy L Thompson // LCOV_EXCL_START 11082b730f8bSJeremy L Thompson int CeedErrorReturn(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 1109d1d35e2fSjeremylt return err_code; 1110477729cfSJeremy L Thompson } 1111477729cfSJeremy L Thompson // LCOV_EXCL_STOP 1112477729cfSJeremy L Thompson 1113477729cfSJeremy L Thompson /** 1114*ea61e9acSJeremy L Thompson @brief Error handler that stores the error message for future use and returns the error. 1115477729cfSJeremy L Thompson 1116477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1117477729cfSJeremy L Thompson 1118477729cfSJeremy L Thompson @ref Developer 1119477729cfSJeremy L Thompson **/ 1120477729cfSJeremy L Thompson // LCOV_EXCL_START 11212b730f8bSJeremy L Thompson int CeedErrorStore(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 11222b730f8bSJeremy L Thompson if (ceed->parent) return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format, args); 11232b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func, err_code, format, args); 1124477729cfSJeremy L Thompson 1125477729cfSJeremy L Thompson // Build message 1126990fdeb6SJeremy L Thompson int len; 11272b730f8bSJeremy L Thompson len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ", filename, line_no, func); 112878464608Sjeremylt // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized 112978464608Sjeremylt vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args); // NOLINT 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 prints to stderr and aborts 1136477729cfSJeremy L Thompson 1137477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1138477729cfSJeremy L Thompson 1139477729cfSJeremy L Thompson @ref Developer 1140477729cfSJeremy L Thompson **/ 1141477729cfSJeremy L Thompson // LCOV_EXCL_START 11422b730f8bSJeremy L Thompson int CeedErrorAbort(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 1143d1d35e2fSjeremylt fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func); 1144f9982c62SWill Pazner vfprintf(stderr, format, *args); 1145477729cfSJeremy L Thompson fprintf(stderr, "\n"); 1146477729cfSJeremy L Thompson abort(); 1147d1d35e2fSjeremylt return err_code; 1148477729cfSJeremy L Thompson } 1149477729cfSJeremy L Thompson // LCOV_EXCL_STOP 1150477729cfSJeremy L Thompson 1151477729cfSJeremy L Thompson /** 1152477729cfSJeremy L Thompson @brief Error handler that prints to stderr and exits 1153477729cfSJeremy L Thompson 1154477729cfSJeremy L Thompson Pass this to CeedSetErrorHandler() to obtain this error handling behavior. 1155477729cfSJeremy L Thompson 1156*ea61e9acSJeremy L Thompson In contrast to CeedErrorAbort(), this exits without a signal, so atexit() handlers (e.g., as used by gcov) are run. 1157477729cfSJeremy L Thompson 1158477729cfSJeremy L Thompson @ref Developer 1159477729cfSJeremy L Thompson **/ 11602b730f8bSJeremy L Thompson int CeedErrorExit(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) { 1161d1d35e2fSjeremylt fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func); 116278464608Sjeremylt // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized 116378464608Sjeremylt vfprintf(stderr, format, *args); // NOLINT 1164477729cfSJeremy L Thompson fprintf(stderr, "\n"); 1165d1d35e2fSjeremylt exit(err_code); 1166d1d35e2fSjeremylt return err_code; 1167477729cfSJeremy L Thompson } 1168477729cfSJeremy L Thompson 1169477729cfSJeremy L Thompson /** 1170477729cfSJeremy L Thompson @brief Set error handler 1171477729cfSJeremy L Thompson 1172*ea61e9acSJeremy L Thompson A default error handler is set in CeedInit(). 1173*ea61e9acSJeremy L Thompson Use this function to change the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined error handler. 1174477729cfSJeremy L Thompson 1175477729cfSJeremy L Thompson @ref Developer 1176477729cfSJeremy L Thompson **/ 1177d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) { 1178d1d35e2fSjeremylt ceed->Error = handler; 1179d1d35e2fSjeremylt if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler); 11802b730f8bSJeremy L Thompson for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler); 1181e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1182477729cfSJeremy L Thompson } 1183477729cfSJeremy L Thompson 1184477729cfSJeremy L Thompson /** 1185477729cfSJeremy L Thompson @brief Get error message 1186477729cfSJeremy L Thompson 1187*ea61e9acSJeremy L Thompson The error message is only stored when using the error handler CeedErrorStore() 1188477729cfSJeremy L Thompson 1189*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to retrieve error message 1190d1d35e2fSjeremylt @param[out] err_msg Char pointer to hold error message 1191477729cfSJeremy L Thompson 1192477729cfSJeremy L Thompson @ref Developer 1193477729cfSJeremy L Thompson **/ 1194d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) { 11952b730f8bSJeremy L Thompson if (ceed->parent) return CeedGetErrorMessage(ceed->parent, err_msg); 11962b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg); 1197d1d35e2fSjeremylt *err_msg = ceed->err_msg; 1198e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1199477729cfSJeremy L Thompson } 1200477729cfSJeremy L Thompson 1201477729cfSJeremy L Thompson /** 1202477729cfSJeremy L Thompson @brief Restore error message 1203477729cfSJeremy L Thompson 1204*ea61e9acSJeremy L Thompson The error message is only stored when using the error handler CeedErrorStore() 1205477729cfSJeremy L Thompson 1206*ea61e9acSJeremy L Thompson @param[in] ceed Ceed context to restore error message 1207d1d35e2fSjeremylt @param[out] err_msg Char pointer that holds error message 1208477729cfSJeremy L Thompson 1209477729cfSJeremy L Thompson @ref Developer 1210477729cfSJeremy L Thompson **/ 1211d1d35e2fSjeremylt int CeedResetErrorMessage(Ceed ceed, const char **err_msg) { 12122b730f8bSJeremy L Thompson if (ceed->parent) return CeedResetErrorMessage(ceed->parent, err_msg); 12132b730f8bSJeremy L Thompson if (ceed->op_fallback_parent) return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg); 1214d1d35e2fSjeremylt *err_msg = NULL; 1215d1d35e2fSjeremylt memcpy(ceed->err_msg, "No error message stored", 24); 1216e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1217477729cfSJeremy L Thompson } 1218477729cfSJeremy L Thompson 12191070991dSJed Brown /** 12201070991dSJed Brown @brief Get libCEED library version info 12211070991dSJed Brown 1222*ea61e9acSJeremy L Thompson libCEED version numbers have the form major.minor.patch. 1223*ea61e9acSJeremy L Thompson Non-release versions may contain unstable interfaces. 12241070991dSJed Brown 12251070991dSJed Brown @param[out] major Major version of the library 12261070991dSJed Brown @param[out] minor Minor version of the library 12271070991dSJed Brown @param[out] patch Patch (subminor) version of the library 12281070991dSJed Brown @param[out] release True for releases; false for development branches. 12291070991dSJed Brown 12301070991dSJed Brown The caller may pass NULL for any arguments that are not needed. 12311070991dSJed Brown 12321070991dSJed Brown @sa CEED_VERSION_GE() 12331070991dSJed Brown 12341070991dSJed Brown @ref Developer 12351070991dSJed Brown */ 12361070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) { 12371070991dSJed Brown if (major) *major = CEED_VERSION_MAJOR; 12381070991dSJed Brown if (minor) *minor = CEED_VERSION_MINOR; 12391070991dSJed Brown if (patch) *patch = CEED_VERSION_PATCH; 12401070991dSJed Brown if (release) *release = CEED_VERSION_RELEASE; 12411070991dSJed Brown return 0; 12421070991dSJed Brown } 12431070991dSJed Brown 124480a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) { 124580a9ef05SNatalie Beams *scalar_type = CEED_SCALAR_TYPE; 124680a9ef05SNatalie Beams return 0; 124780a9ef05SNatalie Beams } 124880a9ef05SNatalie Beams 1249d7b241e6Sjeremylt /// @} 1250