xref: /libCEED/rust/libceed-sys/c-src/interface/ceed.c (revision 8687e1d445b8fb5c2aba1a76f10bd56e6cda067d)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
8d7b241e6Sjeremylt #define _POSIX_C_SOURCE 200112
9ec3da8bcSJed Brown #include <ceed/ceed.h>
10ec3da8bcSJed Brown #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <ceed-impl.h>
12aedaa0e5Sjeremylt #include <limits.h>
13d7b241e6Sjeremylt #include <stdarg.h>
146e79d475Sjeremylt #include <stddef.h>
15d7b241e6Sjeremylt #include <stdio.h>
16d7b241e6Sjeremylt #include <stdlib.h>
17d7b241e6Sjeremylt #include <string.h>
18d7b241e6Sjeremylt 
19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP
20d7b241e6Sjeremylt static CeedRequest ceed_request_immediate;
21d7b241e6Sjeremylt static CeedRequest ceed_request_ordered;
22d7b241e6Sjeremylt 
23d7b241e6Sjeremylt static struct {
24d7b241e6Sjeremylt   char prefix[CEED_MAX_RESOURCE_LEN];
25d7b241e6Sjeremylt   int (*init)(const char *resource, Ceed f);
26d7b241e6Sjeremylt   unsigned int priority;
27d7b241e6Sjeremylt } backends[32];
28d7b241e6Sjeremylt static size_t num_backends;
29fe2413ffSjeremylt 
306e79d475Sjeremylt #define CEED_FTABLE_ENTRY(class, method) \
316e79d475Sjeremylt   {#class #method, offsetof(struct class ##_private, method)}
32d7b241e6Sjeremylt /// @endcond
33d7b241e6Sjeremylt 
34d7b241e6Sjeremylt /// @file
35d7b241e6Sjeremylt /// Implementation of core components of Ceed library
367a982d89SJeremy L. Thompson 
377a982d89SJeremy L. Thompson /// @addtogroup CeedUser
38d7b241e6Sjeremylt /// @{
39d7b241e6Sjeremylt 
40dfdf5a53Sjeremylt /**
41dfdf5a53Sjeremylt   @brief Request immediate completion
42dfdf5a53Sjeremylt 
43dfdf5a53Sjeremylt   This predefined constant is passed as the \ref CeedRequest argument to
44dfdf5a53Sjeremylt   interfaces when the caller wishes for the operation to be performed
45dfdf5a53Sjeremylt   immediately.  The code
46dfdf5a53Sjeremylt 
47dfdf5a53Sjeremylt   @code
48dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE);
49dfdf5a53Sjeremylt   @endcode
50dfdf5a53Sjeremylt 
51dfdf5a53Sjeremylt   is semantically equivalent to
52dfdf5a53Sjeremylt 
53dfdf5a53Sjeremylt   @code
54dfdf5a53Sjeremylt     CeedRequest request;
55dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., &request);
56dfdf5a53Sjeremylt     CeedRequestWait(&request);
57dfdf5a53Sjeremylt   @endcode
58dfdf5a53Sjeremylt 
59dfdf5a53Sjeremylt   @sa CEED_REQUEST_ORDERED
60dfdf5a53Sjeremylt **/
61d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate;
62d7b241e6Sjeremylt 
63d7b241e6Sjeremylt /**
64b11c1e72Sjeremylt   @brief Request ordered completion
65d7b241e6Sjeremylt 
66d7b241e6Sjeremylt   This predefined constant is passed as the \ref CeedRequest argument to
67d7b241e6Sjeremylt   interfaces when the caller wishes for the operation to be completed in the
68d7b241e6Sjeremylt   order that it is submitted to the device.  It is typically used in a construct
69d7b241e6Sjeremylt   such as
70d7b241e6Sjeremylt 
71d7b241e6Sjeremylt   @code
72d7b241e6Sjeremylt     CeedRequest request;
73d7b241e6Sjeremylt     CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED);
74d7b241e6Sjeremylt     CeedOperatorApply(op2, ..., &request);
75d7b241e6Sjeremylt     // other optional work
768b2d6f4aSMatthew Knepley     CeedRequestWait(&request);
77d7b241e6Sjeremylt   @endcode
78d7b241e6Sjeremylt 
79d7b241e6Sjeremylt   which allows the sequence to complete asynchronously but does not start
80d7b241e6Sjeremylt   `op2` until `op1` has completed.
81d7b241e6Sjeremylt 
82288c0443SJeremy L Thompson   @todo The current implementation is overly strict, offering equivalent
834cc79fe7SJed Brown   semantics to @ref CEED_REQUEST_IMMEDIATE.
84d7b241e6Sjeremylt 
85d7b241e6Sjeremylt   @sa CEED_REQUEST_IMMEDIATE
86d7b241e6Sjeremylt  */
87d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered;
88d7b241e6Sjeremylt 
89b11c1e72Sjeremylt /**
907a982d89SJeremy L. Thompson   @brief Wait for a CeedRequest to complete.
91dfdf5a53Sjeremylt 
927a982d89SJeremy L. Thompson   Calling CeedRequestWait on a NULL request is a no-op.
937a982d89SJeremy L. Thompson 
947a982d89SJeremy L. Thompson   @param req Address of CeedRequest to wait for; zeroed on completion.
957a982d89SJeremy L. Thompson 
967a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
977a982d89SJeremy L. Thompson 
987a982d89SJeremy L. Thompson   @ref User
99b11c1e72Sjeremylt **/
1007a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) {
1017a982d89SJeremy L. Thompson   if (!*req)
102e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
103e15f9bd0SJeremy L Thompson   return CeedError(NULL, CEED_ERROR_UNSUPPORTED,
104e15f9bd0SJeremy L Thompson                    "CeedRequestWait not implemented");
105683faae0SJed Brown }
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson /// @}
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1107a982d89SJeremy L. Thompson /// Ceed Library Internal Functions
1117a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1127a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper
1137a982d89SJeremy L. Thompson /// @{
114d7b241e6Sjeremylt 
1156a406739SJeremy L Thompson /**
1166a406739SJeremy L Thompson   @brief Register a Ceed backend internally.
1176a406739SJeremy L Thompson            Note: Backends should call `CeedRegister` instead.
1186a406739SJeremy L Thompson 
1196a406739SJeremy L Thompson   @param prefix    Prefix of resources for this backend to respond to.  For
1206a406739SJeremy L Thompson                      example, the reference backend responds to "/cpu/self".
1216a406739SJeremy L Thompson   @param init      Initialization function called by CeedInit() when the backend
1226a406739SJeremy L Thompson                      is selected to drive the requested resource.
1236a406739SJeremy L Thompson   @param priority  Integer priority.  Lower values are preferred in case the
1246a406739SJeremy L Thompson                      resource requested by CeedInit() has non-unique best prefix
1256a406739SJeremy L Thompson                      match.
1266a406739SJeremy L Thompson 
1276a406739SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1286a406739SJeremy L Thompson 
1296a406739SJeremy L Thompson   @ref Developer
1306a406739SJeremy L Thompson **/
1316a406739SJeremy L Thompson int CeedRegisterImpl(const char *prefix, int (*init)(const char *, Ceed),
1326a406739SJeremy L Thompson                      unsigned int priority) {
1336a406739SJeremy L Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0]))
1346a406739SJeremy L Thompson     // LCOV_EXCL_START
1356a406739SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many backends");
1366a406739SJeremy L Thompson   // LCOV_EXCL_STOP
1376a406739SJeremy L Thompson 
1386a406739SJeremy L Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
1396a406739SJeremy L Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0;
1406a406739SJeremy L Thompson   backends[num_backends].init = init;
1416a406739SJeremy L Thompson   backends[num_backends].priority = priority;
1426a406739SJeremy L Thompson   num_backends++;
1436a406739SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1446a406739SJeremy L Thompson }
1456a406739SJeremy L Thompson 
1467a982d89SJeremy L. Thompson /// @}
147d7b241e6Sjeremylt 
1487a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1497a982d89SJeremy L. Thompson /// Ceed Backend API
1507a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1517a982d89SJeremy L. Thompson /// @addtogroup CeedBackend
1527a982d89SJeremy L. Thompson /// @{
153d7b241e6Sjeremylt 
154b11c1e72Sjeremylt /**
1553f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
15660f9e2d6SJeremy L Thompson 
15760f9e2d6SJeremy L Thompson   @param ceed    Ceed context
15860f9e2d6SJeremy L Thompson 
1593f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1603f21f6b1SJeremy L Thompson                          false - debugging mode disabled
16160f9e2d6SJeremy L Thompson 
16260f9e2d6SJeremy L Thompson   @ref Backend
16360f9e2d6SJeremy L Thompson **/
164fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
1653f21f6b1SJeremy L Thompson bool CeedDebugFlag(const Ceed ceed) {
1663f21f6b1SJeremy L Thompson   return ceed->is_debug;
16760f9e2d6SJeremy L Thompson }
168fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
16960f9e2d6SJeremy L Thompson 
17060f9e2d6SJeremy L Thompson /**
1713f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
17260f9e2d6SJeremy L Thompson 
1733f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1743f21f6b1SJeremy L Thompson                          false - debugging mode disabled
1753f21f6b1SJeremy L Thompson 
1763f21f6b1SJeremy L Thompson   @ref Backend
1773f21f6b1SJeremy L Thompson **/
1783f21f6b1SJeremy L Thompson // LCOV_EXCL_START
1793f21f6b1SJeremy L Thompson bool CeedDebugFlagEnv(void) {
1803f21f6b1SJeremy L Thompson   return !!getenv("CEED_DEBUG") || !!getenv("DEBUG") || !!getenv("DBG");
1813f21f6b1SJeremy L Thompson }
1823f21f6b1SJeremy L Thompson // LCOV_EXCL_STOP
1833f21f6b1SJeremy L Thompson 
1843f21f6b1SJeremy L Thompson /**
1853f21f6b1SJeremy L Thompson   @brief Print debugging information in color
1863f21f6b1SJeremy L Thompson 
18760f9e2d6SJeremy L Thompson   @param color   Color to print
18860f9e2d6SJeremy L Thompson   @param format  Printing format
18960f9e2d6SJeremy L Thompson 
19060f9e2d6SJeremy L Thompson   @ref Backend
19160f9e2d6SJeremy L Thompson **/
192fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
1933f21f6b1SJeremy L Thompson void CeedDebugImpl256(const unsigned char color, const char *format,...) {
19460f9e2d6SJeremy L Thompson   va_list args;
19560f9e2d6SJeremy L Thompson   va_start(args, format);
19660f9e2d6SJeremy L Thompson   fflush(stdout);
1973f21f6b1SJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE)
19860f9e2d6SJeremy L Thompson     fprintf(stdout, "\033[38;5;%dm", color);
19960f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
2003f21f6b1SJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE)
20160f9e2d6SJeremy L Thompson     fprintf(stdout, "\033[m");
20260f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
20360f9e2d6SJeremy L Thompson   fflush(stdout);
20460f9e2d6SJeremy L Thompson   va_end(args);
20560f9e2d6SJeremy L Thompson }
206fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
20760f9e2d6SJeremy L Thompson 
20860f9e2d6SJeremy L Thompson /**
209b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
210b11c1e72Sjeremylt 
211b11c1e72Sjeremylt   Memory usage can be tracked by the library.  This ensures sufficient
212b11c1e72Sjeremylt     alignment for vectorization and should be used for large allocations.
213b11c1e72Sjeremylt 
214b11c1e72Sjeremylt   @param n     Number of units to allocate
215b11c1e72Sjeremylt   @param unit  Size of each unit
216b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
217b11c1e72Sjeremylt 
218b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
219b11c1e72Sjeremylt 
220b11c1e72Sjeremylt   @sa CeedFree()
221dfdf5a53Sjeremylt 
2227a982d89SJeremy L. Thompson   @ref Backend
223b11c1e72Sjeremylt **/
224d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
225d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit);
226d7b241e6Sjeremylt   if (ierr)
227c042f62fSJeremy L Thompson     // LCOV_EXCL_START
228e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
229e15f9bd0SJeremy L Thompson                      "posix_memalign failed to allocate %zd "
2301d102b48SJeremy L Thompson                      "members of size %zd\n", n, unit);
231c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
232e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
233d7b241e6Sjeremylt }
234d7b241e6Sjeremylt 
235b11c1e72Sjeremylt /**
236b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
237b11c1e72Sjeremylt 
238b11c1e72Sjeremylt   Memory usage can be tracked by the library.
239b11c1e72Sjeremylt 
240b11c1e72Sjeremylt   @param n     Number of units to allocate
241b11c1e72Sjeremylt   @param unit  Size of each unit
242b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
243b11c1e72Sjeremylt 
244b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
245b11c1e72Sjeremylt 
246b11c1e72Sjeremylt   @sa CeedFree()
247dfdf5a53Sjeremylt 
2487a982d89SJeremy L. Thompson   @ref Backend
249b11c1e72Sjeremylt **/
250d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
251d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
252d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
253c042f62fSJeremy L Thompson     // LCOV_EXCL_START
254e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
255e15f9bd0SJeremy L Thompson                      "calloc failed to allocate %zd members of size "
2561d102b48SJeremy L Thompson                      "%zd\n", n, unit);
257c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
258e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
259d7b241e6Sjeremylt }
260d7b241e6Sjeremylt 
261b11c1e72Sjeremylt /**
262b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
263b11c1e72Sjeremylt 
264b11c1e72Sjeremylt   Memory usage can be tracked by the library.
265b11c1e72Sjeremylt 
266b11c1e72Sjeremylt   @param n     Number of units to allocate
267b11c1e72Sjeremylt   @param unit  Size of each unit
268b11c1e72Sjeremylt   @param p     Address of pointer to hold the result.
269b11c1e72Sjeremylt 
270b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
271b11c1e72Sjeremylt 
272b11c1e72Sjeremylt   @sa CeedFree()
273dfdf5a53Sjeremylt 
2747a982d89SJeremy L. Thompson   @ref Backend
275b11c1e72Sjeremylt **/
276d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
277d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n*unit);
278d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
279c042f62fSJeremy L Thompson     // LCOV_EXCL_START
280e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
281e15f9bd0SJeremy L Thompson                      "realloc failed to allocate %zd members of size "
2821d102b48SJeremy L Thompson                      "%zd\n", n, unit);
283c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
284e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
285d7b241e6Sjeremylt }
286d7b241e6Sjeremylt 
287f7e22acaSJeremy L Thompson /**
288f7e22acaSJeremy L Thompson   @brief Allocate a cleared string buffer on the host
289f7e22acaSJeremy L Thompson 
290f7e22acaSJeremy L Thompson   Memory usage can be tracked by the library.
291f7e22acaSJeremy L Thompson 
292f7e22acaSJeremy L Thompson   @param source Pointer to string to be copied
293f7e22acaSJeremy L Thompson   @param copy   Pointer to variable to hold newly allocated string copy
294f7e22acaSJeremy L Thompson 
295f7e22acaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
296f7e22acaSJeremy L Thompson 
297f7e22acaSJeremy L Thompson   @sa CeedFree()
298f7e22acaSJeremy L Thompson 
299f7e22acaSJeremy L Thompson   @ref Backend
300f7e22acaSJeremy L Thompson **/
301f7e22acaSJeremy L Thompson int CeedStringAllocCopy(const char *source, char **copy) {
302f7e22acaSJeremy L Thompson   int ierr;
303f7e22acaSJeremy L Thompson   size_t len = strlen(source);
304f7e22acaSJeremy L Thompson   ierr = CeedCalloc(len + 1, copy); CeedChk(ierr);
305d602d780SJeremy L Thompson   memcpy(*copy, source, len);
306f7e22acaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
307f7e22acaSJeremy L Thompson }
308f7e22acaSJeremy L Thompson 
30934138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
31034138859Sjeremylt 
31134138859Sjeremylt   @param p  address of pointer to memory.  This argument is of type void* to
31234138859Sjeremylt               avoid needing a cast, but is the address of the pointer (which is
31334138859Sjeremylt               zeroed) rather than the pointer.
31434138859Sjeremylt **/
315d7b241e6Sjeremylt int CeedFree(void *p) {
316d7b241e6Sjeremylt   free(*(void **)p);
317d7b241e6Sjeremylt   *(void **)p = NULL;
318e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
319d7b241e6Sjeremylt }
320d7b241e6Sjeremylt 
321d7b241e6Sjeremylt /**
3227a982d89SJeremy L. Thompson   @brief Register a Ceed backend
323d7b241e6Sjeremylt 
3247a982d89SJeremy L. Thompson   @param prefix    Prefix of resources for this backend to respond to.  For
3257a982d89SJeremy L. Thompson                      example, the reference backend responds to "/cpu/self".
3267a982d89SJeremy L. Thompson   @param init      Initialization function called by CeedInit() when the backend
3277a982d89SJeremy L. Thompson                      is selected to drive the requested resource.
3287a982d89SJeremy L. Thompson   @param priority  Integer priority.  Lower values are preferred in case the
3297a982d89SJeremy L. Thompson                      resource requested by CeedInit() has non-unique best prefix
3307a982d89SJeremy L. Thompson                      match.
331b11c1e72Sjeremylt 
332b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
333dfdf5a53Sjeremylt 
3347a982d89SJeremy L. Thompson   @ref Backend
335b11c1e72Sjeremylt **/
3367a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed),
3377a982d89SJeremy L. Thompson                  unsigned int priority) {
33810243053SJeremy L Thompson   CeedDebugEnv("Backend Register: %s", prefix);
3396a406739SJeremy L Thompson   CeedRegisterImpl(prefix, init, priority);
340e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
341d7b241e6Sjeremylt }
342d7b241e6Sjeremylt 
343b11c1e72Sjeremylt /**
34460f9e2d6SJeremy L Thompson   @brief Return debugging status flag
34560f9e2d6SJeremy L Thompson 
34660f9e2d6SJeremy L Thompson   @param ceed      Ceed context to get debugging flag
347d1d35e2fSjeremylt   @param is_debug  Variable to store debugging flag
34860f9e2d6SJeremy L Thompson 
34960f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
35060f9e2d6SJeremy L Thompson 
351d1d35e2fSjeremylt   @ref Backend
35260f9e2d6SJeremy L Thompson **/
353d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) {
3543f21f6b1SJeremy L Thompson   *is_debug = ceed->is_debug;
355e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
35660f9e2d6SJeremy L Thompson }
35760f9e2d6SJeremy L Thompson 
35860f9e2d6SJeremy L Thompson /**
3597a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3607a982d89SJeremy L. Thompson 
3617a982d89SJeremy L. Thompson   @param ceed         Ceed context to retrieve parent of
3627a982d89SJeremy L. Thompson   @param[out] parent  Address to save the parent to
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3657a982d89SJeremy L. Thompson 
3667a982d89SJeremy L. Thompson   @ref Backend
3677a982d89SJeremy L. Thompson **/
3687a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3697a982d89SJeremy L. Thompson   int ierr;
3707a982d89SJeremy L. Thompson   if (ceed->parent) {
3717a982d89SJeremy L. Thompson     ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr);
372e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
3737a982d89SJeremy L. Thompson   }
3747a982d89SJeremy L. Thompson   *parent = ceed;
375e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3767a982d89SJeremy L. Thompson }
3777a982d89SJeremy L. Thompson 
3787a982d89SJeremy L. Thompson /**
3797a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3807a982d89SJeremy L. Thompson 
3817a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3827a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson   @ref Backend
3877a982d89SJeremy L. Thompson **/
3887a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3897a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
390e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3917a982d89SJeremy L. Thompson }
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson /**
3947a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
3957a982d89SJeremy L. Thompson 
3967a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context. All
3977a982d89SJeremy L. Thompson     backend implementations default to the delegate Ceed context, unless
3987a982d89SJeremy L. Thompson     overridden.
3997a982d89SJeremy L. Thompson 
4007a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
4017a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
4027a982d89SJeremy L. Thompson 
4037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4047a982d89SJeremy L. Thompson 
4057a982d89SJeremy L. Thompson   @ref Backend
4067a982d89SJeremy L. Thompson **/
4077a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
4087a982d89SJeremy L. Thompson   ceed->delegate = delegate;
4097a982d89SJeremy L. Thompson   delegate->parent = ceed;
410e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4117a982d89SJeremy L. Thompson }
4127a982d89SJeremy L. Thompson 
4137a982d89SJeremy L. Thompson /**
4147a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
4177a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
418d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to retrieve delegate for
4197a982d89SJeremy L. Thompson 
4207a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4217a982d89SJeremy L. Thompson 
4227a982d89SJeremy L. Thompson   @ref Backend
4237a982d89SJeremy L. Thompson **/
424d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) {
4257a982d89SJeremy L. Thompson   CeedInt ierr;
4267a982d89SJeremy L. Thompson 
4277a982d89SJeremy L. Thompson   // Check for object delegate
428d1d35e2fSjeremylt   for (CeedInt i=0; i<ceed->obj_delegate_count; i++)
429d1d35e2fSjeremylt     if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) {
430d1d35e2fSjeremylt       *delegate = ceed->obj_delegates->delegate;
431e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
4327a982d89SJeremy L. Thompson     }
4337a982d89SJeremy L. Thompson 
4347a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
4357a982d89SJeremy L. Thompson   ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr);
436e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4377a982d89SJeremy L. Thompson }
4387a982d89SJeremy L. Thompson 
4397a982d89SJeremy L. Thompson /**
4407a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
4417a982d89SJeremy L. Thompson 
4427a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context for a
4437a982d89SJeremy L. Thompson     given type of Ceed object. All backend implementations default to the
4447a982d89SJeremy L. Thompson     delegate Ceed context for this object. For example,
4457a982d89SJeremy L. Thompson     CeedSetObjectDelegate(ceed, refceed, "Basis")
4467a982d89SJeremy L. Thompson   uses refceed implementations for all CeedBasis backend functions.
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
4497a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
450d1d35e2fSjeremylt   @param[in] obj_name   Name of the object type to set delegate for
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson   @ref Backend
4557a982d89SJeremy L. Thompson **/
456d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) {
457f7e22acaSJeremy L Thompson   int ierr;
458d1d35e2fSjeremylt   CeedInt count = ceed->obj_delegate_count;
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson   // Malloc or Realloc
4617a982d89SJeremy L. Thompson   if (count) {
462d1d35e2fSjeremylt     ierr = CeedRealloc(count+1, &ceed->obj_delegates); CeedChk(ierr);
4637a982d89SJeremy L. Thompson   } else {
464d1d35e2fSjeremylt     ierr = CeedCalloc(1, &ceed->obj_delegates); CeedChk(ierr);
4657a982d89SJeremy L. Thompson   }
466d1d35e2fSjeremylt   ceed->obj_delegate_count++;
4677a982d89SJeremy L. Thompson 
4687a982d89SJeremy L. Thompson   // Set object delegate
469d1d35e2fSjeremylt   ceed->obj_delegates[count].delegate = delegate;
470f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(obj_name, &ceed->obj_delegates[count].obj_name);
471f7e22acaSJeremy L Thompson   CeedChk(ierr);
4727a982d89SJeremy L. Thompson 
4737a982d89SJeremy L. Thompson   // Set delegate parent
4747a982d89SJeremy L. Thompson   delegate->parent = ceed;
475e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4767a982d89SJeremy L. Thompson }
4777a982d89SJeremy L. Thompson 
4787a982d89SJeremy L. Thompson /**
4797a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4807a982d89SJeremy L. Thompson 
4817a982d89SJeremy L. Thompson   @param ceed           Ceed context
4827a982d89SJeremy L. Thompson   @param[out] resource  Variable to store fallback resource
4837a982d89SJeremy L. Thompson 
4847a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4857a982d89SJeremy L. Thompson 
4867a982d89SJeremy L. Thompson   @ref Backend
4877a982d89SJeremy L. Thompson **/
4887a982d89SJeremy L. Thompson 
4897a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
490d1d35e2fSjeremylt   *resource = (const char *)ceed->op_fallback_resource;
491e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4927a982d89SJeremy L. Thompson }
4937a982d89SJeremy L. Thompson 
4947a982d89SJeremy L. Thompson /**
495*8687e1d4SJeremy L Thompson   @brief Get the fallback Ceed for CeedOperators
496*8687e1d4SJeremy L Thompson 
497*8687e1d4SJeremy L Thompson   @param ceed                Ceed context
498*8687e1d4SJeremy L Thompson   @param[out] fallback_ceed  Variable to store fallback Ceed
499*8687e1d4SJeremy L Thompson 
500*8687e1d4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
501*8687e1d4SJeremy L Thompson 
502*8687e1d4SJeremy L Thompson   @ref Backend
503*8687e1d4SJeremy L Thompson **/
504*8687e1d4SJeremy L Thompson 
505*8687e1d4SJeremy L Thompson int CeedGetOperatorFallbackCeed(Ceed ceed, Ceed *fallback_ceed) {
506*8687e1d4SJeremy L Thompson   int ierr;
507*8687e1d4SJeremy L Thompson 
508*8687e1d4SJeremy L Thompson   // Create fallback Ceed if uninitalized
509*8687e1d4SJeremy L Thompson   if (!ceed->op_fallback_ceed) {
510*8687e1d4SJeremy L Thompson     // Check resource
511*8687e1d4SJeremy L Thompson     const char *resource, *fallback_resource;
512*8687e1d4SJeremy L Thompson     ierr = CeedGetResource(ceed, &resource); CeedChk(ierr);
513*8687e1d4SJeremy L Thompson     ierr = CeedGetOperatorFallbackResource(ceed, &fallback_resource); CeedChk(ierr);
514*8687e1d4SJeremy L Thompson     if (!strcmp(resource, fallback_resource))
515*8687e1d4SJeremy L Thompson       // LCOV_EXCL_START
516*8687e1d4SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
517*8687e1d4SJeremy L Thompson                        "Backend %s cannot create an operator"
518*8687e1d4SJeremy L Thompson                        "fallback to resource %s", resource, fallback_resource);
519*8687e1d4SJeremy L Thompson       // LCOV_EXCL_STOP
520*8687e1d4SJeremy L Thompson 
521*8687e1d4SJeremy L Thompson     // Create fallback
522*8687e1d4SJeremy L Thompson     Ceed fallback_ceed;
523*8687e1d4SJeremy L Thompson     ierr = CeedInit(fallback_resource, &fallback_ceed); CeedChk(ierr);
524*8687e1d4SJeremy L Thompson     fallback_ceed->op_fallback_parent = ceed;
525*8687e1d4SJeremy L Thompson     fallback_ceed->Error = ceed->Error;
526*8687e1d4SJeremy L Thompson     ceed->op_fallback_ceed = fallback_ceed;
527*8687e1d4SJeremy L Thompson   }
528*8687e1d4SJeremy L Thompson   *fallback_ceed = ceed->op_fallback_ceed;
529*8687e1d4SJeremy L Thompson 
530*8687e1d4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
531*8687e1d4SJeremy L Thompson }
532*8687e1d4SJeremy L Thompson 
533*8687e1d4SJeremy L Thompson /**
5347a982d89SJeremy L. Thompson   @brief Set the fallback resource for CeedOperators. The current resource, if
5357a982d89SJeremy L. Thompson            any, is freed by calling this function. This string is freed upon the
5367a982d89SJeremy L. Thompson            destruction of the Ceed context.
5377a982d89SJeremy L. Thompson 
5387a982d89SJeremy L. Thompson   @param[out] ceed Ceed context
5397a982d89SJeremy L. Thompson   @param resource  Fallback resource to set
5407a982d89SJeremy L. Thompson 
5417a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5427a982d89SJeremy L. Thompson 
5437a982d89SJeremy L. Thompson   @ref Backend
5447a982d89SJeremy L. Thompson **/
5457a982d89SJeremy L. Thompson 
5467a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
5477a982d89SJeremy L. Thompson   int ierr;
5487a982d89SJeremy L. Thompson 
5497a982d89SJeremy L. Thompson   // Free old
550d1d35e2fSjeremylt   ierr = CeedFree(&ceed->op_fallback_resource); CeedChk(ierr);
5517a982d89SJeremy L. Thompson 
5527a982d89SJeremy L. Thompson   // Set new
553f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(resource, (char **)&ceed->op_fallback_resource);
554f7e22acaSJeremy L Thompson   CeedChk(ierr);
555e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5567a982d89SJeremy L. Thompson }
5577a982d89SJeremy L. Thompson 
5587a982d89SJeremy L. Thompson /**
5597a982d89SJeremy L. Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context
5607a982d89SJeremy L. Thompson            for a CeedOperator
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson   @param ceed         Ceed context
5637a982d89SJeremy L. Thompson   @param[out] parent  Variable to store parent Ceed context
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 **/
5697a982d89SJeremy L. Thompson 
5707a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
571d1d35e2fSjeremylt   *parent = ceed->op_fallback_parent;
572e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5737a982d89SJeremy L. Thompson }
5747a982d89SJeremy L. Thompson 
5757a982d89SJeremy L. Thompson /**
5769525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
5779525855cSJeremy L Thompson 
5789525855cSJeremy L Thompson   @param ceed                   Ceed to flag as deterministic
57996b902e2Sjeremylt   @param[out] is_deterministic  Deterministic status to set
5809525855cSJeremy L Thompson 
5819525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5829525855cSJeremy L Thompson 
5839525855cSJeremy L Thompson   @ref Backend
5849525855cSJeremy L Thompson **/
5859525855cSJeremy L Thompson 
586d1d35e2fSjeremylt int CeedSetDeterministic(Ceed ceed, bool is_deterministic) {
587d1d35e2fSjeremylt   ceed->is_deterministic = is_deterministic;
588e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5899525855cSJeremy L Thompson }
5909525855cSJeremy L Thompson 
5919525855cSJeremy L Thompson /**
5927a982d89SJeremy L. Thompson   @brief Set a backend function
5937a982d89SJeremy L. Thompson 
5947a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5957a982d89SJeremy L. Thompson   the Ceed objects. For example,
5967a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5977a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5987a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5997a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
6007a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
6017a982d89SJeremy L. Thompson 
6027a982d89SJeremy L. Thompson   @param ceed         Ceed context for error handling
6037a982d89SJeremy L. Thompson   @param type         Type of Ceed object to set function for
6047a982d89SJeremy L. Thompson   @param[out] object  Ceed object to set function for
605d1d35e2fSjeremylt   @param func_name    Name of function to set
6067a982d89SJeremy L. Thompson   @param f            Function to set
6077a982d89SJeremy L. Thompson 
6087a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6097a982d89SJeremy L. Thompson 
6107a982d89SJeremy L. Thompson   @ref Backend
6117a982d89SJeremy L. Thompson **/
6127a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
613d1d35e2fSjeremylt                            const char *func_name, int (*f)()) {
614d1d35e2fSjeremylt   char lookup_name[CEED_MAX_RESOURCE_LEN+1] = "";
6157a982d89SJeremy L. Thompson 
6167a982d89SJeremy L. Thompson   // Build lookup name
6177a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
618d1d35e2fSjeremylt     strncat (lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN);
619d1d35e2fSjeremylt   strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN);
620d1d35e2fSjeremylt   strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN);
6217a982d89SJeremy L. Thompson 
6227a982d89SJeremy L. Thompson   // Find and use offset
623d1d35e2fSjeremylt   for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++)
624d1d35e2fSjeremylt     if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) {
625d1d35e2fSjeremylt       size_t offset = ceed->f_offsets[i].offset;
6267a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
6277a982d89SJeremy L. Thompson       *fpointer = f;
628e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
6297a982d89SJeremy L. Thompson     }
6307a982d89SJeremy L. Thompson 
6317a982d89SJeremy L. Thompson   // LCOV_EXCL_START
632e15f9bd0SJeremy L Thompson   return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
633e15f9bd0SJeremy L Thompson                    "Requested function '%s' was not found for CEED "
634d1d35e2fSjeremylt                    "object '%s'", func_name, type);
6357a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
6367a982d89SJeremy L. Thompson }
6377a982d89SJeremy L. Thompson 
6387a982d89SJeremy L. Thompson /**
6397a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
6407a982d89SJeremy L. Thompson 
6417a982d89SJeremy L. Thompson   @param ceed       Ceed context to retrieve data of
6427a982d89SJeremy L. Thompson   @param[out] data  Address to save data to
6437a982d89SJeremy L. Thompson 
6447a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6457a982d89SJeremy L. Thompson 
6467a982d89SJeremy L. Thompson   @ref Backend
6477a982d89SJeremy L. Thompson **/
648777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
649777ff853SJeremy L Thompson   *(void **)data = ceed->data;
650e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6517a982d89SJeremy L. Thompson }
6527a982d89SJeremy L. Thompson 
6537a982d89SJeremy L. Thompson /**
6547a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
6557a982d89SJeremy L. Thompson 
6567a982d89SJeremy L. Thompson   @param ceed  Ceed context to set data of
6577a982d89SJeremy L. Thompson   @param data  Address of data to set
6587a982d89SJeremy L. Thompson 
6597a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6607a982d89SJeremy L. Thompson 
6617a982d89SJeremy L. Thompson   @ref Backend
6627a982d89SJeremy L. Thompson **/
663777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
664777ff853SJeremy L Thompson   ceed->data = data;
665e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6667a982d89SJeremy L. Thompson }
6677a982d89SJeremy L. Thompson 
66834359f16Sjeremylt /**
66934359f16Sjeremylt   @brief Increment the reference counter for a Ceed context
67034359f16Sjeremylt 
67134359f16Sjeremylt   @param ceed  Ceed context to increment the reference counter
67234359f16Sjeremylt 
67334359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
67434359f16Sjeremylt 
67534359f16Sjeremylt   @ref Backend
67634359f16Sjeremylt **/
6779560d06aSjeremylt int CeedReference(Ceed ceed) {
67834359f16Sjeremylt   ceed->ref_count++;
67934359f16Sjeremylt   return CEED_ERROR_SUCCESS;
68034359f16Sjeremylt }
68134359f16Sjeremylt 
6827a982d89SJeremy L. Thompson /// @}
6837a982d89SJeremy L. Thompson 
6847a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6857a982d89SJeremy L. Thompson /// Ceed Public API
6867a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6877a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6887a982d89SJeremy L. Thompson /// @{
6897a982d89SJeremy L. Thompson 
6907a982d89SJeremy L. Thompson /**
69192ee7d1cSjeremylt   @brief Get the list of available resource names for Ceed contexts
6929ff86846Sjeremylt   Note: The caller is responsible for `free()`ing the resources and priorities arrays,
6939ff86846Sjeremylt           but should not `free()` the contents of the resources array.
69422e44211Sjeremylt 
69592ee7d1cSjeremylt   @param[out] n           Number of available resources
69692ee7d1cSjeremylt   @param[out] resources   List of available resource names
69722e44211Sjeremylt   @param[out] priorities  Resource name prioritization values, lower is better
69822e44211Sjeremylt 
69922e44211Sjeremylt   @return An error code: 0 - success, otherwise - failure
70022e44211Sjeremylt 
70122e44211Sjeremylt   @ref User
70222e44211Sjeremylt **/
70322e44211Sjeremylt // LCOV_EXCL_START
70422e44211Sjeremylt int CeedRegistryGetList(size_t *n, char ***const resources,
70522e44211Sjeremylt                         CeedInt **priorities) {
706d0c91ce9Sjeremylt   *n = 0;
7079ff86846Sjeremylt   *resources = malloc(num_backends * sizeof(**resources));
7089ff86846Sjeremylt   if (!resources)
7099ff86846Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
7109ff86846Sjeremylt   if (priorities) {
7119ff86846Sjeremylt     *priorities = malloc(num_backends * sizeof(**priorities));
7129ff86846Sjeremylt     if (!priorities)
7139ff86846Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "malloc() failure");
7149ff86846Sjeremylt   }
71522e44211Sjeremylt   for (size_t i=0; i<num_backends; i++) {
716d0c91ce9Sjeremylt     // Only report compiled backends
717d0c91ce9Sjeremylt     if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) {
71822e44211Sjeremylt       *resources[i] = backends[i].prefix;
7199ff86846Sjeremylt       if (priorities) *priorities[i] = backends[i].priority;
720d0c91ce9Sjeremylt       *n += 1;
721d0c91ce9Sjeremylt     }
722d0c91ce9Sjeremylt   }
72378464608Sjeremylt   if (*n == 0)
72478464608Sjeremylt     // LCOV_EXCL_START
72578464608Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "No backends installed");
72678464608Sjeremylt   // LCOV_EXCL_STOP
727d0c91ce9Sjeremylt   *resources = realloc(*resources, *n * sizeof(**resources));
728d0c91ce9Sjeremylt   if (!resources)
729d0c91ce9Sjeremylt     return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
730d0c91ce9Sjeremylt   if (priorities) {
731d0c91ce9Sjeremylt     *priorities = realloc(*priorities, *n * sizeof(**priorities));
732d0c91ce9Sjeremylt     if (!priorities)
733d0c91ce9Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "realloc() failure");
73422e44211Sjeremylt   }
73522e44211Sjeremylt   return CEED_ERROR_SUCCESS;
73645f1e315Sjeremylt }
73722e44211Sjeremylt // LCOV_EXCL_STOP
73822e44211Sjeremylt 
73922e44211Sjeremylt /**
740d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
74122e44211Sjeremylt   Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self")
74222e44211Sjeremylt     will result in CeedInt printing the current libCEED version number
74392ee7d1cSjeremylt     and a list of current available backend resources to stderr.
744b11c1e72Sjeremylt 
745b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
746b11c1e72Sjeremylt   @param ceed      The library context
747b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
748b11c1e72Sjeremylt 
749b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
750dfdf5a53Sjeremylt 
7517a982d89SJeremy L. Thompson   @ref User
752b11c1e72Sjeremylt **/
753d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
754d7b241e6Sjeremylt   int ierr;
755f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX,
756d0c91ce9Sjeremylt          match_priority = CEED_MAX_BACKEND_PRIORITY, priority;
757d7b241e6Sjeremylt 
758fe2413ffSjeremylt   // Find matching backend
7591d102b48SJeremy L Thompson   if (!resource)
76013873f79Sjeremylt     // LCOV_EXCL_START
761e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "No resource provided");
76213873f79Sjeremylt   // LCOV_EXCL_STOP
7631d013790SJed Brown   ierr = CeedRegisterAll(); CeedChk(ierr);
76413873f79Sjeremylt 
76522e44211Sjeremylt   // Check for help request
76622e44211Sjeremylt   const char *help_prefix = "help";
76722e44211Sjeremylt   size_t match_help;
76822e44211Sjeremylt   for (match_help=0; match_help<4
76922e44211Sjeremylt        && resource[match_help] == help_prefix[match_help]; match_help++) {}
77022e44211Sjeremylt   if (match_help == 4) {
77122e44211Sjeremylt     fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR,
77222e44211Sjeremylt             CEED_VERSION_MINOR, CEED_VERSION_PATCH,
77322e44211Sjeremylt             CEED_VERSION_RELEASE ? "" : "+development");
77492ee7d1cSjeremylt     fprintf(stderr, "Available backend resources:\n");
77522e44211Sjeremylt     for (size_t i=0; i<num_backends; i++) {
776d0c91ce9Sjeremylt       // Only report compiled backends
777d0c91ce9Sjeremylt       if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY)
77822e44211Sjeremylt         fprintf(stderr, "  %s\n", backends[i].prefix);
77922e44211Sjeremylt     }
78022e44211Sjeremylt     fflush(stderr);
78122e44211Sjeremylt     match_help = 5; // Delineating character expected
78222e44211Sjeremylt   } else {
78322e44211Sjeremylt     match_help = 0;
78422e44211Sjeremylt   }
78522e44211Sjeremylt 
7869c9a0587SLeila Ghaffari   // Find best match, computed as number of matching characters
7879c9a0587SLeila Ghaffari   //   from requested resource stem
7882bbc7fe8Sjeremylt   size_t stem_length;
78922e44211Sjeremylt   for (stem_length=0; resource[stem_length+match_help]
79022e44211Sjeremylt        && resource[stem_length+match_help] != ':'; stem_length++) {}
791d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
792d7b241e6Sjeremylt     size_t n;
793d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
79422e44211Sjeremylt     for (n=0; prefix[n] && prefix[n] == resource[n+match_help]; n++) {}
795d7b241e6Sjeremylt     priority = backends[i].priority;
796d1d35e2fSjeremylt     if (n > match_len || (n == match_len && match_priority > priority)) {
797d1d35e2fSjeremylt       match_len = n;
798d1d35e2fSjeremylt       match_priority = priority;
799f7e22acaSJeremy L Thompson       match_index = i;
800d7b241e6Sjeremylt     }
801d7b241e6Sjeremylt   }
8029c9a0587SLeila Ghaffari   // Using Levenshtein distance to find closest match
8039c9a0587SLeila Ghaffari   if (match_len <= 1 || match_len != stem_length) {
804203015caSLeila Ghaffari     // LCOV_EXCL_START
8059c9a0587SLeila Ghaffari     size_t lev_dis = UINT_MAX;
806f7e22acaSJeremy L Thompson     size_t lev_index = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY;
8079c9a0587SLeila Ghaffari     for (size_t i=0; i<num_backends; i++) {
8089c9a0587SLeila Ghaffari       const char *prefix = backends[i].prefix;
8099c9a0587SLeila Ghaffari       size_t prefix_length = strlen(backends[i].prefix);
8109c9a0587SLeila Ghaffari       size_t min_len = (prefix_length < stem_length) ? prefix_length : stem_length;
811092904ddSLeila Ghaffari       size_t column[min_len+1];
812092904ddSLeila Ghaffari       for (size_t j=0; j<=min_len; j++) column[j] = j;
8139c9a0587SLeila Ghaffari       for (size_t j=1; j<=min_len; j++) {
8149c9a0587SLeila Ghaffari         column[0] = j;
8159c9a0587SLeila Ghaffari         for (size_t k=1, last_diag=j-1; k<=min_len; k++) {
816092904ddSLeila Ghaffari           size_t old_diag = column[k];
8179c9a0587SLeila Ghaffari           size_t min_1 = (column[k] < column[k-1]) ? column[k]+1 : column[k-1]+1;
8189c9a0587SLeila Ghaffari           size_t min_2 = last_diag + (resource[k-1] == prefix[j-1] ? 0 : 1);
8199c9a0587SLeila Ghaffari           column[k] = (min_1 < min_2) ? min_1 : min_2;
8209c9a0587SLeila Ghaffari           last_diag = old_diag;
8219c9a0587SLeila Ghaffari         }
8229c9a0587SLeila Ghaffari       }
8239c9a0587SLeila Ghaffari       size_t n = column[min_len];
8249c9a0587SLeila Ghaffari       priority = backends[i].priority;
8259c9a0587SLeila Ghaffari       if (n < lev_dis || (n == lev_dis
8269c9a0587SLeila Ghaffari                           && lev_priority > priority)) {
8279c9a0587SLeila Ghaffari         lev_dis = n;
8289c9a0587SLeila Ghaffari         lev_priority = priority;
829f7e22acaSJeremy L Thompson         lev_index = i;
8309c9a0587SLeila Ghaffari       }
8319c9a0587SLeila Ghaffari     }
832f7e22acaSJeremy L Thompson     const char *prefix_lev = backends[lev_index].prefix;
8339c9a0587SLeila Ghaffari     size_t lev_length;
8349c9a0587SLeila Ghaffari     for (lev_length=0; prefix_lev[lev_length]
8359c9a0587SLeila Ghaffari          && prefix_lev[lev_length] != '\0'; lev_length++) {}
8369c9a0587SLeila Ghaffari     size_t m = (lev_length < stem_length) ? lev_length : stem_length;
8379c9a0587SLeila Ghaffari     if (lev_dis+1 >= m) {
838e15f9bd0SJeremy L Thompson       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s",
839e15f9bd0SJeremy L Thompson                        resource);
8409c9a0587SLeila Ghaffari     } else {
84187250337Sjeremylt       return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s\n"
842f7e22acaSJeremy L Thompson                        "Closest match: %s", resource, backends[lev_index].prefix);
8432bbc7fe8Sjeremylt     }
844203015caSLeila Ghaffari     // LCOV_EXCL_STOP
8459c9a0587SLeila Ghaffari   }
846fe2413ffSjeremylt 
847fe2413ffSjeremylt   // Setup Ceed
848d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
849032e71eaSJeremy L Thompson   ierr = CeedCalloc(1, &(*ceed)->jit_source_roots); CeedChk(ierr);
850bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
8511d102b48SJeremy L Thompson   if (!ceed_error_handler)
8521d102b48SJeremy L Thompson     ceed_error_handler = "abort";
853bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
85456e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
855477729cfSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store"))
856477729cfSJeremy L Thompson     (*ceed)->Error = CeedErrorStore;
85756e866f4SJed Brown   else
858d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
859d1d35e2fSjeremylt   memcpy((*ceed)->err_msg, "No error message stored", 24);
860d1d35e2fSjeremylt   (*ceed)->ref_count = 1;
861d7b241e6Sjeremylt   (*ceed)->data = NULL;
862fe2413ffSjeremylt 
863fe2413ffSjeremylt   // Set lookup table
864d1d35e2fSjeremylt   FOffset f_offsets[] = {
8656e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
8666e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
8676e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
868f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
8696e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
870fc0567d9Srezgarshakeri     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateOriented),
8716e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
8726e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
8736e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
87450c301a5SRezgar Shakeri     CEED_FTABLE_ENTRY(Ceed, BasisCreateHdiv),
8756e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
8766e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
877777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
8786e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
8796e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
8809c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, HasValidArray),
8819c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, HasBorrowedArrayOfType),
8826e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
8836a6c615bSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, TakeArray),
8846e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
885f48ed27dSnbeams     CEED_FTABLE_ENTRY(CeedVector, SyncArray),
8866e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
8876e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
8889c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, GetArrayWrite),
8896e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
8906e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
891547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
892e0dd3b27Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Scale),
8930f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, AXPY),
8940f7fd0f8Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, PointwiseMult),
895d99fa3c5SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
8966e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Destroy),
8976e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
8986e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
899bd33150aSjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
9006e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
9016e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Apply),
9026e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Destroy),
9036e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
9046e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
9056e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Apply),
9068c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
9078c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
9086e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
9099c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, HasValidData),
9109c774eddSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, HasBorrowedDataOfType),
911777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
912891038deSjeremylt     CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData),
913777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
91428bfd0b7SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetDataRead),
915777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
91628bfd0b7SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreDataRead),
917777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
91880ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
91970a7ffb3SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate),
92080ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
9219e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
92280ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
9239e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
924e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic),
925e2f04181SAndrew T. Barker     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble),
926713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
9276e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
928250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
929cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
930250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
9316e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
9326e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
9336e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
9341dfeef1dSjeremylt   };
935fe2413ffSjeremylt 
936d1d35e2fSjeremylt   ierr = CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets); CeedChk(ierr);
937d1d35e2fSjeremylt   memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets));
938fe2413ffSjeremylt 
9395107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
940e2f04181SAndrew T. Barker   const char fallbackresource[] = "";
9415107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
9425107b09fSJeremy L Thompson   CeedChk(ierr);
9435107b09fSJeremy L Thompson 
94460f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
9453f21f6b1SJeremy L Thompson   (*ceed)->is_debug = !!getenv("CEED_DEBUG") || !!getenv("DEBUG") ||
9463f21f6b1SJeremy L Thompson                       !!getenv("DBG");
94760f9e2d6SJeremy L Thompson 
948fe2413ffSjeremylt   // Backend specific setup
949f7e22acaSJeremy L Thompson   ierr = backends[match_index].init(&resource[match_help], *ceed); CeedChk(ierr);
950fe2413ffSjeremylt 
95122e44211Sjeremylt   // Copy resource prefix, if backend setup successful
952f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(backends[match_index].prefix,
953f7e22acaSJeremy L Thompson                              (char **)&(*ceed)->resource);
954f7e22acaSJeremy L Thompson   CeedChk(ierr);
955ee5a26f2SJeremy L Thompson 
956ee5a26f2SJeremy L Thompson   // Set default JiT source root
9576155f12fSJeremy L Thompson   // Note: there will always be the default root for every Ceed
9586155f12fSJeremy L Thompson   // but all additional paths are added to the top-most parent
959ee5a26f2SJeremy L Thompson   ierr = CeedAddJitSourceRoot(*ceed, (char *)CeedJitSourceRootDefault);
960ee5a26f2SJeremy L Thompson   CeedChk(ierr);
961ee5a26f2SJeremy L Thompson 
962e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
963d7b241e6Sjeremylt }
964d7b241e6Sjeremylt 
965d7b241e6Sjeremylt /**
9669560d06aSjeremylt   @brief Copy the pointer to a Ceed context. Both pointers should
9679560d06aSjeremylt            be destroyed with `CeedDestroy()`;
9689560d06aSjeremylt            Note: If `*ceed_copy` is non-NULL, then it is assumed that
9699560d06aSjeremylt            `*ceed_copy` is a pointer to a Ceed context. This Ceed
9709560d06aSjeremylt            context will be destroyed if `*ceed_copy` is the only
9719560d06aSjeremylt            reference to this Ceed context.
9729560d06aSjeremylt 
9739560d06aSjeremylt   @param ceed            Ceed context to copy reference to
9749560d06aSjeremylt   @param[out] ceed_copy  Variable to store copied reference
9759560d06aSjeremylt 
9769560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
9779560d06aSjeremylt 
9789560d06aSjeremylt   @ref User
9799560d06aSjeremylt **/
9809560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) {
9819560d06aSjeremylt   int ierr;
9829560d06aSjeremylt 
9839560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
9849560d06aSjeremylt   ierr = CeedDestroy(ceed_copy); CeedChk(ierr);
9859560d06aSjeremylt   *ceed_copy = ceed;
9869560d06aSjeremylt   return CEED_ERROR_SUCCESS;
9879560d06aSjeremylt }
9889560d06aSjeremylt 
9899560d06aSjeremylt /**
9907a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
9912f86a920SJeremy L Thompson 
9927a982d89SJeremy L. Thompson   @param ceed           Ceed context to get resource name of
9937a982d89SJeremy L. Thompson   @param[out] resource  Variable to store resource name
9942f86a920SJeremy L Thompson 
9952f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9962f86a920SJeremy L Thompson 
9977a982d89SJeremy L. Thompson   @ref User
9985107b09fSJeremy L Thompson **/
9997a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
10007a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
1001e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10025107b09fSJeremy L Thompson }
10035107b09fSJeremy L Thompson 
10045107b09fSJeremy L Thompson /**
1005d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
1006c907536fSjeremylt 
1007d79b80ecSjeremylt   @param ceed           Ceed context to get preferred memory type of
1008d1d35e2fSjeremylt   @param[out] mem_type  Address to save preferred memory type to
1009c907536fSjeremylt 
1010c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
1011c907536fSjeremylt 
10127a982d89SJeremy L. Thompson   @ref User
1013c907536fSjeremylt **/
1014d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) {
1015c907536fSjeremylt   int ierr;
1016c263cd57Sjeremylt 
1017c907536fSjeremylt   if (ceed->GetPreferredMemType) {
1018d1d35e2fSjeremylt     ierr = ceed->GetPreferredMemType(mem_type); CeedChk(ierr);
1019c907536fSjeremylt   } else {
1020c263cd57Sjeremylt     Ceed delegate;
1021c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
1022c263cd57Sjeremylt 
1023c263cd57Sjeremylt     if (delegate) {
1024d1d35e2fSjeremylt       ierr = CeedGetPreferredMemType(delegate, mem_type); CeedChk(ierr);
1025c263cd57Sjeremylt     } else {
1026d1d35e2fSjeremylt       *mem_type = CEED_MEM_HOST;
1027c907536fSjeremylt     }
1028c263cd57Sjeremylt   }
1029e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1030c907536fSjeremylt }
1031c907536fSjeremylt 
1032c907536fSjeremylt /**
10339525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
10349525855cSJeremy L Thompson 
10359525855cSJeremy L Thompson   @param[in] ceed               Ceed
1036d1d35e2fSjeremylt   @param[out] is_deterministic  Variable to store deterministic status
10379525855cSJeremy L Thompson 
10389525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10399525855cSJeremy L Thompson 
10409525855cSJeremy L Thompson   @ref User
10419525855cSJeremy L Thompson **/
1042d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) {
1043d1d35e2fSjeremylt   *is_deterministic = ceed->is_deterministic;
1044e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10459525855cSJeremy L Thompson }
10469525855cSJeremy L Thompson 
10479525855cSJeremy L Thompson /**
1048ee5a26f2SJeremy L Thompson   @brief Set additional JiT source root for Ceed
1049ee5a26f2SJeremy L Thompson 
1050ee5a26f2SJeremy L Thompson   @param[in] ceed            Ceed
1051ee5a26f2SJeremy L Thompson   @param[in] jit_source_root Absolute path to additional JiT source directory
1052ee5a26f2SJeremy L Thompson 
1053ee5a26f2SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1054ee5a26f2SJeremy L Thompson 
1055ee5a26f2SJeremy L Thompson   @ref User
1056ee5a26f2SJeremy L Thompson **/
1057ee5a26f2SJeremy L Thompson int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root) {
1058ee5a26f2SJeremy L Thompson   int ierr;
10596155f12fSJeremy L Thompson   Ceed ceed_parent;
1060ee5a26f2SJeremy L Thompson 
10616155f12fSJeremy L Thompson   ierr = CeedGetParent(ceed, &ceed_parent); CeedChk(ierr);
10626155f12fSJeremy L Thompson 
10636155f12fSJeremy L Thompson   CeedInt index = ceed_parent->num_jit_source_roots;
1064ee5a26f2SJeremy L Thompson   size_t path_length = strlen(jit_source_root);
10656155f12fSJeremy L Thompson   ierr = CeedRealloc(index + 1, &ceed_parent->jit_source_roots); CeedChk(ierr);
10666155f12fSJeremy L Thompson   ierr = CeedCalloc(path_length + 1, &ceed_parent->jit_source_roots[index]);
1067ee5a26f2SJeremy L Thompson   CeedChk(ierr);
1068d602d780SJeremy L Thompson   memcpy(ceed_parent->jit_source_roots[index], jit_source_root, path_length);
10696155f12fSJeremy L Thompson   ceed_parent->num_jit_source_roots++;
1070ee5a26f2SJeremy L Thompson 
1071ee5a26f2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1072ee5a26f2SJeremy L Thompson }
1073ee5a26f2SJeremy L Thompson 
1074ee5a26f2SJeremy L Thompson /**
10750a0da059Sjeremylt   @brief View a Ceed
10760a0da059Sjeremylt 
10770a0da059Sjeremylt   @param[in] ceed    Ceed to view
10780a0da059Sjeremylt   @param[in] stream  Filestream to write to
10790a0da059Sjeremylt 
10800a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
10810a0da059Sjeremylt 
10820a0da059Sjeremylt   @ref User
10830a0da059Sjeremylt **/
10840a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
10850a0da059Sjeremylt   int ierr;
1086d1d35e2fSjeremylt   CeedMemType mem_type;
10870a0da059Sjeremylt 
1088d1d35e2fSjeremylt   ierr = CeedGetPreferredMemType(ceed, &mem_type); CeedChk(ierr);
10890a0da059Sjeremylt 
10900a0da059Sjeremylt   fprintf(stream, "Ceed\n"
10910a0da059Sjeremylt           "  Ceed Resource: %s\n"
10920a0da059Sjeremylt           "  Preferred MemType: %s\n",
1093d1d35e2fSjeremylt           ceed->resource, CeedMemTypes[mem_type]);
1094e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10950a0da059Sjeremylt }
10960a0da059Sjeremylt 
10970a0da059Sjeremylt /**
1098b11c1e72Sjeremylt   @brief Destroy a Ceed context
1099d7b241e6Sjeremylt 
1100d7b241e6Sjeremylt   @param ceed  Address of Ceed context to destroy
1101b11c1e72Sjeremylt 
1102b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1103dfdf5a53Sjeremylt 
11047a982d89SJeremy L. Thompson   @ref User
1105b11c1e72Sjeremylt **/
1106d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
1107d7b241e6Sjeremylt   int ierr;
1108d1d35e2fSjeremylt   if (!*ceed || --(*ceed)->ref_count > 0) return CEED_ERROR_SUCCESS;
11095fe0d4faSjeremylt   if ((*ceed)->delegate) {
11105fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
11115fe0d4faSjeremylt   }
11120ace9bf2Sjeremylt 
1113d1d35e2fSjeremylt   if ((*ceed)->obj_delegate_count > 0) {
111492ae7e47SJeremy L Thompson     for (CeedInt i = 0; i < (*ceed)->obj_delegate_count; i++) {
1115d1d35e2fSjeremylt       ierr = CeedDestroy(&((*ceed)->obj_delegates[i].delegate)); CeedChk(ierr);
1116d1d35e2fSjeremylt       ierr = CeedFree(&(*ceed)->obj_delegates[i].obj_name); CeedChk(ierr);
1117aefd8378Sjeremylt     }
1118d1d35e2fSjeremylt     ierr = CeedFree(&(*ceed)->obj_delegates); CeedChk(ierr);
1119aefd8378Sjeremylt   }
11200ace9bf2Sjeremylt 
1121d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
1122d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
1123d7b241e6Sjeremylt   }
11240ace9bf2Sjeremylt 
112592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*ceed)->num_jit_source_roots; i++) {
1126032e71eaSJeremy L Thompson     ierr = CeedFree(&(*ceed)->jit_source_roots[i]); CeedChk(ierr);
1127032e71eaSJeremy L Thompson   }
1128032e71eaSJeremy L Thompson   ierr = CeedFree(&(*ceed)->jit_source_roots); CeedChk(ierr);
1129032e71eaSJeremy L Thompson 
1130d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->f_offsets); CeedChk(ierr);
1131e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
1132d1d35e2fSjeremylt   ierr = CeedDestroy(&(*ceed)->op_fallback_ceed); CeedChk(ierr);
1133d1d35e2fSjeremylt   ierr = CeedFree(&(*ceed)->op_fallback_resource); CeedChk(ierr);
1134d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
1135e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1136d7b241e6Sjeremylt }
1137d7b241e6Sjeremylt 
1138f9982c62SWill Pazner // LCOV_EXCL_START
1139f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
11403f4a9821SAndrew T. Barker   if (ceed->parent)
11413f4a9821SAndrew T. Barker     return CeedErrorFormat(ceed->parent, format, args);
1142d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1143d1d35e2fSjeremylt     return CeedErrorFormat(ceed->op_fallback_parent, format, args);
114478464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
114578464608Sjeremylt   vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args); // NOLINT
1146d1d35e2fSjeremylt   return ceed->err_msg;
1147f9982c62SWill Pazner }
1148f9982c62SWill Pazner // LCOV_EXCL_STOP
1149f9982c62SWill Pazner 
11507a982d89SJeremy L. Thompson /**
11517a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
11527a982d89SJeremy L. Thompson 
11537a982d89SJeremy L. Thompson   @ref Developer
11547a982d89SJeremy L. Thompson **/
11557a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
11567a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
11577a982d89SJeremy L. Thompson   va_list args;
1158d1d35e2fSjeremylt   int ret_val;
11597a982d89SJeremy L. Thompson   va_start(args, format);
11607a982d89SJeremy L. Thompson   if (ceed) {
1161d1d35e2fSjeremylt     ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
11627a982d89SJeremy L. Thompson   } else {
1163b0d62198Sjeremylt     // LCOV_EXCL_START
1164477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
1165477729cfSJeremy L Thompson     if (!ceed_error_handler)
1166477729cfSJeremy L Thompson       ceed_error_handler = "abort";
1167477729cfSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return"))
1168d1d35e2fSjeremylt       ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
1169477729cfSJeremy L Thompson     else
1170477729cfSJeremy L Thompson       // This function will not return
1171d1d35e2fSjeremylt       ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
11727a982d89SJeremy L. Thompson   }
11737a982d89SJeremy L. Thompson   va_end(args);
1174d1d35e2fSjeremylt   return ret_val;
1175b0d62198Sjeremylt   // LCOV_EXCL_STOP
11767a982d89SJeremy L. Thompson }
11777a982d89SJeremy L. Thompson 
1178477729cfSJeremy L Thompson /**
1179477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
1180477729cfSJeremy L Thompson 
1181477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1182477729cfSJeremy L Thompson 
1183477729cfSJeremy L Thompson   @ref Developer
1184477729cfSJeremy L Thompson **/
1185477729cfSJeremy L Thompson // LCOV_EXCL_START
1186d1d35e2fSjeremylt int CeedErrorReturn(Ceed ceed, const char *filename, int line_no,
1187d1d35e2fSjeremylt                     const char *func, int err_code, const char *format,
1188f9982c62SWill Pazner                     va_list *args) {
1189d1d35e2fSjeremylt   return err_code;
1190477729cfSJeremy L Thompson }
1191477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1192477729cfSJeremy L Thompson 
1193477729cfSJeremy L Thompson /**
1194477729cfSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns
1195477729cfSJeremy L Thompson            the error.
1196477729cfSJeremy L Thompson 
1197477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1198477729cfSJeremy L Thompson 
1199477729cfSJeremy L Thompson   @ref Developer
1200477729cfSJeremy L Thompson **/
1201477729cfSJeremy L Thompson // LCOV_EXCL_START
1202d1d35e2fSjeremylt int CeedErrorStore(Ceed ceed, const char *filename, int line_no,
1203d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1204f9982c62SWill Pazner                    va_list *args) {
1205477729cfSJeremy L Thompson   if (ceed->parent)
1206d1d35e2fSjeremylt     return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format,
1207187168c7SJeremy L Thompson                           args);
1208d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1209d1d35e2fSjeremylt     return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func,
1210d1d35e2fSjeremylt                           err_code, format, args);
1211477729cfSJeremy L Thompson 
1212477729cfSJeremy L Thompson   // Build message
1213477729cfSJeremy L Thompson   CeedInt len;
1214d1d35e2fSjeremylt   len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ",
1215d1d35e2fSjeremylt                  filename, line_no, func);
121678464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
121778464608Sjeremylt   // *INDENT-OFF*
121878464608Sjeremylt   vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args); // NOLINT
121978464608Sjeremylt   // *INDENT-ON*
1220d1d35e2fSjeremylt   return err_code;
1221477729cfSJeremy L Thompson }
1222477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1223477729cfSJeremy L Thompson 
1224477729cfSJeremy L Thompson /**
1225477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
1226477729cfSJeremy L Thompson 
1227477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1228477729cfSJeremy L Thompson 
1229477729cfSJeremy L Thompson   @ref Developer
1230477729cfSJeremy L Thompson **/
1231477729cfSJeremy L Thompson // LCOV_EXCL_START
1232d1d35e2fSjeremylt int CeedErrorAbort(Ceed ceed, const char *filename, int line_no,
1233d1d35e2fSjeremylt                    const char *func, int err_code, const char *format,
1234f9982c62SWill Pazner                    va_list *args) {
1235d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
1236f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1237477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1238477729cfSJeremy L Thompson   abort();
1239d1d35e2fSjeremylt   return err_code;
1240477729cfSJeremy L Thompson }
1241477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1242477729cfSJeremy L Thompson 
1243477729cfSJeremy L Thompson /**
1244477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
1245477729cfSJeremy L Thompson 
1246477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1247477729cfSJeremy L Thompson 
1248477729cfSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit()
1249477729cfSJeremy L Thompson   handlers (e.g., as used by gcov) are run.
1250477729cfSJeremy L Thompson 
1251477729cfSJeremy L Thompson   @ref Developer
1252477729cfSJeremy L Thompson **/
1253d1d35e2fSjeremylt int CeedErrorExit(Ceed ceed, const char *filename, int line_no,
1254d1d35e2fSjeremylt                   const char *func, int err_code, const char *format, va_list *args) {
1255d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
125678464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
125778464608Sjeremylt   vfprintf(stderr, format, *args); // NOLINT
1258477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1259d1d35e2fSjeremylt   exit(err_code);
1260d1d35e2fSjeremylt   return err_code;
1261477729cfSJeremy L Thompson }
1262477729cfSJeremy L Thompson 
1263477729cfSJeremy L Thompson /**
1264477729cfSJeremy L Thompson   @brief Set error handler
1265477729cfSJeremy L Thompson 
1266477729cfSJeremy L Thompson   A default error handler is set in CeedInit().  Use this function to change
1267477729cfSJeremy L Thompson   the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined
1268477729cfSJeremy L Thompson   error handler.
1269477729cfSJeremy L Thompson 
1270477729cfSJeremy L Thompson   @ref Developer
1271477729cfSJeremy L Thompson **/
1272d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) {
1273d1d35e2fSjeremylt   ceed->Error = handler;
1274d1d35e2fSjeremylt   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler);
127592ae7e47SJeremy L Thompson   for (CeedInt i=0; i<ceed->obj_delegate_count; i++)
1276d1d35e2fSjeremylt     CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler);
1277e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1278477729cfSJeremy L Thompson }
1279477729cfSJeremy L Thompson 
1280477729cfSJeremy L Thompson /**
1281477729cfSJeremy L Thompson   @brief Get error message
1282477729cfSJeremy L Thompson 
1283477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1284477729cfSJeremy L Thompson     CeedErrorStore()
1285477729cfSJeremy L Thompson 
1286477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to retrieve error message
1287d1d35e2fSjeremylt   @param[out] err_msg  Char pointer to hold error message
1288477729cfSJeremy L Thompson 
1289477729cfSJeremy L Thompson   @ref Developer
1290477729cfSJeremy L Thompson **/
1291d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) {
12923f4a9821SAndrew T. Barker   if (ceed->parent)
1293d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->parent, err_msg);
1294d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1295d1d35e2fSjeremylt     return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg);
1296d1d35e2fSjeremylt   *err_msg = ceed->err_msg;
1297e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1298477729cfSJeremy L Thompson }
1299477729cfSJeremy L Thompson 
1300477729cfSJeremy L Thompson /**
1301477729cfSJeremy L Thompson   @brief Restore error message
1302477729cfSJeremy L Thompson 
1303477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1304477729cfSJeremy L Thompson     CeedErrorStore()
1305477729cfSJeremy L Thompson 
1306477729cfSJeremy L Thompson   @param[in] ceed      Ceed contex to restore error message
1307d1d35e2fSjeremylt   @param[out] err_msg  Char pointer that holds error message
1308477729cfSJeremy L Thompson 
1309477729cfSJeremy L Thompson   @ref Developer
1310477729cfSJeremy L Thompson **/
1311d1d35e2fSjeremylt int CeedResetErrorMessage(Ceed ceed, const char **err_msg) {
13123f4a9821SAndrew T. Barker   if (ceed->parent)
1313d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->parent, err_msg);
1314d1d35e2fSjeremylt   if (ceed->op_fallback_parent)
1315d1d35e2fSjeremylt     return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg);
1316d1d35e2fSjeremylt   *err_msg = NULL;
1317d1d35e2fSjeremylt   memcpy(ceed->err_msg, "No error message stored", 24);
1318e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1319477729cfSJeremy L Thompson }
1320477729cfSJeremy L Thompson 
13211070991dSJed Brown /**
13221070991dSJed Brown   @brief Get libCEED library version info
13231070991dSJed Brown 
13241070991dSJed Brown   libCEED version numbers have the form major.minor.patch. Non-release versions
13251070991dSJed Brown   may contain unstable interfaces.
13261070991dSJed Brown 
13271070991dSJed Brown   @param[out] major    Major version of the library
13281070991dSJed Brown   @param[out] minor    Minor version of the library
13291070991dSJed Brown   @param[out] patch    Patch (subminor) version of the library
13301070991dSJed Brown   @param[out] release  True for releases; false for development branches.
13311070991dSJed Brown 
13321070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
13331070991dSJed Brown 
13341070991dSJed Brown   @sa CEED_VERSION_GE()
13351070991dSJed Brown 
13361070991dSJed Brown   @ref Developer
13371070991dSJed Brown */
13381070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
13391070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
13401070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
13411070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
13421070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
13431070991dSJed Brown   return 0;
13441070991dSJed Brown }
13451070991dSJed Brown 
134680a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) {
134780a9ef05SNatalie Beams   *scalar_type = CEED_SCALAR_TYPE;
134880a9ef05SNatalie Beams   return 0;
134980a9ef05SNatalie Beams }
135080a9ef05SNatalie Beams 
135180a9ef05SNatalie Beams 
1352d7b241e6Sjeremylt /// @}
1353