xref: /libCEED/interface/ceed.c (revision 22e44211502a6e3fc3d20507741b90abef954fb3)
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
85d7b241e6Sjeremylt     CeedWait(&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
30460f9e2d6SJeremy L Thompson   @param isDebug  Variable to store debugging flag
30560f9e2d6SJeremy L Thompson 
30660f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
30760f9e2d6SJeremy L Thompson 
30860f9e2d6SJeremy L Thompson   @ref Bcakend
30960f9e2d6SJeremy L Thompson **/
31060f9e2d6SJeremy L Thompson int CeedIsDebug(Ceed ceed, bool *isDebug) {
31160f9e2d6SJeremy L Thompson   *isDebug = 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
3757a982d89SJeremy L. Thompson   @param[in] objname    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 **/
3817a982d89SJeremy L. Thompson int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *objname) {
3827a982d89SJeremy L. Thompson   CeedInt ierr;
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson   // Check for object delegate
3857a982d89SJeremy L. Thompson   for (CeedInt i=0; i<ceed->objdelegatecount; i++)
3867a982d89SJeremy L. Thompson     if (!strcmp(objname, ceed->objdelegates->objname)) {
3877a982d89SJeremy L. Thompson       *delegate = ceed->objdelegates->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
4077a982d89SJeremy L. Thompson   @param[in] objname   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 **/
4137a982d89SJeremy L. Thompson int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *objname) {
4147a982d89SJeremy L. Thompson   CeedInt ierr;
4157a982d89SJeremy L. Thompson   CeedInt count = ceed->objdelegatecount;
4167a982d89SJeremy L. Thompson 
4177a982d89SJeremy L. Thompson   // Malloc or Realloc
4187a982d89SJeremy L. Thompson   if (count) {
4197a982d89SJeremy L. Thompson     ierr = CeedRealloc(count+1, &ceed->objdelegates); CeedChk(ierr);
4207a982d89SJeremy L. Thompson   } else {
4217a982d89SJeremy L. Thompson     ierr = CeedCalloc(1, &ceed->objdelegates); CeedChk(ierr);
4227a982d89SJeremy L. Thompson   }
4237a982d89SJeremy L. Thompson   ceed->objdelegatecount++;
4247a982d89SJeremy L. Thompson 
4257a982d89SJeremy L. Thompson   // Set object delegate
4267a982d89SJeremy L. Thompson   ceed->objdelegates[count].delegate = delegate;
4277a982d89SJeremy L. Thompson   size_t slen = strlen(objname) + 1;
4287a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &ceed->objdelegates[count].objname); CeedChk(ierr);
4297a982d89SJeremy L. Thompson   memcpy(ceed->objdelegates[count].objname, objname, 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) {
4487a982d89SJeremy L. Thompson   *resource = (const char *)ceed->opfallbackresource;
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
4697a982d89SJeremy L. Thompson   ierr = CeedFree(&ceed->opfallbackresource); 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);
4767a982d89SJeremy L. Thompson   ceed->opfallbackresource = 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) {
4937a982d89SJeremy L. Thompson   *parent = ceed->opfallbackparent;
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
5019525855cSJeremy L Thompson 
5029525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5039525855cSJeremy L Thompson 
5049525855cSJeremy L Thompson   @ref Backend
5059525855cSJeremy L Thompson **/
5069525855cSJeremy L Thompson 
5079525855cSJeremy L Thompson int CeedSetDeterministic(Ceed ceed, bool isDeterministic) {
5089525855cSJeremy L Thompson   ceed->isDeterministic = isDeterministic;
509e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5109525855cSJeremy L Thompson }
5119525855cSJeremy L Thompson 
5129525855cSJeremy L Thompson /**
5137a982d89SJeremy L. Thompson   @brief Set a backend function
5147a982d89SJeremy L. Thompson 
5157a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5167a982d89SJeremy L. Thompson   the Ceed objects. For example,
5177a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5187a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5197a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5207a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
5217a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
5227a982d89SJeremy L. Thompson 
5237a982d89SJeremy L. Thompson   @param ceed           Ceed context for error handling
5247a982d89SJeremy L. Thompson   @param type           Type of Ceed object to set function for
5257a982d89SJeremy L. Thompson   @param[out] object    Ceed object to set function for
5267a982d89SJeremy L. Thompson   @param fname          Name of function to set
5277a982d89SJeremy L. Thompson   @param f              Function to set
5287a982d89SJeremy L. Thompson 
5297a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson   @ref Backend
5327a982d89SJeremy L. Thompson **/
5337a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
5347a982d89SJeremy L. Thompson                            const char *fname, int (*f)()) {
5357a982d89SJeremy L. Thompson   char lookupname[CEED_MAX_RESOURCE_LEN+1] = "";
5367a982d89SJeremy L. Thompson 
5377a982d89SJeremy L. Thompson   // Build lookup name
5387a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
5397a982d89SJeremy L. Thompson     strncat (lookupname, "Ceed", CEED_MAX_RESOURCE_LEN);
5407a982d89SJeremy L. Thompson   strncat(lookupname, type, CEED_MAX_RESOURCE_LEN);
5417a982d89SJeremy L. Thompson   strncat(lookupname, fname, CEED_MAX_RESOURCE_LEN);
5427a982d89SJeremy L. Thompson 
5437a982d89SJeremy L. Thompson   // Find and use offset
5447a982d89SJeremy L. Thompson   for (CeedInt i = 0; ceed->foffsets[i].fname; i++)
5457a982d89SJeremy L. Thompson     if (!strcmp(ceed->foffsets[i].fname, lookupname)) {
5467a982d89SJeremy L. Thompson       size_t offset = ceed->foffsets[i].offset;
5477a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
5487a982d89SJeremy L. Thompson       *fpointer = f;
549e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
5507a982d89SJeremy L. Thompson     }
5517a982d89SJeremy L. Thompson 
5527a982d89SJeremy L. Thompson   // LCOV_EXCL_START
553e15f9bd0SJeremy L Thompson   return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
554e15f9bd0SJeremy L Thompson                    "Requested function '%s' was not found for CEED "
5557a982d89SJeremy L. Thompson                    "object '%s'", fname, type);
5567a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
5577a982d89SJeremy L. Thompson }
5587a982d89SJeremy L. Thompson 
5597a982d89SJeremy L. Thompson /**
5607a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson   @param ceed      Ceed context to retrieve data of
5637a982d89SJeremy L. Thompson   @param[out] data Address to save data to
5647a982d89SJeremy L. Thompson 
5657a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5667a982d89SJeremy L. Thompson 
5677a982d89SJeremy L. Thompson   @ref Backend
5687a982d89SJeremy L. Thompson **/
569777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
570777ff853SJeremy L Thompson   *(void **)data = ceed->data;
571e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5727a982d89SJeremy L. Thompson }
5737a982d89SJeremy L. Thompson 
5747a982d89SJeremy L. Thompson /**
5757a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson   @param ceed           Ceed context to set data of
5787a982d89SJeremy L. Thompson   @param data           Address of data to set
5797a982d89SJeremy L. Thompson 
5807a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   @ref Backend
5837a982d89SJeremy L. Thompson **/
584777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
585777ff853SJeremy L Thompson   ceed->data = data;
586e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5877a982d89SJeremy L. Thompson }
5887a982d89SJeremy L. Thompson 
5897a982d89SJeremy L. Thompson /// @}
5907a982d89SJeremy L. Thompson 
5917a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5927a982d89SJeremy L. Thompson /// Ceed Public API
5937a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5947a982d89SJeremy L. Thompson /// @addtogroup CeedUser
5957a982d89SJeremy L. Thompson /// @{
5967a982d89SJeremy L. Thompson 
5977a982d89SJeremy L. Thompson /**
598*22e44211Sjeremylt   @brief Get the list of avaliable resource names for Ceed contexts
599*22e44211Sjeremylt   Note: The caller is responsible for freeing the resources and priorities arrays,
600*22e44211Sjeremylt           but should not free the contents of the resources array.
601*22e44211Sjeremylt 
602*22e44211Sjeremylt   @param[out] n          Number of avaliable resources
603*22e44211Sjeremylt   @param[out] resources  List of avaliable resource names
604*22e44211Sjeremylt   @param[out] priorities Resource name prioritization values, lower is better
605*22e44211Sjeremylt 
606*22e44211Sjeremylt   @return An error code: 0 - success, otherwise - failure
607*22e44211Sjeremylt 
608*22e44211Sjeremylt   @ref User
609*22e44211Sjeremylt **/
610*22e44211Sjeremylt // LCOV_EXCL_START
611*22e44211Sjeremylt int CeedRegistryGetList(size_t *n, char ***const resources,
612*22e44211Sjeremylt                         CeedInt **priorities) {
613*22e44211Sjeremylt   int ierr;
614*22e44211Sjeremylt 
615*22e44211Sjeremylt   *n = num_backends;
616*22e44211Sjeremylt   ierr = CeedMalloc(*n, resources); CeedChk(ierr);
617*22e44211Sjeremylt   ierr = CeedMalloc(*n, priorities); CeedChk(ierr);
618*22e44211Sjeremylt   for (size_t i=0; i<num_backends; i++) {
619*22e44211Sjeremylt     *resources[i] = backends[i].prefix;
620*22e44211Sjeremylt     *priorities[i] = backends[i].priority;
621*22e44211Sjeremylt   }
622*22e44211Sjeremylt   return CEED_ERROR_SUCCESS;
623*22e44211Sjeremylt };
624*22e44211Sjeremylt // LCOV_EXCL_STOP
625*22e44211Sjeremylt 
626*22e44211Sjeremylt /**
627d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
628*22e44211Sjeremylt   Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self")
629*22e44211Sjeremylt     will result in CeedInt printing the current libCEED version number
630*22e44211Sjeremylt     and a list of current avaliable backend resources to stderr.
631b11c1e72Sjeremylt 
632b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
633b11c1e72Sjeremylt   @param ceed      The library context
634b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
635b11c1e72Sjeremylt 
636b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
637dfdf5a53Sjeremylt 
6387a982d89SJeremy L. Thompson   @ref User
639b11c1e72Sjeremylt **/
640d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
641d7b241e6Sjeremylt   int ierr;
642aedaa0e5Sjeremylt   size_t matchlen = 0, matchidx = UINT_MAX, matchpriority = UINT_MAX, priority;
643d7b241e6Sjeremylt 
644fe2413ffSjeremylt   // Find matching backend
6451d102b48SJeremy L Thompson   if (!resource)
64613873f79Sjeremylt     // LCOV_EXCL_START
647e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided");
64813873f79Sjeremylt   // LCOV_EXCL_STOP
6491d013790SJed Brown   ierr = CeedRegisterAll(); CeedChk(ierr);
65013873f79Sjeremylt 
651*22e44211Sjeremylt   // Check for help request
652*22e44211Sjeremylt   const char *help_prefix = "help";
653*22e44211Sjeremylt   size_t match_help;
654*22e44211Sjeremylt   for (match_help=0; match_help<4
655*22e44211Sjeremylt        && resource[match_help] == help_prefix[match_help]; match_help++) {}
656*22e44211Sjeremylt   if (match_help == 4) {
657*22e44211Sjeremylt     fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR,
658*22e44211Sjeremylt             CEED_VERSION_MINOR, CEED_VERSION_PATCH,
659*22e44211Sjeremylt             CEED_VERSION_RELEASE ? "" : "+development");
660*22e44211Sjeremylt     fprintf(stderr, "Avaliable backend resources:\n");
661*22e44211Sjeremylt     for (size_t i=0; i<num_backends; i++) {
662*22e44211Sjeremylt       fprintf(stderr, "  %s\n", backends[i].prefix);
663*22e44211Sjeremylt     }
664*22e44211Sjeremylt     fflush(stderr);
665*22e44211Sjeremylt     match_help = 5; // Delineating character expected
666*22e44211Sjeremylt   } else {
667*22e44211Sjeremylt     match_help = 0;
668*22e44211Sjeremylt   }
669*22e44211Sjeremylt 
670*22e44211Sjeremylt   // Find best match, currently computed as number of matching characters
671*22e44211Sjeremylt   //   from requested resource stem but may use Levenshtein in future
6722bbc7fe8Sjeremylt   size_t stem_length;
673*22e44211Sjeremylt   for (stem_length=0; resource[stem_length+match_help]
674*22e44211Sjeremylt        && resource[stem_length+match_help] != ':'; stem_length++) {}
675d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
676d7b241e6Sjeremylt     size_t n;
677d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
678*22e44211Sjeremylt     for (n=0; prefix[n] && prefix[n] == resource[n+match_help]; n++) {}
679d7b241e6Sjeremylt     priority = backends[i].priority;
680d7b241e6Sjeremylt     if (n > matchlen || (n == matchlen && matchpriority > priority)) {
681d7b241e6Sjeremylt       matchlen = n;
682d7b241e6Sjeremylt       matchpriority = priority;
683d7b241e6Sjeremylt       matchidx = i;
684d7b241e6Sjeremylt     }
685d7b241e6Sjeremylt   }
6862bbc7fe8Sjeremylt   if (matchlen <= 1) {
68713873f79Sjeremylt     // LCOV_EXCL_START
688e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s",
689e15f9bd0SJeremy L Thompson                      resource);
69013873f79Sjeremylt     // LCOV_EXCL_STOP
6912bbc7fe8Sjeremylt   } else if (matchlen != stem_length) {
6922bbc7fe8Sjeremylt     // LCOV_EXCL_START
6932bbc7fe8Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s "
6942bbc7fe8Sjeremylt                      "Closest match: %s", resource, backends[matchidx].prefix);
6952bbc7fe8Sjeremylt     // LCOV_EXCL_STOP
6962bbc7fe8Sjeremylt   }
697fe2413ffSjeremylt 
698fe2413ffSjeremylt   // Setup Ceed
699d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
700bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
7011d102b48SJeremy L Thompson   if (!ceed_error_handler)
7021d102b48SJeremy L Thompson     ceed_error_handler = "abort";
703bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
70456e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
705477729cfSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store"))
706477729cfSJeremy L Thompson     (*ceed)->Error = CeedErrorStore;
70756e866f4SJed Brown   else
708d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
709477729cfSJeremy L Thompson   memcpy((*ceed)->errmsg, "No error message stored", 24);
710d7b241e6Sjeremylt   (*ceed)->refcount = 1;
711d7b241e6Sjeremylt   (*ceed)->data = NULL;
712fe2413ffSjeremylt 
713fe2413ffSjeremylt   // Set lookup table
7146e79d475Sjeremylt   foffset foffsets[] = {
7156e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
7166e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
7176e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
718f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
7196e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
7206e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
7216e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
7226e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
7236e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
7246e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
725777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
7266e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
7276e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
7286e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
7296a6c615bSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, TakeArray),
7306e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
7316e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
7326e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
7336e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
7346e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
735547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
736d99fa3c5SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
7376e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Destroy),
7386e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
7396e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
740bd33150aSjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
7416e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
7426e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Apply),
7436e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Destroy),
7446e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
7456e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
7466e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Apply),
7478c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
7488c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
7496e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
750777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
751777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
752777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
753777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
75480ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
75580ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
7569e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
75780ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
7589e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
759e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic),
760e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble),
761713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
7626e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
763250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
764cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
765250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
7666e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
7676e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
7686e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
7691dfeef1dSjeremylt   };
770fe2413ffSjeremylt 
7716e79d475Sjeremylt   ierr = CeedCalloc(sizeof(foffsets), &(*ceed)->foffsets); CeedChk(ierr);
7726e79d475Sjeremylt   memcpy((*ceed)->foffsets, foffsets, sizeof(foffsets));
773fe2413ffSjeremylt 
7745107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
775e2f04181SAndrew T. Barker   const char fallbackresource[] = "";
7765107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
7775107b09fSJeremy L Thompson   CeedChk(ierr);
7785107b09fSJeremy L Thompson 
77960f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
78060f9e2d6SJeremy L Thompson   (*ceed)->debug = !!getenv("CEED_DEBUG") || !!getenv("DBG");
78160f9e2d6SJeremy L Thompson 
782fe2413ffSjeremylt   // Backend specific setup
783*22e44211Sjeremylt   ierr = backends[matchidx].init(&resource[match_help], *ceed); CeedChk(ierr);
784fe2413ffSjeremylt 
785*22e44211Sjeremylt   // Copy resource prefix, if backend setup successful
786e07206deSjeremylt   size_t len = strlen(backends[matchidx].prefix);
787e07206deSjeremylt   char *tmp;
788e07206deSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
789e07206deSjeremylt   memcpy(tmp, backends[matchidx].prefix, len+1);
790e07206deSjeremylt   (*ceed)->resource = tmp;
791e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
792d7b241e6Sjeremylt }
793d7b241e6Sjeremylt 
794d7b241e6Sjeremylt /**
7957a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
7962f86a920SJeremy L Thompson 
7977a982d89SJeremy L. Thompson   @param ceed            Ceed context to get resource name of
7987a982d89SJeremy L. Thompson   @param[out] resource   Variable to store resource name
7992f86a920SJeremy L Thompson 
8002f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
8012f86a920SJeremy L Thompson 
8027a982d89SJeremy L. Thompson   @ref User
8035107b09fSJeremy L Thompson **/
8045107b09fSJeremy L Thompson 
8057a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
8067a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
807e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8085107b09fSJeremy L Thompson }
8095107b09fSJeremy L Thompson 
8105107b09fSJeremy L Thompson /**
811d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
812c907536fSjeremylt 
813d79b80ecSjeremylt   @param ceed      Ceed context to get preferred memory type of
814288c0443SJeremy L Thompson   @param[out] type Address to save preferred memory type to
815c907536fSjeremylt 
816c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
817c907536fSjeremylt 
8187a982d89SJeremy L. Thompson   @ref User
819c907536fSjeremylt **/
820c907536fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type) {
821c907536fSjeremylt   int ierr;
822c263cd57Sjeremylt 
823c907536fSjeremylt   if (ceed->GetPreferredMemType) {
824c907536fSjeremylt     ierr = ceed->GetPreferredMemType(type); CeedChk(ierr);
825c907536fSjeremylt   } else {
826c263cd57Sjeremylt     Ceed delegate;
827c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
828c263cd57Sjeremylt 
829c263cd57Sjeremylt     if (delegate) {
830c263cd57Sjeremylt       ierr = CeedGetPreferredMemType(delegate, type); CeedChk(ierr);
831c263cd57Sjeremylt     } else {
832c907536fSjeremylt       *type = CEED_MEM_HOST;
833c907536fSjeremylt     }
834c263cd57Sjeremylt   }
835e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
836c907536fSjeremylt }
837c907536fSjeremylt 
838c907536fSjeremylt /**
8399525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
8409525855cSJeremy L Thompson 
8419525855cSJeremy L Thompson   @param[in] ceed              Ceed
8429525855cSJeremy L Thompson   @param[out] isDeterministic  Variable to store deterministic status
8439525855cSJeremy L Thompson 
8449525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
8459525855cSJeremy L Thompson 
8469525855cSJeremy L Thompson   @ref User
8479525855cSJeremy L Thompson **/
8489525855cSJeremy L Thompson int CeedIsDeterministic(Ceed ceed, bool *isDeterministic) {
8499525855cSJeremy L Thompson   *isDeterministic = ceed->isDeterministic;
850e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8519525855cSJeremy L Thompson }
8529525855cSJeremy L Thompson 
8539525855cSJeremy L Thompson /**
8540a0da059Sjeremylt   @brief View a Ceed
8550a0da059Sjeremylt 
8560a0da059Sjeremylt   @param[in] ceed          Ceed to view
8570a0da059Sjeremylt   @param[in] stream        Filestream to write to
8580a0da059Sjeremylt 
8590a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
8600a0da059Sjeremylt 
8610a0da059Sjeremylt   @ref User
8620a0da059Sjeremylt **/
8630a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
8640a0da059Sjeremylt   int ierr;
8650a0da059Sjeremylt   CeedMemType memtype;
8660a0da059Sjeremylt 
8670a0da059Sjeremylt   ierr = CeedGetPreferredMemType(ceed, &memtype); CeedChk(ierr);
8680a0da059Sjeremylt 
8690a0da059Sjeremylt   fprintf(stream, "Ceed\n"
8700a0da059Sjeremylt           "  Ceed Resource: %s\n"
8710a0da059Sjeremylt           "  Preferred MemType: %s\n",
8720a0da059Sjeremylt           ceed->resource, CeedMemTypes[memtype]);
873e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8740a0da059Sjeremylt }
8750a0da059Sjeremylt 
8760a0da059Sjeremylt /**
877b11c1e72Sjeremylt   @brief Destroy a Ceed context
878d7b241e6Sjeremylt 
879d7b241e6Sjeremylt   @param ceed Address of Ceed context to destroy
880b11c1e72Sjeremylt 
881b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
882dfdf5a53Sjeremylt 
8837a982d89SJeremy L. Thompson   @ref User
884b11c1e72Sjeremylt **/
885d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
886d7b241e6Sjeremylt   int ierr;
887e15f9bd0SJeremy L Thompson   if (!*ceed || --(*ceed)->refcount > 0) return CEED_ERROR_SUCCESS;
8885fe0d4faSjeremylt   if ((*ceed)->delegate) {
8895fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
8905fe0d4faSjeremylt   }
8910ace9bf2Sjeremylt 
892aefd8378Sjeremylt   if ((*ceed)->objdelegatecount > 0) {
893aefd8378Sjeremylt     for (int i=0; i<(*ceed)->objdelegatecount; i++) {
894aefd8378Sjeremylt       ierr = CeedDestroy(&((*ceed)->objdelegates[i].delegate)); CeedChk(ierr);
895aefd8378Sjeremylt       ierr = CeedFree(&(*ceed)->objdelegates[i].objname); CeedChk(ierr);
896aefd8378Sjeremylt     }
897aefd8378Sjeremylt     ierr = CeedFree(&(*ceed)->objdelegates); CeedChk(ierr);
898aefd8378Sjeremylt   }
8990ace9bf2Sjeremylt 
900d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
901d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
902d7b241e6Sjeremylt   }
9030ace9bf2Sjeremylt 
9046e79d475Sjeremylt   ierr = CeedFree(&(*ceed)->foffsets); CeedChk(ierr);
905e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
9065107b09fSJeremy L Thompson   ierr = CeedDestroy(&(*ceed)->opfallbackceed); CeedChk(ierr);
9075107b09fSJeremy L Thompson   ierr = CeedFree(&(*ceed)->opfallbackresource); CeedChk(ierr);
908d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
909e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
910d7b241e6Sjeremylt }
911d7b241e6Sjeremylt 
912f9982c62SWill Pazner // LCOV_EXCL_START
913f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
9143f4a9821SAndrew T. Barker   if (ceed->parent)
9153f4a9821SAndrew T. Barker     return CeedErrorFormat(ceed->parent, format, args);
9163f4a9821SAndrew T. Barker   if (ceed->opfallbackparent)
9173f4a9821SAndrew T. Barker     return CeedErrorFormat(ceed->opfallbackparent, format, args);
918f9982c62SWill Pazner   vsnprintf(ceed->errmsg, CEED_MAX_RESOURCE_LEN, format, *args);
919f9982c62SWill Pazner   return ceed->errmsg;
920f9982c62SWill Pazner }
921f9982c62SWill Pazner // LCOV_EXCL_STOP
922f9982c62SWill Pazner 
9237a982d89SJeremy L. Thompson /**
9247a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
9257a982d89SJeremy L. Thompson 
9267a982d89SJeremy L. Thompson   @ref Developer
9277a982d89SJeremy L. Thompson **/
9287a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
9297a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
9307a982d89SJeremy L. Thompson   va_list args;
9317a982d89SJeremy L. Thompson   int retval;
9327a982d89SJeremy L. Thompson   va_start(args, format);
9337a982d89SJeremy L. Thompson   if (ceed) {
934f9982c62SWill Pazner     retval = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
9357a982d89SJeremy L. Thompson   } else {
936b0d62198Sjeremylt     // LCOV_EXCL_START
937477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
938477729cfSJeremy L Thompson     if (!ceed_error_handler)
939477729cfSJeremy L Thompson       ceed_error_handler = "abort";
940477729cfSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return"))
941f9982c62SWill Pazner       retval = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
942477729cfSJeremy L Thompson     else
943477729cfSJeremy L Thompson       // This function will not return
944f9982c62SWill Pazner       retval = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
9457a982d89SJeremy L. Thompson   }
9467a982d89SJeremy L. Thompson   va_end(args);
9477a982d89SJeremy L. Thompson   return retval;
948b0d62198Sjeremylt   // LCOV_EXCL_STOP
9497a982d89SJeremy L. Thompson }
9507a982d89SJeremy L. Thompson 
951477729cfSJeremy L Thompson /**
952477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
953477729cfSJeremy L Thompson 
954477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
955477729cfSJeremy L Thompson 
956477729cfSJeremy L Thompson   @ref Developer
957477729cfSJeremy L Thompson **/
958477729cfSJeremy L Thompson // LCOV_EXCL_START
959477729cfSJeremy L Thompson int CeedErrorReturn(Ceed ceed, const char *filename, int lineno,
960477729cfSJeremy L Thompson                     const char *func, int ecode, const char *format,
961f9982c62SWill Pazner                     va_list *args) {
962477729cfSJeremy L Thompson   return ecode;
963477729cfSJeremy L Thompson }
964477729cfSJeremy L Thompson // LCOV_EXCL_STOP
965477729cfSJeremy L Thompson 
966477729cfSJeremy L Thompson /**
967477729cfSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns
968477729cfSJeremy L Thompson            the error.
969477729cfSJeremy L Thompson 
970477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
971477729cfSJeremy L Thompson 
972477729cfSJeremy L Thompson   @ref Developer
973477729cfSJeremy L Thompson **/
974477729cfSJeremy L Thompson // LCOV_EXCL_START
975477729cfSJeremy L Thompson int CeedErrorStore(Ceed ceed, const char *filename, int lineno,
976477729cfSJeremy L Thompson                    const char *func, int ecode, const char *format,
977f9982c62SWill Pazner                    va_list *args) {
978477729cfSJeremy L Thompson   if (ceed->parent)
979187168c7SJeremy L Thompson     return CeedErrorStore(ceed->parent, filename, lineno, func, ecode, format,
980187168c7SJeremy L Thompson                           args);
981fc164cf7SWill Pazner   if (ceed->opfallbackparent)
982fc164cf7SWill Pazner     return CeedErrorStore(ceed->opfallbackparent, filename, lineno, func, ecode,
983fc164cf7SWill Pazner                           format, args);
984477729cfSJeremy L Thompson 
985477729cfSJeremy L Thompson   // Build message
986477729cfSJeremy L Thompson   CeedInt len;
987477729cfSJeremy L Thompson   len = snprintf(ceed->errmsg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ",
988477729cfSJeremy L Thompson                  filename, lineno, func);
989f9982c62SWill Pazner   vsnprintf(ceed->errmsg + len, CEED_MAX_RESOURCE_LEN - len, format, *args);
990477729cfSJeremy L Thompson   return ecode;
991477729cfSJeremy L Thompson }
992477729cfSJeremy L Thompson // LCOV_EXCL_STOP
993477729cfSJeremy L Thompson 
994477729cfSJeremy L Thompson /**
995477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
996477729cfSJeremy L Thompson 
997477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
998477729cfSJeremy L Thompson 
999477729cfSJeremy L Thompson   @ref Developer
1000477729cfSJeremy L Thompson **/
1001477729cfSJeremy L Thompson // LCOV_EXCL_START
1002477729cfSJeremy L Thompson int CeedErrorAbort(Ceed ceed, const char *filename, int lineno,
1003477729cfSJeremy L Thompson                    const char *func, int ecode, const char *format,
1004f9982c62SWill Pazner                    va_list *args) {
1005477729cfSJeremy L Thompson   fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func);
1006f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1007477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1008477729cfSJeremy L Thompson   abort();
1009477729cfSJeremy L Thompson   return ecode;
1010477729cfSJeremy L Thompson }
1011477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1012477729cfSJeremy L Thompson 
1013477729cfSJeremy L Thompson /**
1014477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
1015477729cfSJeremy L Thompson 
1016477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1017477729cfSJeremy L Thompson 
1018477729cfSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit()
1019477729cfSJeremy L Thompson   handlers (e.g., as used by gcov) are run.
1020477729cfSJeremy L Thompson 
1021477729cfSJeremy L Thompson   @ref Developer
1022477729cfSJeremy L Thompson **/
1023477729cfSJeremy L Thompson int CeedErrorExit(Ceed ceed, const char *filename, int lineno, const char *func,
1024f9982c62SWill Pazner                   int ecode, const char *format, va_list *args) {
1025477729cfSJeremy L Thompson   fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func);
1026f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1027477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1028477729cfSJeremy L Thompson   exit(ecode);
1029477729cfSJeremy L Thompson   return ecode;
1030477729cfSJeremy L Thompson }
1031477729cfSJeremy L Thompson 
1032477729cfSJeremy L Thompson /**
1033477729cfSJeremy L Thompson   @brief Set error handler
1034477729cfSJeremy L Thompson 
1035477729cfSJeremy L Thompson   A default error handler is set in CeedInit().  Use this function to change
1036477729cfSJeremy L Thompson   the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined
1037477729cfSJeremy L Thompson   error handler.
1038477729cfSJeremy L Thompson 
1039477729cfSJeremy L Thompson   @ref Developer
1040477729cfSJeremy L Thompson **/
1041e15f9bd0SJeremy L Thompson int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler eh) {
1042477729cfSJeremy L Thompson   ceed->Error = eh;
1043477729cfSJeremy L Thompson   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, eh);
1044477729cfSJeremy L Thompson   for (int i=0; i<ceed->objdelegatecount; i++)
1045477729cfSJeremy L Thompson     CeedSetErrorHandler(ceed->objdelegates[i].delegate, eh);
1046e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1047477729cfSJeremy L Thompson }
1048477729cfSJeremy L Thompson 
1049477729cfSJeremy L Thompson /**
1050477729cfSJeremy L Thompson   @brief Get error message
1051477729cfSJeremy L Thompson 
1052477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1053477729cfSJeremy L Thompson     CeedErrorStore()
1054477729cfSJeremy L Thompson 
1055477729cfSJeremy L Thompson   @param[in] ceed     Ceed contex to retrieve error message
1056477729cfSJeremy L Thompson   @param[out] errmsg  Char pointer to hold error message
1057477729cfSJeremy L Thompson 
1058477729cfSJeremy L Thompson   @ref Developer
1059477729cfSJeremy L Thompson **/
1060477729cfSJeremy L Thompson int CeedGetErrorMessage(Ceed ceed, const char **errmsg) {
10613f4a9821SAndrew T. Barker   if (ceed->parent)
10623f4a9821SAndrew T. Barker     return CeedGetErrorMessage(ceed->parent, errmsg);
10633f4a9821SAndrew T. Barker   if (ceed->opfallbackparent)
10643f4a9821SAndrew T. Barker     return CeedGetErrorMessage(ceed->opfallbackparent, errmsg);
1065477729cfSJeremy L Thompson   *errmsg = ceed->errmsg;
1066e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1067477729cfSJeremy L Thompson }
1068477729cfSJeremy L Thompson 
1069477729cfSJeremy L Thompson /**
1070477729cfSJeremy L Thompson   @brief Restore error message
1071477729cfSJeremy L Thompson 
1072477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1073477729cfSJeremy L Thompson     CeedErrorStore()
1074477729cfSJeremy L Thompson 
1075477729cfSJeremy L Thompson   @param[in] ceed     Ceed contex to restore error message
1076477729cfSJeremy L Thompson   @param[out] errmsg  Char pointer that holds error message
1077477729cfSJeremy L Thompson 
1078477729cfSJeremy L Thompson   @ref Developer
1079477729cfSJeremy L Thompson **/
1080477729cfSJeremy L Thompson int CeedResetErrorMessage(Ceed ceed, const char **errmsg) {
10813f4a9821SAndrew T. Barker   if (ceed->parent)
10823f4a9821SAndrew T. Barker     return CeedResetErrorMessage(ceed->parent, errmsg);
10833f4a9821SAndrew T. Barker   if (ceed->opfallbackparent)
10843f4a9821SAndrew T. Barker     return CeedResetErrorMessage(ceed->opfallbackparent, errmsg);
1085477729cfSJeremy L Thompson   *errmsg = NULL;
1086477729cfSJeremy L Thompson   memcpy(ceed->errmsg, "No error message stored", 24);
1087e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1088477729cfSJeremy L Thompson }
1089477729cfSJeremy L Thompson 
10901070991dSJed Brown /**
10911070991dSJed Brown   @brief Get libCEED library version info
10921070991dSJed Brown 
10931070991dSJed Brown   libCEED version numbers have the form major.minor.patch. Non-release versions
10941070991dSJed Brown   may contain unstable interfaces.
10951070991dSJed Brown 
10961070991dSJed Brown   @param[out] major    Major version of the library
10971070991dSJed Brown   @param[out] minor    Minor version of the library
10981070991dSJed Brown   @param[out] patch    Patch (subminor) version of the library
10991070991dSJed Brown   @param[out] release  True for releases; false for development branches.
11001070991dSJed Brown 
11011070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
11021070991dSJed Brown 
11031070991dSJed Brown   @sa CEED_VERSION_GE()
11041070991dSJed Brown 
11051070991dSJed Brown   @ref Developer
11061070991dSJed Brown */
11071070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
11081070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
11091070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
11101070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
11111070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
11121070991dSJed Brown   return 0;
11131070991dSJed Brown }
11141070991dSJed Brown 
1115d7b241e6Sjeremylt /// @}
1116