xref: /libCEED/interface/ceed.c (revision ecc88aeb6e47c91d01ac230341895a796d10ab95)
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)                     \
31b559d91bSJed Brown   {                                                          \
32b559d91bSJed Brown #class #method, offsetof(struct class##_private, method) \
33b559d91bSJed Brown   }
34d7b241e6Sjeremylt /// @endcond
35d7b241e6Sjeremylt 
36d7b241e6Sjeremylt /// @file
37d7b241e6Sjeremylt /// Implementation of core components of Ceed library
387a982d89SJeremy L. Thompson 
397a982d89SJeremy L. Thompson /// @addtogroup CeedUser
40d7b241e6Sjeremylt /// @{
41d7b241e6Sjeremylt 
42dfdf5a53Sjeremylt /**
43dfdf5a53Sjeremylt   @brief Request immediate completion
44dfdf5a53Sjeremylt 
45ea61e9acSJeremy L Thompson   This predefined constant is passed as the \ref CeedRequest argument to interfaces when the caller wishes for the operation to be performed
46dfdf5a53Sjeremylt immediately. The code
47dfdf5a53Sjeremylt 
48dfdf5a53Sjeremylt   @code
49dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE);
50dfdf5a53Sjeremylt   @endcode
51dfdf5a53Sjeremylt 
52dfdf5a53Sjeremylt   is semantically equivalent to
53dfdf5a53Sjeremylt 
54dfdf5a53Sjeremylt   @code
55dfdf5a53Sjeremylt     CeedRequest request;
56dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., &request);
57dfdf5a53Sjeremylt     CeedRequestWait(&request);
58dfdf5a53Sjeremylt   @endcode
59dfdf5a53Sjeremylt 
60dfdf5a53Sjeremylt   @sa CEED_REQUEST_ORDERED
61dfdf5a53Sjeremylt **/
62d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate;
63d7b241e6Sjeremylt 
64d7b241e6Sjeremylt /**
65b11c1e72Sjeremylt   @brief Request ordered completion
66d7b241e6Sjeremylt 
67ea61e9acSJeremy 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
68ea61e9acSJeremy L Thompson   order that it is submitted to the device. It is typically used in a construct such as
69d7b241e6Sjeremylt 
70d7b241e6Sjeremylt   @code
71d7b241e6Sjeremylt     CeedRequest request;
72d7b241e6Sjeremylt     CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED);
73d7b241e6Sjeremylt     CeedOperatorApply(op2, ..., &request);
74d7b241e6Sjeremylt     // other optional work
758b2d6f4aSMatthew Knepley     CeedRequestWait(&request);
76d7b241e6Sjeremylt   @endcode
77d7b241e6Sjeremylt 
78ea61e9acSJeremy L Thompson   which allows the sequence to complete asynchronously but does not start `op2` until `op1` has completed.
79d7b241e6Sjeremylt 
80ea61e9acSJeremy L Thompson   @todo The current implementation is overly strict, offering equivalent semantics to @ref CEED_REQUEST_IMMEDIATE.
81d7b241e6Sjeremylt 
82d7b241e6Sjeremylt   @sa CEED_REQUEST_IMMEDIATE
83d7b241e6Sjeremylt  */
84d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered;
85d7b241e6Sjeremylt 
86b11c1e72Sjeremylt /**
877a982d89SJeremy L. Thompson   @brief Wait for a CeedRequest to complete.
88dfdf5a53Sjeremylt 
897a982d89SJeremy L. Thompson   Calling CeedRequestWait on a NULL request is a no-op.
907a982d89SJeremy L. Thompson 
917a982d89SJeremy L. Thompson   @param req Address of CeedRequest to wait for; zeroed on completion.
927a982d89SJeremy L. Thompson 
937a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
947a982d89SJeremy L. Thompson 
957a982d89SJeremy L. Thompson   @ref User
96b11c1e72Sjeremylt **/
977a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) {
982b730f8bSJeremy L Thompson   if (!*req) return CEED_ERROR_SUCCESS;
992b730f8bSJeremy L Thompson   return CeedError(NULL, CEED_ERROR_UNSUPPORTED, "CeedRequestWait not implemented");
100683faae0SJed Brown }
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson /// @}
1037a982d89SJeremy L. Thompson 
1047a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1057a982d89SJeremy L. Thompson /// Ceed Library Internal Functions
1067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1077a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper
1087a982d89SJeremy L. Thompson /// @{
109d7b241e6Sjeremylt 
1106a406739SJeremy L Thompson /**
1116a406739SJeremy L Thompson   @brief Register a Ceed backend internally.
1126a406739SJeremy L Thompson            Note: Backends should call `CeedRegister` instead.
1136a406739SJeremy L Thompson 
114ea61e9acSJeremy L Thompson   @param[in] prefix    Prefix of resources for this backend to respond to.
115ea61e9acSJeremy L Thompson                          For example, the reference backend responds to "/cpu/self".
116ea61e9acSJeremy L Thompson   @param[in] init      Initialization function called by CeedInit() when the backend is selected to drive the requested resource.
117ea61e9acSJeremy L Thompson   @param[in] priority  Integer priority.
118ea61e9acSJeremy L Thompson                          Lower values are preferred in case the resource requested by CeedInit() has non-unique best prefix match.
1196a406739SJeremy L Thompson 
1206a406739SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1216a406739SJeremy L Thompson 
1226a406739SJeremy L Thompson   @ref Developer
1236a406739SJeremy L Thompson **/
1242b730f8bSJeremy L Thompson int CeedRegisterImpl(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) {
125*ecc88aebSJeremy L Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0])) {
1266a406739SJeremy L Thompson     // LCOV_EXCL_START
1276a406739SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many backends");
1286a406739SJeremy L Thompson     // LCOV_EXCL_STOP
129*ecc88aebSJeremy L Thompson   }
1306a406739SJeremy L Thompson 
1316a406739SJeremy L Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
1326a406739SJeremy L Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN - 1] = 0;
1336a406739SJeremy L Thompson   backends[num_backends].init                              = init;
1346a406739SJeremy L Thompson   backends[num_backends].priority                          = priority;
1356a406739SJeremy L Thompson   num_backends++;
1366a406739SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1376a406739SJeremy L Thompson }
1386a406739SJeremy L Thompson 
1397a982d89SJeremy L. Thompson /// @}
140d7b241e6Sjeremylt 
1417a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1427a982d89SJeremy L. Thompson /// Ceed Backend API
1437a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1447a982d89SJeremy L. Thompson /// @addtogroup CeedBackend
1457a982d89SJeremy L. Thompson /// @{
146d7b241e6Sjeremylt 
147b11c1e72Sjeremylt /**
1483f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
14960f9e2d6SJeremy L Thompson 
150ea61e9acSJeremy L Thompson   @param[in] ceed Ceed context
15160f9e2d6SJeremy L Thompson 
1523f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1533f21f6b1SJeremy L Thompson                          false - debugging mode disabled
15460f9e2d6SJeremy L Thompson 
15560f9e2d6SJeremy L Thompson   @ref Backend
15660f9e2d6SJeremy L Thompson **/
157fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
1582b730f8bSJeremy L Thompson bool CeedDebugFlag(const Ceed ceed) { return ceed->is_debug; }
159fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
16060f9e2d6SJeremy L Thompson 
16160f9e2d6SJeremy L Thompson /**
1623f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
16360f9e2d6SJeremy L Thompson 
1643f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1653f21f6b1SJeremy L Thompson                          false - debugging mode disabled
1663f21f6b1SJeremy L Thompson 
1673f21f6b1SJeremy L Thompson   @ref Backend
1683f21f6b1SJeremy L Thompson **/
1693f21f6b1SJeremy L Thompson // LCOV_EXCL_START
1702b730f8bSJeremy L Thompson bool CeedDebugFlagEnv(void) { return !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG"); }
1713f21f6b1SJeremy L Thompson // LCOV_EXCL_STOP
1723f21f6b1SJeremy L Thompson 
1733f21f6b1SJeremy L Thompson /**
1743f21f6b1SJeremy L Thompson   @brief Print debugging information in color
1753f21f6b1SJeremy L Thompson 
17660f9e2d6SJeremy L Thompson   @param color   Color to print
17760f9e2d6SJeremy L Thompson   @param format  Printing format
17860f9e2d6SJeremy L Thompson 
17960f9e2d6SJeremy L Thompson   @ref Backend
18060f9e2d6SJeremy L Thompson **/
181fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
1823f21f6b1SJeremy L Thompson void CeedDebugImpl256(const unsigned char color, const char *format, ...) {
18360f9e2d6SJeremy L Thompson   va_list args;
18460f9e2d6SJeremy L Thompson   va_start(args, format);
18560f9e2d6SJeremy L Thompson   fflush(stdout);
1862b730f8bSJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[38;5;%dm", color);
18760f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
1882b730f8bSJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[m");
18960f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
19060f9e2d6SJeremy L Thompson   fflush(stdout);
19160f9e2d6SJeremy L Thompson   va_end(args);
19260f9e2d6SJeremy L Thompson }
193fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
19460f9e2d6SJeremy L Thompson 
19560f9e2d6SJeremy L Thompson /**
196b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
197b11c1e72Sjeremylt 
198ea61e9acSJeremy L Thompson   Memory usage can be tracked by the library.
199ea61e9acSJeremy L Thompson   This ensures sufficient alignment for vectorization and should be used for large allocations.
200b11c1e72Sjeremylt 
201ea61e9acSJeremy L Thompson   @param[in]  n    Number of units to allocate
202ea61e9acSJeremy L Thompson   @param[in]  unit Size of each unit
203ea61e9acSJeremy L Thompson   @param[out] p    Address of pointer to hold the result.
204b11c1e72Sjeremylt 
205b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
206b11c1e72Sjeremylt 
207b11c1e72Sjeremylt   @sa CeedFree()
208dfdf5a53Sjeremylt 
2097a982d89SJeremy L. Thompson   @ref Backend
210b11c1e72Sjeremylt **/
211d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
212d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n * unit);
2132b730f8bSJeremy L Thompson   if (ierr) {
214c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2152b730f8bSJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "posix_memalign failed to allocate %zd members of size %zd\n", n, unit);
216c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
2172b730f8bSJeremy L Thompson   }
218e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
219d7b241e6Sjeremylt }
220d7b241e6Sjeremylt 
221b11c1e72Sjeremylt /**
222b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
223b11c1e72Sjeremylt 
224b11c1e72Sjeremylt   Memory usage can be tracked by the library.
225b11c1e72Sjeremylt 
226ea61e9acSJeremy L Thompson   @param[in]  n    Number of units to allocate
227ea61e9acSJeremy L Thompson   @param[in]  unit Size of each unit
228ea61e9acSJeremy L Thompson   @param[out] p    Address of pointer to hold the result.
229b11c1e72Sjeremylt 
230b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
231b11c1e72Sjeremylt 
232b11c1e72Sjeremylt   @sa CeedFree()
233dfdf5a53Sjeremylt 
2347a982d89SJeremy L. Thompson   @ref Backend
235b11c1e72Sjeremylt **/
236d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
237d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
2382b730f8bSJeremy L Thompson   if (n && unit && !*(void **)p) {
239c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2402b730f8bSJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "calloc failed to allocate %zd members of size %zd\n", n, unit);
241c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
2422b730f8bSJeremy L Thompson   }
243e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
244d7b241e6Sjeremylt }
245d7b241e6Sjeremylt 
246b11c1e72Sjeremylt /**
247b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
248b11c1e72Sjeremylt 
249b11c1e72Sjeremylt   Memory usage can be tracked by the library.
250b11c1e72Sjeremylt 
251ea61e9acSJeremy L Thompson   @param[in]  n    Number of units to allocate
252ea61e9acSJeremy L Thompson   @param[in]  unit Size of each unit
253ea61e9acSJeremy L Thompson   @param[out] p    Address of pointer to hold the result.
254b11c1e72Sjeremylt 
255b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
256b11c1e72Sjeremylt 
257b11c1e72Sjeremylt   @sa CeedFree()
258dfdf5a53Sjeremylt 
2597a982d89SJeremy L. Thompson   @ref Backend
260b11c1e72Sjeremylt **/
261d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
262d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n * unit);
2632b730f8bSJeremy L Thompson   if (n && unit && !*(void **)p) {
264c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2652b730f8bSJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "realloc failed to allocate %zd members of size %zd\n", n, unit);
266c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
2672b730f8bSJeremy L Thompson   }
268e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
269d7b241e6Sjeremylt }
270d7b241e6Sjeremylt 
271f7e22acaSJeremy L Thompson /**
272f7e22acaSJeremy L Thompson   @brief Allocate a cleared string buffer on the host
273f7e22acaSJeremy L Thompson 
274f7e22acaSJeremy L Thompson   Memory usage can be tracked by the library.
275f7e22acaSJeremy L Thompson 
276ea61e9acSJeremy L Thompson   @param[in]  source Pointer to string to be copied
277ea61e9acSJeremy L Thompson   @param[out] copy   Pointer to variable to hold newly allocated string copy
278f7e22acaSJeremy L Thompson 
279f7e22acaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
280f7e22acaSJeremy L Thompson 
281f7e22acaSJeremy L Thompson   @sa CeedFree()
282f7e22acaSJeremy L Thompson 
283f7e22acaSJeremy L Thompson   @ref Backend
284f7e22acaSJeremy L Thompson **/
285f7e22acaSJeremy L Thompson int CeedStringAllocCopy(const char *source, char **copy) {
286f7e22acaSJeremy L Thompson   size_t len = strlen(source);
2872b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(len + 1, copy));
288d602d780SJeremy L Thompson   memcpy(*copy, source, len);
289f7e22acaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
290f7e22acaSJeremy L Thompson }
291f7e22acaSJeremy L Thompson 
29234138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
29334138859Sjeremylt 
294ea61e9acSJeremy L Thompson   @param[in,out] p  address of pointer to memory.
295ea61e9acSJeremy 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
296ea61e9acSJeremy L Thompson pointer.
29734138859Sjeremylt **/
298d7b241e6Sjeremylt int CeedFree(void *p) {
299d7b241e6Sjeremylt   free(*(void **)p);
300d7b241e6Sjeremylt   *(void **)p = NULL;
301e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
302d7b241e6Sjeremylt }
303d7b241e6Sjeremylt 
304d7b241e6Sjeremylt /**
3057a982d89SJeremy L. Thompson   @brief Register a Ceed backend
306d7b241e6Sjeremylt 
307ea61e9acSJeremy L Thompson   @param[in] prefix   Prefix of resources for this backend to respond to.
308ea61e9acSJeremy L Thompson                         For example, the reference backend responds to "/cpu/self".
309ea61e9acSJeremy L Thompson   @param[in] init     Initialization function called by CeedInit() when the backend is selected to drive the requested resource.
310ea61e9acSJeremy L Thompson   @param[in] priority Integer priority.
311ea61e9acSJeremy L Thompson                         Lower values are preferred in case the resource requested by CeedInit() has non-unique best prefix match.
312b11c1e72Sjeremylt 
313b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
314dfdf5a53Sjeremylt 
3157a982d89SJeremy L. Thompson   @ref Backend
316b11c1e72Sjeremylt **/
3172b730f8bSJeremy L Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) {
31810243053SJeremy L Thompson   CeedDebugEnv("Backend Register: %s", prefix);
3196a406739SJeremy L Thompson   CeedRegisterImpl(prefix, init, priority);
320e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
321d7b241e6Sjeremylt }
322d7b241e6Sjeremylt 
323b11c1e72Sjeremylt /**
32460f9e2d6SJeremy L Thompson   @brief Return debugging status flag
32560f9e2d6SJeremy L Thompson 
326ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to get debugging flag
327ea61e9acSJeremy L Thompson   @param[out] is_debug Variable to store debugging flag
32860f9e2d6SJeremy L Thompson 
32960f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
33060f9e2d6SJeremy L Thompson 
331d1d35e2fSjeremylt   @ref Backend
33260f9e2d6SJeremy L Thompson **/
333d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) {
3343f21f6b1SJeremy L Thompson   *is_debug = ceed->is_debug;
335e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
33660f9e2d6SJeremy L Thompson }
33760f9e2d6SJeremy L Thompson 
33860f9e2d6SJeremy L Thompson /**
3397a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3407a982d89SJeremy L. Thompson 
341ea61e9acSJeremy L Thompson   @param[in]  ceed   Ceed context to retrieve parent of
3427a982d89SJeremy L. Thompson   @param[out] parent Address to save the parent to
3437a982d89SJeremy L. Thompson 
3447a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3457a982d89SJeremy L. Thompson 
3467a982d89SJeremy L. Thompson   @ref Backend
3477a982d89SJeremy L. Thompson **/
3487a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3497a982d89SJeremy L. Thompson   if (ceed->parent) {
3502b730f8bSJeremy L Thompson     CeedCall(CeedGetParent(ceed->parent, parent));
351e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
3527a982d89SJeremy L. Thompson   }
3537a982d89SJeremy L. Thompson   *parent = ceed;
354e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3557a982d89SJeremy L. Thompson }
3567a982d89SJeremy L. Thompson 
3577a982d89SJeremy L. Thompson /**
3587a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3597a982d89SJeremy L. Thompson 
360ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to retrieve delegate of
3617a982d89SJeremy L. Thompson   @param[out] delegate Address to save the delegate to
3627a982d89SJeremy L. Thompson 
3637a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson   @ref Backend
3667a982d89SJeremy L. Thompson **/
3677a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3687a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
369e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3707a982d89SJeremy L. Thompson }
3717a982d89SJeremy L. Thompson 
3727a982d89SJeremy L. Thompson /**
3737a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
3747a982d89SJeremy L. Thompson 
375ea61e9acSJeremy L Thompson   This function allows a Ceed context to set a delegate Ceed context.
376ea61e9acSJeremy L Thompson     All backend implementations default to the delegate Ceed context, unless overridden.
3777a982d89SJeremy L. Thompson 
378ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to set delegate of
3797a982d89SJeremy L. Thompson   @param[out] delegate Address to set the delegate to
3807a982d89SJeremy L. Thompson 
3817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3827a982d89SJeremy L. Thompson 
3837a982d89SJeremy L. Thompson   @ref Backend
3847a982d89SJeremy L. Thompson **/
3857a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
3867a982d89SJeremy L. Thompson   ceed->delegate   = delegate;
3877a982d89SJeremy L. Thompson   delegate->parent = ceed;
388e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3897a982d89SJeremy L. Thompson }
3907a982d89SJeremy L. Thompson 
3917a982d89SJeremy L. Thompson /**
3927a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
3937a982d89SJeremy L. Thompson 
394ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to retrieve delegate of
3957a982d89SJeremy L. Thompson   @param[out] delegate Address to save the delegate to
396d1d35e2fSjeremylt   @param[in]  obj_name Name of the object type to retrieve delegate for
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3997a982d89SJeremy L. Thompson 
4007a982d89SJeremy L. Thompson   @ref Backend
4017a982d89SJeremy L. Thompson **/
402d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) {
4037a982d89SJeremy L. Thompson   // Check for object delegate
4042b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) {
405d1d35e2fSjeremylt     if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) {
406d1d35e2fSjeremylt       *delegate = ceed->obj_delegates->delegate;
407e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
4087a982d89SJeremy L. Thompson     }
4092b730f8bSJeremy L Thompson   }
4107a982d89SJeremy L. Thompson 
4117a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
4122b730f8bSJeremy L Thompson   CeedCall(CeedGetDelegate(ceed, delegate));
413e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4147a982d89SJeremy L. Thompson }
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson /**
4177a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
4187a982d89SJeremy L. Thompson 
419ea61e9acSJeremy L Thompson   This function allows a Ceed context to set a delegate Ceed context for a given type of Ceed object.
420ea61e9acSJeremy L Thompson     All backend implementations default to the delegate Ceed context for this object.
421ea61e9acSJeremy L Thompson     For example, CeedSetObjectDelegate(ceed, refceed, "Basis") uses refceed implementations for all CeedBasis backend functions.
4227a982d89SJeremy L. Thompson 
423ea61e9acSJeremy L Thompson   @param[in,out] ceed     Ceed context to set delegate of
4247a982d89SJeremy L. Thompson   @param[out]    delegate Address to set the delegate to
425d1d35e2fSjeremylt   @param[in]     obj_name Name of the object type to set delegate for
4267a982d89SJeremy L. Thompson 
4277a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4287a982d89SJeremy L. Thompson 
4297a982d89SJeremy L. Thompson   @ref Backend
4307a982d89SJeremy L. Thompson **/
431d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) {
432d1d35e2fSjeremylt   CeedInt count = ceed->obj_delegate_count;
4337a982d89SJeremy L. Thompson 
4347a982d89SJeremy L. Thompson   // Malloc or Realloc
4357a982d89SJeremy L. Thompson   if (count) {
4362b730f8bSJeremy L Thompson     CeedCall(CeedRealloc(count + 1, &ceed->obj_delegates));
4377a982d89SJeremy L. Thompson   } else {
4382b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &ceed->obj_delegates));
4397a982d89SJeremy L. Thompson   }
440d1d35e2fSjeremylt   ceed->obj_delegate_count++;
4417a982d89SJeremy L. Thompson 
4427a982d89SJeremy L. Thompson   // Set object delegate
443d1d35e2fSjeremylt   ceed->obj_delegates[count].delegate = delegate;
4442b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(obj_name, &ceed->obj_delegates[count].obj_name));
4457a982d89SJeremy L. Thompson 
4467a982d89SJeremy L. Thompson   // Set delegate parent
4477a982d89SJeremy L. Thompson   delegate->parent = ceed;
448e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4497a982d89SJeremy L. Thompson }
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson /**
4527a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4537a982d89SJeremy L. Thompson 
454ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context
4557a982d89SJeremy L. Thompson   @param[out] resource Variable to store fallback resource
4567a982d89SJeremy L. Thompson 
4577a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4587a982d89SJeremy L. Thompson 
4597a982d89SJeremy L. Thompson   @ref Backend
4607a982d89SJeremy L. Thompson **/
4617a982d89SJeremy L. Thompson 
4627a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
463d1d35e2fSjeremylt   *resource = (const char *)ceed->op_fallback_resource;
464e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4657a982d89SJeremy L. Thompson }
4667a982d89SJeremy L. Thompson 
4677a982d89SJeremy L. Thompson /**
4688687e1d4SJeremy L Thompson   @brief Get the fallback Ceed for CeedOperators
4698687e1d4SJeremy L Thompson 
470ea61e9acSJeremy L Thompson   @param[in]  ceed          Ceed context
4718687e1d4SJeremy L Thompson   @param[out] fallback_ceed Variable to store fallback Ceed
4728687e1d4SJeremy L Thompson 
4738687e1d4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4748687e1d4SJeremy L Thompson 
4758687e1d4SJeremy L Thompson   @ref Backend
4768687e1d4SJeremy L Thompson **/
4778687e1d4SJeremy L Thompson 
4788687e1d4SJeremy L Thompson int CeedGetOperatorFallbackCeed(Ceed ceed, Ceed *fallback_ceed) {
479d04bbc78SJeremy L Thompson   if (ceed->has_valid_op_fallback_resource) {
480d04bbc78SJeremy L Thompson     CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
4812b730f8bSJeremy L Thompson     CeedDebug(ceed, "Getting fallback from %s to %s\n", ceed->resource, ceed->op_fallback_resource);
482d04bbc78SJeremy L Thompson   }
4838687e1d4SJeremy L Thompson 
484d04bbc78SJeremy L Thompson   // Create fallback Ceed if uninitalized
485d04bbc78SJeremy L Thompson   if (!ceed->op_fallback_ceed && ceed->has_valid_op_fallback_resource) {
48613f886e9SJeremy L Thompson     CeedDebug(ceed, "Creating fallback Ceed");
487d04bbc78SJeremy L Thompson 
4888687e1d4SJeremy L Thompson     Ceed        fallback_ceed;
489d04bbc78SJeremy L Thompson     const char *fallback_resource;
490d04bbc78SJeremy L Thompson 
4912b730f8bSJeremy L Thompson     CeedCall(CeedGetOperatorFallbackResource(ceed, &fallback_resource));
4922b730f8bSJeremy L Thompson     CeedCall(CeedInit(fallback_resource, &fallback_ceed));
4938687e1d4SJeremy L Thompson     fallback_ceed->op_fallback_parent = ceed;
4948687e1d4SJeremy L Thompson     fallback_ceed->Error              = ceed->Error;
4958687e1d4SJeremy L Thompson     ceed->op_fallback_ceed            = fallback_ceed;
4968687e1d4SJeremy L Thompson   }
4978687e1d4SJeremy L Thompson   *fallback_ceed = ceed->op_fallback_ceed;
4988687e1d4SJeremy L Thompson 
4998687e1d4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5008687e1d4SJeremy L Thompson }
5018687e1d4SJeremy L Thompson 
5028687e1d4SJeremy L Thompson /**
503ea61e9acSJeremy L Thompson   @brief Set the fallback resource for CeedOperators.
504ea61e9acSJeremy L Thompson            The current resource, if any, is freed by calling this function.
505ea61e9acSJeremy L Thompson            This string is freed upon the destruction of the Ceed context.
5067a982d89SJeremy L. Thompson 
507ea61e9acSJeremy L Thompson   @param[in,out] ceed     Ceed context
508ea61e9acSJeremy L Thompson   @param[in]     resource Fallback resource to set
5097a982d89SJeremy L. Thompson 
5107a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5117a982d89SJeremy L. Thompson 
5127a982d89SJeremy L. Thompson   @ref Backend
5137a982d89SJeremy L. Thompson **/
5147a982d89SJeremy L. Thompson 
5157a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
5167a982d89SJeremy L. Thompson   // Free old
5172b730f8bSJeremy L Thompson   CeedCall(CeedFree(&ceed->op_fallback_resource));
5187a982d89SJeremy L. Thompson 
5197a982d89SJeremy L. Thompson   // Set new
5202b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(resource, (char **)&ceed->op_fallback_resource));
521d04bbc78SJeremy L Thompson 
522d04bbc78SJeremy L Thompson   // Check validity
5232b730f8bSJeremy L Thompson   ceed->has_valid_op_fallback_resource = ceed->op_fallback_resource && ceed->resource && strcmp(ceed->op_fallback_resource, ceed->resource);
524d04bbc78SJeremy L Thompson 
525e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5267a982d89SJeremy L. Thompson }
5277a982d89SJeremy L. Thompson 
5287a982d89SJeremy L. Thompson /**
529ea61e9acSJeremy L Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context for a CeedOperator
5307a982d89SJeremy L. Thompson 
531ea61e9acSJeremy L Thompson   @param[in]  ceed   Ceed context
5327a982d89SJeremy L. Thompson   @param[out] parent Variable to store parent Ceed context
5337a982d89SJeremy L. Thompson 
5347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5357a982d89SJeremy L. Thompson 
5367a982d89SJeremy L. Thompson   @ref Backend
5377a982d89SJeremy L. Thompson **/
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
540d1d35e2fSjeremylt   *parent = ceed->op_fallback_parent;
541e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5427a982d89SJeremy L. Thompson }
5437a982d89SJeremy L. Thompson 
5447a982d89SJeremy L. Thompson /**
5459525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
5469525855cSJeremy L Thompson 
547ea61e9acSJeremy L Thompson   @param[in]  ceed             Ceed to flag as deterministic
54896b902e2Sjeremylt   @param[out] is_deterministic Deterministic status to set
5499525855cSJeremy L Thompson 
5509525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5519525855cSJeremy L Thompson 
5529525855cSJeremy L Thompson   @ref Backend
5539525855cSJeremy L Thompson **/
5549525855cSJeremy L Thompson 
555d1d35e2fSjeremylt int CeedSetDeterministic(Ceed ceed, bool is_deterministic) {
556d1d35e2fSjeremylt   ceed->is_deterministic = is_deterministic;
557e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5589525855cSJeremy L Thompson }
5599525855cSJeremy L Thompson 
5609525855cSJeremy L Thompson /**
5617a982d89SJeremy L. Thompson   @brief Set a backend function
5627a982d89SJeremy L. Thompson 
563ea61e9acSJeremy L Thompson   This function is used for a backend to set the function associated with the Ceed objects.
564ea61e9acSJeremy L Thompson     For example, CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate) sets the backend implementation of 'CeedVectorCreate'
565ea61e9acSJeremy L Thompson and CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply) sets the backend implementation of 'CeedBasisApply'. Note, the prefix
566ea61e9acSJeremy L Thompson 'Ceed' is not required for the object type ("Basis" vs "CeedBasis").
5677a982d89SJeremy L. Thompson 
568ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed context for error handling
569ea61e9acSJeremy L Thompson   @param[in]  type      Type of Ceed object to set function for
5707a982d89SJeremy L. Thompson   @param[out] object    Ceed object to set function for
571ea61e9acSJeremy L Thompson   @param[in]  func_name Name of function to set
572ea61e9acSJeremy L Thompson   @param[in]  f         Function to set
5737a982d89SJeremy L. Thompson 
5747a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5757a982d89SJeremy L. Thompson 
5767a982d89SJeremy L. Thompson   @ref Backend
5777a982d89SJeremy L. Thompson **/
5782b730f8bSJeremy L Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object, const char *func_name, int (*f)()) {
579d1d35e2fSjeremylt   char lookup_name[CEED_MAX_RESOURCE_LEN + 1] = "";
5807a982d89SJeremy L. Thompson 
5817a982d89SJeremy L. Thompson   // Build lookup name
5822b730f8bSJeremy L Thompson   if (strcmp(type, "Ceed")) strncat(lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN);
583d1d35e2fSjeremylt   strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN);
584d1d35e2fSjeremylt   strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN);
5857a982d89SJeremy L. Thompson 
5867a982d89SJeremy L. Thompson   // Find and use offset
5872b730f8bSJeremy L Thompson   for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++) {
588d1d35e2fSjeremylt     if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) {
589d1d35e2fSjeremylt       size_t offset          = ceed->f_offsets[i].offset;
5907a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset);  // *NOPAD*
5917a982d89SJeremy L. Thompson       *fpointer              = f;
592e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
5937a982d89SJeremy L. Thompson     }
5942b730f8bSJeremy L Thompson   }
5957a982d89SJeremy L. Thompson 
5967a982d89SJeremy L. Thompson   // LCOV_EXCL_START
5972b730f8bSJeremy L Thompson   return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Requested function '%s' was not found for CEED object '%s'", func_name, type);
5987a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
5997a982d89SJeremy L. Thompson }
6007a982d89SJeremy L. Thompson 
6017a982d89SJeremy L. Thompson /**
6027a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
6037a982d89SJeremy L. Thompson 
604ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed context to retrieve data of
6057a982d89SJeremy L. Thompson   @param[out] data Address to save data to
6067a982d89SJeremy L. Thompson 
6077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6087a982d89SJeremy L. Thompson 
6097a982d89SJeremy L. Thompson   @ref Backend
6107a982d89SJeremy L. Thompson **/
611777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
612777ff853SJeremy L Thompson   *(void **)data = ceed->data;
613e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6147a982d89SJeremy L. Thompson }
6157a982d89SJeremy L. Thompson 
6167a982d89SJeremy L. Thompson /**
6177a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
6187a982d89SJeremy L. Thompson 
619ea61e9acSJeremy L Thompson   @param[in,out] ceed Ceed context to set data of
620ea61e9acSJeremy L Thompson   @param[in]     data Address of data to set
6217a982d89SJeremy L. Thompson 
6227a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6237a982d89SJeremy L. Thompson 
6247a982d89SJeremy L. Thompson   @ref Backend
6257a982d89SJeremy L. Thompson **/
626777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
627777ff853SJeremy L Thompson   ceed->data = data;
628e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6297a982d89SJeremy L. Thompson }
6307a982d89SJeremy L. Thompson 
63134359f16Sjeremylt /**
63234359f16Sjeremylt   @brief Increment the reference counter for a Ceed context
63334359f16Sjeremylt 
634ea61e9acSJeremy L Thompson   @param[in,out] ceed Ceed context to increment the reference counter
63534359f16Sjeremylt 
63634359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
63734359f16Sjeremylt 
63834359f16Sjeremylt   @ref Backend
63934359f16Sjeremylt **/
6409560d06aSjeremylt int CeedReference(Ceed ceed) {
64134359f16Sjeremylt   ceed->ref_count++;
64234359f16Sjeremylt   return CEED_ERROR_SUCCESS;
64334359f16Sjeremylt }
64434359f16Sjeremylt 
6457a982d89SJeremy L. Thompson /// @}
6467a982d89SJeremy L. Thompson 
6477a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6487a982d89SJeremy L. Thompson /// Ceed Public API
6497a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6507a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6517a982d89SJeremy L. Thompson /// @{
6527a982d89SJeremy L. Thompson 
6537a982d89SJeremy L. Thompson /**
65492ee7d1cSjeremylt   @brief Get the list of available resource names for Ceed contexts
655ea61e9acSJeremy L Thompson            Note: The caller is responsible for `free()`ing the resources and priorities arrays, but should not `free()` the contents of the resources
656ea61e9acSJeremy L Thompson array.
65722e44211Sjeremylt 
65892ee7d1cSjeremylt   @param[out] n          Number of available resources
65992ee7d1cSjeremylt   @param[out] resources  List of available resource names
66022e44211Sjeremylt   @param[out] priorities Resource name prioritization values, lower is better
66122e44211Sjeremylt 
66222e44211Sjeremylt   @return An error code: 0 - success, otherwise - failure
66322e44211Sjeremylt 
66422e44211Sjeremylt   @ref User
66522e44211Sjeremylt **/
66622e44211Sjeremylt // LCOV_EXCL_START
6672b730f8bSJeremy L Thompson int CeedRegistryGetList(size_t *n, char ***const resources, CeedInt **priorities) {
668d0c91ce9Sjeremylt   *n         = 0;
6699ff86846Sjeremylt   *resources = malloc(num_backends * sizeof(**resources));
6702b730f8bSJeremy L Thompson   if (!resources) return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6719ff86846Sjeremylt   if (priorities) {
6729ff86846Sjeremylt     *priorities = malloc(num_backends * sizeof(**priorities));
6732b730f8bSJeremy L Thompson     if (!priorities) return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6749ff86846Sjeremylt   }
67522e44211Sjeremylt   for (size_t i = 0; i < num_backends; i++) {
676d0c91ce9Sjeremylt     // Only report compiled backends
677d0c91ce9Sjeremylt     if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) {
67822e44211Sjeremylt       *resources[i] = backends[i].prefix;
6799ff86846Sjeremylt       if (priorities) *priorities[i] = backends[i].priority;
680d0c91ce9Sjeremylt       *n += 1;
681d0c91ce9Sjeremylt     }
682d0c91ce9Sjeremylt   }
6832b730f8bSJeremy L Thompson   if (*n == 0) {
68478464608Sjeremylt     // LCOV_EXCL_START
68578464608Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "No backends installed");
68678464608Sjeremylt     // LCOV_EXCL_STOP
6872b730f8bSJeremy L Thompson   }
688d0c91ce9Sjeremylt   *resources = realloc(*resources, *n * sizeof(**resources));
6892b730f8bSJeremy L Thompson   if (!resources) return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
690d0c91ce9Sjeremylt   if (priorities) {
691d0c91ce9Sjeremylt     *priorities = realloc(*priorities, *n * sizeof(**priorities));
6922b730f8bSJeremy L Thompson     if (!priorities) return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
69322e44211Sjeremylt   }
69422e44211Sjeremylt   return CEED_ERROR_SUCCESS;
69545f1e315Sjeremylt }
69622e44211Sjeremylt // LCOV_EXCL_STOP
69722e44211Sjeremylt 
69822e44211Sjeremylt /**
699d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
700ea61e9acSJeremy 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
701ea61e9acSJeremy L Thompson list of current available backend resources to stderr.
702b11c1e72Sjeremylt 
703ea61e9acSJeremy L Thompson   @param[in]  resource Resource to use, e.g., "/cpu/self"
704ea61e9acSJeremy L Thompson   @param[out] ceed     The library context
705b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
706b11c1e72Sjeremylt 
707b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
708dfdf5a53Sjeremylt 
7097a982d89SJeremy L. Thompson   @ref User
710b11c1e72Sjeremylt **/
711d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
7122b730f8bSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX, match_priority = CEED_MAX_BACKEND_PRIORITY, priority;
713d7b241e6Sjeremylt 
714fe2413ffSjeremylt   // Find matching backend
7152b730f8bSJeremy L Thompson   if (!resource) {
71613873f79Sjeremylt     // LCOV_EXCL_START
717e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided");
71813873f79Sjeremylt     // LCOV_EXCL_STOP
7192b730f8bSJeremy L Thompson   }
7202b730f8bSJeremy L Thompson   CeedCall(CeedRegisterAll());
72113873f79Sjeremylt 
72222e44211Sjeremylt   // Check for help request
72322e44211Sjeremylt   const char *help_prefix = "help";
7242b730f8bSJeremy L Thompson   size_t      match_help  = 0;
7252b730f8bSJeremy L Thompson   while (match_help < 4 && resource[match_help] == help_prefix[match_help]) match_help++;
72622e44211Sjeremylt   if (match_help == 4) {
7272b730f8bSJeremy L Thompson     fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR, CEED_VERSION_MINOR, CEED_VERSION_PATCH,
72822e44211Sjeremylt             CEED_VERSION_RELEASE ? "" : "+development");
72992ee7d1cSjeremylt     fprintf(stderr, "Available backend resources:\n");
73022e44211Sjeremylt     for (size_t i = 0; i < num_backends; i++) {
731d0c91ce9Sjeremylt       // Only report compiled backends
7322b730f8bSJeremy L Thompson       if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) fprintf(stderr, "  %s\n", backends[i].prefix);
73322e44211Sjeremylt     }
73422e44211Sjeremylt     fflush(stderr);
73522e44211Sjeremylt     match_help = 5;  // Delineating character expected
73622e44211Sjeremylt   } else {
73722e44211Sjeremylt     match_help = 0;
73822e44211Sjeremylt   }
73922e44211Sjeremylt 
740ea61e9acSJeremy L Thompson   // Find best match, computed as number of matching characters from requested resource stem
7412b730f8bSJeremy L Thompson   size_t stem_length = 0;
7422b730f8bSJeremy L Thompson   while (resource[stem_length + match_help] && resource[stem_length + match_help] != ':') stem_length++;
743d7b241e6Sjeremylt   for (size_t i = 0; i < num_backends; i++) {
7442b730f8bSJeremy L Thompson     size_t      n      = 0;
745d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
7462b730f8bSJeremy L Thompson     while (prefix[n] && prefix[n] == resource[n + match_help]) n++;
747d7b241e6Sjeremylt     priority = backends[i].priority;
748d1d35e2fSjeremylt     if (n > match_len || (n == match_len && match_priority > priority)) {
749d1d35e2fSjeremylt       match_len      = n;
750d1d35e2fSjeremylt       match_priority = priority;
751f7e22acaSJeremy L Thompson       match_index    = i;
752d7b241e6Sjeremylt     }
753d7b241e6Sjeremylt   }
7549c9a0587SLeila Ghaffari   // Using Levenshtein distance to find closest match
7559c9a0587SLeila Ghaffari   if (match_len <= 1 || match_len != stem_length) {
756203015caSLeila Ghaffari     // LCOV_EXCL_START
7579c9a0587SLeila Ghaffari     size_t lev_dis   = UINT_MAX;
758f7e22acaSJeremy L Thompson     size_t lev_index = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY;
7599c9a0587SLeila Ghaffari     for (size_t i = 0; i < num_backends; i++) {
7609c9a0587SLeila Ghaffari       const char *prefix        = backends[i].prefix;
7619c9a0587SLeila Ghaffari       size_t      prefix_length = strlen(backends[i].prefix);
7629c9a0587SLeila Ghaffari       size_t      min_len       = (prefix_length < stem_length) ? prefix_length : stem_length;
763092904ddSLeila Ghaffari       size_t      column[min_len + 1];
764092904ddSLeila Ghaffari       for (size_t j = 0; j <= min_len; j++) column[j] = j;
7659c9a0587SLeila Ghaffari       for (size_t j = 1; j <= min_len; j++) {
7669c9a0587SLeila Ghaffari         column[0] = j;
7679c9a0587SLeila Ghaffari         for (size_t k = 1, last_diag = j - 1; k <= min_len; k++) {
768092904ddSLeila Ghaffari           size_t old_diag = column[k];
7699c9a0587SLeila Ghaffari           size_t min_1    = (column[k] < column[k - 1]) ? column[k] + 1 : column[k - 1] + 1;
7709c9a0587SLeila Ghaffari           size_t min_2    = last_diag + (resource[k - 1] == prefix[j - 1] ? 0 : 1);
7719c9a0587SLeila Ghaffari           column[k]       = (min_1 < min_2) ? min_1 : min_2;
7729c9a0587SLeila Ghaffari           last_diag       = old_diag;
7739c9a0587SLeila Ghaffari         }
7749c9a0587SLeila Ghaffari       }
7759c9a0587SLeila Ghaffari       size_t n = column[min_len];
7769c9a0587SLeila Ghaffari       priority = backends[i].priority;
7772b730f8bSJeremy L Thompson       if (n < lev_dis || (n == lev_dis && lev_priority > priority)) {
7789c9a0587SLeila Ghaffari         lev_dis      = n;
7799c9a0587SLeila Ghaffari         lev_priority = priority;
780f7e22acaSJeremy L Thompson         lev_index    = i;
7819c9a0587SLeila Ghaffari       }
7829c9a0587SLeila Ghaffari     }
783f7e22acaSJeremy L Thompson     const char *prefix_lev = backends[lev_index].prefix;
7842b730f8bSJeremy L Thompson     size_t      lev_length = 0;
7852b730f8bSJeremy L Thompson     while (prefix_lev[lev_length] && prefix_lev[lev_length] != '\0') lev_length++;
7869c9a0587SLeila Ghaffari     size_t m = (lev_length < stem_length) ? lev_length : stem_length;
7879c9a0587SLeila Ghaffari     if (lev_dis + 1 >= m) {
7882b730f8bSJeremy L Thompson       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s", resource);
7899c9a0587SLeila Ghaffari     } else {
7902b730f8bSJeremy L Thompson       return CeedError(NULL, CEED_ERROR_MAJOR,
7912b730f8bSJeremy L Thompson                        "No suitable backend: %s\n"
7922b730f8bSJeremy L Thompson                        "Closest match: %s",
7932b730f8bSJeremy L Thompson                        resource, backends[lev_index].prefix);
7942bbc7fe8Sjeremylt     }
795203015caSLeila Ghaffari     // LCOV_EXCL_STOP
7969c9a0587SLeila Ghaffari   }
797fe2413ffSjeremylt 
798fe2413ffSjeremylt   // Setup Ceed
7992b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, ceed));
8002b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &(*ceed)->jit_source_roots));
801bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
8022b730f8bSJeremy L Thompson   if (!ceed_error_handler) ceed_error_handler = "abort";
8032b730f8bSJeremy L Thompson   if (!strcmp(ceed_error_handler, "exit")) (*ceed)->Error = CeedErrorExit;
8042b730f8bSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store")) (*ceed)->Error = CeedErrorStore;
8052b730f8bSJeremy L Thompson   else (*ceed)->Error = CeedErrorAbort;
806d1d35e2fSjeremylt   memcpy((*ceed)->err_msg, "No error message stored", 24);
807d1d35e2fSjeremylt   (*ceed)->ref_count = 1;
808d7b241e6Sjeremylt   (*ceed)->data      = NULL;
809fe2413ffSjeremylt 
810fe2413ffSjeremylt   // Set lookup table
811d1d35e2fSjeremylt   FOffset f_offsets[] = {
8126e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, Error),
8136e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
8146e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, Destroy),
815f8902d9eSjeremylt       CEED_FTABLE_ENTRY(Ceed, VectorCreate),
8166e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
817fc0567d9Srezgarshakeri       CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateOriented),
8186e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
8196e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
8206e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
82150c301a5SRezgar Shakeri       CEED_FTABLE_ENTRY(Ceed, BasisCreateHdiv),
8226e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
8236e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
824777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
8256e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
8266e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
8279c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, HasValidArray),
8289c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, HasBorrowedArrayOfType),
8296e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, SetArray),
8306a6c615bSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, TakeArray),
8316e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, SetValue),
832f48ed27dSnbeams       CEED_FTABLE_ENTRY(CeedVector, SyncArray),
8336e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, GetArray),
8346e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
8359c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, GetArrayWrite),
8366e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
8376e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
838547d9b97Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, Norm),
839e0dd3b27Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, Scale),
8400f7fd0f8Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, AXPY),
8410f7fd0f8Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, PointwiseMult),
842d99fa3c5SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
8436e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, Destroy),
8446e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
8456e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
846bd33150aSjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
8476e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
8486e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedBasis, Apply),
8496e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedBasis, Destroy),
8506e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
8516e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
8526e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, Apply),
8538c84ac63Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
8548c84ac63Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
8556e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
8569c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, HasValidData),
8579c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, HasBorrowedDataOfType),
858777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
859891038deSjeremylt       CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData),
860777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
86128bfd0b7SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, GetDataRead),
862777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
86328bfd0b7SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreDataRead),
8642e64a2b9SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, DataDestroy),
865777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
86680ac2e43SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
86770a7ffb3SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate),
86880ac2e43SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
8699e9210b8SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
87080ac2e43SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
8719e9210b8SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
872e2f04181SAndrew T. Barker       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic),
873e2f04181SAndrew T. Barker       CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble),
874cefa2673SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSingle),
875713f43c3Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
8766e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, Apply),
877250756a7Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
878cae8b89aSjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
879250756a7Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
8806e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
8816e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, Destroy),
8826e79d475Sjeremylt       {NULL, 0}  // End of lookup table - used in SetBackendFunction loop
8831dfeef1dSjeremylt   };
884fe2413ffSjeremylt 
8852b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets));
886d1d35e2fSjeremylt   memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets));
887fe2413ffSjeremylt 
8885107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
889e2f04181SAndrew T. Barker   const char fallbackresource[] = "";
8902b730f8bSJeremy L Thompson   CeedCall(CeedSetOperatorFallbackResource(*ceed, fallbackresource));
8915107b09fSJeremy L Thompson 
89260f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
8932b730f8bSJeremy L Thompson   (*ceed)->is_debug = !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG");
89460f9e2d6SJeremy L Thompson 
89522e44211Sjeremylt   // Copy resource prefix, if backend setup successful
8962b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(backends[match_index].prefix, (char **)&(*ceed)->resource));
897ee5a26f2SJeremy L Thompson 
898ee5a26f2SJeremy L Thompson   // Set default JiT source root
899ea61e9acSJeremy L Thompson   // Note: there will always be the default root for every Ceed but all additional paths are added to the top-most parent
9002b730f8bSJeremy L Thompson   CeedCall(CeedAddJitSourceRoot(*ceed, (char *)CeedJitSourceRootDefault));
901ee5a26f2SJeremy L Thompson 
902d04bbc78SJeremy L Thompson   // Backend specific setup
9032b730f8bSJeremy L Thompson   CeedCall(backends[match_index].init(&resource[match_help], *ceed));
904d04bbc78SJeremy L Thompson 
905e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
906d7b241e6Sjeremylt }
907d7b241e6Sjeremylt 
908d7b241e6Sjeremylt /**
909ea61e9acSJeremy L Thompson   @brief Copy the pointer to a Ceed context.
910ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedDestroy()`;
911ea61e9acSJeremy L Thompson            Note: If `*ceed_copy` is non-NULL, then it is assumed that `*ceed_copy` is a pointer to a Ceed context.
912ea61e9acSJeremy L Thompson              This Ceed context will be destroyed if `*ceed_copy` is the only reference to this Ceed context.
9139560d06aSjeremylt 
914ea61e9acSJeremy L Thompson   @param[in]     ceed      Ceed context to copy reference to
915ea61e9acSJeremy L Thompson   @param[in,out] ceed_copy Variable to store copied reference
9169560d06aSjeremylt 
9179560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
9189560d06aSjeremylt 
9199560d06aSjeremylt   @ref User
9209560d06aSjeremylt **/
9219560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) {
9222b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
9232b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(ceed_copy));
9249560d06aSjeremylt   *ceed_copy = ceed;
9259560d06aSjeremylt   return CEED_ERROR_SUCCESS;
9269560d06aSjeremylt }
9279560d06aSjeremylt 
9289560d06aSjeremylt /**
9297a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
9302f86a920SJeremy L Thompson 
931ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to get resource name of
9327a982d89SJeremy L. Thompson   @param[out] resource Variable to store resource name
9332f86a920SJeremy L Thompson 
9342f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9352f86a920SJeremy L Thompson 
9367a982d89SJeremy L. Thompson   @ref User
9375107b09fSJeremy L Thompson **/
9387a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
9397a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
940e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9415107b09fSJeremy L Thompson }
9425107b09fSJeremy L Thompson 
9435107b09fSJeremy L Thompson /**
944d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
945c907536fSjeremylt 
946ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to get preferred memory type of
947d1d35e2fSjeremylt   @param[out] mem_type Address to save preferred memory type to
948c907536fSjeremylt 
949c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
950c907536fSjeremylt 
9517a982d89SJeremy L. Thompson   @ref User
952c907536fSjeremylt **/
953d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) {
954c907536fSjeremylt   if (ceed->GetPreferredMemType) {
9552b730f8bSJeremy L Thompson     CeedCall(ceed->GetPreferredMemType(mem_type));
956c907536fSjeremylt   } else {
957c263cd57Sjeremylt     Ceed delegate;
9582b730f8bSJeremy L Thompson     CeedCall(CeedGetDelegate(ceed, &delegate));
959c263cd57Sjeremylt 
960c263cd57Sjeremylt     if (delegate) {
9612b730f8bSJeremy L Thompson       CeedCall(CeedGetPreferredMemType(delegate, mem_type));
962c263cd57Sjeremylt     } else {
963d1d35e2fSjeremylt       *mem_type = CEED_MEM_HOST;
964c907536fSjeremylt     }
965c263cd57Sjeremylt   }
966e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
967c907536fSjeremylt }
968c907536fSjeremylt 
969c907536fSjeremylt /**
9709525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
9719525855cSJeremy L Thompson 
9729525855cSJeremy L Thompson   @param[in]  ceed             Ceed
973d1d35e2fSjeremylt   @param[out] is_deterministic Variable to store deterministic status
9749525855cSJeremy L Thompson 
9759525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9769525855cSJeremy L Thompson 
9779525855cSJeremy L Thompson   @ref User
9789525855cSJeremy L Thompson **/
979d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) {
980d1d35e2fSjeremylt   *is_deterministic = ceed->is_deterministic;
981e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9829525855cSJeremy L Thompson }
9839525855cSJeremy L Thompson 
9849525855cSJeremy L Thompson /**
985ee5a26f2SJeremy L Thompson   @brief Set additional JiT source root for Ceed
986ee5a26f2SJeremy L Thompson 
987ea61e9acSJeremy L Thompson   @param[in,out] ceed            Ceed
988ee5a26f2SJeremy L Thompson   @param[in]     jit_source_root Absolute path to additional JiT source directory
989ee5a26f2SJeremy L Thompson 
990ee5a26f2SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
991ee5a26f2SJeremy L Thompson 
992ee5a26f2SJeremy L Thompson   @ref User
993ee5a26f2SJeremy L Thompson **/
994ee5a26f2SJeremy L Thompson int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root) {
9956155f12fSJeremy L Thompson   Ceed ceed_parent;
996ee5a26f2SJeremy L Thompson 
9972b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(ceed, &ceed_parent));
9986155f12fSJeremy L Thompson 
9996155f12fSJeremy L Thompson   CeedInt index       = ceed_parent->num_jit_source_roots;
1000ee5a26f2SJeremy L Thompson   size_t  path_length = strlen(jit_source_root);
10012b730f8bSJeremy L Thompson   CeedCall(CeedRealloc(index + 1, &ceed_parent->jit_source_roots));
10022b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(path_length + 1, &ceed_parent->jit_source_roots[index]));
1003d602d780SJeremy L Thompson   memcpy(ceed_parent->jit_source_roots[index], jit_source_root, path_length);
10046155f12fSJeremy L Thompson   ceed_parent->num_jit_source_roots++;
1005ee5a26f2SJeremy L Thompson 
1006ee5a26f2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1007ee5a26f2SJeremy L Thompson }
1008ee5a26f2SJeremy L Thompson 
1009ee5a26f2SJeremy L Thompson /**
10100a0da059Sjeremylt   @brief View a Ceed
10110a0da059Sjeremylt 
10120a0da059Sjeremylt   @param[in] ceed   Ceed to view
10130a0da059Sjeremylt   @param[in] stream Filestream to write to
10140a0da059Sjeremylt 
10150a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
10160a0da059Sjeremylt 
10170a0da059Sjeremylt   @ref User
10180a0da059Sjeremylt **/
10190a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
1020d1d35e2fSjeremylt   CeedMemType mem_type;
10210a0da059Sjeremylt 
10222b730f8bSJeremy L Thompson   CeedCall(CeedGetPreferredMemType(ceed, &mem_type));
10230a0da059Sjeremylt 
10242b730f8bSJeremy L Thompson   fprintf(stream,
10252b730f8bSJeremy L Thompson           "Ceed\n"
10260a0da059Sjeremylt           "  Ceed Resource: %s\n"
10270a0da059Sjeremylt           "  Preferred MemType: %s\n",
1028d1d35e2fSjeremylt           ceed->resource, CeedMemTypes[mem_type]);
1029e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10300a0da059Sjeremylt }
10310a0da059Sjeremylt 
10320a0da059Sjeremylt /**
1033b11c1e72Sjeremylt   @brief Destroy a Ceed context
1034d7b241e6Sjeremylt 
1035ea61e9acSJeremy L Thompson   @param[in,out] ceed Address of Ceed context to destroy
1036b11c1e72Sjeremylt 
1037b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1038dfdf5a53Sjeremylt 
10397a982d89SJeremy L. Thompson   @ref User
1040b11c1e72Sjeremylt **/
1041d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
1042d1d35e2fSjeremylt   if (!*ceed || --(*ceed)->ref_count > 0) return CEED_ERROR_SUCCESS;
10432b730f8bSJeremy L Thompson   if ((*ceed)->delegate) CeedCall(CeedDestroy(&(*ceed)->delegate));
10440ace9bf2Sjeremylt 
1045d1d35e2fSjeremylt   if ((*ceed)->obj_delegate_count > 0) {
104692ae7e47SJeremy L Thompson     for (CeedInt i = 0; i < (*ceed)->obj_delegate_count; i++) {
10472b730f8bSJeremy L Thompson       CeedCall(CeedDestroy(&((*ceed)->obj_delegates[i].delegate)));
10482b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*ceed)->obj_delegates[i].obj_name));
1049aefd8378Sjeremylt     }
10502b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ceed)->obj_delegates));
1051aefd8378Sjeremylt   }
10520ace9bf2Sjeremylt 
10532b730f8bSJeremy L Thompson   if ((*ceed)->Destroy) CeedCall((*ceed)->Destroy(*ceed));
10540ace9bf2Sjeremylt 
105592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*ceed)->num_jit_source_roots; i++) {
10562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ceed)->jit_source_roots[i]));
1057032e71eaSJeremy L Thompson   }
10582b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->jit_source_roots));
1059032e71eaSJeremy L Thompson 
10602b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->f_offsets));
10612b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->resource));
10622b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*ceed)->op_fallback_ceed));
10632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->op_fallback_resource));
10642b730f8bSJeremy L Thompson   CeedCall(CeedFree(ceed));
1065e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1066d7b241e6Sjeremylt }
1067d7b241e6Sjeremylt 
1068f9982c62SWill Pazner // LCOV_EXCL_START
1069f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
10702b730f8bSJeremy L Thompson   if (ceed->parent) return CeedErrorFormat(ceed->parent, format, args);
10712b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedErrorFormat(ceed->op_fallback_parent, format, args);
107278464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
107378464608Sjeremylt   vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args);  // NOLINT
1074d1d35e2fSjeremylt   return ceed->err_msg;
1075f9982c62SWill Pazner }
1076f9982c62SWill Pazner // LCOV_EXCL_STOP
1077f9982c62SWill Pazner 
10787a982d89SJeremy L. Thompson /**
10797a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
10807a982d89SJeremy L. Thompson 
10817a982d89SJeremy L. Thompson   @ref Developer
10827a982d89SJeremy L. Thompson **/
10832b730f8bSJeremy L Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func, int ecode, const char *format, ...) {
10847a982d89SJeremy L. Thompson   va_list args;
1085d1d35e2fSjeremylt   int     ret_val;
10867a982d89SJeremy L. Thompson   va_start(args, format);
10877a982d89SJeremy L. Thompson   if (ceed) {
1088d1d35e2fSjeremylt     ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
10897a982d89SJeremy L. Thompson   } else {
1090b0d62198Sjeremylt     // LCOV_EXCL_START
1091477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
10922b730f8bSJeremy L Thompson     if (!ceed_error_handler) ceed_error_handler = "abort";
10932b730f8bSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return")) ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
1094477729cfSJeremy L Thompson     else
1095477729cfSJeremy L Thompson       // This function will not return
1096d1d35e2fSjeremylt       ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
10977a982d89SJeremy L. Thompson   }
10987a982d89SJeremy L. Thompson   va_end(args);
1099d1d35e2fSjeremylt   return ret_val;
1100b0d62198Sjeremylt   // LCOV_EXCL_STOP
11017a982d89SJeremy L. Thompson }
11027a982d89SJeremy L. Thompson 
1103477729cfSJeremy L Thompson /**
1104477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
1105477729cfSJeremy L Thompson 
1106477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1107477729cfSJeremy L Thompson 
1108477729cfSJeremy L Thompson   @ref Developer
1109477729cfSJeremy L Thompson **/
1110477729cfSJeremy L Thompson // LCOV_EXCL_START
11112b730f8bSJeremy L Thompson int CeedErrorReturn(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
1112d1d35e2fSjeremylt   return err_code;
1113477729cfSJeremy L Thompson }
1114477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1115477729cfSJeremy L Thompson 
1116477729cfSJeremy L Thompson /**
1117ea61e9acSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns the error.
1118477729cfSJeremy L Thompson 
1119477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1120477729cfSJeremy L Thompson 
1121477729cfSJeremy L Thompson   @ref Developer
1122477729cfSJeremy L Thompson **/
1123477729cfSJeremy L Thompson // LCOV_EXCL_START
11242b730f8bSJeremy L Thompson int CeedErrorStore(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
11252b730f8bSJeremy L Thompson   if (ceed->parent) return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format, args);
11262b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func, err_code, format, args);
1127477729cfSJeremy L Thompson 
1128477729cfSJeremy L Thompson   // Build message
1129990fdeb6SJeremy L Thompson   int len;
11302b730f8bSJeremy L Thompson   len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ", filename, line_no, func);
113178464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
113278464608Sjeremylt   vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args);  // NOLINT
1133d1d35e2fSjeremylt   return err_code;
1134477729cfSJeremy L Thompson }
1135477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1136477729cfSJeremy L Thompson 
1137477729cfSJeremy L Thompson /**
1138477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
1139477729cfSJeremy L Thompson 
1140477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1141477729cfSJeremy L Thompson 
1142477729cfSJeremy L Thompson   @ref Developer
1143477729cfSJeremy L Thompson **/
1144477729cfSJeremy L Thompson // LCOV_EXCL_START
11452b730f8bSJeremy L Thompson int CeedErrorAbort(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
1146d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
1147f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1148477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1149477729cfSJeremy L Thompson   abort();
1150d1d35e2fSjeremylt   return err_code;
1151477729cfSJeremy L Thompson }
1152477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1153477729cfSJeremy L Thompson 
1154477729cfSJeremy L Thompson /**
1155477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
1156477729cfSJeremy L Thompson 
1157477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1158477729cfSJeremy L Thompson 
1159ea61e9acSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit() handlers (e.g., as used by gcov) are run.
1160477729cfSJeremy L Thompson 
1161477729cfSJeremy L Thompson   @ref Developer
1162477729cfSJeremy L Thompson **/
11632b730f8bSJeremy L Thompson int CeedErrorExit(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
1164d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
116578464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
116678464608Sjeremylt   vfprintf(stderr, format, *args);  // NOLINT
1167477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1168d1d35e2fSjeremylt   exit(err_code);
1169d1d35e2fSjeremylt   return err_code;
1170477729cfSJeremy L Thompson }
1171477729cfSJeremy L Thompson 
1172477729cfSJeremy L Thompson /**
1173477729cfSJeremy L Thompson   @brief Set error handler
1174477729cfSJeremy L Thompson 
1175ea61e9acSJeremy L Thompson   A default error handler is set in CeedInit().
1176ea61e9acSJeremy L Thompson   Use this function to change the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined error handler.
1177477729cfSJeremy L Thompson 
1178477729cfSJeremy L Thompson   @ref Developer
1179477729cfSJeremy L Thompson **/
1180d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) {
1181d1d35e2fSjeremylt   ceed->Error = handler;
1182d1d35e2fSjeremylt   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler);
11832b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler);
1184e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1185477729cfSJeremy L Thompson }
1186477729cfSJeremy L Thompson 
1187477729cfSJeremy L Thompson /**
1188477729cfSJeremy L Thompson   @brief Get error message
1189477729cfSJeremy L Thompson 
1190ea61e9acSJeremy L Thompson   The error message is only stored when using the error handler CeedErrorStore()
1191477729cfSJeremy L Thompson 
1192ea61e9acSJeremy L Thompson   @param[in]  ceed    Ceed context to retrieve error message
1193d1d35e2fSjeremylt   @param[out] err_msg Char pointer to hold error message
1194477729cfSJeremy L Thompson 
1195477729cfSJeremy L Thompson   @ref Developer
1196477729cfSJeremy L Thompson **/
1197d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) {
11982b730f8bSJeremy L Thompson   if (ceed->parent) return CeedGetErrorMessage(ceed->parent, err_msg);
11992b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg);
1200d1d35e2fSjeremylt   *err_msg = ceed->err_msg;
1201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1202477729cfSJeremy L Thompson }
1203477729cfSJeremy L Thompson 
1204477729cfSJeremy L Thompson /**
1205477729cfSJeremy L Thompson   @brief Restore error message
1206477729cfSJeremy L Thompson 
1207ea61e9acSJeremy L Thompson   The error message is only stored when using the error handler CeedErrorStore()
1208477729cfSJeremy L Thompson 
1209ea61e9acSJeremy L Thompson   @param[in]  ceed    Ceed context to restore error message
1210d1d35e2fSjeremylt   @param[out] err_msg Char pointer that holds error message
1211477729cfSJeremy L Thompson 
1212477729cfSJeremy L Thompson   @ref Developer
1213477729cfSJeremy L Thompson **/
1214d1d35e2fSjeremylt int CeedResetErrorMessage(Ceed ceed, const char **err_msg) {
12152b730f8bSJeremy L Thompson   if (ceed->parent) return CeedResetErrorMessage(ceed->parent, err_msg);
12162b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg);
1217d1d35e2fSjeremylt   *err_msg = NULL;
1218d1d35e2fSjeremylt   memcpy(ceed->err_msg, "No error message stored", 24);
1219e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1220477729cfSJeremy L Thompson }
1221477729cfSJeremy L Thompson 
12221070991dSJed Brown /**
12231070991dSJed Brown   @brief Get libCEED library version info
12241070991dSJed Brown 
1225ea61e9acSJeremy L Thompson   libCEED version numbers have the form major.minor.patch.
1226ea61e9acSJeremy L Thompson   Non-release versions may contain unstable interfaces.
12271070991dSJed Brown 
12281070991dSJed Brown   @param[out] major   Major version of the library
12291070991dSJed Brown   @param[out] minor   Minor version of the library
12301070991dSJed Brown   @param[out] patch   Patch (subminor) version of the library
12311070991dSJed Brown   @param[out] release True for releases; false for development branches.
12321070991dSJed Brown 
12331070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
12341070991dSJed Brown 
12351070991dSJed Brown   @sa CEED_VERSION_GE()
12361070991dSJed Brown 
12371070991dSJed Brown   @ref Developer
12381070991dSJed Brown */
12391070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
12401070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
12411070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
12421070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
12431070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
12441070991dSJed Brown   return 0;
12451070991dSJed Brown }
12461070991dSJed Brown 
124780a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) {
124880a9ef05SNatalie Beams   *scalar_type = CEED_SCALAR_TYPE;
124980a9ef05SNatalie Beams   return 0;
125080a9ef05SNatalie Beams }
125180a9ef05SNatalie Beams 
1252d7b241e6Sjeremylt /// @}
1253