xref: /libCEED/interface/ceed.c (revision 9c9a05879f28f863d24c6d8fa69098ef3a83a2c8)
1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details.
4d7b241e6Sjeremylt //
5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and
8d7b241e6Sjeremylt // source code availability see http://github.com/ceed.
9d7b241e6Sjeremylt //
10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early
15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16d7b241e6Sjeremylt 
17d7b241e6Sjeremylt #define _POSIX_C_SOURCE 200112
18ec3da8bcSJed Brown #include <ceed/ceed.h>
19ec3da8bcSJed Brown #include <ceed/backend.h>
203d576824SJeremy L Thompson #include <ceed-impl.h>
21aedaa0e5Sjeremylt #include <limits.h>
22d7b241e6Sjeremylt #include <stdarg.h>
236e79d475Sjeremylt #include <stddef.h>
24d7b241e6Sjeremylt #include <stdio.h>
25d7b241e6Sjeremylt #include <stdlib.h>
26d7b241e6Sjeremylt #include <string.h>
27d7b241e6Sjeremylt 
28d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP
29d7b241e6Sjeremylt static CeedRequest ceed_request_immediate;
30d7b241e6Sjeremylt static CeedRequest ceed_request_ordered;
31d7b241e6Sjeremylt 
32d7b241e6Sjeremylt static struct {
33d7b241e6Sjeremylt   char prefix[CEED_MAX_RESOURCE_LEN];
34d7b241e6Sjeremylt   int (*init)(const char *resource, Ceed f);
35d7b241e6Sjeremylt   unsigned int priority;
36d7b241e6Sjeremylt } backends[32];
37d7b241e6Sjeremylt static size_t num_backends;
38fe2413ffSjeremylt 
396e79d475Sjeremylt #define CEED_FTABLE_ENTRY(class, method) \
406e79d475Sjeremylt   {#class #method, offsetof(struct class ##_private, method)}
41d7b241e6Sjeremylt /// @endcond
42d7b241e6Sjeremylt 
43d7b241e6Sjeremylt /// @file
44d7b241e6Sjeremylt /// Implementation of core components of Ceed library
457a982d89SJeremy L. Thompson 
467a982d89SJeremy L. Thompson /// @addtogroup CeedUser
47d7b241e6Sjeremylt /// @{
48d7b241e6Sjeremylt 
49dfdf5a53Sjeremylt /**
50dfdf5a53Sjeremylt   @brief Request immediate completion
51dfdf5a53Sjeremylt 
52dfdf5a53Sjeremylt   This predefined constant is passed as the \ref CeedRequest argument to
53dfdf5a53Sjeremylt   interfaces when the caller wishes for the operation to be performed
54dfdf5a53Sjeremylt   immediately.  The code
55dfdf5a53Sjeremylt 
56dfdf5a53Sjeremylt   @code
57dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE);
58dfdf5a53Sjeremylt   @endcode
59dfdf5a53Sjeremylt 
60dfdf5a53Sjeremylt   is semantically equivalent to
61dfdf5a53Sjeremylt 
62dfdf5a53Sjeremylt   @code
63dfdf5a53Sjeremylt     CeedRequest request;
64dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., &request);
65dfdf5a53Sjeremylt     CeedRequestWait(&request);
66dfdf5a53Sjeremylt   @endcode
67dfdf5a53Sjeremylt 
68dfdf5a53Sjeremylt   @sa CEED_REQUEST_ORDERED
69dfdf5a53Sjeremylt **/
70d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate;
71d7b241e6Sjeremylt 
72d7b241e6Sjeremylt /**
73b11c1e72Sjeremylt   @brief Request ordered completion
74d7b241e6Sjeremylt 
75d7b241e6Sjeremylt   This predefined constant is passed as the \ref CeedRequest argument to
76d7b241e6Sjeremylt   interfaces when the caller wishes for the operation to be completed in the
77d7b241e6Sjeremylt   order that it is submitted to the device.  It is typically used in a construct
78d7b241e6Sjeremylt   such as
79d7b241e6Sjeremylt 
80d7b241e6Sjeremylt   @code
81d7b241e6Sjeremylt     CeedRequest request;
82d7b241e6Sjeremylt     CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED);
83d7b241e6Sjeremylt     CeedOperatorApply(op2, ..., &request);
84d7b241e6Sjeremylt     // other optional work
858b2d6f4aSMatthew Knepley     CeedRequestWait(&request);
86d7b241e6Sjeremylt   @endcode
87d7b241e6Sjeremylt 
88d7b241e6Sjeremylt   which allows the sequence to complete asynchronously but does not start
89d7b241e6Sjeremylt   `op2` until `op1` has completed.
90d7b241e6Sjeremylt 
91288c0443SJeremy L Thompson   @todo The current implementation is overly strict, offering equivalent
924cc79fe7SJed Brown   semantics to @ref CEED_REQUEST_IMMEDIATE.
93d7b241e6Sjeremylt 
94d7b241e6Sjeremylt   @sa CEED_REQUEST_IMMEDIATE
95d7b241e6Sjeremylt  */
96d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered;
97d7b241e6Sjeremylt 
98b11c1e72Sjeremylt /**
997a982d89SJeremy L. Thompson   @brief Wait for a CeedRequest to complete.
100dfdf5a53Sjeremylt 
1017a982d89SJeremy L. Thompson   Calling CeedRequestWait on a NULL request is a no-op.
1027a982d89SJeremy L. Thompson 
1037a982d89SJeremy L. Thompson   @param req Address of CeedRequest to wait for; zeroed on completion.
1047a982d89SJeremy L. Thompson 
1057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   @ref User
108b11c1e72Sjeremylt **/
1097a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) {
1107a982d89SJeremy L. Thompson   if (!*req)
111e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
112e15f9bd0SJeremy L Thompson   return CeedError(NULL, CEED_ERROR_UNSUPPORTED,
113e15f9bd0SJeremy L Thompson                    "CeedRequestWait not implemented");
114683faae0SJed Brown }
1157a982d89SJeremy L. Thompson 
1167a982d89SJeremy L. Thompson /// @}
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1197a982d89SJeremy L. Thompson /// Ceed Library Internal Functions
1207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1217a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper
1227a982d89SJeremy L. Thompson /// @{
123d7b241e6Sjeremylt 
1247a982d89SJeremy L. Thompson /// @}
125d7b241e6Sjeremylt 
1267a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1277a982d89SJeremy L. Thompson /// Ceed Backend API
1287a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1297a982d89SJeremy L. Thompson /// @addtogroup CeedBackend
1307a982d89SJeremy L. Thompson /// @{
131d7b241e6Sjeremylt 
132b11c1e72Sjeremylt /**
13360f9e2d6SJeremy L Thompson   @brief Print Ceed debugging information
13460f9e2d6SJeremy L Thompson 
13560f9e2d6SJeremy L Thompson   @param ceed    Ceed context
13660f9e2d6SJeremy L Thompson   @param format  Printing format
13760f9e2d6SJeremy L Thompson 
13860f9e2d6SJeremy L Thompson   @return None
13960f9e2d6SJeremy L Thompson 
14060f9e2d6SJeremy L Thompson   @ref Backend
14160f9e2d6SJeremy L Thompson **/
142fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
14360f9e2d6SJeremy L Thompson void CeedDebugImpl(const Ceed ceed, const char *format,...) {
14460f9e2d6SJeremy L Thompson   if (!ceed->debug) return;
14560f9e2d6SJeremy L Thompson   va_list args;
14660f9e2d6SJeremy L Thompson   va_start(args, format);
14760f9e2d6SJeremy L Thompson   CeedDebugImpl256(ceed, 0, format, args);
14860f9e2d6SJeremy L Thompson   va_end(args);
14960f9e2d6SJeremy L Thompson }
150fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
15160f9e2d6SJeremy L Thompson 
15260f9e2d6SJeremy L Thompson /**
15360f9e2d6SJeremy L Thompson   @brief Print Ceed debugging information in color
15460f9e2d6SJeremy L Thompson 
15560f9e2d6SJeremy L Thompson   @param ceed    Ceed context
15660f9e2d6SJeremy L Thompson   @param color   Color to print
15760f9e2d6SJeremy L Thompson   @param format  Printing format
15860f9e2d6SJeremy L Thompson 
15960f9e2d6SJeremy L Thompson   @return None
16060f9e2d6SJeremy L Thompson 
16160f9e2d6SJeremy L Thompson   @ref Backend
16260f9e2d6SJeremy L Thompson **/
163fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
16460f9e2d6SJeremy L Thompson void CeedDebugImpl256(const Ceed ceed, const unsigned char color,
16560f9e2d6SJeremy L Thompson                       const char *format,...) {
16660f9e2d6SJeremy L Thompson   if (!ceed->debug) return;
16760f9e2d6SJeremy L Thompson   va_list args;
16860f9e2d6SJeremy L Thompson   va_start(args, format);
16960f9e2d6SJeremy L Thompson   fflush(stdout);
17060f9e2d6SJeremy L Thompson   fprintf(stdout, "\033[38;5;%dm", color);
17160f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
17260f9e2d6SJeremy L Thompson   fprintf(stdout, "\033[m");
17360f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
17460f9e2d6SJeremy L Thompson   fflush(stdout);
17560f9e2d6SJeremy L Thompson   va_end(args);
17660f9e2d6SJeremy L Thompson }
177fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
17860f9e2d6SJeremy L Thompson 
17960f9e2d6SJeremy L Thompson /**
180b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
181b11c1e72Sjeremylt 
182b11c1e72Sjeremylt   Memory usage can be tracked by the library.  This ensures sufficient
183b11c1e72Sjeremylt     alignment for vectorization and should be used for large allocations.
184b11c1e72Sjeremylt 
185b11c1e72Sjeremylt   @param n     Number of units to allocate
186b11c1e72Sjeremylt   @param unit  Size of each unit
187b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
188b11c1e72Sjeremylt 
189b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
190b11c1e72Sjeremylt 
191b11c1e72Sjeremylt   @sa CeedFree()
192dfdf5a53Sjeremylt 
1937a982d89SJeremy L. Thompson   @ref Backend
194b11c1e72Sjeremylt **/
195d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
196d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit);
197d7b241e6Sjeremylt   if (ierr)
198c042f62fSJeremy L Thompson     // LCOV_EXCL_START
199e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
200e15f9bd0SJeremy L Thompson                      "posix_memalign failed to allocate %zd "
2011d102b48SJeremy L Thompson                      "members of size %zd\n", n, unit);
202c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
203e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
204d7b241e6Sjeremylt }
205d7b241e6Sjeremylt 
206b11c1e72Sjeremylt /**
207b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
208b11c1e72Sjeremylt 
209b11c1e72Sjeremylt   Memory usage can be tracked by the library.
210b11c1e72Sjeremylt 
211b11c1e72Sjeremylt   @param n     Number of units to allocate
212b11c1e72Sjeremylt   @param unit  Size of each unit
213b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
214b11c1e72Sjeremylt 
215b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
216b11c1e72Sjeremylt 
217b11c1e72Sjeremylt   @sa CeedFree()
218dfdf5a53Sjeremylt 
2197a982d89SJeremy L. Thompson   @ref Backend
220b11c1e72Sjeremylt **/
221d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
222d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
223d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
224c042f62fSJeremy L Thompson     // LCOV_EXCL_START
225e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
226e15f9bd0SJeremy L Thompson                      "calloc failed to allocate %zd members of size "
2271d102b48SJeremy L Thompson                      "%zd\n", n, unit);
228c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
229e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
230d7b241e6Sjeremylt }
231d7b241e6Sjeremylt 
232b11c1e72Sjeremylt /**
233b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
234b11c1e72Sjeremylt 
235b11c1e72Sjeremylt   Memory usage can be tracked by the library.
236b11c1e72Sjeremylt 
237b11c1e72Sjeremylt   @param n     Number of units to allocate
238b11c1e72Sjeremylt   @param unit  Size of each unit
239b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
240b11c1e72Sjeremylt 
241b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
242b11c1e72Sjeremylt 
243b11c1e72Sjeremylt   @sa CeedFree()
244dfdf5a53Sjeremylt 
2457a982d89SJeremy L. Thompson   @ref Backend
246b11c1e72Sjeremylt **/
247d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
248d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n*unit);
249d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
250c042f62fSJeremy L Thompson     // LCOV_EXCL_START
251e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
252e15f9bd0SJeremy L Thompson                      "realloc failed to allocate %zd members of size "
2531d102b48SJeremy L Thompson                      "%zd\n", n, unit);
254c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
255e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
256d7b241e6Sjeremylt }
257d7b241e6Sjeremylt 
25834138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
25934138859Sjeremylt 
26034138859Sjeremylt   @param p  address of pointer to memory.  This argument is of type void* to
26134138859Sjeremylt               avoid needing a cast, but is the address of the pointer (which is
26234138859Sjeremylt               zeroed) rather than the pointer.
26334138859Sjeremylt **/
264d7b241e6Sjeremylt int CeedFree(void *p) {
265d7b241e6Sjeremylt   free(*(void **)p);
266d7b241e6Sjeremylt   *(void **)p = NULL;
267e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
268d7b241e6Sjeremylt }
269d7b241e6Sjeremylt 
270d7b241e6Sjeremylt /**
2717a982d89SJeremy L. Thompson   @brief Register a Ceed backend
272d7b241e6Sjeremylt 
2737a982d89SJeremy L. Thompson   @param prefix    Prefix of resources for this backend to respond to.  For
2747a982d89SJeremy L. Thompson                      example, the reference backend responds to "/cpu/self".
2757a982d89SJeremy L. Thompson   @param init      Initialization function called by CeedInit() when the backend
2767a982d89SJeremy L. Thompson                      is selected to drive the requested resource.
2777a982d89SJeremy L. Thompson   @param priority  Integer priority.  Lower values are preferred in case the
2787a982d89SJeremy L. Thompson                      resource requested by CeedInit() has non-unique best prefix
2797a982d89SJeremy L. Thompson                      match.
280b11c1e72Sjeremylt 
281b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
282dfdf5a53Sjeremylt 
2837a982d89SJeremy L. Thompson   @ref Backend
284b11c1e72Sjeremylt **/
2857a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed),
2867a982d89SJeremy L. Thompson                  unsigned int priority) {
2877a982d89SJeremy L. Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0]))
2887a982d89SJeremy L. Thompson     // LCOV_EXCL_START
289e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many backends");
2907a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
2917a982d89SJeremy L. Thompson 
2927a982d89SJeremy L. Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
2937a982d89SJeremy L. Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0;
2947a982d89SJeremy L. Thompson   backends[num_backends].init = init;
2957a982d89SJeremy L. Thompson   backends[num_backends].priority = priority;
2967a982d89SJeremy L. Thompson   num_backends++;
297e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
298d7b241e6Sjeremylt }
299d7b241e6Sjeremylt 
300b11c1e72Sjeremylt /**
30160f9e2d6SJeremy L Thompson   @brief Return debugging status flag
30260f9e2d6SJeremy L Thompson 
30360f9e2d6SJeremy L Thompson   @param ceed      Ceed context to get debugging flag
304d1d35e2fSjeremylt   @param is_debug  Variable to store debugging flag
30560f9e2d6SJeremy L Thompson 
30660f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
30760f9e2d6SJeremy L Thompson 
308d1d35e2fSjeremylt   @ref Backend
30960f9e2d6SJeremy L Thompson **/
310d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) {
311d1d35e2fSjeremylt   *is_debug = ceed->debug;
312e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
31360f9e2d6SJeremy L Thompson }
31460f9e2d6SJeremy L Thompson 
31560f9e2d6SJeremy L Thompson /**
3167a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson   @param ceed         Ceed context to retrieve parent of
3197a982d89SJeremy L. Thompson   @param[out] parent  Address to save the parent to
3207a982d89SJeremy L. Thompson 
3217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3227a982d89SJeremy L. Thompson 
3237a982d89SJeremy L. Thompson   @ref Backend
3247a982d89SJeremy L. Thompson **/
3257a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3267a982d89SJeremy L. Thompson   int ierr;
3277a982d89SJeremy L. Thompson   if (ceed->parent) {
3287a982d89SJeremy L. Thompson     ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr);
329e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
3307a982d89SJeremy L. Thompson   }
3317a982d89SJeremy L. Thompson   *parent = ceed;
332e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3337a982d89SJeremy L. Thompson }
3347a982d89SJeremy L. Thompson 
3357a982d89SJeremy L. Thompson /**
3367a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3377a982d89SJeremy L. Thompson 
3387a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3397a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate 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 CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3467a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
347e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3487a982d89SJeremy L. Thompson }
3497a982d89SJeremy L. Thompson 
3507a982d89SJeremy L. Thompson /**
3517a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
3527a982d89SJeremy L. Thompson 
3537a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context. All
3547a982d89SJeremy L. Thompson     backend implementations default to the delegate Ceed context, unless
3557a982d89SJeremy L. Thompson     overridden.
3567a982d89SJeremy L. Thompson 
3577a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
3587a982d89SJeremy L. Thompson   @param[out] delegate  Address to set 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 CeedSetDelegate(Ceed ceed, Ceed delegate) {
3657a982d89SJeremy L. Thompson   ceed->delegate = delegate;
3667a982d89SJeremy L. Thompson   delegate->parent = ceed;
367e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3687a982d89SJeremy L. Thompson }
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson /**
3717a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
3727a982d89SJeremy L. Thompson 
3737a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3747a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
375d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to retrieve delegate for
3767a982d89SJeremy L. Thompson 
3777a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3787a982d89SJeremy L. Thompson 
3797a982d89SJeremy L. Thompson   @ref Backend
3807a982d89SJeremy L. Thompson **/
381d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) {
3827a982d89SJeremy L. Thompson   CeedInt ierr;
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson   // Check for object delegate
385d1d35e2fSjeremylt   for (CeedInt i=0; i<ceed->obj_delegate_count; i++)
386d1d35e2fSjeremylt     if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) {
387d1d35e2fSjeremylt       *delegate = ceed->obj_delegates->delegate;
388e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
3897a982d89SJeremy L. Thompson     }
3907a982d89SJeremy L. Thompson 
3917a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
3927a982d89SJeremy L. Thompson   ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr);
393e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3947a982d89SJeremy L. Thompson }
3957a982d89SJeremy L. Thompson 
3967a982d89SJeremy L. Thompson /**
3977a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
3987a982d89SJeremy L. Thompson 
3997a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context for a
4007a982d89SJeremy L. Thompson     given type of Ceed object. All backend implementations default to the
4017a982d89SJeremy L. Thompson     delegate Ceed context for this object. For example,
4027a982d89SJeremy L. Thompson     CeedSetObjectDelegate(ceed, refceed, "Basis")
4037a982d89SJeremy L. Thompson   uses refceed implementations for all CeedBasis backend functions.
4047a982d89SJeremy L. Thompson 
4057a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
4067a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
407d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to set delegate for
4087a982d89SJeremy L. Thompson 
4097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4107a982d89SJeremy L. Thompson 
4117a982d89SJeremy L. Thompson   @ref Backend
4127a982d89SJeremy L. Thompson **/
413d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) {
4147a982d89SJeremy L. Thompson   CeedInt ierr;
415d1d35e2fSjeremylt   CeedInt count = ceed->obj_delegate_count;
4167a982d89SJeremy L. Thompson 
4177a982d89SJeremy L. Thompson   // Malloc or Realloc
4187a982d89SJeremy L. Thompson   if (count) {
419d1d35e2fSjeremylt     ierr = CeedRealloc(count+1, &ceed->obj_delegates); CeedChk(ierr);
4207a982d89SJeremy L. Thompson   } else {
421d1d35e2fSjeremylt     ierr = CeedCalloc(1, &ceed->obj_delegates); CeedChk(ierr);
4227a982d89SJeremy L. Thompson   }
423d1d35e2fSjeremylt   ceed->obj_delegate_count++;
4247a982d89SJeremy L. Thompson 
4257a982d89SJeremy L. Thompson   // Set object delegate
426d1d35e2fSjeremylt   ceed->obj_delegates[count].delegate = delegate;
427d1d35e2fSjeremylt   size_t slen = strlen(obj_name) + 1;
428d1d35e2fSjeremylt   ierr = CeedMalloc(slen, &ceed->obj_delegates[count].obj_name); CeedChk(ierr);
429d1d35e2fSjeremylt   memcpy(ceed->obj_delegates[count].obj_name, obj_name, slen);
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson   // Set delegate parent
4327a982d89SJeremy L. Thompson   delegate->parent = ceed;
433e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4347a982d89SJeremy L. Thompson }
4357a982d89SJeremy L. Thompson 
4367a982d89SJeremy L. Thompson /**
4377a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4387a982d89SJeremy L. Thompson 
4397a982d89SJeremy L. Thompson   @param ceed           Ceed context
4407a982d89SJeremy L. Thompson   @param[out] resource  Variable to store fallback resource
4417a982d89SJeremy L. Thompson 
4427a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4437a982d89SJeremy L. Thompson 
4447a982d89SJeremy L. Thompson   @ref Backend
4457a982d89SJeremy L. Thompson **/
4467a982d89SJeremy L. Thompson 
4477a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
448d1d35e2fSjeremylt   *resource = (const char *)ceed->op_fallback_resource;
449e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4507a982d89SJeremy L. Thompson }
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson /**
4537a982d89SJeremy L. Thompson   @brief Set the fallback resource for CeedOperators. The current resource, if
4547a982d89SJeremy L. Thompson            any, is freed by calling this function. This string is freed upon the
4557a982d89SJeremy L. Thompson            destruction of the Ceed context.
4567a982d89SJeremy L. Thompson 
4577a982d89SJeremy L. Thompson   @param[out] ceed Ceed context
4587a982d89SJeremy L. Thompson   @param resource  Fallback resource to set
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4617a982d89SJeremy L. Thompson 
4627a982d89SJeremy L. Thompson   @ref Backend
4637a982d89SJeremy L. Thompson **/
4647a982d89SJeremy L. Thompson 
4657a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
4667a982d89SJeremy L. Thompson   int ierr;
4677a982d89SJeremy L. Thompson 
4687a982d89SJeremy L. Thompson   // Free old
469d1d35e2fSjeremylt   ierr = CeedFree(&ceed->op_fallback_resource); CeedChk(ierr);
4707a982d89SJeremy L. Thompson 
4717a982d89SJeremy L. Thompson   // Set new
4727a982d89SJeremy L. Thompson   size_t len = strlen(resource);
4737a982d89SJeremy L. Thompson   char *tmp;
4747a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
4757a982d89SJeremy L. Thompson   memcpy(tmp, resource, len+1);
476d1d35e2fSjeremylt   ceed->op_fallback_resource = tmp;
477e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4787a982d89SJeremy L. Thompson }
4797a982d89SJeremy L. Thompson 
4807a982d89SJeremy L. Thompson /**
4817a982d89SJeremy L. Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context
4827a982d89SJeremy L. Thompson            for a CeedOperator
4837a982d89SJeremy L. Thompson 
4847a982d89SJeremy L. Thompson   @param ceed         Ceed context
4857a982d89SJeremy L. Thompson   @param[out] parent  Variable to store parent Ceed context
4867a982d89SJeremy L. Thompson 
4877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4887a982d89SJeremy L. Thompson 
4897a982d89SJeremy L. Thompson   @ref Backend
4907a982d89SJeremy L. Thompson **/
4917a982d89SJeremy L. Thompson 
4927a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
493d1d35e2fSjeremylt   *parent = ceed->op_fallback_parent;
494e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4957a982d89SJeremy L. Thompson }
4967a982d89SJeremy L. Thompson 
4977a982d89SJeremy L. Thompson /**
4989525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
4999525855cSJeremy L Thompson 
5009525855cSJeremy L Thompson   @param ceed                   Ceed to flag as deterministic
50196b902e2Sjeremylt   @param[out] is_deterministic  Deterministic status to set
5029525855cSJeremy L Thompson 
5039525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5049525855cSJeremy L Thompson 
5059525855cSJeremy L Thompson   @ref Backend
5069525855cSJeremy L Thompson **/
5079525855cSJeremy L Thompson 
508d1d35e2fSjeremylt int CeedSetDeterministic(Ceed ceed, bool is_deterministic) {
509d1d35e2fSjeremylt   ceed->is_deterministic = is_deterministic;
510e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5119525855cSJeremy L Thompson }
5129525855cSJeremy L Thompson 
5139525855cSJeremy L Thompson /**
5147a982d89SJeremy L. Thompson   @brief Set a backend function
5157a982d89SJeremy L. Thompson 
5167a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5177a982d89SJeremy L. Thompson   the Ceed objects. For example,
5187a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5197a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5207a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5217a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
5227a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
5237a982d89SJeremy L. Thompson 
5247a982d89SJeremy L. Thompson   @param ceed         Ceed context for error handling
5257a982d89SJeremy L. Thompson   @param type         Type of Ceed object to set function for
5267a982d89SJeremy L. Thompson   @param[out] object  Ceed object to set function for
527d1d35e2fSjeremylt   @param func_name    Name of function to set
5287a982d89SJeremy L. Thompson   @param f            Function to set
5297a982d89SJeremy L. Thompson 
5307a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5317a982d89SJeremy L. Thompson 
5327a982d89SJeremy L. Thompson   @ref Backend
5337a982d89SJeremy L. Thompson **/
5347a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
535d1d35e2fSjeremylt                            const char *func_name, int (*f)()) {
536d1d35e2fSjeremylt   char lookup_name[CEED_MAX_RESOURCE_LEN+1] = "";
5377a982d89SJeremy L. Thompson 
5387a982d89SJeremy L. Thompson   // Build lookup name
5397a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
540d1d35e2fSjeremylt     strncat (lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN);
541d1d35e2fSjeremylt   strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN);
542d1d35e2fSjeremylt   strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN);
5437a982d89SJeremy L. Thompson 
5447a982d89SJeremy L. Thompson   // Find and use offset
545d1d35e2fSjeremylt   for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++)
546d1d35e2fSjeremylt     if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) {
547d1d35e2fSjeremylt       size_t offset = ceed->f_offsets[i].offset;
5487a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
5497a982d89SJeremy L. Thompson       *fpointer = f;
550e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
5517a982d89SJeremy L. Thompson     }
5527a982d89SJeremy L. Thompson 
5537a982d89SJeremy L. Thompson   // LCOV_EXCL_START
554e15f9bd0SJeremy L Thompson   return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
555e15f9bd0SJeremy L Thompson                    "Requested function '%s' was not found for CEED "
556d1d35e2fSjeremylt                    "object '%s'", func_name, type);
5577a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
5587a982d89SJeremy L. Thompson }
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson /**
5617a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
5627a982d89SJeremy L. Thompson 
5637a982d89SJeremy L. Thompson   @param ceed       Ceed context to retrieve data of
5647a982d89SJeremy L. Thompson   @param[out] data  Address to save data to
5657a982d89SJeremy L. Thompson 
5667a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5677a982d89SJeremy L. Thompson 
5687a982d89SJeremy L. Thompson   @ref Backend
5697a982d89SJeremy L. Thompson **/
570777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
571777ff853SJeremy L Thompson   *(void **)data = ceed->data;
572e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5737a982d89SJeremy L. Thompson }
5747a982d89SJeremy L. Thompson 
5757a982d89SJeremy L. Thompson /**
5767a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
5777a982d89SJeremy L. Thompson 
5787a982d89SJeremy L. Thompson   @param ceed  Ceed context to set data of
5797a982d89SJeremy L. Thompson   @param data  Address of data to set
5807a982d89SJeremy L. Thompson 
5817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5827a982d89SJeremy L. Thompson 
5837a982d89SJeremy L. Thompson   @ref Backend
5847a982d89SJeremy L. Thompson **/
585777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
586777ff853SJeremy L Thompson   ceed->data = data;
587e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5887a982d89SJeremy L. Thompson }
5897a982d89SJeremy L. Thompson 
59034359f16Sjeremylt /**
59134359f16Sjeremylt   @brief Increment the reference counter for a Ceed context
59234359f16Sjeremylt 
59334359f16Sjeremylt   @param ceed  Ceed context to increment the reference counter
59434359f16Sjeremylt 
59534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
59634359f16Sjeremylt 
59734359f16Sjeremylt   @ref Backend
59834359f16Sjeremylt **/
5999560d06aSjeremylt int CeedReference(Ceed ceed) {
60034359f16Sjeremylt   ceed->ref_count++;
60134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
60234359f16Sjeremylt }
60334359f16Sjeremylt 
6047a982d89SJeremy L. Thompson /// @}
6057a982d89SJeremy L. Thompson 
6067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6077a982d89SJeremy L. Thompson /// Ceed Public API
6087a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6097a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6107a982d89SJeremy L. Thompson /// @{
6117a982d89SJeremy L. Thompson 
6127a982d89SJeremy L. Thompson /**
61392ee7d1cSjeremylt   @brief Get the list of available resource names for Ceed contexts
6149ff86846Sjeremylt   Note: The caller is responsible for `free()`ing the resources and priorities arrays,
6159ff86846Sjeremylt           but should not `free()` the contents of the resources array.
61622e44211Sjeremylt 
61792ee7d1cSjeremylt   @param[out] n           Number of available resources
61892ee7d1cSjeremylt   @param[out] resources   List of available resource names
61922e44211Sjeremylt   @param[out] priorities  Resource name prioritization values, lower is better
62022e44211Sjeremylt 
62122e44211Sjeremylt   @return An error code: 0 - success, otherwise - failure
62222e44211Sjeremylt 
62322e44211Sjeremylt   @ref User
62422e44211Sjeremylt **/
62522e44211Sjeremylt // LCOV_EXCL_START
62622e44211Sjeremylt int CeedRegistryGetList(size_t *n, char ***const resources,
62722e44211Sjeremylt                         CeedInt **priorities) {
628d0c91ce9Sjeremylt   *n = 0;
6299ff86846Sjeremylt   *resources = malloc(num_backends * sizeof(**resources));
6309ff86846Sjeremylt   if (!resources)
6319ff86846Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6329ff86846Sjeremylt   if (priorities) {
6339ff86846Sjeremylt     *priorities = malloc(num_backends * sizeof(**priorities));
6349ff86846Sjeremylt     if (!priorities)
6359ff86846Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6369ff86846Sjeremylt   }
63722e44211Sjeremylt   for (size_t i=0; i<num_backends; i++) {
638d0c91ce9Sjeremylt     // Only report compiled backends
639d0c91ce9Sjeremylt     if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) {
64022e44211Sjeremylt       *resources[i] = backends[i].prefix;
6419ff86846Sjeremylt       if (priorities) *priorities[i] = backends[i].priority;
642d0c91ce9Sjeremylt       *n += 1;
643d0c91ce9Sjeremylt     }
644d0c91ce9Sjeremylt   }
64578464608Sjeremylt   if (*n == 0)
64678464608Sjeremylt     // LCOV_EXCL_START
64778464608Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "No backends installed");
64878464608Sjeremylt   // LCOV_EXCL_STOP
649d0c91ce9Sjeremylt   *resources = realloc(*resources, *n * sizeof(**resources));
650d0c91ce9Sjeremylt   if (!resources)
651d0c91ce9Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
652d0c91ce9Sjeremylt   if (priorities) {
653d0c91ce9Sjeremylt     *priorities = realloc(*priorities, *n * sizeof(**priorities));
654d0c91ce9Sjeremylt     if (!priorities)
655d0c91ce9Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
65622e44211Sjeremylt   }
65722e44211Sjeremylt   return CEED_ERROR_SUCCESS;
65845f1e315Sjeremylt }
65922e44211Sjeremylt // LCOV_EXCL_STOP
66022e44211Sjeremylt 
66122e44211Sjeremylt /**
662d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
66322e44211Sjeremylt   Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self")
66422e44211Sjeremylt     will result in CeedInt printing the current libCEED version number
66592ee7d1cSjeremylt     and a list of current available backend resources to stderr.
666b11c1e72Sjeremylt 
667b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
668b11c1e72Sjeremylt   @param ceed      The library context
669b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
670b11c1e72Sjeremylt 
671b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
672dfdf5a53Sjeremylt 
6737a982d89SJeremy L. Thompson   @ref User
674b11c1e72Sjeremylt **/
675d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
676d7b241e6Sjeremylt   int ierr;
677d0c91ce9Sjeremylt   size_t match_len = 0, match_idx = UINT_MAX,
678d0c91ce9Sjeremylt          match_priority = CEED_MAX_BACKEND_PRIORITY, priority;
679d7b241e6Sjeremylt 
680fe2413ffSjeremylt   // Find matching backend
6811d102b48SJeremy L Thompson   if (!resource)
68213873f79Sjeremylt     // LCOV_EXCL_START
683e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided");
68413873f79Sjeremylt   // LCOV_EXCL_STOP
6851d013790SJed Brown   ierr = CeedRegisterAll(); CeedChk(ierr);
68613873f79Sjeremylt 
68722e44211Sjeremylt   // Check for help request
68822e44211Sjeremylt   const char *help_prefix = "help";
68922e44211Sjeremylt   size_t match_help;
69022e44211Sjeremylt   for (match_help=0; match_help<4
69122e44211Sjeremylt        && resource[match_help] == help_prefix[match_help]; match_help++) {}
69222e44211Sjeremylt   if (match_help == 4) {
69322e44211Sjeremylt     fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR,
69422e44211Sjeremylt             CEED_VERSION_MINOR, CEED_VERSION_PATCH,
69522e44211Sjeremylt             CEED_VERSION_RELEASE ? "" : "+development");
69692ee7d1cSjeremylt     fprintf(stderr, "Available backend resources:\n");
69722e44211Sjeremylt     for (size_t i=0; i<num_backends; i++) {
698d0c91ce9Sjeremylt       // Only report compiled backends
699d0c91ce9Sjeremylt       if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY)
70022e44211Sjeremylt         fprintf(stderr, "  %s\n", backends[i].prefix);
70122e44211Sjeremylt     }
70222e44211Sjeremylt     fflush(stderr);
70322e44211Sjeremylt     match_help = 5; // Delineating character expected
70422e44211Sjeremylt   } else {
70522e44211Sjeremylt     match_help = 0;
70622e44211Sjeremylt   }
70722e44211Sjeremylt 
708*9c9a0587SLeila Ghaffari   // Find best match, computed as number of matching characters
709*9c9a0587SLeila Ghaffari   //   from requested resource stem
7102bbc7fe8Sjeremylt   size_t stem_length;
71122e44211Sjeremylt   for (stem_length=0; resource[stem_length+match_help]
71222e44211Sjeremylt        && resource[stem_length+match_help] != ':'; stem_length++) {}
713d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
714d7b241e6Sjeremylt     size_t n;
715d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
71622e44211Sjeremylt     for (n=0; prefix[n] && prefix[n] == resource[n+match_help]; n++) {}
717d7b241e6Sjeremylt     priority = backends[i].priority;
718d1d35e2fSjeremylt     if (n > match_len || (n == match_len && match_priority > priority)) {
719d1d35e2fSjeremylt       match_len = n;
720d1d35e2fSjeremylt       match_priority = priority;
721d1d35e2fSjeremylt       match_idx = i;
722d7b241e6Sjeremylt     }
723d7b241e6Sjeremylt   }
724*9c9a0587SLeila Ghaffari   // Using Levenshtein distance to find closest match
725*9c9a0587SLeila Ghaffari   if (match_len <= 1 || match_len != stem_length) {
726*9c9a0587SLeila Ghaffari     size_t lev_dis = UINT_MAX;
727*9c9a0587SLeila Ghaffari     size_t lev_idx = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY;
728*9c9a0587SLeila Ghaffari     for (size_t i=0; i<num_backends; i++) {
729*9c9a0587SLeila Ghaffari       const char *prefix = backends[i].prefix;
730*9c9a0587SLeila Ghaffari       size_t prefix_length = strlen(backends[i].prefix);
731*9c9a0587SLeila Ghaffari       size_t min_len = (prefix_length < stem_length) ? prefix_length : stem_length;
732*9c9a0587SLeila Ghaffari       size_t column[min_len+1], last_diag, old_diag;
733*9c9a0587SLeila Ghaffari       for (size_t j=1; j<=min_len; j++) column[j] = j;
734*9c9a0587SLeila Ghaffari       for (size_t j=1; j<=min_len; j++) {
735*9c9a0587SLeila Ghaffari         column[0] = j;
736*9c9a0587SLeila Ghaffari         for (size_t k=1, last_diag=j-1; k<=min_len; k++) {
737*9c9a0587SLeila Ghaffari           old_diag = column[k];
738*9c9a0587SLeila Ghaffari           size_t min_1 = (column[k] < column[k-1]) ? column[k]+1 : column[k-1]+1;
739*9c9a0587SLeila Ghaffari           size_t min_2 = last_diag + (resource[k-1] == prefix[j-1] ? 0 : 1);
740*9c9a0587SLeila Ghaffari           column[k] = (min_1<min_2) ? min_1 : min_2;
741*9c9a0587SLeila Ghaffari           last_diag = old_diag;
742*9c9a0587SLeila Ghaffari         }
743*9c9a0587SLeila Ghaffari       }
744*9c9a0587SLeila Ghaffari       size_t n = column[min_len];
745*9c9a0587SLeila Ghaffari       priority = backends[i].priority;
746*9c9a0587SLeila Ghaffari       if (n < lev_dis || (n == lev_dis
747*9c9a0587SLeila Ghaffari                           && lev_priority > priority)) {
748*9c9a0587SLeila Ghaffari         lev_dis = n;
749*9c9a0587SLeila Ghaffari         lev_priority = priority;
750*9c9a0587SLeila Ghaffari         lev_idx = i;
751*9c9a0587SLeila Ghaffari       }
752*9c9a0587SLeila Ghaffari     }
753*9c9a0587SLeila Ghaffari     const char *prefix_lev = backends[lev_idx].prefix;
754*9c9a0587SLeila Ghaffari     size_t lev_length;
755*9c9a0587SLeila Ghaffari     for (lev_length=0; prefix_lev[lev_length]
756*9c9a0587SLeila Ghaffari          && prefix_lev[lev_length] != '\0'; lev_length++) {}
757*9c9a0587SLeila Ghaffari     size_t m = (lev_length < stem_length) ? lev_length : stem_length;
758*9c9a0587SLeila Ghaffari     if (lev_dis+1 >= m) {
75913873f79Sjeremylt       // LCOV_EXCL_START
760e15f9bd0SJeremy L Thompson       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s",
761e15f9bd0SJeremy L Thompson                        resource);
76213873f79Sjeremylt       // LCOV_EXCL_STOP
763*9c9a0587SLeila Ghaffari     } else {
7642bbc7fe8Sjeremylt       // LCOV_EXCL_START
76587250337Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s\n"
766*9c9a0587SLeila Ghaffari                        "Closest match: %s", resource, backends[lev_idx].prefix);
7672bbc7fe8Sjeremylt       // LCOV_EXCL_STOP
7682bbc7fe8Sjeremylt     }
769*9c9a0587SLeila Ghaffari   }
770fe2413ffSjeremylt 
771fe2413ffSjeremylt   // Setup Ceed
772d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
773bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
7741d102b48SJeremy L Thompson   if (!ceed_error_handler)
7751d102b48SJeremy L Thompson     ceed_error_handler = "abort";
776bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
77756e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
778477729cfSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store"))
779477729cfSJeremy L Thompson     (*ceed)->Error = CeedErrorStore;
78056e866f4SJed Brown   else
781d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
782d1d35e2fSjeremylt   memcpy((*ceed)->err_msg, "No error message stored", 24);
783d1d35e2fSjeremylt   (*ceed)->ref_count = 1;
784d7b241e6Sjeremylt   (*ceed)->data = NULL;
785fe2413ffSjeremylt 
786fe2413ffSjeremylt   // Set lookup table
787d1d35e2fSjeremylt   FOffset f_offsets[] = {
7886e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
7896e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
7906e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
791f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
7926e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
7936e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
7946e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
7956e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
7966e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
7976e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
798777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
7996e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
8006e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
8016e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
8026a6c615bSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, TakeArray),
8036e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
8046e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
8056e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
8066e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
8076e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
808547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
809e0dd3b27Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Scale),
8100f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, AXPY),
8110f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, PointwiseMult),
812d99fa3c5SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
8136e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Destroy),
8146e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
8156e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
816bd33150aSjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
8176e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
8186e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Apply),
8196e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Destroy),
8206e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
8216e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
8226e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Apply),
8238c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
8248c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
8256e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
826777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
827891038deSjeremylt     CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData),
828777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
829777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
830777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
83180ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
83270a7ffb3SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate),
83380ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
8349e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
83580ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
8369e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
837e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic),
838e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble),
839713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
8406e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
841250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
842cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
843250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
8446e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
8456e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
8466e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
8471dfeef1dSjeremylt   };
848fe2413ffSjeremylt 
849d1d35e2fSjeremylt   ierr = CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets); CeedChk(ierr);
850d1d35e2fSjeremylt   memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets));
851fe2413ffSjeremylt 
8525107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
853e2f04181SAndrew T. Barker   const char fallbackresource[] = "";
8545107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
8555107b09fSJeremy L Thompson   CeedChk(ierr);
8565107b09fSJeremy L Thompson 
85760f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
85860f9e2d6SJeremy L Thompson   (*ceed)->debug = !!getenv("CEED_DEBUG") || !!getenv("DBG");
85960f9e2d6SJeremy L Thompson 
860fe2413ffSjeremylt   // Backend specific setup
861d1d35e2fSjeremylt   ierr = backends[match_idx].init(&resource[match_help], *ceed); CeedChk(ierr);
862fe2413ffSjeremylt 
86322e44211Sjeremylt   // Copy resource prefix, if backend setup successful
864d1d35e2fSjeremylt   size_t len = strlen(backends[match_idx].prefix);
865e07206deSjeremylt   char *tmp;
866e07206deSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
867d1d35e2fSjeremylt   memcpy(tmp, backends[match_idx].prefix, len+1);
868e07206deSjeremylt   (*ceed)->resource = tmp;
869e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
870d7b241e6Sjeremylt }
871d7b241e6Sjeremylt 
872d7b241e6Sjeremylt /**
8739560d06aSjeremylt   @brief Copy the pointer to a Ceed context. Both pointers should
8749560d06aSjeremylt            be destroyed with `CeedDestroy()`;
8759560d06aSjeremylt            Note: If `*ceed_copy` is non-NULL, then it is assumed that
8769560d06aSjeremylt            `*ceed_copy` is a pointer to a Ceed context. This Ceed
8779560d06aSjeremylt            context will be destroyed if `*ceed_copy` is the only
8789560d06aSjeremylt            reference to this Ceed context.
8799560d06aSjeremylt 
8809560d06aSjeremylt   @param ceed            Ceed context to copy reference to
8819560d06aSjeremylt   @param[out] ceed_copy  Variable to store copied reference
8829560d06aSjeremylt 
8839560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8849560d06aSjeremylt 
8859560d06aSjeremylt   @ref User
8869560d06aSjeremylt **/
8879560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) {
8889560d06aSjeremylt   int ierr;
8899560d06aSjeremylt 
8909560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
8919560d06aSjeremylt   ierr = CeedDestroy(ceed_copy); CeedChk(ierr);
8929560d06aSjeremylt   *ceed_copy = ceed;
8939560d06aSjeremylt   return CEED_ERROR_SUCCESS;
8949560d06aSjeremylt }
8959560d06aSjeremylt 
8969560d06aSjeremylt /**
8977a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
8982f86a920SJeremy L Thompson 
8997a982d89SJeremy L. Thompson   @param ceed           Ceed context to get resource name of
9007a982d89SJeremy L. Thompson   @param[out] resource  Variable to store resource name
9012f86a920SJeremy L Thompson 
9022f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9032f86a920SJeremy L Thompson 
9047a982d89SJeremy L. Thompson   @ref User
9055107b09fSJeremy L Thompson **/
9067a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
9077a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
908e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9095107b09fSJeremy L Thompson }
9105107b09fSJeremy L Thompson 
9115107b09fSJeremy L Thompson /**
912d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
913c907536fSjeremylt 
914d79b80ecSjeremylt   @param ceed           Ceed context to get preferred memory type of
915d1d35e2fSjeremylt   @param[out] mem_type  Address to save preferred memory type to
916c907536fSjeremylt 
917c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
918c907536fSjeremylt 
9197a982d89SJeremy L. Thompson   @ref User
920c907536fSjeremylt **/
921d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) {
922c907536fSjeremylt   int ierr;
923c263cd57Sjeremylt 
924c907536fSjeremylt   if (ceed->GetPreferredMemType) {
925d1d35e2fSjeremylt     ierr = ceed->GetPreferredMemType(mem_type); CeedChk(ierr);
926c907536fSjeremylt   } else {
927c263cd57Sjeremylt     Ceed delegate;
928c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
929c263cd57Sjeremylt 
930c263cd57Sjeremylt     if (delegate) {
931d1d35e2fSjeremylt       ierr = CeedGetPreferredMemType(delegate, mem_type); CeedChk(ierr);
932c263cd57Sjeremylt     } else {
933d1d35e2fSjeremylt       *mem_type = CEED_MEM_HOST;
934c907536fSjeremylt     }
935c263cd57Sjeremylt   }
936e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
937c907536fSjeremylt }
938c907536fSjeremylt 
939c907536fSjeremylt /**
9409525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
9419525855cSJeremy L Thompson 
9429525855cSJeremy L Thompson   @param[in] ceed               Ceed
943d1d35e2fSjeremylt   @param[out] is_deterministic  Variable to store deterministic status
9449525855cSJeremy L Thompson 
9459525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9469525855cSJeremy L Thompson 
9479525855cSJeremy L Thompson   @ref User
9489525855cSJeremy L Thompson **/
949d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) {
950d1d35e2fSjeremylt   *is_deterministic = ceed->is_deterministic;
951e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9529525855cSJeremy L Thompson }
9539525855cSJeremy L Thompson 
9549525855cSJeremy L Thompson /**
9550a0da059Sjeremylt   @brief View a Ceed
9560a0da059Sjeremylt 
9570a0da059Sjeremylt   @param[in] ceed    Ceed to view
9580a0da059Sjeremylt   @param[in] stream  Filestream to write to
9590a0da059Sjeremylt 
9600a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
9610a0da059Sjeremylt 
9620a0da059Sjeremylt   @ref User
9630a0da059Sjeremylt **/
9640a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
9650a0da059Sjeremylt   int ierr;
966d1d35e2fSjeremylt   CeedMemType mem_type;
9670a0da059Sjeremylt 
968d1d35e2fSjeremylt   ierr = CeedGetPreferredMemType(ceed, &mem_type); CeedChk(ierr);
9690a0da059Sjeremylt 
9700a0da059Sjeremylt   fprintf(stream, "Ceed\n"
9710a0da059Sjeremylt           "  Ceed Resource: %s\n"
9720a0da059Sjeremylt           "  Preferred MemType: %s\n",
973d1d35e2fSjeremylt           ceed->resource, CeedMemTypes[mem_type]);
974e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9750a0da059Sjeremylt }
9760a0da059Sjeremylt 
9770a0da059Sjeremylt /**
978b11c1e72Sjeremylt   @brief Destroy a Ceed context
979d7b241e6Sjeremylt 
980d7b241e6Sjeremylt   @param ceed  Address of Ceed context to destroy
981b11c1e72Sjeremylt 
982b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
983dfdf5a53Sjeremylt 
9847a982d89SJeremy L. Thompson   @ref User
985b11c1e72Sjeremylt **/
986d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
987d7b241e6Sjeremylt   int ierr;
988d1d35e2fSjeremylt   if (!*ceed || --(*ceed)->ref_count > 0) return CEED_ERROR_SUCCESS;
9895fe0d4faSjeremylt   if ((*ceed)->delegate) {
9905fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
9915fe0d4faSjeremylt   }
9920ace9bf2Sjeremylt 
993d1d35e2fSjeremylt   if ((*ceed)->obj_delegate_count > 0) {
994d1d35e2fSjeremylt     for (int i=0; i<(*ceed)->obj_delegate_count; i++) {
995d1d35e2fSjeremylt       ierr = CeedDestroy(&((*ceed)->obj_delegates[i].delegate)); CeedChk(ierr);
996d1d35e2fSjeremylt       ierr = CeedFree(&(*ceed)->obj_delegates[i].obj_name); CeedChk(ierr);
997aefd8378Sjeremylt     }
998d1d35e2fSjeremylt     ierr = CeedFree(&(*ceed)->obj_delegates); CeedChk(ierr);
999aefd8378Sjeremylt   }
10000ace9bf2Sjeremylt 
1001d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
1002d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
1003d7b241e6Sjeremylt   }
10040ace9bf2Sjeremylt 
1005d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->f_offsets); CeedChk(ierr);
1006e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
1007d1d35e2fSjeremylt   ierr = CeedDestroy(&(*ceed)->op_fallback_ceed); CeedChk(ierr);
1008d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->op_fallback_resource); CeedChk(ierr);
1009d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
1010e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1011d7b241e6Sjeremylt }
1012d7b241e6Sjeremylt 
1013f9982c62SWill Pazner // LCOV_EXCL_START
1014f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
10153f4a9821SAndrew T. Barker   if (ceed->parent)
10163f4a9821SAndrew T. Barker     return CeedErrorFormat(ceed->parent, format, args);
1017d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1018d1d35e2fSjeremylt     return CeedErrorFormat(ceed->op_fallback_parent, format, args);
101978464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
102078464608Sjeremylt   vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args); // NOLINT
1021d1d35e2fSjeremylt   return ceed->err_msg;
1022f9982c62SWill Pazner }
1023f9982c62SWill Pazner // LCOV_EXCL_STOP
1024f9982c62SWill Pazner 
10257a982d89SJeremy L. Thompson /**
10267a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
10277a982d89SJeremy L. Thompson 
10287a982d89SJeremy L. Thompson   @ref Developer
10297a982d89SJeremy L. Thompson **/
10307a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
10317a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
10327a982d89SJeremy L. Thompson   va_list args;
1033d1d35e2fSjeremylt   int ret_val;
10347a982d89SJeremy L. Thompson   va_start(args, format);
10357a982d89SJeremy L. Thompson   if (ceed) {
1036d1d35e2fSjeremylt     ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
10377a982d89SJeremy L. Thompson   } else {
1038b0d62198Sjeremylt     // LCOV_EXCL_START
1039477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
1040477729cfSJeremy L Thompson     if (!ceed_error_handler)
1041477729cfSJeremy L Thompson       ceed_error_handler = "abort";
1042477729cfSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return"))
1043d1d35e2fSjeremylt       ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
1044477729cfSJeremy L Thompson     else
1045477729cfSJeremy L Thompson       // This function will not return
1046d1d35e2fSjeremylt       ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
10477a982d89SJeremy L. Thompson   }
10487a982d89SJeremy L. Thompson   va_end(args);
1049d1d35e2fSjeremylt   return ret_val;
1050b0d62198Sjeremylt   // LCOV_EXCL_STOP
10517a982d89SJeremy L. Thompson }
10527a982d89SJeremy L. Thompson 
1053477729cfSJeremy L Thompson /**
1054477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
1055477729cfSJeremy L Thompson 
1056477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1057477729cfSJeremy L Thompson 
1058477729cfSJeremy L Thompson   @ref Developer
1059477729cfSJeremy L Thompson **/
1060477729cfSJeremy L Thompson // LCOV_EXCL_START
1061d1d35e2fSjeremylt int CeedErrorReturn(Ceed ceed, const char *filename, int line_no,
1062d1d35e2fSjeremylt                     const char *func, int err_code, const char *format,
1063f9982c62SWill Pazner                     va_list *args) {
1064d1d35e2fSjeremylt   return err_code;
1065477729cfSJeremy L Thompson }
1066477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1067477729cfSJeremy L Thompson 
1068477729cfSJeremy L Thompson /**
1069477729cfSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns
1070477729cfSJeremy L Thompson            the error.
1071477729cfSJeremy L Thompson 
1072477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1073477729cfSJeremy L Thompson 
1074477729cfSJeremy L Thompson   @ref Developer
1075477729cfSJeremy L Thompson **/
1076477729cfSJeremy L Thompson // LCOV_EXCL_START
1077d1d35e2fSjeremylt int CeedErrorStore(Ceed ceed, const char *filename, int line_no,
1078d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1079f9982c62SWill Pazner                    va_list *args) {
1080477729cfSJeremy L Thompson   if (ceed->parent)
1081d1d35e2fSjeremylt     return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format,
1082187168c7SJeremy L Thompson                           args);
1083d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1084d1d35e2fSjeremylt     return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func,
1085d1d35e2fSjeremylt                           err_code, format, args);
1086477729cfSJeremy L Thompson 
1087477729cfSJeremy L Thompson   // Build message
1088477729cfSJeremy L Thompson   CeedInt len;
1089d1d35e2fSjeremylt   len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ",
1090d1d35e2fSjeremylt                  filename, line_no, func);
109178464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
109278464608Sjeremylt   // *INDENT-OFF*
109378464608Sjeremylt   vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args); // NOLINT
109478464608Sjeremylt   // *INDENT-ON*
1095d1d35e2fSjeremylt   return err_code;
1096477729cfSJeremy L Thompson }
1097477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1098477729cfSJeremy L Thompson 
1099477729cfSJeremy L Thompson /**
1100477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
1101477729cfSJeremy L Thompson 
1102477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1103477729cfSJeremy L Thompson 
1104477729cfSJeremy L Thompson   @ref Developer
1105477729cfSJeremy L Thompson **/
1106477729cfSJeremy L Thompson // LCOV_EXCL_START
1107d1d35e2fSjeremylt int CeedErrorAbort(Ceed ceed, const char *filename, int line_no,
1108d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1109f9982c62SWill Pazner                    va_list *args) {
1110d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
1111f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1112477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1113477729cfSJeremy L Thompson   abort();
1114d1d35e2fSjeremylt   return err_code;
1115477729cfSJeremy L Thompson }
1116477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1117477729cfSJeremy L Thompson 
1118477729cfSJeremy L Thompson /**
1119477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
1120477729cfSJeremy L Thompson 
1121477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1122477729cfSJeremy L Thompson 
1123477729cfSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit()
1124477729cfSJeremy L Thompson   handlers (e.g., as used by gcov) are run.
1125477729cfSJeremy L Thompson 
1126477729cfSJeremy L Thompson   @ref Developer
1127477729cfSJeremy L Thompson **/
1128d1d35e2fSjeremylt int CeedErrorExit(Ceed ceed, const char *filename, int line_no,
1129d1d35e2fSjeremylt                   const char *func, int err_code, const char *format, va_list *args) {
1130d1d35e2fSjeremylt   fprintf(stderr, "%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   vfprintf(stderr, format, *args); // NOLINT
1133477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1134d1d35e2fSjeremylt   exit(err_code);
1135d1d35e2fSjeremylt   return err_code;
1136477729cfSJeremy L Thompson }
1137477729cfSJeremy L Thompson 
1138477729cfSJeremy L Thompson /**
1139477729cfSJeremy L Thompson   @brief Set error handler
1140477729cfSJeremy L Thompson 
1141477729cfSJeremy L Thompson   A default error handler is set in CeedInit().  Use this function to change
1142477729cfSJeremy L Thompson   the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined
1143477729cfSJeremy L Thompson   error handler.
1144477729cfSJeremy L Thompson 
1145477729cfSJeremy L Thompson   @ref Developer
1146477729cfSJeremy L Thompson **/
1147d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) {
1148d1d35e2fSjeremylt   ceed->Error = handler;
1149d1d35e2fSjeremylt   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler);
1150d1d35e2fSjeremylt   for (int i=0; i<ceed->obj_delegate_count; i++)
1151d1d35e2fSjeremylt     CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler);
1152e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1153477729cfSJeremy L Thompson }
1154477729cfSJeremy L Thompson 
1155477729cfSJeremy L Thompson /**
1156477729cfSJeremy L Thompson   @brief Get error message
1157477729cfSJeremy L Thompson 
1158477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1159477729cfSJeremy L Thompson     CeedErrorStore()
1160477729cfSJeremy L Thompson 
1161477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to retrieve error message
1162d1d35e2fSjeremylt   @param[out] err_msg  Char pointer to hold error message
1163477729cfSJeremy L Thompson 
1164477729cfSJeremy L Thompson   @ref Developer
1165477729cfSJeremy L Thompson **/
1166d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) {
11673f4a9821SAndrew T. Barker   if (ceed->parent)
1168d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->parent, err_msg);
1169d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1170d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg);
1171d1d35e2fSjeremylt   *err_msg = ceed->err_msg;
1172e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1173477729cfSJeremy L Thompson }
1174477729cfSJeremy L Thompson 
1175477729cfSJeremy L Thompson /**
1176477729cfSJeremy L Thompson   @brief Restore error message
1177477729cfSJeremy L Thompson 
1178477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1179477729cfSJeremy L Thompson     CeedErrorStore()
1180477729cfSJeremy L Thompson 
1181477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to restore error message
1182d1d35e2fSjeremylt   @param[out] err_msg  Char pointer that holds error message
1183477729cfSJeremy L Thompson 
1184477729cfSJeremy L Thompson   @ref Developer
1185477729cfSJeremy L Thompson **/
1186d1d35e2fSjeremylt int CeedResetErrorMessage(Ceed ceed, const char **err_msg) {
11873f4a9821SAndrew T. Barker   if (ceed->parent)
1188d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->parent, err_msg);
1189d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1190d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg);
1191d1d35e2fSjeremylt   *err_msg = NULL;
1192d1d35e2fSjeremylt   memcpy(ceed->err_msg, "No error message stored", 24);
1193e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1194477729cfSJeremy L Thompson }
1195477729cfSJeremy L Thompson 
11961070991dSJed Brown /**
11971070991dSJed Brown   @brief Get libCEED library version info
11981070991dSJed Brown 
11991070991dSJed Brown   libCEED version numbers have the form major.minor.patch. Non-release versions
12001070991dSJed Brown   may contain unstable interfaces.
12011070991dSJed Brown 
12021070991dSJed Brown   @param[out] major    Major version of the library
12031070991dSJed Brown   @param[out] minor    Minor version of the library
12041070991dSJed Brown   @param[out] patch    Patch (subminor) version of the library
12051070991dSJed Brown   @param[out] release  True for releases; false for development branches.
12061070991dSJed Brown 
12071070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
12081070991dSJed Brown 
12091070991dSJed Brown   @sa CEED_VERSION_GE()
12101070991dSJed Brown 
12111070991dSJed Brown   @ref Developer
12121070991dSJed Brown */
12131070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
12141070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
12151070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
12161070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
12171070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
12181070991dSJed Brown   return 0;
12191070991dSJed Brown }
12201070991dSJed Brown 
122180a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) {
122280a9ef05SNatalie Beams   *scalar_type = CEED_SCALAR_TYPE;
122380a9ef05SNatalie Beams   return 0;
122480a9ef05SNatalie Beams }
122580a9ef05SNatalie Beams 
122680a9ef05SNatalie Beams 
1227d7b241e6Sjeremylt /// @}
1228