xref: /libCEED/interface/ceed.c (revision 3f21f6b10abeb5d85d3454ea5cd38498737dc88a)
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 /**
133*3f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
13460f9e2d6SJeremy L Thompson 
13560f9e2d6SJeremy L Thompson   @param ceed    Ceed context
13660f9e2d6SJeremy L Thompson 
137*3f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
138*3f21f6b1SJeremy L Thompson                          false - debugging mode disabled
13960f9e2d6SJeremy L Thompson 
14060f9e2d6SJeremy L Thompson   @ref Backend
14160f9e2d6SJeremy L Thompson **/
142fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
143*3f21f6b1SJeremy L Thompson bool CeedDebugFlag(const Ceed ceed) {
144*3f21f6b1SJeremy L Thompson   return ceed->is_debug;
14560f9e2d6SJeremy L Thompson }
146fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
14760f9e2d6SJeremy L Thompson 
14860f9e2d6SJeremy L Thompson /**
149*3f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
15060f9e2d6SJeremy L Thompson 
151*3f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
152*3f21f6b1SJeremy L Thompson                          false - debugging mode disabled
153*3f21f6b1SJeremy L Thompson 
154*3f21f6b1SJeremy L Thompson   @ref Backend
155*3f21f6b1SJeremy L Thompson **/
156*3f21f6b1SJeremy L Thompson // LCOV_EXCL_START
157*3f21f6b1SJeremy L Thompson bool CeedDebugFlagEnv(void) {
158*3f21f6b1SJeremy L Thompson   return !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG");
159*3f21f6b1SJeremy L Thompson }
160*3f21f6b1SJeremy L Thompson // LCOV_EXCL_STOP
161*3f21f6b1SJeremy L Thompson 
162*3f21f6b1SJeremy L Thompson /**
163*3f21f6b1SJeremy L Thompson   @brief Print debugging information in color
164*3f21f6b1SJeremy L Thompson 
16560f9e2d6SJeremy L Thompson   @param color   Color to print
16660f9e2d6SJeremy L Thompson   @param format  Printing format
16760f9e2d6SJeremy L Thompson 
16860f9e2d6SJeremy L Thompson   @return None
16960f9e2d6SJeremy L Thompson 
17060f9e2d6SJeremy L Thompson   @ref Backend
17160f9e2d6SJeremy L Thompson **/
172fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
173*3f21f6b1SJeremy L Thompson void CeedDebugImpl256(const unsigned char color, const char *format,...) {
17460f9e2d6SJeremy L Thompson   va_list args;
17560f9e2d6SJeremy L Thompson   va_start(args, format);
17660f9e2d6SJeremy L Thompson   fflush(stdout);
177*3f21f6b1SJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE)
17860f9e2d6SJeremy L Thompson     fprintf(stdout, "\033[38;5;%dm", color);
17960f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
180*3f21f6b1SJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE)
18160f9e2d6SJeremy L Thompson     fprintf(stdout, "\033[m");
18260f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
18360f9e2d6SJeremy L Thompson   fflush(stdout);
18460f9e2d6SJeremy L Thompson   va_end(args);
18560f9e2d6SJeremy L Thompson }
186fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
18760f9e2d6SJeremy L Thompson 
18860f9e2d6SJeremy L Thompson /**
189b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
190b11c1e72Sjeremylt 
191b11c1e72Sjeremylt   Memory usage can be tracked by the library.  This ensures sufficient
192b11c1e72Sjeremylt     alignment for vectorization and should be used for large allocations.
193b11c1e72Sjeremylt 
194b11c1e72Sjeremylt   @param n     Number of units to allocate
195b11c1e72Sjeremylt   @param unit  Size of each unit
196b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
197b11c1e72Sjeremylt 
198b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
199b11c1e72Sjeremylt 
200b11c1e72Sjeremylt   @sa CeedFree()
201dfdf5a53Sjeremylt 
2027a982d89SJeremy L. Thompson   @ref Backend
203b11c1e72Sjeremylt **/
204d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
205d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit);
206d7b241e6Sjeremylt   if (ierr)
207c042f62fSJeremy L Thompson     // LCOV_EXCL_START
208e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
209e15f9bd0SJeremy L Thompson                      "posix_memalign failed to allocate %zd "
2101d102b48SJeremy L Thompson                      "members of size %zd\n", n, unit);
211c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
212e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
213d7b241e6Sjeremylt }
214d7b241e6Sjeremylt 
215b11c1e72Sjeremylt /**
216b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
217b11c1e72Sjeremylt 
218b11c1e72Sjeremylt   Memory usage can be tracked by the library.
219b11c1e72Sjeremylt 
220b11c1e72Sjeremylt   @param n     Number of units to allocate
221b11c1e72Sjeremylt   @param unit  Size of each unit
222b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
223b11c1e72Sjeremylt 
224b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
225b11c1e72Sjeremylt 
226b11c1e72Sjeremylt   @sa CeedFree()
227dfdf5a53Sjeremylt 
2287a982d89SJeremy L. Thompson   @ref Backend
229b11c1e72Sjeremylt **/
230d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
231d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
232d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
233c042f62fSJeremy L Thompson     // LCOV_EXCL_START
234e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
235e15f9bd0SJeremy L Thompson                      "calloc failed to allocate %zd members of size "
2361d102b48SJeremy L Thompson                      "%zd\n", n, unit);
237c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
238e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
239d7b241e6Sjeremylt }
240d7b241e6Sjeremylt 
241b11c1e72Sjeremylt /**
242b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
243b11c1e72Sjeremylt 
244b11c1e72Sjeremylt   Memory usage can be tracked by the library.
245b11c1e72Sjeremylt 
246b11c1e72Sjeremylt   @param n     Number of units to allocate
247b11c1e72Sjeremylt   @param unit  Size of each unit
248b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
249b11c1e72Sjeremylt 
250b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
251b11c1e72Sjeremylt 
252b11c1e72Sjeremylt   @sa CeedFree()
253dfdf5a53Sjeremylt 
2547a982d89SJeremy L. Thompson   @ref Backend
255b11c1e72Sjeremylt **/
256d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
257d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n*unit);
258d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
259c042f62fSJeremy L Thompson     // LCOV_EXCL_START
260e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
261e15f9bd0SJeremy L Thompson                      "realloc failed to allocate %zd members of size "
2621d102b48SJeremy L Thompson                      "%zd\n", n, unit);
263c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
264e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
265d7b241e6Sjeremylt }
266d7b241e6Sjeremylt 
26734138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
26834138859Sjeremylt 
26934138859Sjeremylt   @param p  address of pointer to memory.  This argument is of type void* to
27034138859Sjeremylt               avoid needing a cast, but is the address of the pointer (which is
27134138859Sjeremylt               zeroed) rather than the pointer.
27234138859Sjeremylt **/
273d7b241e6Sjeremylt int CeedFree(void *p) {
274d7b241e6Sjeremylt   free(*(void **)p);
275d7b241e6Sjeremylt   *(void **)p = NULL;
276e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
277d7b241e6Sjeremylt }
278d7b241e6Sjeremylt 
279d7b241e6Sjeremylt /**
2807a982d89SJeremy L. Thompson   @brief Register a Ceed backend
281d7b241e6Sjeremylt 
2827a982d89SJeremy L. Thompson   @param prefix    Prefix of resources for this backend to respond to.  For
2837a982d89SJeremy L. Thompson                      example, the reference backend responds to "/cpu/self".
2847a982d89SJeremy L. Thompson   @param init      Initialization function called by CeedInit() when the backend
2857a982d89SJeremy L. Thompson                      is selected to drive the requested resource.
2867a982d89SJeremy L. Thompson   @param priority  Integer priority.  Lower values are preferred in case the
2877a982d89SJeremy L. Thompson                      resource requested by CeedInit() has non-unique best prefix
2887a982d89SJeremy L. Thompson                      match.
289b11c1e72Sjeremylt 
290b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
291dfdf5a53Sjeremylt 
2927a982d89SJeremy L. Thompson   @ref Backend
293b11c1e72Sjeremylt **/
2947a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed),
2957a982d89SJeremy L. Thompson                  unsigned int priority) {
2967a982d89SJeremy L. Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0]))
2977a982d89SJeremy L. Thompson     // LCOV_EXCL_START
298e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many backends");
2997a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
3007a982d89SJeremy L. Thompson 
3017a982d89SJeremy L. Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
3027a982d89SJeremy L. Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0;
3037a982d89SJeremy L. Thompson   backends[num_backends].init = init;
3047a982d89SJeremy L. Thompson   backends[num_backends].priority = priority;
3057a982d89SJeremy L. Thompson   num_backends++;
306e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
307d7b241e6Sjeremylt }
308d7b241e6Sjeremylt 
309b11c1e72Sjeremylt /**
31060f9e2d6SJeremy L Thompson   @brief Return debugging status flag
31160f9e2d6SJeremy L Thompson 
31260f9e2d6SJeremy L Thompson   @param ceed      Ceed context to get debugging flag
313d1d35e2fSjeremylt   @param is_debug  Variable to store debugging flag
31460f9e2d6SJeremy L Thompson 
31560f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
31660f9e2d6SJeremy L Thompson 
317d1d35e2fSjeremylt   @ref Backend
31860f9e2d6SJeremy L Thompson **/
319d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) {
320*3f21f6b1SJeremy L Thompson   *is_debug = ceed->is_debug;
321e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
32260f9e2d6SJeremy L Thompson }
32360f9e2d6SJeremy L Thompson 
32460f9e2d6SJeremy L Thompson /**
3257a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3267a982d89SJeremy L. Thompson 
3277a982d89SJeremy L. Thompson   @param ceed         Ceed context to retrieve parent of
3287a982d89SJeremy L. Thompson   @param[out] parent  Address to save the parent to
3297a982d89SJeremy L. Thompson 
3307a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3317a982d89SJeremy L. Thompson 
3327a982d89SJeremy L. Thompson   @ref Backend
3337a982d89SJeremy L. Thompson **/
3347a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3357a982d89SJeremy L. Thompson   int ierr;
3367a982d89SJeremy L. Thompson   if (ceed->parent) {
3377a982d89SJeremy L. Thompson     ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr);
338e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
3397a982d89SJeremy L. Thompson   }
3407a982d89SJeremy L. Thompson   *parent = ceed;
341e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3427a982d89SJeremy L. Thompson }
3437a982d89SJeremy L. Thompson 
3447a982d89SJeremy L. Thompson /**
3457a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3467a982d89SJeremy L. Thompson 
3477a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3487a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
3497a982d89SJeremy L. Thompson 
3507a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3517a982d89SJeremy L. Thompson 
3527a982d89SJeremy L. Thompson   @ref Backend
3537a982d89SJeremy L. Thompson **/
3547a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3557a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
356e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3577a982d89SJeremy L. Thompson }
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson /**
3607a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
3617a982d89SJeremy L. Thompson 
3627a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context. All
3637a982d89SJeremy L. Thompson     backend implementations default to the delegate Ceed context, unless
3647a982d89SJeremy L. Thompson     overridden.
3657a982d89SJeremy L. Thompson 
3667a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
3677a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
3687a982d89SJeremy L. Thompson 
3697a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson   @ref Backend
3727a982d89SJeremy L. Thompson **/
3737a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
3747a982d89SJeremy L. Thompson   ceed->delegate = delegate;
3757a982d89SJeremy L. Thompson   delegate->parent = ceed;
376e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3777a982d89SJeremy L. Thompson }
3787a982d89SJeremy L. Thompson 
3797a982d89SJeremy L. Thompson /**
3807a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
3817a982d89SJeremy L. Thompson 
3827a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3837a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
384d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to retrieve delegate for
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3877a982d89SJeremy L. Thompson 
3887a982d89SJeremy L. Thompson   @ref Backend
3897a982d89SJeremy L. Thompson **/
390d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) {
3917a982d89SJeremy L. Thompson   CeedInt ierr;
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson   // Check for object delegate
394d1d35e2fSjeremylt   for (CeedInt i=0; i<ceed->obj_delegate_count; i++)
395d1d35e2fSjeremylt     if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) {
396d1d35e2fSjeremylt       *delegate = ceed->obj_delegates->delegate;
397e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
3987a982d89SJeremy L. Thompson     }
3997a982d89SJeremy L. Thompson 
4007a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
4017a982d89SJeremy L. Thompson   ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr);
402e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4037a982d89SJeremy L. Thompson }
4047a982d89SJeremy L. Thompson 
4057a982d89SJeremy L. Thompson /**
4067a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context for a
4097a982d89SJeremy L. Thompson     given type of Ceed object. All backend implementations default to the
4107a982d89SJeremy L. Thompson     delegate Ceed context for this object. For example,
4117a982d89SJeremy L. Thompson     CeedSetObjectDelegate(ceed, refceed, "Basis")
4127a982d89SJeremy L. Thompson   uses refceed implementations for all CeedBasis backend functions.
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
4157a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
416d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to set delegate for
4177a982d89SJeremy L. Thompson 
4187a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4197a982d89SJeremy L. Thompson 
4207a982d89SJeremy L. Thompson   @ref Backend
4217a982d89SJeremy L. Thompson **/
422d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) {
4237a982d89SJeremy L. Thompson   CeedInt ierr;
424d1d35e2fSjeremylt   CeedInt count = ceed->obj_delegate_count;
4257a982d89SJeremy L. Thompson 
4267a982d89SJeremy L. Thompson   // Malloc or Realloc
4277a982d89SJeremy L. Thompson   if (count) {
428d1d35e2fSjeremylt     ierr = CeedRealloc(count+1, &ceed->obj_delegates); CeedChk(ierr);
4297a982d89SJeremy L. Thompson   } else {
430d1d35e2fSjeremylt     ierr = CeedCalloc(1, &ceed->obj_delegates); CeedChk(ierr);
4317a982d89SJeremy L. Thompson   }
432d1d35e2fSjeremylt   ceed->obj_delegate_count++;
4337a982d89SJeremy L. Thompson 
4347a982d89SJeremy L. Thompson   // Set object delegate
435d1d35e2fSjeremylt   ceed->obj_delegates[count].delegate = delegate;
436d1d35e2fSjeremylt   size_t slen = strlen(obj_name) + 1;
437d1d35e2fSjeremylt   ierr = CeedMalloc(slen, &ceed->obj_delegates[count].obj_name); CeedChk(ierr);
438d1d35e2fSjeremylt   memcpy(ceed->obj_delegates[count].obj_name, obj_name, slen);
4397a982d89SJeremy L. Thompson 
4407a982d89SJeremy L. Thompson   // Set delegate parent
4417a982d89SJeremy L. Thompson   delegate->parent = ceed;
442e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4437a982d89SJeremy L. Thompson }
4447a982d89SJeremy L. Thompson 
4457a982d89SJeremy L. Thompson /**
4467a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson   @param ceed           Ceed context
4497a982d89SJeremy L. Thompson   @param[out] resource  Variable to store fallback resource
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4527a982d89SJeremy L. Thompson 
4537a982d89SJeremy L. Thompson   @ref Backend
4547a982d89SJeremy L. Thompson **/
4557a982d89SJeremy L. Thompson 
4567a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
457d1d35e2fSjeremylt   *resource = (const char *)ceed->op_fallback_resource;
458e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4597a982d89SJeremy L. Thompson }
4607a982d89SJeremy L. Thompson 
4617a982d89SJeremy L. Thompson /**
4627a982d89SJeremy L. Thompson   @brief Set the fallback resource for CeedOperators. The current resource, if
4637a982d89SJeremy L. Thompson            any, is freed by calling this function. This string is freed upon the
4647a982d89SJeremy L. Thompson            destruction of the Ceed context.
4657a982d89SJeremy L. Thompson 
4667a982d89SJeremy L. Thompson   @param[out] ceed Ceed context
4677a982d89SJeremy L. Thompson   @param resource  Fallback resource to set
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4707a982d89SJeremy L. Thompson 
4717a982d89SJeremy L. Thompson   @ref Backend
4727a982d89SJeremy L. Thompson **/
4737a982d89SJeremy L. Thompson 
4747a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
4757a982d89SJeremy L. Thompson   int ierr;
4767a982d89SJeremy L. Thompson 
4777a982d89SJeremy L. Thompson   // Free old
478d1d35e2fSjeremylt   ierr = CeedFree(&ceed->op_fallback_resource); CeedChk(ierr);
4797a982d89SJeremy L. Thompson 
4807a982d89SJeremy L. Thompson   // Set new
4817a982d89SJeremy L. Thompson   size_t len = strlen(resource);
4827a982d89SJeremy L. Thompson   char *tmp;
4837a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
4847a982d89SJeremy L. Thompson   memcpy(tmp, resource, len+1);
485d1d35e2fSjeremylt   ceed->op_fallback_resource = tmp;
486e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4877a982d89SJeremy L. Thompson }
4887a982d89SJeremy L. Thompson 
4897a982d89SJeremy L. Thompson /**
4907a982d89SJeremy L. Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context
4917a982d89SJeremy L. Thompson            for a CeedOperator
4927a982d89SJeremy L. Thompson 
4937a982d89SJeremy L. Thompson   @param ceed         Ceed context
4947a982d89SJeremy L. Thompson   @param[out] parent  Variable to store parent Ceed context
4957a982d89SJeremy L. Thompson 
4967a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4977a982d89SJeremy L. Thompson 
4987a982d89SJeremy L. Thompson   @ref Backend
4997a982d89SJeremy L. Thompson **/
5007a982d89SJeremy L. Thompson 
5017a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
502d1d35e2fSjeremylt   *parent = ceed->op_fallback_parent;
503e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5047a982d89SJeremy L. Thompson }
5057a982d89SJeremy L. Thompson 
5067a982d89SJeremy L. Thompson /**
5079525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
5089525855cSJeremy L Thompson 
5099525855cSJeremy L Thompson   @param ceed                   Ceed to flag as deterministic
51096b902e2Sjeremylt   @param[out] is_deterministic  Deterministic status to set
5119525855cSJeremy L Thompson 
5129525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5139525855cSJeremy L Thompson 
5149525855cSJeremy L Thompson   @ref Backend
5159525855cSJeremy L Thompson **/
5169525855cSJeremy L Thompson 
517d1d35e2fSjeremylt int CeedSetDeterministic(Ceed ceed, bool is_deterministic) {
518d1d35e2fSjeremylt   ceed->is_deterministic = is_deterministic;
519e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5209525855cSJeremy L Thompson }
5219525855cSJeremy L Thompson 
5229525855cSJeremy L Thompson /**
5237a982d89SJeremy L. Thompson   @brief Set a backend function
5247a982d89SJeremy L. Thompson 
5257a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5267a982d89SJeremy L. Thompson   the Ceed objects. For example,
5277a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5287a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5297a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5307a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
5317a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
5327a982d89SJeremy L. Thompson 
5337a982d89SJeremy L. Thompson   @param ceed         Ceed context for error handling
5347a982d89SJeremy L. Thompson   @param type         Type of Ceed object to set function for
5357a982d89SJeremy L. Thompson   @param[out] object  Ceed object to set function for
536d1d35e2fSjeremylt   @param func_name    Name of function to set
5377a982d89SJeremy L. Thompson   @param f            Function to set
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5407a982d89SJeremy L. Thompson 
5417a982d89SJeremy L. Thompson   @ref Backend
5427a982d89SJeremy L. Thompson **/
5437a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
544d1d35e2fSjeremylt                            const char *func_name, int (*f)()) {
545d1d35e2fSjeremylt   char lookup_name[CEED_MAX_RESOURCE_LEN+1] = "";
5467a982d89SJeremy L. Thompson 
5477a982d89SJeremy L. Thompson   // Build lookup name
5487a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
549d1d35e2fSjeremylt     strncat (lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN);
550d1d35e2fSjeremylt   strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN);
551d1d35e2fSjeremylt   strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN);
5527a982d89SJeremy L. Thompson 
5537a982d89SJeremy L. Thompson   // Find and use offset
554d1d35e2fSjeremylt   for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++)
555d1d35e2fSjeremylt     if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) {
556d1d35e2fSjeremylt       size_t offset = ceed->f_offsets[i].offset;
5577a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
5587a982d89SJeremy L. Thompson       *fpointer = f;
559e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
5607a982d89SJeremy L. Thompson     }
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson   // LCOV_EXCL_START
563e15f9bd0SJeremy L Thompson   return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
564e15f9bd0SJeremy L Thompson                    "Requested function '%s' was not found for CEED "
565d1d35e2fSjeremylt                    "object '%s'", func_name, type);
5667a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
5677a982d89SJeremy L. Thompson }
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson /**
5707a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
5717a982d89SJeremy L. Thompson 
5727a982d89SJeremy L. Thompson   @param ceed       Ceed context to retrieve data of
5737a982d89SJeremy L. Thompson   @param[out] data  Address to save data to
5747a982d89SJeremy L. Thompson 
5757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson   @ref Backend
5787a982d89SJeremy L. Thompson **/
579777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
580777ff853SJeremy L Thompson   *(void **)data = ceed->data;
581e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5827a982d89SJeremy L. Thompson }
5837a982d89SJeremy L. Thompson 
5847a982d89SJeremy L. Thompson /**
5857a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
5867a982d89SJeremy L. Thompson 
5877a982d89SJeremy L. Thompson   @param ceed  Ceed context to set data of
5887a982d89SJeremy L. Thompson   @param data  Address of data to set
5897a982d89SJeremy L. Thompson 
5907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5917a982d89SJeremy L. Thompson 
5927a982d89SJeremy L. Thompson   @ref Backend
5937a982d89SJeremy L. Thompson **/
594777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
595777ff853SJeremy L Thompson   ceed->data = data;
596e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5977a982d89SJeremy L. Thompson }
5987a982d89SJeremy L. Thompson 
59934359f16Sjeremylt /**
60034359f16Sjeremylt   @brief Increment the reference counter for a Ceed context
60134359f16Sjeremylt 
60234359f16Sjeremylt   @param ceed  Ceed context to increment the reference counter
60334359f16Sjeremylt 
60434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
60534359f16Sjeremylt 
60634359f16Sjeremylt   @ref Backend
60734359f16Sjeremylt **/
6089560d06aSjeremylt int CeedReference(Ceed ceed) {
60934359f16Sjeremylt   ceed->ref_count++;
61034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
61134359f16Sjeremylt }
61234359f16Sjeremylt 
6137a982d89SJeremy L. Thompson /// @}
6147a982d89SJeremy L. Thompson 
6157a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6167a982d89SJeremy L. Thompson /// Ceed Public API
6177a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6187a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6197a982d89SJeremy L. Thompson /// @{
6207a982d89SJeremy L. Thompson 
6217a982d89SJeremy L. Thompson /**
62292ee7d1cSjeremylt   @brief Get the list of available resource names for Ceed contexts
6239ff86846Sjeremylt   Note: The caller is responsible for `free()`ing the resources and priorities arrays,
6249ff86846Sjeremylt           but should not `free()` the contents of the resources array.
62522e44211Sjeremylt 
62692ee7d1cSjeremylt   @param[out] n           Number of available resources
62792ee7d1cSjeremylt   @param[out] resources   List of available resource names
62822e44211Sjeremylt   @param[out] priorities  Resource name prioritization values, lower is better
62922e44211Sjeremylt 
63022e44211Sjeremylt   @return An error code: 0 - success, otherwise - failure
63122e44211Sjeremylt 
63222e44211Sjeremylt   @ref User
63322e44211Sjeremylt **/
63422e44211Sjeremylt // LCOV_EXCL_START
63522e44211Sjeremylt int CeedRegistryGetList(size_t *n, char ***const resources,
63622e44211Sjeremylt                         CeedInt **priorities) {
637d0c91ce9Sjeremylt   *n = 0;
6389ff86846Sjeremylt   *resources = malloc(num_backends * sizeof(**resources));
6399ff86846Sjeremylt   if (!resources)
6409ff86846Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6419ff86846Sjeremylt   if (priorities) {
6429ff86846Sjeremylt     *priorities = malloc(num_backends * sizeof(**priorities));
6439ff86846Sjeremylt     if (!priorities)
6449ff86846Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6459ff86846Sjeremylt   }
64622e44211Sjeremylt   for (size_t i=0; i<num_backends; i++) {
647d0c91ce9Sjeremylt     // Only report compiled backends
648d0c91ce9Sjeremylt     if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) {
64922e44211Sjeremylt       *resources[i] = backends[i].prefix;
6509ff86846Sjeremylt       if (priorities) *priorities[i] = backends[i].priority;
651d0c91ce9Sjeremylt       *n += 1;
652d0c91ce9Sjeremylt     }
653d0c91ce9Sjeremylt   }
65478464608Sjeremylt   if (*n == 0)
65578464608Sjeremylt     // LCOV_EXCL_START
65678464608Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "No backends installed");
65778464608Sjeremylt   // LCOV_EXCL_STOP
658d0c91ce9Sjeremylt   *resources = realloc(*resources, *n * sizeof(**resources));
659d0c91ce9Sjeremylt   if (!resources)
660d0c91ce9Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
661d0c91ce9Sjeremylt   if (priorities) {
662d0c91ce9Sjeremylt     *priorities = realloc(*priorities, *n * sizeof(**priorities));
663d0c91ce9Sjeremylt     if (!priorities)
664d0c91ce9Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
66522e44211Sjeremylt   }
66622e44211Sjeremylt   return CEED_ERROR_SUCCESS;
66745f1e315Sjeremylt }
66822e44211Sjeremylt // LCOV_EXCL_STOP
66922e44211Sjeremylt 
67022e44211Sjeremylt /**
671d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
67222e44211Sjeremylt   Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self")
67322e44211Sjeremylt     will result in CeedInt printing the current libCEED version number
67492ee7d1cSjeremylt     and a list of current available backend resources to stderr.
675b11c1e72Sjeremylt 
676b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
677b11c1e72Sjeremylt   @param ceed      The library context
678b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
679b11c1e72Sjeremylt 
680b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
681dfdf5a53Sjeremylt 
6827a982d89SJeremy L. Thompson   @ref User
683b11c1e72Sjeremylt **/
684d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
685d7b241e6Sjeremylt   int ierr;
686d0c91ce9Sjeremylt   size_t match_len = 0, match_idx = UINT_MAX,
687d0c91ce9Sjeremylt          match_priority = CEED_MAX_BACKEND_PRIORITY, priority;
688d7b241e6Sjeremylt 
689fe2413ffSjeremylt   // Find matching backend
6901d102b48SJeremy L Thompson   if (!resource)
69113873f79Sjeremylt     // LCOV_EXCL_START
692e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided");
69313873f79Sjeremylt   // LCOV_EXCL_STOP
6941d013790SJed Brown   ierr = CeedRegisterAll(); CeedChk(ierr);
69513873f79Sjeremylt 
69622e44211Sjeremylt   // Check for help request
69722e44211Sjeremylt   const char *help_prefix = "help";
69822e44211Sjeremylt   size_t match_help;
69922e44211Sjeremylt   for (match_help=0; match_help<4
70022e44211Sjeremylt        && resource[match_help] == help_prefix[match_help]; match_help++) {}
70122e44211Sjeremylt   if (match_help == 4) {
70222e44211Sjeremylt     fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR,
70322e44211Sjeremylt             CEED_VERSION_MINOR, CEED_VERSION_PATCH,
70422e44211Sjeremylt             CEED_VERSION_RELEASE ? "" : "+development");
70592ee7d1cSjeremylt     fprintf(stderr, "Available backend resources:\n");
70622e44211Sjeremylt     for (size_t i=0; i<num_backends; i++) {
707d0c91ce9Sjeremylt       // Only report compiled backends
708d0c91ce9Sjeremylt       if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY)
70922e44211Sjeremylt         fprintf(stderr, "  %s\n", backends[i].prefix);
71022e44211Sjeremylt     }
71122e44211Sjeremylt     fflush(stderr);
71222e44211Sjeremylt     match_help = 5; // Delineating character expected
71322e44211Sjeremylt   } else {
71422e44211Sjeremylt     match_help = 0;
71522e44211Sjeremylt   }
71622e44211Sjeremylt 
7179c9a0587SLeila Ghaffari   // Find best match, computed as number of matching characters
7189c9a0587SLeila Ghaffari   //   from requested resource stem
7192bbc7fe8Sjeremylt   size_t stem_length;
72022e44211Sjeremylt   for (stem_length=0; resource[stem_length+match_help]
72122e44211Sjeremylt        && resource[stem_length+match_help] != ':'; stem_length++) {}
722d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
723d7b241e6Sjeremylt     size_t n;
724d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
72522e44211Sjeremylt     for (n=0; prefix[n] && prefix[n] == resource[n+match_help]; n++) {}
726d7b241e6Sjeremylt     priority = backends[i].priority;
727d1d35e2fSjeremylt     if (n > match_len || (n == match_len && match_priority > priority)) {
728d1d35e2fSjeremylt       match_len = n;
729d1d35e2fSjeremylt       match_priority = priority;
730d1d35e2fSjeremylt       match_idx = i;
731d7b241e6Sjeremylt     }
732d7b241e6Sjeremylt   }
7339c9a0587SLeila Ghaffari   // Using Levenshtein distance to find closest match
7349c9a0587SLeila Ghaffari   if (match_len <= 1 || match_len != stem_length) {
735203015caSLeila Ghaffari     // LCOV_EXCL_START
7369c9a0587SLeila Ghaffari     size_t lev_dis = UINT_MAX;
7379c9a0587SLeila Ghaffari     size_t lev_idx = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY;
7389c9a0587SLeila Ghaffari     for (size_t i=0; i<num_backends; i++) {
7399c9a0587SLeila Ghaffari       const char *prefix = backends[i].prefix;
7409c9a0587SLeila Ghaffari       size_t prefix_length = strlen(backends[i].prefix);
7419c9a0587SLeila Ghaffari       size_t min_len = (prefix_length < stem_length) ? prefix_length : stem_length;
742092904ddSLeila Ghaffari       size_t column[min_len+1];
743092904ddSLeila Ghaffari       for (size_t j=0; j<=min_len; j++) column[j] = j;
7449c9a0587SLeila Ghaffari       for (size_t j=1; j<=min_len; j++) {
7459c9a0587SLeila Ghaffari         column[0] = j;
7469c9a0587SLeila Ghaffari         for (size_t k=1, last_diag=j-1; k<=min_len; k++) {
747092904ddSLeila Ghaffari           size_t old_diag = column[k];
7489c9a0587SLeila Ghaffari           size_t min_1 = (column[k] < column[k-1]) ? column[k]+1 : column[k-1]+1;
7499c9a0587SLeila Ghaffari           size_t min_2 = last_diag + (resource[k-1] == prefix[j-1] ? 0 : 1);
7509c9a0587SLeila Ghaffari           column[k] = (min_1 < min_2) ? min_1 : min_2;
7519c9a0587SLeila Ghaffari           last_diag = old_diag;
7529c9a0587SLeila Ghaffari         }
7539c9a0587SLeila Ghaffari       }
7549c9a0587SLeila Ghaffari       size_t n = column[min_len];
7559c9a0587SLeila Ghaffari       priority = backends[i].priority;
7569c9a0587SLeila Ghaffari       if (n < lev_dis || (n == lev_dis
7579c9a0587SLeila Ghaffari                           && lev_priority > priority)) {
7589c9a0587SLeila Ghaffari         lev_dis = n;
7599c9a0587SLeila Ghaffari         lev_priority = priority;
7609c9a0587SLeila Ghaffari         lev_idx = i;
7619c9a0587SLeila Ghaffari       }
7629c9a0587SLeila Ghaffari     }
7639c9a0587SLeila Ghaffari     const char *prefix_lev = backends[lev_idx].prefix;
7649c9a0587SLeila Ghaffari     size_t lev_length;
7659c9a0587SLeila Ghaffari     for (lev_length=0; prefix_lev[lev_length]
7669c9a0587SLeila Ghaffari          && prefix_lev[lev_length] != '\0'; lev_length++) {}
7679c9a0587SLeila Ghaffari     size_t m = (lev_length < stem_length) ? lev_length : stem_length;
7689c9a0587SLeila Ghaffari     if (lev_dis+1 >= m) {
769e15f9bd0SJeremy L Thompson       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s",
770e15f9bd0SJeremy L Thompson                        resource);
7719c9a0587SLeila Ghaffari     } else {
77287250337Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s\n"
7739c9a0587SLeila Ghaffari                        "Closest match: %s", resource, backends[lev_idx].prefix);
7742bbc7fe8Sjeremylt     }
775203015caSLeila Ghaffari     // LCOV_EXCL_STOP
7769c9a0587SLeila Ghaffari   }
777fe2413ffSjeremylt 
778fe2413ffSjeremylt   // Setup Ceed
779d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
780bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
7811d102b48SJeremy L Thompson   if (!ceed_error_handler)
7821d102b48SJeremy L Thompson     ceed_error_handler = "abort";
783bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
78456e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
785477729cfSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store"))
786477729cfSJeremy L Thompson     (*ceed)->Error = CeedErrorStore;
78756e866f4SJed Brown   else
788d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
789d1d35e2fSjeremylt   memcpy((*ceed)->err_msg, "No error message stored", 24);
790d1d35e2fSjeremylt   (*ceed)->ref_count = 1;
791d7b241e6Sjeremylt   (*ceed)->data = NULL;
792fe2413ffSjeremylt 
793fe2413ffSjeremylt   // Set lookup table
794d1d35e2fSjeremylt   FOffset f_offsets[] = {
7956e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
7966e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
7976e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
798f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
7996e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
8006e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
8016e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
8026e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
8036e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
8046e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
805777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
8066e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
8076e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
8086e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
8096a6c615bSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, TakeArray),
8106e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
8116e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
8126e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
8136e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
8146e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
815547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
816e0dd3b27Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Scale),
8170f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, AXPY),
8180f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, PointwiseMult),
819d99fa3c5SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
8206e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Destroy),
8216e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
8226e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
823bd33150aSjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
8246e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
8256e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Apply),
8266e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Destroy),
8276e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
8286e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
8296e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Apply),
8308c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
8318c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
8326e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
833777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
834891038deSjeremylt     CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData),
835777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
836777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
837777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
83880ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
83970a7ffb3SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate),
84080ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
8419e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
84280ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
8439e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
844e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic),
845e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble),
846713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
8476e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
848250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
849cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
850250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
8516e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
8526e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
8536e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
8541dfeef1dSjeremylt   };
855fe2413ffSjeremylt 
856d1d35e2fSjeremylt   ierr = CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets); CeedChk(ierr);
857d1d35e2fSjeremylt   memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets));
858fe2413ffSjeremylt 
8595107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
860e2f04181SAndrew T. Barker   const char fallbackresource[] = "";
8615107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
8625107b09fSJeremy L Thompson   CeedChk(ierr);
8635107b09fSJeremy L Thompson 
86460f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
865*3f21f6b1SJeremy L Thompson   (*ceed)->is_debug = !!getenv("CEED_DEBUG") || !!getenv("DEBUG") ||
866*3f21f6b1SJeremy L Thompson                       !!getenv("DBG");
86760f9e2d6SJeremy L Thompson 
868fe2413ffSjeremylt   // Backend specific setup
869d1d35e2fSjeremylt   ierr = backends[match_idx].init(&resource[match_help], *ceed); CeedChk(ierr);
870fe2413ffSjeremylt 
87122e44211Sjeremylt   // Copy resource prefix, if backend setup successful
872d1d35e2fSjeremylt   size_t len = strlen(backends[match_idx].prefix);
873e07206deSjeremylt   char *tmp;
874e07206deSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
875d1d35e2fSjeremylt   memcpy(tmp, backends[match_idx].prefix, len+1);
876e07206deSjeremylt   (*ceed)->resource = tmp;
877e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
878d7b241e6Sjeremylt }
879d7b241e6Sjeremylt 
880d7b241e6Sjeremylt /**
8819560d06aSjeremylt   @brief Copy the pointer to a Ceed context. Both pointers should
8829560d06aSjeremylt            be destroyed with `CeedDestroy()`;
8839560d06aSjeremylt            Note: If `*ceed_copy` is non-NULL, then it is assumed that
8849560d06aSjeremylt            `*ceed_copy` is a pointer to a Ceed context. This Ceed
8859560d06aSjeremylt            context will be destroyed if `*ceed_copy` is the only
8869560d06aSjeremylt            reference to this Ceed context.
8879560d06aSjeremylt 
8889560d06aSjeremylt   @param ceed            Ceed context to copy reference to
8899560d06aSjeremylt   @param[out] ceed_copy  Variable to store copied reference
8909560d06aSjeremylt 
8919560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8929560d06aSjeremylt 
8939560d06aSjeremylt   @ref User
8949560d06aSjeremylt **/
8959560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) {
8969560d06aSjeremylt   int ierr;
8979560d06aSjeremylt 
8989560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
8999560d06aSjeremylt   ierr = CeedDestroy(ceed_copy); CeedChk(ierr);
9009560d06aSjeremylt   *ceed_copy = ceed;
9019560d06aSjeremylt   return CEED_ERROR_SUCCESS;
9029560d06aSjeremylt }
9039560d06aSjeremylt 
9049560d06aSjeremylt /**
9057a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
9062f86a920SJeremy L Thompson 
9077a982d89SJeremy L. Thompson   @param ceed           Ceed context to get resource name of
9087a982d89SJeremy L. Thompson   @param[out] resource  Variable to store resource name
9092f86a920SJeremy L Thompson 
9102f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9112f86a920SJeremy L Thompson 
9127a982d89SJeremy L. Thompson   @ref User
9135107b09fSJeremy L Thompson **/
9147a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
9157a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
916e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9175107b09fSJeremy L Thompson }
9185107b09fSJeremy L Thompson 
9195107b09fSJeremy L Thompson /**
920d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
921c907536fSjeremylt 
922d79b80ecSjeremylt   @param ceed           Ceed context to get preferred memory type of
923d1d35e2fSjeremylt   @param[out] mem_type  Address to save preferred memory type to
924c907536fSjeremylt 
925c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
926c907536fSjeremylt 
9277a982d89SJeremy L. Thompson   @ref User
928c907536fSjeremylt **/
929d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) {
930c907536fSjeremylt   int ierr;
931c263cd57Sjeremylt 
932c907536fSjeremylt   if (ceed->GetPreferredMemType) {
933d1d35e2fSjeremylt     ierr = ceed->GetPreferredMemType(mem_type); CeedChk(ierr);
934c907536fSjeremylt   } else {
935c263cd57Sjeremylt     Ceed delegate;
936c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
937c263cd57Sjeremylt 
938c263cd57Sjeremylt     if (delegate) {
939d1d35e2fSjeremylt       ierr = CeedGetPreferredMemType(delegate, mem_type); CeedChk(ierr);
940c263cd57Sjeremylt     } else {
941d1d35e2fSjeremylt       *mem_type = CEED_MEM_HOST;
942c907536fSjeremylt     }
943c263cd57Sjeremylt   }
944e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
945c907536fSjeremylt }
946c907536fSjeremylt 
947c907536fSjeremylt /**
9489525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
9499525855cSJeremy L Thompson 
9509525855cSJeremy L Thompson   @param[in] ceed               Ceed
951d1d35e2fSjeremylt   @param[out] is_deterministic  Variable to store deterministic status
9529525855cSJeremy L Thompson 
9539525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9549525855cSJeremy L Thompson 
9559525855cSJeremy L Thompson   @ref User
9569525855cSJeremy L Thompson **/
957d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) {
958d1d35e2fSjeremylt   *is_deterministic = ceed->is_deterministic;
959e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9609525855cSJeremy L Thompson }
9619525855cSJeremy L Thompson 
9629525855cSJeremy L Thompson /**
9630a0da059Sjeremylt   @brief View a Ceed
9640a0da059Sjeremylt 
9650a0da059Sjeremylt   @param[in] ceed    Ceed to view
9660a0da059Sjeremylt   @param[in] stream  Filestream to write to
9670a0da059Sjeremylt 
9680a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
9690a0da059Sjeremylt 
9700a0da059Sjeremylt   @ref User
9710a0da059Sjeremylt **/
9720a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
9730a0da059Sjeremylt   int ierr;
974d1d35e2fSjeremylt   CeedMemType mem_type;
9750a0da059Sjeremylt 
976d1d35e2fSjeremylt   ierr = CeedGetPreferredMemType(ceed, &mem_type); CeedChk(ierr);
9770a0da059Sjeremylt 
9780a0da059Sjeremylt   fprintf(stream, "Ceed\n"
9790a0da059Sjeremylt           "  Ceed Resource: %s\n"
9800a0da059Sjeremylt           "  Preferred MemType: %s\n",
981d1d35e2fSjeremylt           ceed->resource, CeedMemTypes[mem_type]);
982e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9830a0da059Sjeremylt }
9840a0da059Sjeremylt 
9850a0da059Sjeremylt /**
986b11c1e72Sjeremylt   @brief Destroy a Ceed context
987d7b241e6Sjeremylt 
988d7b241e6Sjeremylt   @param ceed  Address of Ceed context to destroy
989b11c1e72Sjeremylt 
990b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
991dfdf5a53Sjeremylt 
9927a982d89SJeremy L. Thompson   @ref User
993b11c1e72Sjeremylt **/
994d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
995d7b241e6Sjeremylt   int ierr;
996d1d35e2fSjeremylt   if (!*ceed || --(*ceed)->ref_count > 0) return CEED_ERROR_SUCCESS;
9975fe0d4faSjeremylt   if ((*ceed)->delegate) {
9985fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
9995fe0d4faSjeremylt   }
10000ace9bf2Sjeremylt 
1001d1d35e2fSjeremylt   if ((*ceed)->obj_delegate_count > 0) {
1002d1d35e2fSjeremylt     for (int i=0; i<(*ceed)->obj_delegate_count; i++) {
1003d1d35e2fSjeremylt       ierr = CeedDestroy(&((*ceed)->obj_delegates[i].delegate)); CeedChk(ierr);
1004d1d35e2fSjeremylt       ierr = CeedFree(&(*ceed)->obj_delegates[i].obj_name); CeedChk(ierr);
1005aefd8378Sjeremylt     }
1006d1d35e2fSjeremylt     ierr = CeedFree(&(*ceed)->obj_delegates); CeedChk(ierr);
1007aefd8378Sjeremylt   }
10080ace9bf2Sjeremylt 
1009d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
1010d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
1011d7b241e6Sjeremylt   }
10120ace9bf2Sjeremylt 
1013d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->f_offsets); CeedChk(ierr);
1014e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
1015d1d35e2fSjeremylt   ierr = CeedDestroy(&(*ceed)->op_fallback_ceed); CeedChk(ierr);
1016d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->op_fallback_resource); CeedChk(ierr);
1017d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
1018e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1019d7b241e6Sjeremylt }
1020d7b241e6Sjeremylt 
1021f9982c62SWill Pazner // LCOV_EXCL_START
1022f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
10233f4a9821SAndrew T. Barker   if (ceed->parent)
10243f4a9821SAndrew T. Barker     return CeedErrorFormat(ceed->parent, format, args);
1025d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1026d1d35e2fSjeremylt     return CeedErrorFormat(ceed->op_fallback_parent, format, args);
102778464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
102878464608Sjeremylt   vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args); // NOLINT
1029d1d35e2fSjeremylt   return ceed->err_msg;
1030f9982c62SWill Pazner }
1031f9982c62SWill Pazner // LCOV_EXCL_STOP
1032f9982c62SWill Pazner 
10337a982d89SJeremy L. Thompson /**
10347a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
10357a982d89SJeremy L. Thompson 
10367a982d89SJeremy L. Thompson   @ref Developer
10377a982d89SJeremy L. Thompson **/
10387a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
10397a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
10407a982d89SJeremy L. Thompson   va_list args;
1041d1d35e2fSjeremylt   int ret_val;
10427a982d89SJeremy L. Thompson   va_start(args, format);
10437a982d89SJeremy L. Thompson   if (ceed) {
1044d1d35e2fSjeremylt     ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
10457a982d89SJeremy L. Thompson   } else {
1046b0d62198Sjeremylt     // LCOV_EXCL_START
1047477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
1048477729cfSJeremy L Thompson     if (!ceed_error_handler)
1049477729cfSJeremy L Thompson       ceed_error_handler = "abort";
1050477729cfSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return"))
1051d1d35e2fSjeremylt       ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
1052477729cfSJeremy L Thompson     else
1053477729cfSJeremy L Thompson       // This function will not return
1054d1d35e2fSjeremylt       ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
10557a982d89SJeremy L. Thompson   }
10567a982d89SJeremy L. Thompson   va_end(args);
1057d1d35e2fSjeremylt   return ret_val;
1058b0d62198Sjeremylt   // LCOV_EXCL_STOP
10597a982d89SJeremy L. Thompson }
10607a982d89SJeremy L. Thompson 
1061477729cfSJeremy L Thompson /**
1062477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
1063477729cfSJeremy L Thompson 
1064477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1065477729cfSJeremy L Thompson 
1066477729cfSJeremy L Thompson   @ref Developer
1067477729cfSJeremy L Thompson **/
1068477729cfSJeremy L Thompson // LCOV_EXCL_START
1069d1d35e2fSjeremylt int CeedErrorReturn(Ceed ceed, const char *filename, int line_no,
1070d1d35e2fSjeremylt                     const char *func, int err_code, const char *format,
1071f9982c62SWill Pazner                     va_list *args) {
1072d1d35e2fSjeremylt   return err_code;
1073477729cfSJeremy L Thompson }
1074477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1075477729cfSJeremy L Thompson 
1076477729cfSJeremy L Thompson /**
1077477729cfSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns
1078477729cfSJeremy L Thompson            the error.
1079477729cfSJeremy L Thompson 
1080477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1081477729cfSJeremy L Thompson 
1082477729cfSJeremy L Thompson   @ref Developer
1083477729cfSJeremy L Thompson **/
1084477729cfSJeremy L Thompson // LCOV_EXCL_START
1085d1d35e2fSjeremylt int CeedErrorStore(Ceed ceed, const char *filename, int line_no,
1086d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1087f9982c62SWill Pazner                    va_list *args) {
1088477729cfSJeremy L Thompson   if (ceed->parent)
1089d1d35e2fSjeremylt     return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format,
1090187168c7SJeremy L Thompson                           args);
1091d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1092d1d35e2fSjeremylt     return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func,
1093d1d35e2fSjeremylt                           err_code, format, args);
1094477729cfSJeremy L Thompson 
1095477729cfSJeremy L Thompson   // Build message
1096477729cfSJeremy L Thompson   CeedInt len;
1097d1d35e2fSjeremylt   len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ",
1098d1d35e2fSjeremylt                  filename, line_no, func);
109978464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
110078464608Sjeremylt   // *INDENT-OFF*
110178464608Sjeremylt   vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args); // NOLINT
110278464608Sjeremylt   // *INDENT-ON*
1103d1d35e2fSjeremylt   return err_code;
1104477729cfSJeremy L Thompson }
1105477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1106477729cfSJeremy L Thompson 
1107477729cfSJeremy L Thompson /**
1108477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
1109477729cfSJeremy L Thompson 
1110477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1111477729cfSJeremy L Thompson 
1112477729cfSJeremy L Thompson   @ref Developer
1113477729cfSJeremy L Thompson **/
1114477729cfSJeremy L Thompson // LCOV_EXCL_START
1115d1d35e2fSjeremylt int CeedErrorAbort(Ceed ceed, const char *filename, int line_no,
1116d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1117f9982c62SWill Pazner                    va_list *args) {
1118d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
1119f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1120477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1121477729cfSJeremy L Thompson   abort();
1122d1d35e2fSjeremylt   return err_code;
1123477729cfSJeremy L Thompson }
1124477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1125477729cfSJeremy L Thompson 
1126477729cfSJeremy L Thompson /**
1127477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
1128477729cfSJeremy L Thompson 
1129477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1130477729cfSJeremy L Thompson 
1131477729cfSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit()
1132477729cfSJeremy L Thompson   handlers (e.g., as used by gcov) are run.
1133477729cfSJeremy L Thompson 
1134477729cfSJeremy L Thompson   @ref Developer
1135477729cfSJeremy L Thompson **/
1136d1d35e2fSjeremylt int CeedErrorExit(Ceed ceed, const char *filename, int line_no,
1137d1d35e2fSjeremylt                   const char *func, int err_code, const char *format, va_list *args) {
1138d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
113978464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
114078464608Sjeremylt   vfprintf(stderr, format, *args); // NOLINT
1141477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1142d1d35e2fSjeremylt   exit(err_code);
1143d1d35e2fSjeremylt   return err_code;
1144477729cfSJeremy L Thompson }
1145477729cfSJeremy L Thompson 
1146477729cfSJeremy L Thompson /**
1147477729cfSJeremy L Thompson   @brief Set error handler
1148477729cfSJeremy L Thompson 
1149477729cfSJeremy L Thompson   A default error handler is set in CeedInit().  Use this function to change
1150477729cfSJeremy L Thompson   the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined
1151477729cfSJeremy L Thompson   error handler.
1152477729cfSJeremy L Thompson 
1153477729cfSJeremy L Thompson   @ref Developer
1154477729cfSJeremy L Thompson **/
1155d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) {
1156d1d35e2fSjeremylt   ceed->Error = handler;
1157d1d35e2fSjeremylt   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler);
1158d1d35e2fSjeremylt   for (int i=0; i<ceed->obj_delegate_count; i++)
1159d1d35e2fSjeremylt     CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler);
1160e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1161477729cfSJeremy L Thompson }
1162477729cfSJeremy L Thompson 
1163477729cfSJeremy L Thompson /**
1164477729cfSJeremy L Thompson   @brief Get error message
1165477729cfSJeremy L Thompson 
1166477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1167477729cfSJeremy L Thompson     CeedErrorStore()
1168477729cfSJeremy L Thompson 
1169477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to retrieve error message
1170d1d35e2fSjeremylt   @param[out] err_msg  Char pointer to hold error message
1171477729cfSJeremy L Thompson 
1172477729cfSJeremy L Thompson   @ref Developer
1173477729cfSJeremy L Thompson **/
1174d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) {
11753f4a9821SAndrew T. Barker   if (ceed->parent)
1176d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->parent, err_msg);
1177d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1178d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg);
1179d1d35e2fSjeremylt   *err_msg = ceed->err_msg;
1180e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1181477729cfSJeremy L Thompson }
1182477729cfSJeremy L Thompson 
1183477729cfSJeremy L Thompson /**
1184477729cfSJeremy L Thompson   @brief Restore error message
1185477729cfSJeremy L Thompson 
1186477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1187477729cfSJeremy L Thompson     CeedErrorStore()
1188477729cfSJeremy L Thompson 
1189477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to restore error message
1190d1d35e2fSjeremylt   @param[out] err_msg  Char pointer that holds error message
1191477729cfSJeremy L Thompson 
1192477729cfSJeremy L Thompson   @ref Developer
1193477729cfSJeremy L Thompson **/
1194d1d35e2fSjeremylt int CeedResetErrorMessage(Ceed ceed, const char **err_msg) {
11953f4a9821SAndrew T. Barker   if (ceed->parent)
1196d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->parent, err_msg);
1197d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1198d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg);
1199d1d35e2fSjeremylt   *err_msg = NULL;
1200d1d35e2fSjeremylt   memcpy(ceed->err_msg, "No error message stored", 24);
1201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1202477729cfSJeremy L Thompson }
1203477729cfSJeremy L Thompson 
12041070991dSJed Brown /**
12051070991dSJed Brown   @brief Get libCEED library version info
12061070991dSJed Brown 
12071070991dSJed Brown   libCEED version numbers have the form major.minor.patch. Non-release versions
12081070991dSJed Brown   may contain unstable interfaces.
12091070991dSJed Brown 
12101070991dSJed Brown   @param[out] major    Major version of the library
12111070991dSJed Brown   @param[out] minor    Minor version of the library
12121070991dSJed Brown   @param[out] patch    Patch (subminor) version of the library
12131070991dSJed Brown   @param[out] release  True for releases; false for development branches.
12141070991dSJed Brown 
12151070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
12161070991dSJed Brown 
12171070991dSJed Brown   @sa CEED_VERSION_GE()
12181070991dSJed Brown 
12191070991dSJed Brown   @ref Developer
12201070991dSJed Brown */
12211070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
12221070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
12231070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
12241070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
12251070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
12261070991dSJed Brown   return 0;
12271070991dSJed Brown }
12281070991dSJed Brown 
122980a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) {
123080a9ef05SNatalie Beams   *scalar_type = CEED_SCALAR_TYPE;
123180a9ef05SNatalie Beams   return 0;
123280a9ef05SNatalie Beams }
123380a9ef05SNatalie Beams 
123480a9ef05SNatalie Beams 
1235d7b241e6Sjeremylt /// @}
1236