xref: /libCEED/rust/libceed-sys/c-src/interface/ceed.c (revision 28bfd0b742d5bb98b1c0887e682aa0983923c5c6)
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 
1246a406739SJeremy L Thompson /**
1256a406739SJeremy L Thompson   @brief Register a Ceed backend internally.
1266a406739SJeremy L Thompson            Note: Backends should call `CeedRegister` instead.
1276a406739SJeremy L Thompson 
1286a406739SJeremy L Thompson   @param prefix    Prefix of resources for this backend to respond to.  For
1296a406739SJeremy L Thompson                      example, the reference backend responds to "/cpu/self".
1306a406739SJeremy L Thompson   @param init      Initialization function called by CeedInit() when the backend
1316a406739SJeremy L Thompson                      is selected to drive the requested resource.
1326a406739SJeremy L Thompson   @param priority  Integer priority.  Lower values are preferred in case the
1336a406739SJeremy L Thompson                      resource requested by CeedInit() has non-unique best prefix
1346a406739SJeremy L Thompson                      match.
1356a406739SJeremy L Thompson 
1366a406739SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1376a406739SJeremy L Thompson 
1386a406739SJeremy L Thompson   @ref Developer
1396a406739SJeremy L Thompson **/
1406a406739SJeremy L Thompson int CeedRegisterImpl(const char *prefix, int (*init)(const char *, Ceed),
1416a406739SJeremy L Thompson                      unsigned int priority) {
1426a406739SJeremy L Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0]))
1436a406739SJeremy L Thompson     // LCOV_EXCL_START
1446a406739SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many backends");
1456a406739SJeremy L Thompson   // LCOV_EXCL_STOP
1466a406739SJeremy L Thompson 
1476a406739SJeremy L Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
1486a406739SJeremy L Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0;
1496a406739SJeremy L Thompson   backends[num_backends].init = init;
1506a406739SJeremy L Thompson   backends[num_backends].priority = priority;
1516a406739SJeremy L Thompson   num_backends++;
1526a406739SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1536a406739SJeremy L Thompson }
1546a406739SJeremy L Thompson 
1557a982d89SJeremy L. Thompson /// @}
156d7b241e6Sjeremylt 
1577a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1587a982d89SJeremy L. Thompson /// Ceed Backend API
1597a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1607a982d89SJeremy L. Thompson /// @addtogroup CeedBackend
1617a982d89SJeremy L. Thompson /// @{
162d7b241e6Sjeremylt 
163b11c1e72Sjeremylt /**
1643f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
16560f9e2d6SJeremy L Thompson 
16660f9e2d6SJeremy L Thompson   @param ceed    Ceed context
16760f9e2d6SJeremy L Thompson 
1683f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1693f21f6b1SJeremy L Thompson                          false - debugging mode disabled
17060f9e2d6SJeremy L Thompson 
17160f9e2d6SJeremy L Thompson   @ref Backend
17260f9e2d6SJeremy L Thompson **/
173fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
1743f21f6b1SJeremy L Thompson bool CeedDebugFlag(const Ceed ceed) {
1753f21f6b1SJeremy L Thompson   return ceed->is_debug;
17660f9e2d6SJeremy L Thompson }
177fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
17860f9e2d6SJeremy L Thompson 
17960f9e2d6SJeremy L Thompson /**
1803f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
18160f9e2d6SJeremy L Thompson 
1823f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1833f21f6b1SJeremy L Thompson                          false - debugging mode disabled
1843f21f6b1SJeremy L Thompson 
1853f21f6b1SJeremy L Thompson   @ref Backend
1863f21f6b1SJeremy L Thompson **/
1873f21f6b1SJeremy L Thompson // LCOV_EXCL_START
1883f21f6b1SJeremy L Thompson bool CeedDebugFlagEnv(void) {
1893f21f6b1SJeremy L Thompson   return !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG");
1903f21f6b1SJeremy L Thompson }
1913f21f6b1SJeremy L Thompson // LCOV_EXCL_STOP
1923f21f6b1SJeremy L Thompson 
1933f21f6b1SJeremy L Thompson /**
1943f21f6b1SJeremy L Thompson   @brief Print debugging information in color
1953f21f6b1SJeremy L Thompson 
19660f9e2d6SJeremy L Thompson   @param color   Color to print
19760f9e2d6SJeremy L Thompson   @param format  Printing format
19860f9e2d6SJeremy L Thompson 
19960f9e2d6SJeremy L Thompson   @ref Backend
20060f9e2d6SJeremy L Thompson **/
201fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
2023f21f6b1SJeremy L Thompson void CeedDebugImpl256(const unsigned char color, const char *format,...) {
20360f9e2d6SJeremy L Thompson   va_list args;
20460f9e2d6SJeremy L Thompson   va_start(args, format);
20560f9e2d6SJeremy L Thompson   fflush(stdout);
2063f21f6b1SJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE)
20760f9e2d6SJeremy L Thompson     fprintf(stdout, "\033[38;5;%dm", color);
20860f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
2093f21f6b1SJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE)
21060f9e2d6SJeremy L Thompson     fprintf(stdout, "\033[m");
21160f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
21260f9e2d6SJeremy L Thompson   fflush(stdout);
21360f9e2d6SJeremy L Thompson   va_end(args);
21460f9e2d6SJeremy L Thompson }
215fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
21660f9e2d6SJeremy L Thompson 
21760f9e2d6SJeremy L Thompson /**
218b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
219b11c1e72Sjeremylt 
220b11c1e72Sjeremylt   Memory usage can be tracked by the library.  This ensures sufficient
221b11c1e72Sjeremylt     alignment for vectorization and should be used for large allocations.
222b11c1e72Sjeremylt 
223b11c1e72Sjeremylt   @param n     Number of units to allocate
224b11c1e72Sjeremylt   @param unit  Size of each unit
225b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
226b11c1e72Sjeremylt 
227b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
228b11c1e72Sjeremylt 
229b11c1e72Sjeremylt   @sa CeedFree()
230dfdf5a53Sjeremylt 
2317a982d89SJeremy L. Thompson   @ref Backend
232b11c1e72Sjeremylt **/
233d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
234d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit);
235d7b241e6Sjeremylt   if (ierr)
236c042f62fSJeremy L Thompson     // LCOV_EXCL_START
237e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
238e15f9bd0SJeremy L Thompson                      "posix_memalign failed to allocate %zd "
2391d102b48SJeremy L Thompson                      "members of size %zd\n", n, unit);
240c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
241e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
242d7b241e6Sjeremylt }
243d7b241e6Sjeremylt 
244b11c1e72Sjeremylt /**
245b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
246b11c1e72Sjeremylt 
247b11c1e72Sjeremylt   Memory usage can be tracked by the library.
248b11c1e72Sjeremylt 
249b11c1e72Sjeremylt   @param n     Number of units to allocate
250b11c1e72Sjeremylt   @param unit  Size of each unit
251b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
252b11c1e72Sjeremylt 
253b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
254b11c1e72Sjeremylt 
255b11c1e72Sjeremylt   @sa CeedFree()
256dfdf5a53Sjeremylt 
2577a982d89SJeremy L. Thompson   @ref Backend
258b11c1e72Sjeremylt **/
259d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
260d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
261d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
262c042f62fSJeremy L Thompson     // LCOV_EXCL_START
263e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
264e15f9bd0SJeremy L Thompson                      "calloc failed to allocate %zd members of size "
2651d102b48SJeremy L Thompson                      "%zd\n", n, unit);
266c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
267e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
268d7b241e6Sjeremylt }
269d7b241e6Sjeremylt 
270b11c1e72Sjeremylt /**
271b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
272b11c1e72Sjeremylt 
273b11c1e72Sjeremylt   Memory usage can be tracked by the library.
274b11c1e72Sjeremylt 
275b11c1e72Sjeremylt   @param n     Number of units to allocate
276b11c1e72Sjeremylt   @param unit  Size of each unit
277b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
278b11c1e72Sjeremylt 
279b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
280b11c1e72Sjeremylt 
281b11c1e72Sjeremylt   @sa CeedFree()
282dfdf5a53Sjeremylt 
2837a982d89SJeremy L. Thompson   @ref Backend
284b11c1e72Sjeremylt **/
285d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
286d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n*unit);
287d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
288c042f62fSJeremy L Thompson     // LCOV_EXCL_START
289e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
290e15f9bd0SJeremy L Thompson                      "realloc failed to allocate %zd members of size "
2911d102b48SJeremy L Thompson                      "%zd\n", n, unit);
292c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
293e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
294d7b241e6Sjeremylt }
295d7b241e6Sjeremylt 
296f7e22acaSJeremy L Thompson /**
297f7e22acaSJeremy L Thompson   @brief Allocate a cleared string buffer on the host
298f7e22acaSJeremy L Thompson 
299f7e22acaSJeremy L Thompson   Memory usage can be tracked by the library.
300f7e22acaSJeremy L Thompson 
301f7e22acaSJeremy L Thompson   @param source Pointer to string to be copied
302f7e22acaSJeremy L Thompson   @param copy   Pointer to variable to hold newly allocated string copy
303f7e22acaSJeremy L Thompson 
304f7e22acaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
305f7e22acaSJeremy L Thompson 
306f7e22acaSJeremy L Thompson   @sa CeedFree()
307f7e22acaSJeremy L Thompson 
308f7e22acaSJeremy L Thompson   @ref Backend
309f7e22acaSJeremy L Thompson **/
310f7e22acaSJeremy L Thompson int CeedStringAllocCopy(const char *source, char **copy) {
311f7e22acaSJeremy L Thompson   int ierr;
312f7e22acaSJeremy L Thompson   size_t len = strlen(source);
313f7e22acaSJeremy L Thompson   ierr = CeedCalloc(len + 1, copy); CeedChk(ierr);
314f7e22acaSJeremy L Thompson   memcpy(*copy, source, len + 1);
315f7e22acaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
316f7e22acaSJeremy L Thompson }
317f7e22acaSJeremy L Thompson 
31834138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
31934138859Sjeremylt 
32034138859Sjeremylt   @param p  address of pointer to memory.  This argument is of type void* to
32134138859Sjeremylt               avoid needing a cast, but is the address of the pointer (which is
32234138859Sjeremylt               zeroed) rather than the pointer.
32334138859Sjeremylt **/
324d7b241e6Sjeremylt int CeedFree(void *p) {
325d7b241e6Sjeremylt   free(*(void **)p);
326d7b241e6Sjeremylt   *(void **)p = NULL;
327e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
328d7b241e6Sjeremylt }
329d7b241e6Sjeremylt 
330d7b241e6Sjeremylt /**
3317a982d89SJeremy L. Thompson   @brief Register a Ceed backend
332d7b241e6Sjeremylt 
3337a982d89SJeremy L. Thompson   @param prefix    Prefix of resources for this backend to respond to.  For
3347a982d89SJeremy L. Thompson                      example, the reference backend responds to "/cpu/self".
3357a982d89SJeremy L. Thompson   @param init      Initialization function called by CeedInit() when the backend
3367a982d89SJeremy L. Thompson                      is selected to drive the requested resource.
3377a982d89SJeremy L. Thompson   @param priority  Integer priority.  Lower values are preferred in case the
3387a982d89SJeremy L. Thompson                      resource requested by CeedInit() has non-unique best prefix
3397a982d89SJeremy L. Thompson                      match.
340b11c1e72Sjeremylt 
341b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
342dfdf5a53Sjeremylt 
3437a982d89SJeremy L. Thompson   @ref Backend
344b11c1e72Sjeremylt **/
3457a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed),
3467a982d89SJeremy L. Thompson                  unsigned int priority) {
34710243053SJeremy L Thompson   CeedDebugEnv("Backend Register: %s", prefix);
3486a406739SJeremy L Thompson   CeedRegisterImpl(prefix, init, priority);
349e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
350d7b241e6Sjeremylt }
351d7b241e6Sjeremylt 
352b11c1e72Sjeremylt /**
35360f9e2d6SJeremy L Thompson   @brief Return debugging status flag
35460f9e2d6SJeremy L Thompson 
35560f9e2d6SJeremy L Thompson   @param ceed      Ceed context to get debugging flag
356d1d35e2fSjeremylt   @param is_debug  Variable to store debugging flag
35760f9e2d6SJeremy L Thompson 
35860f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
35960f9e2d6SJeremy L Thompson 
360d1d35e2fSjeremylt   @ref Backend
36160f9e2d6SJeremy L Thompson **/
362d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) {
3633f21f6b1SJeremy L Thompson   *is_debug = ceed->is_debug;
364e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
36560f9e2d6SJeremy L Thompson }
36660f9e2d6SJeremy L Thompson 
36760f9e2d6SJeremy L Thompson /**
3687a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson   @param ceed         Ceed context to retrieve parent of
3717a982d89SJeremy L. Thompson   @param[out] parent  Address to save the parent to
3727a982d89SJeremy L. Thompson 
3737a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3747a982d89SJeremy L. Thompson 
3757a982d89SJeremy L. Thompson   @ref Backend
3767a982d89SJeremy L. Thompson **/
3777a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3787a982d89SJeremy L. Thompson   int ierr;
3797a982d89SJeremy L. Thompson   if (ceed->parent) {
3807a982d89SJeremy L. Thompson     ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr);
381e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
3827a982d89SJeremy L. Thompson   }
3837a982d89SJeremy L. Thompson   *parent = ceed;
384e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3857a982d89SJeremy L. Thompson }
3867a982d89SJeremy L. Thompson 
3877a982d89SJeremy L. Thompson /**
3887a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3917a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson   @ref Backend
3967a982d89SJeremy L. Thompson **/
3977a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3987a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
399e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4007a982d89SJeremy L. Thompson }
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson /**
4037a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
4047a982d89SJeremy L. Thompson 
4057a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context. All
4067a982d89SJeremy L. Thompson     backend implementations default to the delegate Ceed context, unless
4077a982d89SJeremy L. Thompson     overridden.
4087a982d89SJeremy L. Thompson 
4097a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
4107a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
4117a982d89SJeremy L. Thompson 
4127a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson   @ref Backend
4157a982d89SJeremy L. Thompson **/
4167a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
4177a982d89SJeremy L. Thompson   ceed->delegate = delegate;
4187a982d89SJeremy L. Thompson   delegate->parent = ceed;
419e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4207a982d89SJeremy L. Thompson }
4217a982d89SJeremy L. Thompson 
4227a982d89SJeremy L. Thompson /**
4237a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
4247a982d89SJeremy L. Thompson 
4257a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
4267a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
427d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to retrieve delegate for
4287a982d89SJeremy L. Thompson 
4297a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson   @ref Backend
4327a982d89SJeremy L. Thompson **/
433d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) {
4347a982d89SJeremy L. Thompson   CeedInt ierr;
4357a982d89SJeremy L. Thompson 
4367a982d89SJeremy L. Thompson   // Check for object delegate
437d1d35e2fSjeremylt   for (CeedInt i=0; i<ceed->obj_delegate_count; i++)
438d1d35e2fSjeremylt     if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) {
439d1d35e2fSjeremylt       *delegate = ceed->obj_delegates->delegate;
440e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
4417a982d89SJeremy L. Thompson     }
4427a982d89SJeremy L. Thompson 
4437a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
4447a982d89SJeremy L. Thompson   ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr);
445e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4467a982d89SJeremy L. Thompson }
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson /**
4497a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context for a
4527a982d89SJeremy L. Thompson     given type of Ceed object. All backend implementations default to the
4537a982d89SJeremy L. Thompson     delegate Ceed context for this object. For example,
4547a982d89SJeremy L. Thompson     CeedSetObjectDelegate(ceed, refceed, "Basis")
4557a982d89SJeremy L. Thompson   uses refceed implementations for all CeedBasis backend functions.
4567a982d89SJeremy L. Thompson 
4577a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
4587a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
459d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to set delegate for
4607a982d89SJeremy L. Thompson 
4617a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4627a982d89SJeremy L. Thompson 
4637a982d89SJeremy L. Thompson   @ref Backend
4647a982d89SJeremy L. Thompson **/
465d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) {
466f7e22acaSJeremy L Thompson   int ierr;
467d1d35e2fSjeremylt   CeedInt count = ceed->obj_delegate_count;
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson   // Malloc or Realloc
4707a982d89SJeremy L. Thompson   if (count) {
471d1d35e2fSjeremylt     ierr = CeedRealloc(count+1, &ceed->obj_delegates); CeedChk(ierr);
4727a982d89SJeremy L. Thompson   } else {
473d1d35e2fSjeremylt     ierr = CeedCalloc(1, &ceed->obj_delegates); CeedChk(ierr);
4747a982d89SJeremy L. Thompson   }
475d1d35e2fSjeremylt   ceed->obj_delegate_count++;
4767a982d89SJeremy L. Thompson 
4777a982d89SJeremy L. Thompson   // Set object delegate
478d1d35e2fSjeremylt   ceed->obj_delegates[count].delegate = delegate;
479f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(obj_name, &ceed->obj_delegates[count].obj_name);
480f7e22acaSJeremy L Thompson   CeedChk(ierr);
4817a982d89SJeremy L. Thompson 
4827a982d89SJeremy L. Thompson   // Set delegate parent
4837a982d89SJeremy L. Thompson   delegate->parent = ceed;
484e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4857a982d89SJeremy L. Thompson }
4867a982d89SJeremy L. Thompson 
4877a982d89SJeremy L. Thompson /**
4887a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4897a982d89SJeremy L. Thompson 
4907a982d89SJeremy L. Thompson   @param ceed           Ceed context
4917a982d89SJeremy L. Thompson   @param[out] resource  Variable to store fallback resource
4927a982d89SJeremy L. Thompson 
4937a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4947a982d89SJeremy L. Thompson 
4957a982d89SJeremy L. Thompson   @ref Backend
4967a982d89SJeremy L. Thompson **/
4977a982d89SJeremy L. Thompson 
4987a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
499d1d35e2fSjeremylt   *resource = (const char *)ceed->op_fallback_resource;
500e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5017a982d89SJeremy L. Thompson }
5027a982d89SJeremy L. Thompson 
5037a982d89SJeremy L. Thompson /**
5047a982d89SJeremy L. Thompson   @brief Set the fallback resource for CeedOperators. The current resource, if
5057a982d89SJeremy L. Thompson            any, is freed by calling this function. This string is freed upon the
5067a982d89SJeremy L. Thompson            destruction of the Ceed context.
5077a982d89SJeremy L. Thompson 
5087a982d89SJeremy L. Thompson   @param[out] ceed Ceed context
5097a982d89SJeremy L. Thompson   @param resource  Fallback resource to set
5107a982d89SJeremy L. Thompson 
5117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5127a982d89SJeremy L. Thompson 
5137a982d89SJeremy L. Thompson   @ref Backend
5147a982d89SJeremy L. Thompson **/
5157a982d89SJeremy L. Thompson 
5167a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
5177a982d89SJeremy L. Thompson   int ierr;
5187a982d89SJeremy L. Thompson 
5197a982d89SJeremy L. Thompson   // Free old
520d1d35e2fSjeremylt   ierr = CeedFree(&ceed->op_fallback_resource); CeedChk(ierr);
5217a982d89SJeremy L. Thompson 
5227a982d89SJeremy L. Thompson   // Set new
523f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(resource, (char **)&ceed->op_fallback_resource);
524f7e22acaSJeremy L Thompson   CeedChk(ierr);
525e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5267a982d89SJeremy L. Thompson }
5277a982d89SJeremy L. Thompson 
5287a982d89SJeremy L. Thompson /**
5297a982d89SJeremy L. Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context
5307a982d89SJeremy L. Thompson            for a CeedOperator
5317a982d89SJeremy L. Thompson 
5327a982d89SJeremy L. Thompson   @param ceed         Ceed context
5337a982d89SJeremy L. Thompson   @param[out] parent  Variable to store parent Ceed context
5347a982d89SJeremy L. Thompson 
5357a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5367a982d89SJeremy L. Thompson 
5377a982d89SJeremy L. Thompson   @ref Backend
5387a982d89SJeremy L. Thompson **/
5397a982d89SJeremy L. Thompson 
5407a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
541d1d35e2fSjeremylt   *parent = ceed->op_fallback_parent;
542e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5437a982d89SJeremy L. Thompson }
5447a982d89SJeremy L. Thompson 
5457a982d89SJeremy L. Thompson /**
5469525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
5479525855cSJeremy L Thompson 
5489525855cSJeremy L Thompson   @param ceed                   Ceed to flag as deterministic
54996b902e2Sjeremylt   @param[out] is_deterministic  Deterministic status to set
5509525855cSJeremy L Thompson 
5519525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5529525855cSJeremy L Thompson 
5539525855cSJeremy L Thompson   @ref Backend
5549525855cSJeremy L Thompson **/
5559525855cSJeremy L Thompson 
556d1d35e2fSjeremylt int CeedSetDeterministic(Ceed ceed, bool is_deterministic) {
557d1d35e2fSjeremylt   ceed->is_deterministic = is_deterministic;
558e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5599525855cSJeremy L Thompson }
5609525855cSJeremy L Thompson 
5619525855cSJeremy L Thompson /**
5627a982d89SJeremy L. Thompson   @brief Set a backend function
5637a982d89SJeremy L. Thompson 
5647a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5657a982d89SJeremy L. Thompson   the Ceed objects. For example,
5667a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5677a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5687a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5697a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
5707a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
5717a982d89SJeremy L. Thompson 
5727a982d89SJeremy L. Thompson   @param ceed         Ceed context for error handling
5737a982d89SJeremy L. Thompson   @param type         Type of Ceed object to set function for
5747a982d89SJeremy L. Thompson   @param[out] object  Ceed object to set function for
575d1d35e2fSjeremylt   @param func_name    Name of function to set
5767a982d89SJeremy L. Thompson   @param f            Function to set
5777a982d89SJeremy L. Thompson 
5787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5797a982d89SJeremy L. Thompson 
5807a982d89SJeremy L. Thompson   @ref Backend
5817a982d89SJeremy L. Thompson **/
5827a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
583d1d35e2fSjeremylt                            const char *func_name, int (*f)()) {
584d1d35e2fSjeremylt   char lookup_name[CEED_MAX_RESOURCE_LEN+1] = "";
5857a982d89SJeremy L. Thompson 
5867a982d89SJeremy L. Thompson   // Build lookup name
5877a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
588d1d35e2fSjeremylt     strncat (lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN);
589d1d35e2fSjeremylt   strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN);
590d1d35e2fSjeremylt   strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN);
5917a982d89SJeremy L. Thompson 
5927a982d89SJeremy L. Thompson   // Find and use offset
593d1d35e2fSjeremylt   for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++)
594d1d35e2fSjeremylt     if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) {
595d1d35e2fSjeremylt       size_t offset = ceed->f_offsets[i].offset;
5967a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
5977a982d89SJeremy L. Thompson       *fpointer = f;
598e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
5997a982d89SJeremy L. Thompson     }
6007a982d89SJeremy L. Thompson 
6017a982d89SJeremy L. Thompson   // LCOV_EXCL_START
602e15f9bd0SJeremy L Thompson   return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
603e15f9bd0SJeremy L Thompson                    "Requested function '%s' was not found for CEED "
604d1d35e2fSjeremylt                    "object '%s'", func_name, type);
6057a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
6067a982d89SJeremy L. Thompson }
6077a982d89SJeremy L. Thompson 
6087a982d89SJeremy L. Thompson /**
6097a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
6107a982d89SJeremy L. Thompson 
6117a982d89SJeremy L. Thompson   @param ceed       Ceed context to retrieve data of
6127a982d89SJeremy L. Thompson   @param[out] data  Address to save data to
6137a982d89SJeremy L. Thompson 
6147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6157a982d89SJeremy L. Thompson 
6167a982d89SJeremy L. Thompson   @ref Backend
6177a982d89SJeremy L. Thompson **/
618777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
619777ff853SJeremy L Thompson   *(void **)data = ceed->data;
620e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6217a982d89SJeremy L. Thompson }
6227a982d89SJeremy L. Thompson 
6237a982d89SJeremy L. Thompson /**
6247a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
6257a982d89SJeremy L. Thompson 
6267a982d89SJeremy L. Thompson   @param ceed  Ceed context to set data of
6277a982d89SJeremy L. Thompson   @param data  Address of data to set
6287a982d89SJeremy L. Thompson 
6297a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6307a982d89SJeremy L. Thompson 
6317a982d89SJeremy L. Thompson   @ref Backend
6327a982d89SJeremy L. Thompson **/
633777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
634777ff853SJeremy L Thompson   ceed->data = data;
635e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6367a982d89SJeremy L. Thompson }
6377a982d89SJeremy L. Thompson 
63834359f16Sjeremylt /**
63934359f16Sjeremylt   @brief Increment the reference counter for a Ceed context
64034359f16Sjeremylt 
64134359f16Sjeremylt   @param ceed  Ceed context to increment the reference counter
64234359f16Sjeremylt 
64334359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
64434359f16Sjeremylt 
64534359f16Sjeremylt   @ref Backend
64634359f16Sjeremylt **/
6479560d06aSjeremylt int CeedReference(Ceed ceed) {
64834359f16Sjeremylt   ceed->ref_count++;
64934359f16Sjeremylt   return CEED_ERROR_SUCCESS;
65034359f16Sjeremylt }
65134359f16Sjeremylt 
6527a982d89SJeremy L. Thompson /// @}
6537a982d89SJeremy L. Thompson 
6547a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6557a982d89SJeremy L. Thompson /// Ceed Public API
6567a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6577a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6587a982d89SJeremy L. Thompson /// @{
6597a982d89SJeremy L. Thompson 
6607a982d89SJeremy L. Thompson /**
66192ee7d1cSjeremylt   @brief Get the list of available resource names for Ceed contexts
6629ff86846Sjeremylt   Note: The caller is responsible for `free()`ing the resources and priorities arrays,
6639ff86846Sjeremylt           but should not `free()` the contents of the resources array.
66422e44211Sjeremylt 
66592ee7d1cSjeremylt   @param[out] n           Number of available resources
66692ee7d1cSjeremylt   @param[out] resources   List of available resource names
66722e44211Sjeremylt   @param[out] priorities  Resource name prioritization values, lower is better
66822e44211Sjeremylt 
66922e44211Sjeremylt   @return An error code: 0 - success, otherwise - failure
67022e44211Sjeremylt 
67122e44211Sjeremylt   @ref User
67222e44211Sjeremylt **/
67322e44211Sjeremylt // LCOV_EXCL_START
67422e44211Sjeremylt int CeedRegistryGetList(size_t *n, char ***const resources,
67522e44211Sjeremylt                         CeedInt **priorities) {
676d0c91ce9Sjeremylt   *n = 0;
6779ff86846Sjeremylt   *resources = malloc(num_backends * sizeof(**resources));
6789ff86846Sjeremylt   if (!resources)
6799ff86846Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6809ff86846Sjeremylt   if (priorities) {
6819ff86846Sjeremylt     *priorities = malloc(num_backends * sizeof(**priorities));
6829ff86846Sjeremylt     if (!priorities)
6839ff86846Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
6849ff86846Sjeremylt   }
68522e44211Sjeremylt   for (size_t i=0; i<num_backends; i++) {
686d0c91ce9Sjeremylt     // Only report compiled backends
687d0c91ce9Sjeremylt     if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) {
68822e44211Sjeremylt       *resources[i] = backends[i].prefix;
6899ff86846Sjeremylt       if (priorities) *priorities[i] = backends[i].priority;
690d0c91ce9Sjeremylt       *n += 1;
691d0c91ce9Sjeremylt     }
692d0c91ce9Sjeremylt   }
69378464608Sjeremylt   if (*n == 0)
69478464608Sjeremylt     // LCOV_EXCL_START
69578464608Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "No backends installed");
69678464608Sjeremylt   // LCOV_EXCL_STOP
697d0c91ce9Sjeremylt   *resources = realloc(*resources, *n * sizeof(**resources));
698d0c91ce9Sjeremylt   if (!resources)
699d0c91ce9Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
700d0c91ce9Sjeremylt   if (priorities) {
701d0c91ce9Sjeremylt     *priorities = realloc(*priorities, *n * sizeof(**priorities));
702d0c91ce9Sjeremylt     if (!priorities)
703d0c91ce9Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
70422e44211Sjeremylt   }
70522e44211Sjeremylt   return CEED_ERROR_SUCCESS;
70645f1e315Sjeremylt }
70722e44211Sjeremylt // LCOV_EXCL_STOP
70822e44211Sjeremylt 
70922e44211Sjeremylt /**
710d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
71122e44211Sjeremylt   Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self")
71222e44211Sjeremylt     will result in CeedInt printing the current libCEED version number
71392ee7d1cSjeremylt     and a list of current available backend resources to stderr.
714b11c1e72Sjeremylt 
715b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
716b11c1e72Sjeremylt   @param ceed      The library context
717b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
718b11c1e72Sjeremylt 
719b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
720dfdf5a53Sjeremylt 
7217a982d89SJeremy L. Thompson   @ref User
722b11c1e72Sjeremylt **/
723d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
724d7b241e6Sjeremylt   int ierr;
725f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX,
726d0c91ce9Sjeremylt          match_priority = CEED_MAX_BACKEND_PRIORITY, priority;
727d7b241e6Sjeremylt 
728fe2413ffSjeremylt   // Find matching backend
7291d102b48SJeremy L Thompson   if (!resource)
73013873f79Sjeremylt     // LCOV_EXCL_START
731e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided");
73213873f79Sjeremylt   // LCOV_EXCL_STOP
7331d013790SJed Brown   ierr = CeedRegisterAll(); CeedChk(ierr);
73413873f79Sjeremylt 
73522e44211Sjeremylt   // Check for help request
73622e44211Sjeremylt   const char *help_prefix = "help";
73722e44211Sjeremylt   size_t match_help;
73822e44211Sjeremylt   for (match_help=0; match_help<4
73922e44211Sjeremylt        && resource[match_help] == help_prefix[match_help]; match_help++) {}
74022e44211Sjeremylt   if (match_help == 4) {
74122e44211Sjeremylt     fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR,
74222e44211Sjeremylt             CEED_VERSION_MINOR, CEED_VERSION_PATCH,
74322e44211Sjeremylt             CEED_VERSION_RELEASE ? "" : "+development");
74492ee7d1cSjeremylt     fprintf(stderr, "Available backend resources:\n");
74522e44211Sjeremylt     for (size_t i=0; i<num_backends; i++) {
746d0c91ce9Sjeremylt       // Only report compiled backends
747d0c91ce9Sjeremylt       if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY)
74822e44211Sjeremylt         fprintf(stderr, "  %s\n", backends[i].prefix);
74922e44211Sjeremylt     }
75022e44211Sjeremylt     fflush(stderr);
75122e44211Sjeremylt     match_help = 5; // Delineating character expected
75222e44211Sjeremylt   } else {
75322e44211Sjeremylt     match_help = 0;
75422e44211Sjeremylt   }
75522e44211Sjeremylt 
7569c9a0587SLeila Ghaffari   // Find best match, computed as number of matching characters
7579c9a0587SLeila Ghaffari   //   from requested resource stem
7582bbc7fe8Sjeremylt   size_t stem_length;
75922e44211Sjeremylt   for (stem_length=0; resource[stem_length+match_help]
76022e44211Sjeremylt        && resource[stem_length+match_help] != ':'; stem_length++) {}
761d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
762d7b241e6Sjeremylt     size_t n;
763d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
76422e44211Sjeremylt     for (n=0; prefix[n] && prefix[n] == resource[n+match_help]; n++) {}
765d7b241e6Sjeremylt     priority = backends[i].priority;
766d1d35e2fSjeremylt     if (n > match_len || (n == match_len && match_priority > priority)) {
767d1d35e2fSjeremylt       match_len = n;
768d1d35e2fSjeremylt       match_priority = priority;
769f7e22acaSJeremy L Thompson       match_index = i;
770d7b241e6Sjeremylt     }
771d7b241e6Sjeremylt   }
7729c9a0587SLeila Ghaffari   // Using Levenshtein distance to find closest match
7739c9a0587SLeila Ghaffari   if (match_len <= 1 || match_len != stem_length) {
774203015caSLeila Ghaffari     // LCOV_EXCL_START
7759c9a0587SLeila Ghaffari     size_t lev_dis = UINT_MAX;
776f7e22acaSJeremy L Thompson     size_t lev_index = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY;
7779c9a0587SLeila Ghaffari     for (size_t i=0; i<num_backends; i++) {
7789c9a0587SLeila Ghaffari       const char *prefix = backends[i].prefix;
7799c9a0587SLeila Ghaffari       size_t prefix_length = strlen(backends[i].prefix);
7809c9a0587SLeila Ghaffari       size_t min_len = (prefix_length < stem_length) ? prefix_length : stem_length;
781092904ddSLeila Ghaffari       size_t column[min_len+1];
782092904ddSLeila Ghaffari       for (size_t j=0; j<=min_len; j++) column[j] = j;
7839c9a0587SLeila Ghaffari       for (size_t j=1; j<=min_len; j++) {
7849c9a0587SLeila Ghaffari         column[0] = j;
7859c9a0587SLeila Ghaffari         for (size_t k=1, last_diag=j-1; k<=min_len; k++) {
786092904ddSLeila Ghaffari           size_t old_diag = column[k];
7879c9a0587SLeila Ghaffari           size_t min_1 = (column[k] < column[k-1]) ? column[k]+1 : column[k-1]+1;
7889c9a0587SLeila Ghaffari           size_t min_2 = last_diag + (resource[k-1] == prefix[j-1] ? 0 : 1);
7899c9a0587SLeila Ghaffari           column[k] = (min_1 < min_2) ? min_1 : min_2;
7909c9a0587SLeila Ghaffari           last_diag = old_diag;
7919c9a0587SLeila Ghaffari         }
7929c9a0587SLeila Ghaffari       }
7939c9a0587SLeila Ghaffari       size_t n = column[min_len];
7949c9a0587SLeila Ghaffari       priority = backends[i].priority;
7959c9a0587SLeila Ghaffari       if (n < lev_dis || (n == lev_dis
7969c9a0587SLeila Ghaffari                           && lev_priority > priority)) {
7979c9a0587SLeila Ghaffari         lev_dis = n;
7989c9a0587SLeila Ghaffari         lev_priority = priority;
799f7e22acaSJeremy L Thompson         lev_index = i;
8009c9a0587SLeila Ghaffari       }
8019c9a0587SLeila Ghaffari     }
802f7e22acaSJeremy L Thompson     const char *prefix_lev = backends[lev_index].prefix;
8039c9a0587SLeila Ghaffari     size_t lev_length;
8049c9a0587SLeila Ghaffari     for (lev_length=0; prefix_lev[lev_length]
8059c9a0587SLeila Ghaffari          && prefix_lev[lev_length] != '\0'; lev_length++) {}
8069c9a0587SLeila Ghaffari     size_t m = (lev_length < stem_length) ? lev_length : stem_length;
8079c9a0587SLeila Ghaffari     if (lev_dis+1 >= m) {
808e15f9bd0SJeremy L Thompson       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s",
809e15f9bd0SJeremy L Thompson                        resource);
8109c9a0587SLeila Ghaffari     } else {
81187250337Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s\n"
812f7e22acaSJeremy L Thompson                        "Closest match: %s", resource, backends[lev_index].prefix);
8132bbc7fe8Sjeremylt     }
814203015caSLeila Ghaffari     // LCOV_EXCL_STOP
8159c9a0587SLeila Ghaffari   }
816fe2413ffSjeremylt 
817fe2413ffSjeremylt   // Setup Ceed
818d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
819bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
8201d102b48SJeremy L Thompson   if (!ceed_error_handler)
8211d102b48SJeremy L Thompson     ceed_error_handler = "abort";
822bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
82356e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
824477729cfSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store"))
825477729cfSJeremy L Thompson     (*ceed)->Error = CeedErrorStore;
82656e866f4SJed Brown   else
827d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
828d1d35e2fSjeremylt   memcpy((*ceed)->err_msg, "No error message stored", 24);
829d1d35e2fSjeremylt   (*ceed)->ref_count = 1;
830d7b241e6Sjeremylt   (*ceed)->data = NULL;
831fe2413ffSjeremylt 
832fe2413ffSjeremylt   // Set lookup table
833d1d35e2fSjeremylt   FOffset f_offsets[] = {
8346e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
8356e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
8366e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
837f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
8386e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
839fc0567d9Srezgarshakeri     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateOriented),
8406e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
8416e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
8426e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
84350c301a5SRezgar Shakeri     CEED_FTABLE_ENTRY(Ceed, BasisCreateHdiv),
8446e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
8456e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
846777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
8476e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
8486e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
8499c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, HasValidArray),
8509c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, HasBorrowedArrayOfType),
8516e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
8526a6c615bSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, TakeArray),
8536e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
8546e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
8556e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
8569c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, GetArrayWrite),
8576e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
8586e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
859547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
860e0dd3b27Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Scale),
8610f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, AXPY),
8620f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, PointwiseMult),
863d99fa3c5SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
8646e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Destroy),
8656e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
8666e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
867bd33150aSjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
8686e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
8696e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Apply),
8706e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Destroy),
8716e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
8726e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
8736e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Apply),
8748c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
8758c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
8766e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
8779c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, HasValidData),
8789c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, HasBorrowedDataOfType),
879777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
880891038deSjeremylt     CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData),
881777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
882*28bfd0b7SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetDataRead),
883777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
884*28bfd0b7SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreDataRead),
885777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
88680ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
88770a7ffb3SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate),
88880ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
8899e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
89080ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
8919e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
892e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic),
893e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble),
894713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
8956e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
896250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
897cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
898250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
8996e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
9006e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
9016e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
9021dfeef1dSjeremylt   };
903fe2413ffSjeremylt 
904d1d35e2fSjeremylt   ierr = CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets); CeedChk(ierr);
905d1d35e2fSjeremylt   memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets));
906fe2413ffSjeremylt 
9075107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
908e2f04181SAndrew T. Barker   const char fallbackresource[] = "";
9095107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
9105107b09fSJeremy L Thompson   CeedChk(ierr);
9115107b09fSJeremy L Thompson 
91260f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
9133f21f6b1SJeremy L Thompson   (*ceed)->is_debug = !!getenv("CEED_DEBUG") || !!getenv("DEBUG") ||
9143f21f6b1SJeremy L Thompson                       !!getenv("DBG");
91560f9e2d6SJeremy L Thompson 
916fe2413ffSjeremylt   // Backend specific setup
917f7e22acaSJeremy L Thompson   ierr = backends[match_index].init(&resource[match_help], *ceed); CeedChk(ierr);
918fe2413ffSjeremylt 
91922e44211Sjeremylt   // Copy resource prefix, if backend setup successful
920f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(backends[match_index].prefix,
921f7e22acaSJeremy L Thompson                              (char **)&(*ceed)->resource);
922f7e22acaSJeremy L Thompson   CeedChk(ierr);
923e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
924d7b241e6Sjeremylt }
925d7b241e6Sjeremylt 
926d7b241e6Sjeremylt /**
9279560d06aSjeremylt   @brief Copy the pointer to a Ceed context. Both pointers should
9289560d06aSjeremylt            be destroyed with `CeedDestroy()`;
9299560d06aSjeremylt            Note: If `*ceed_copy` is non-NULL, then it is assumed that
9309560d06aSjeremylt            `*ceed_copy` is a pointer to a Ceed context. This Ceed
9319560d06aSjeremylt            context will be destroyed if `*ceed_copy` is the only
9329560d06aSjeremylt            reference to this Ceed context.
9339560d06aSjeremylt 
9349560d06aSjeremylt   @param ceed            Ceed context to copy reference to
9359560d06aSjeremylt   @param[out] ceed_copy  Variable to store copied reference
9369560d06aSjeremylt 
9379560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
9389560d06aSjeremylt 
9399560d06aSjeremylt   @ref User
9409560d06aSjeremylt **/
9419560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) {
9429560d06aSjeremylt   int ierr;
9439560d06aSjeremylt 
9449560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
9459560d06aSjeremylt   ierr = CeedDestroy(ceed_copy); CeedChk(ierr);
9469560d06aSjeremylt   *ceed_copy = ceed;
9479560d06aSjeremylt   return CEED_ERROR_SUCCESS;
9489560d06aSjeremylt }
9499560d06aSjeremylt 
9509560d06aSjeremylt /**
9517a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
9522f86a920SJeremy L Thompson 
9537a982d89SJeremy L. Thompson   @param ceed           Ceed context to get resource name of
9547a982d89SJeremy L. Thompson   @param[out] resource  Variable to store resource name
9552f86a920SJeremy L Thompson 
9562f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9572f86a920SJeremy L Thompson 
9587a982d89SJeremy L. Thompson   @ref User
9595107b09fSJeremy L Thompson **/
9607a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
9617a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
962e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9635107b09fSJeremy L Thompson }
9645107b09fSJeremy L Thompson 
9655107b09fSJeremy L Thompson /**
966d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
967c907536fSjeremylt 
968d79b80ecSjeremylt   @param ceed           Ceed context to get preferred memory type of
969d1d35e2fSjeremylt   @param[out] mem_type  Address to save preferred memory type to
970c907536fSjeremylt 
971c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
972c907536fSjeremylt 
9737a982d89SJeremy L. Thompson   @ref User
974c907536fSjeremylt **/
975d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) {
976c907536fSjeremylt   int ierr;
977c263cd57Sjeremylt 
978c907536fSjeremylt   if (ceed->GetPreferredMemType) {
979d1d35e2fSjeremylt     ierr = ceed->GetPreferredMemType(mem_type); CeedChk(ierr);
980c907536fSjeremylt   } else {
981c263cd57Sjeremylt     Ceed delegate;
982c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
983c263cd57Sjeremylt 
984c263cd57Sjeremylt     if (delegate) {
985d1d35e2fSjeremylt       ierr = CeedGetPreferredMemType(delegate, mem_type); CeedChk(ierr);
986c263cd57Sjeremylt     } else {
987d1d35e2fSjeremylt       *mem_type = CEED_MEM_HOST;
988c907536fSjeremylt     }
989c263cd57Sjeremylt   }
990e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
991c907536fSjeremylt }
992c907536fSjeremylt 
993c907536fSjeremylt /**
9949525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
9959525855cSJeremy L Thompson 
9969525855cSJeremy L Thompson   @param[in] ceed               Ceed
997d1d35e2fSjeremylt   @param[out] is_deterministic  Variable to store deterministic status
9989525855cSJeremy L Thompson 
9999525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10009525855cSJeremy L Thompson 
10019525855cSJeremy L Thompson   @ref User
10029525855cSJeremy L Thompson **/
1003d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) {
1004d1d35e2fSjeremylt   *is_deterministic = ceed->is_deterministic;
1005e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10069525855cSJeremy L Thompson }
10079525855cSJeremy L Thompson 
10089525855cSJeremy L Thompson /**
10090a0da059Sjeremylt   @brief View a Ceed
10100a0da059Sjeremylt 
10110a0da059Sjeremylt   @param[in] ceed    Ceed to view
10120a0da059Sjeremylt   @param[in] stream  Filestream to write to
10130a0da059Sjeremylt 
10140a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
10150a0da059Sjeremylt 
10160a0da059Sjeremylt   @ref User
10170a0da059Sjeremylt **/
10180a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
10190a0da059Sjeremylt   int ierr;
1020d1d35e2fSjeremylt   CeedMemType mem_type;
10210a0da059Sjeremylt 
1022d1d35e2fSjeremylt   ierr = CeedGetPreferredMemType(ceed, &mem_type); CeedChk(ierr);
10230a0da059Sjeremylt 
10240a0da059Sjeremylt   fprintf(stream, "Ceed\n"
10250a0da059Sjeremylt           "  Ceed Resource: %s\n"
10260a0da059Sjeremylt           "  Preferred MemType: %s\n",
1027d1d35e2fSjeremylt           ceed->resource, CeedMemTypes[mem_type]);
1028e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10290a0da059Sjeremylt }
10300a0da059Sjeremylt 
10310a0da059Sjeremylt /**
1032b11c1e72Sjeremylt   @brief Destroy a Ceed context
1033d7b241e6Sjeremylt 
1034d7b241e6Sjeremylt   @param ceed  Address of Ceed context to destroy
1035b11c1e72Sjeremylt 
1036b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1037dfdf5a53Sjeremylt 
10387a982d89SJeremy L. Thompson   @ref User
1039b11c1e72Sjeremylt **/
1040d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
1041d7b241e6Sjeremylt   int ierr;
1042d1d35e2fSjeremylt   if (!*ceed || --(*ceed)->ref_count > 0) return CEED_ERROR_SUCCESS;
10435fe0d4faSjeremylt   if ((*ceed)->delegate) {
10445fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
10455fe0d4faSjeremylt   }
10460ace9bf2Sjeremylt 
1047d1d35e2fSjeremylt   if ((*ceed)->obj_delegate_count > 0) {
1048d1d35e2fSjeremylt     for (int i=0; i<(*ceed)->obj_delegate_count; i++) {
1049d1d35e2fSjeremylt       ierr = CeedDestroy(&((*ceed)->obj_delegates[i].delegate)); CeedChk(ierr);
1050d1d35e2fSjeremylt       ierr = CeedFree(&(*ceed)->obj_delegates[i].obj_name); CeedChk(ierr);
1051aefd8378Sjeremylt     }
1052d1d35e2fSjeremylt     ierr = CeedFree(&(*ceed)->obj_delegates); CeedChk(ierr);
1053aefd8378Sjeremylt   }
10540ace9bf2Sjeremylt 
1055d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
1056d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
1057d7b241e6Sjeremylt   }
10580ace9bf2Sjeremylt 
1059d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->f_offsets); CeedChk(ierr);
1060e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
1061d1d35e2fSjeremylt   ierr = CeedDestroy(&(*ceed)->op_fallback_ceed); CeedChk(ierr);
1062d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->op_fallback_resource); CeedChk(ierr);
1063d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
1064e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1065d7b241e6Sjeremylt }
1066d7b241e6Sjeremylt 
1067f9982c62SWill Pazner // LCOV_EXCL_START
1068f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
10693f4a9821SAndrew T. Barker   if (ceed->parent)
10703f4a9821SAndrew T. Barker     return CeedErrorFormat(ceed->parent, format, args);
1071d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1072d1d35e2fSjeremylt     return CeedErrorFormat(ceed->op_fallback_parent, format, args);
107378464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
107478464608Sjeremylt   vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args); // NOLINT
1075d1d35e2fSjeremylt   return ceed->err_msg;
1076f9982c62SWill Pazner }
1077f9982c62SWill Pazner // LCOV_EXCL_STOP
1078f9982c62SWill Pazner 
10797a982d89SJeremy L. Thompson /**
10807a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
10817a982d89SJeremy L. Thompson 
10827a982d89SJeremy L. Thompson   @ref Developer
10837a982d89SJeremy L. Thompson **/
10847a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
10857a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
10867a982d89SJeremy L. Thompson   va_list args;
1087d1d35e2fSjeremylt   int ret_val;
10887a982d89SJeremy L. Thompson   va_start(args, format);
10897a982d89SJeremy L. Thompson   if (ceed) {
1090d1d35e2fSjeremylt     ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
10917a982d89SJeremy L. Thompson   } else {
1092b0d62198Sjeremylt     // LCOV_EXCL_START
1093477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
1094477729cfSJeremy L Thompson     if (!ceed_error_handler)
1095477729cfSJeremy L Thompson       ceed_error_handler = "abort";
1096477729cfSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return"))
1097d1d35e2fSjeremylt       ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
1098477729cfSJeremy L Thompson     else
1099477729cfSJeremy L Thompson       // This function will not return
1100d1d35e2fSjeremylt       ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
11017a982d89SJeremy L. Thompson   }
11027a982d89SJeremy L. Thompson   va_end(args);
1103d1d35e2fSjeremylt   return ret_val;
1104b0d62198Sjeremylt   // LCOV_EXCL_STOP
11057a982d89SJeremy L. Thompson }
11067a982d89SJeremy L. Thompson 
1107477729cfSJeremy L Thompson /**
1108477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
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 CeedErrorReturn(Ceed ceed, const char *filename, int line_no,
1116d1d35e2fSjeremylt                     const char *func, int err_code, const char *format,
1117f9982c62SWill Pazner                     va_list *args) {
1118d1d35e2fSjeremylt   return err_code;
1119477729cfSJeremy L Thompson }
1120477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1121477729cfSJeremy L Thompson 
1122477729cfSJeremy L Thompson /**
1123477729cfSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns
1124477729cfSJeremy L Thompson            the error.
1125477729cfSJeremy L Thompson 
1126477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1127477729cfSJeremy L Thompson 
1128477729cfSJeremy L Thompson   @ref Developer
1129477729cfSJeremy L Thompson **/
1130477729cfSJeremy L Thompson // LCOV_EXCL_START
1131d1d35e2fSjeremylt int CeedErrorStore(Ceed ceed, const char *filename, int line_no,
1132d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1133f9982c62SWill Pazner                    va_list *args) {
1134477729cfSJeremy L Thompson   if (ceed->parent)
1135d1d35e2fSjeremylt     return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format,
1136187168c7SJeremy L Thompson                           args);
1137d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1138d1d35e2fSjeremylt     return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func,
1139d1d35e2fSjeremylt                           err_code, format, args);
1140477729cfSJeremy L Thompson 
1141477729cfSJeremy L Thompson   // Build message
1142477729cfSJeremy L Thompson   CeedInt len;
1143d1d35e2fSjeremylt   len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ",
1144d1d35e2fSjeremylt                  filename, line_no, func);
114578464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
114678464608Sjeremylt   // *INDENT-OFF*
114778464608Sjeremylt   vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args); // NOLINT
114878464608Sjeremylt   // *INDENT-ON*
1149d1d35e2fSjeremylt   return err_code;
1150477729cfSJeremy L Thompson }
1151477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1152477729cfSJeremy L Thompson 
1153477729cfSJeremy L Thompson /**
1154477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
1155477729cfSJeremy L Thompson 
1156477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1157477729cfSJeremy L Thompson 
1158477729cfSJeremy L Thompson   @ref Developer
1159477729cfSJeremy L Thompson **/
1160477729cfSJeremy L Thompson // LCOV_EXCL_START
1161d1d35e2fSjeremylt int CeedErrorAbort(Ceed ceed, const char *filename, int line_no,
1162d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1163f9982c62SWill Pazner                    va_list *args) {
1164d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
1165f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1166477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1167477729cfSJeremy L Thompson   abort();
1168d1d35e2fSjeremylt   return err_code;
1169477729cfSJeremy L Thompson }
1170477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1171477729cfSJeremy L Thompson 
1172477729cfSJeremy L Thompson /**
1173477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
1174477729cfSJeremy L Thompson 
1175477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1176477729cfSJeremy L Thompson 
1177477729cfSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit()
1178477729cfSJeremy L Thompson   handlers (e.g., as used by gcov) are run.
1179477729cfSJeremy L Thompson 
1180477729cfSJeremy L Thompson   @ref Developer
1181477729cfSJeremy L Thompson **/
1182d1d35e2fSjeremylt int CeedErrorExit(Ceed ceed, const char *filename, int line_no,
1183d1d35e2fSjeremylt                   const char *func, int err_code, const char *format, va_list *args) {
1184d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
118578464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
118678464608Sjeremylt   vfprintf(stderr, format, *args); // NOLINT
1187477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1188d1d35e2fSjeremylt   exit(err_code);
1189d1d35e2fSjeremylt   return err_code;
1190477729cfSJeremy L Thompson }
1191477729cfSJeremy L Thompson 
1192477729cfSJeremy L Thompson /**
1193477729cfSJeremy L Thompson   @brief Set error handler
1194477729cfSJeremy L Thompson 
1195477729cfSJeremy L Thompson   A default error handler is set in CeedInit().  Use this function to change
1196477729cfSJeremy L Thompson   the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined
1197477729cfSJeremy L Thompson   error handler.
1198477729cfSJeremy L Thompson 
1199477729cfSJeremy L Thompson   @ref Developer
1200477729cfSJeremy L Thompson **/
1201d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) {
1202d1d35e2fSjeremylt   ceed->Error = handler;
1203d1d35e2fSjeremylt   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler);
1204d1d35e2fSjeremylt   for (int i=0; i<ceed->obj_delegate_count; i++)
1205d1d35e2fSjeremylt     CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler);
1206e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1207477729cfSJeremy L Thompson }
1208477729cfSJeremy L Thompson 
1209477729cfSJeremy L Thompson /**
1210477729cfSJeremy L Thompson   @brief Get error message
1211477729cfSJeremy L Thompson 
1212477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1213477729cfSJeremy L Thompson     CeedErrorStore()
1214477729cfSJeremy L Thompson 
1215477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to retrieve error message
1216d1d35e2fSjeremylt   @param[out] err_msg  Char pointer to hold error message
1217477729cfSJeremy L Thompson 
1218477729cfSJeremy L Thompson   @ref Developer
1219477729cfSJeremy L Thompson **/
1220d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) {
12213f4a9821SAndrew T. Barker   if (ceed->parent)
1222d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->parent, err_msg);
1223d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1224d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg);
1225d1d35e2fSjeremylt   *err_msg = ceed->err_msg;
1226e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1227477729cfSJeremy L Thompson }
1228477729cfSJeremy L Thompson 
1229477729cfSJeremy L Thompson /**
1230477729cfSJeremy L Thompson   @brief Restore error message
1231477729cfSJeremy L Thompson 
1232477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1233477729cfSJeremy L Thompson     CeedErrorStore()
1234477729cfSJeremy L Thompson 
1235477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to restore error message
1236d1d35e2fSjeremylt   @param[out] err_msg  Char pointer that holds error message
1237477729cfSJeremy L Thompson 
1238477729cfSJeremy L Thompson   @ref Developer
1239477729cfSJeremy L Thompson **/
1240d1d35e2fSjeremylt int CeedResetErrorMessage(Ceed ceed, const char **err_msg) {
12413f4a9821SAndrew T. Barker   if (ceed->parent)
1242d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->parent, err_msg);
1243d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1244d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg);
1245d1d35e2fSjeremylt   *err_msg = NULL;
1246d1d35e2fSjeremylt   memcpy(ceed->err_msg, "No error message stored", 24);
1247e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1248477729cfSJeremy L Thompson }
1249477729cfSJeremy L Thompson 
12501070991dSJed Brown /**
12511070991dSJed Brown   @brief Get libCEED library version info
12521070991dSJed Brown 
12531070991dSJed Brown   libCEED version numbers have the form major.minor.patch. Non-release versions
12541070991dSJed Brown   may contain unstable interfaces.
12551070991dSJed Brown 
12561070991dSJed Brown   @param[out] major    Major version of the library
12571070991dSJed Brown   @param[out] minor    Minor version of the library
12581070991dSJed Brown   @param[out] patch    Patch (subminor) version of the library
12591070991dSJed Brown   @param[out] release  True for releases; false for development branches.
12601070991dSJed Brown 
12611070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
12621070991dSJed Brown 
12631070991dSJed Brown   @sa CEED_VERSION_GE()
12641070991dSJed Brown 
12651070991dSJed Brown   @ref Developer
12661070991dSJed Brown */
12671070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
12681070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
12691070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
12701070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
12711070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
12721070991dSJed Brown   return 0;
12731070991dSJed Brown }
12741070991dSJed Brown 
127580a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) {
127680a9ef05SNatalie Beams   *scalar_type = CEED_SCALAR_TYPE;
127780a9ef05SNatalie Beams   return 0;
127880a9ef05SNatalie Beams }
127980a9ef05SNatalie Beams 
128080a9ef05SNatalie Beams 
1281d7b241e6Sjeremylt /// @}
1282