xref: /libCEED/rust/libceed-sys/c-src/interface/ceed.c (revision 1c66c397a67401e1a222857807e6e5b7c45b88c0)
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
93d576824SJeremy L Thompson #include <ceed-impl.h>
1049aac155SJeremy L Thompson #include <ceed.h>
112b730f8bSJeremy L Thompson #include <ceed/backend.h>
12aedaa0e5Sjeremylt #include <limits.h>
13d7b241e6Sjeremylt #include <stdarg.h>
1449aac155SJeremy L Thompson #include <stdbool.h>
156e79d475Sjeremylt #include <stddef.h>
16d7b241e6Sjeremylt #include <stdio.h>
17d7b241e6Sjeremylt #include <stdlib.h>
18d7b241e6Sjeremylt #include <string.h>
19d7b241e6Sjeremylt 
20d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP
21d7b241e6Sjeremylt static CeedRequest ceed_request_immediate;
22d7b241e6Sjeremylt static CeedRequest ceed_request_ordered;
23d7b241e6Sjeremylt 
24d7b241e6Sjeremylt static struct {
25d7b241e6Sjeremylt   char prefix[CEED_MAX_RESOURCE_LEN];
26d7b241e6Sjeremylt   int (*init)(const char *resource, Ceed f);
27d7b241e6Sjeremylt   unsigned int priority;
28d7b241e6Sjeremylt } backends[32];
29d7b241e6Sjeremylt static size_t num_backends;
30fe2413ffSjeremylt 
316e79d475Sjeremylt #define CEED_FTABLE_ENTRY(class, method)                     \
32b559d91bSJed Brown   {                                                          \
33b559d91bSJed Brown #class #method, offsetof(struct class##_private, method) \
34b559d91bSJed Brown   }
35d7b241e6Sjeremylt /// @endcond
36d7b241e6Sjeremylt 
37d7b241e6Sjeremylt /// @file
38d7b241e6Sjeremylt /// Implementation of core components of Ceed library
397a982d89SJeremy L. Thompson 
407a982d89SJeremy L. Thompson /// @addtogroup CeedUser
41d7b241e6Sjeremylt /// @{
42d7b241e6Sjeremylt 
43dfdf5a53Sjeremylt /**
44dfdf5a53Sjeremylt   @brief Request immediate completion
45dfdf5a53Sjeremylt 
46ea61e9acSJeremy L Thompson   This predefined constant is passed as the \ref CeedRequest argument to interfaces when the caller wishes for the operation to be performed
47dfdf5a53Sjeremylt immediately. The code
48dfdf5a53Sjeremylt 
49dfdf5a53Sjeremylt   @code
50dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE);
51dfdf5a53Sjeremylt   @endcode
52dfdf5a53Sjeremylt 
53dfdf5a53Sjeremylt   is semantically equivalent to
54dfdf5a53Sjeremylt 
55dfdf5a53Sjeremylt   @code
56dfdf5a53Sjeremylt     CeedRequest request;
57dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., &request);
58dfdf5a53Sjeremylt     CeedRequestWait(&request);
59dfdf5a53Sjeremylt   @endcode
60dfdf5a53Sjeremylt 
61dfdf5a53Sjeremylt   @sa CEED_REQUEST_ORDERED
62dfdf5a53Sjeremylt **/
63d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate;
64d7b241e6Sjeremylt 
65d7b241e6Sjeremylt /**
66b11c1e72Sjeremylt   @brief Request ordered completion
67d7b241e6Sjeremylt 
68ea61e9acSJeremy L Thompson   This predefined constant is passed as the \ref CeedRequest argument to interfaces when the caller wishes for the operation to be completed in the
699fd66db6SSebastian Grimberg   order that it is submitted to the device. It is typically used in a construct 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 
79ea61e9acSJeremy L Thompson   which allows the sequence to complete asynchronously but does not start `op2` until `op1` has completed.
80d7b241e6Sjeremylt 
81ea61e9acSJeremy L Thompson   @todo The current implementation is overly strict, offering equivalent semantics to @ref CEED_REQUEST_IMMEDIATE.
82d7b241e6Sjeremylt 
83d7b241e6Sjeremylt   @sa CEED_REQUEST_IMMEDIATE
84d7b241e6Sjeremylt  */
85d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered;
86d7b241e6Sjeremylt 
87b11c1e72Sjeremylt /**
887a982d89SJeremy L. Thompson   @brief Wait for a CeedRequest to complete.
89dfdf5a53Sjeremylt 
907a982d89SJeremy L. Thompson   Calling CeedRequestWait on a NULL request is a no-op.
917a982d89SJeremy L. Thompson 
927a982d89SJeremy L. Thompson   @param req Address of CeedRequest to wait for; zeroed on completion.
937a982d89SJeremy L. Thompson 
947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
957a982d89SJeremy L. Thompson 
967a982d89SJeremy L. Thompson   @ref User
97b11c1e72Sjeremylt **/
987a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) {
992b730f8bSJeremy L Thompson   if (!*req) return CEED_ERROR_SUCCESS;
1002b730f8bSJeremy L Thompson   return CeedError(NULL, CEED_ERROR_UNSUPPORTED, "CeedRequestWait not implemented");
101683faae0SJed Brown }
1027a982d89SJeremy L. Thompson 
1037a982d89SJeremy L. Thompson /// @}
1047a982d89SJeremy L. Thompson 
1057a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1067a982d89SJeremy L. Thompson /// Ceed Library Internal Functions
1077a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1087a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper
1097a982d89SJeremy L. Thompson /// @{
110d7b241e6Sjeremylt 
1116a406739SJeremy L Thompson /**
1126a406739SJeremy L Thompson   @brief Register a Ceed backend internally.
1134385fb7fSSebastian Grimberg 
1146a406739SJeremy L Thompson   Note: Backends should call `CeedRegister` instead.
1156a406739SJeremy L Thompson 
116ea61e9acSJeremy L Thompson   @param[in] prefix    Prefix of resources for this backend to respond to.
117ea61e9acSJeremy L Thompson                          For example, the reference backend responds to "/cpu/self".
118ea61e9acSJeremy L Thompson   @param[in] init      Initialization function called by CeedInit() when the backend is selected to drive the requested resource.
119ea61e9acSJeremy L Thompson   @param[in] priority  Integer priority.
120ea61e9acSJeremy L Thompson                          Lower values are preferred in case the resource requested by CeedInit() has non-unique best prefix match.
1216a406739SJeremy L Thompson 
1226a406739SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1236a406739SJeremy L Thompson 
1246a406739SJeremy L Thompson   @ref Developer
1256a406739SJeremy L Thompson **/
1262b730f8bSJeremy L Thompson int CeedRegisterImpl(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) {
1276574a04fSJeremy L Thompson   CeedCheck(num_backends < sizeof(backends) / sizeof(backends[0]), NULL, CEED_ERROR_MAJOR, "Too many backends");
1286a406739SJeremy L Thompson 
1296a406739SJeremy L Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
1306a406739SJeremy L Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN - 1] = 0;
1316a406739SJeremy L Thompson   backends[num_backends].init                              = init;
1326a406739SJeremy L Thompson   backends[num_backends].priority                          = priority;
1336a406739SJeremy L Thompson   num_backends++;
1346a406739SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1356a406739SJeremy L Thompson }
1366a406739SJeremy L Thompson 
1377a982d89SJeremy L. Thompson /// @}
138d7b241e6Sjeremylt 
1397a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1407a982d89SJeremy L. Thompson /// Ceed Backend API
1417a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1427a982d89SJeremy L. Thompson /// @addtogroup CeedBackend
1437a982d89SJeremy L. Thompson /// @{
144d7b241e6Sjeremylt 
145b11c1e72Sjeremylt /**
1463f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
14760f9e2d6SJeremy L Thompson 
148ea61e9acSJeremy L Thompson   @param[in] ceed Ceed context
14960f9e2d6SJeremy L Thompson 
1503f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1513f21f6b1SJeremy L Thompson                          false - debugging mode disabled
15260f9e2d6SJeremy L Thompson 
15360f9e2d6SJeremy L Thompson   @ref Backend
15460f9e2d6SJeremy L Thompson **/
155fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
1562b730f8bSJeremy L Thompson bool CeedDebugFlag(const Ceed ceed) { return ceed->is_debug; }
157fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
15860f9e2d6SJeremy L Thompson 
15960f9e2d6SJeremy L Thompson /**
1603f21f6b1SJeremy L Thompson   @brief Return value of CEED_DEBUG environment variable
16160f9e2d6SJeremy L Thompson 
1623f21f6b1SJeremy L Thompson   @return boolean value: true  - debugging mode enabled
1633f21f6b1SJeremy L Thompson                          false - debugging mode disabled
1643f21f6b1SJeremy L Thompson 
1653f21f6b1SJeremy L Thompson   @ref Backend
1663f21f6b1SJeremy L Thompson **/
1673f21f6b1SJeremy L Thompson // LCOV_EXCL_START
168*1c66c397SJeremy L Thompson bool CeedDebugFlagEnv(void) { return getenv("CEED_DEBUG") || getenv("DEBUG") || getenv("DBG"); }
1693f21f6b1SJeremy L Thompson // LCOV_EXCL_STOP
1703f21f6b1SJeremy L Thompson 
1713f21f6b1SJeremy L Thompson /**
1723f21f6b1SJeremy L Thompson   @brief Print debugging information in color
1733f21f6b1SJeremy L Thompson 
17460f9e2d6SJeremy L Thompson   @param color   Color to print
17560f9e2d6SJeremy L Thompson   @param format  Printing format
17660f9e2d6SJeremy L Thompson 
17760f9e2d6SJeremy L Thompson   @ref Backend
17860f9e2d6SJeremy L Thompson **/
179fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
1803f21f6b1SJeremy L Thompson void CeedDebugImpl256(const unsigned char color, const char *format, ...) {
18160f9e2d6SJeremy L Thompson   va_list args;
18260f9e2d6SJeremy L Thompson   va_start(args, format);
18360f9e2d6SJeremy L Thompson   fflush(stdout);
1842b730f8bSJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[38;5;%dm", color);
18560f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
1862b730f8bSJeremy L Thompson   if (color != CEED_DEBUG_COLOR_NONE) fprintf(stdout, "\033[m");
18760f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
18860f9e2d6SJeremy L Thompson   fflush(stdout);
18960f9e2d6SJeremy L Thompson   va_end(args);
19060f9e2d6SJeremy L Thompson }
191fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
19260f9e2d6SJeremy L Thompson 
19360f9e2d6SJeremy L Thompson /**
194b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
195b11c1e72Sjeremylt 
196ea61e9acSJeremy L Thompson   Memory usage can be tracked by the library.
197ea61e9acSJeremy L Thompson   This ensures sufficient alignment for vectorization and should be used for large allocations.
198b11c1e72Sjeremylt 
199ea61e9acSJeremy L Thompson   @param[in]  n    Number of units to allocate
200ea61e9acSJeremy L Thompson   @param[in]  unit Size of each unit
201ea61e9acSJeremy L Thompson   @param[out] p    Address of pointer to hold the result.
202b11c1e72Sjeremylt 
203b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
204b11c1e72Sjeremylt 
205b11c1e72Sjeremylt   @sa CeedFree()
206dfdf5a53Sjeremylt 
2077a982d89SJeremy L. Thompson   @ref Backend
208b11c1e72Sjeremylt **/
209d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
210d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n * unit);
2116574a04fSJeremy L Thompson   CeedCheck(ierr == 0, NULL, CEED_ERROR_MAJOR, "posix_memalign failed to allocate %zd members of size %zd\n", n, unit);
212e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
213d7b241e6Sjeremylt }
214d7b241e6Sjeremylt 
215b11c1e72Sjeremylt /**
216b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
217b11c1e72Sjeremylt 
218b11c1e72Sjeremylt   Memory usage can be tracked by the library.
219b11c1e72Sjeremylt 
220ea61e9acSJeremy L Thompson   @param[in]  n    Number of units to allocate
221ea61e9acSJeremy L Thompson   @param[in]  unit Size of each unit
222ea61e9acSJeremy L Thompson   @param[out] p    Address of pointer to hold the result.
223b11c1e72Sjeremylt 
224b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
225b11c1e72Sjeremylt 
226b11c1e72Sjeremylt   @sa CeedFree()
227dfdf5a53Sjeremylt 
2287a982d89SJeremy L. Thompson   @ref Backend
229b11c1e72Sjeremylt **/
230d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
231d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
2326574a04fSJeremy L Thompson   CeedCheck(!n || !unit || *(void **)p, NULL, CEED_ERROR_MAJOR, "calloc failed to allocate %zd members of size %zd\n", n, unit);
233e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
234d7b241e6Sjeremylt }
235d7b241e6Sjeremylt 
236b11c1e72Sjeremylt /**
237b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
238b11c1e72Sjeremylt 
239b11c1e72Sjeremylt   Memory usage can be tracked by the library.
240b11c1e72Sjeremylt 
241ea61e9acSJeremy L Thompson   @param[in]  n    Number of units to allocate
242ea61e9acSJeremy L Thompson   @param[in]  unit Size of each unit
243ea61e9acSJeremy L Thompson   @param[out] p    Address of pointer to hold the result.
244b11c1e72Sjeremylt 
245b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
246b11c1e72Sjeremylt 
247b11c1e72Sjeremylt   @sa CeedFree()
248dfdf5a53Sjeremylt 
2497a982d89SJeremy L. Thompson   @ref Backend
250b11c1e72Sjeremylt **/
251d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
252d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n * unit);
2536574a04fSJeremy L Thompson   CeedCheck(!n || !unit || *(void **)p, NULL, CEED_ERROR_MAJOR, "realloc failed to allocate %zd members of size %zd\n", n, unit);
254e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
255d7b241e6Sjeremylt }
256d7b241e6Sjeremylt 
257f7e22acaSJeremy L Thompson /**
258f7e22acaSJeremy L Thompson   @brief Allocate a cleared string buffer on the host
259f7e22acaSJeremy L Thompson 
260f7e22acaSJeremy L Thompson   Memory usage can be tracked by the library.
261f7e22acaSJeremy L Thompson 
262ea61e9acSJeremy L Thompson   @param[in]  source Pointer to string to be copied
263ea61e9acSJeremy L Thompson   @param[out] copy   Pointer to variable to hold newly allocated string copy
264f7e22acaSJeremy L Thompson 
265f7e22acaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
266f7e22acaSJeremy L Thompson 
267f7e22acaSJeremy L Thompson   @sa CeedFree()
268f7e22acaSJeremy L Thompson 
269f7e22acaSJeremy L Thompson   @ref Backend
270f7e22acaSJeremy L Thompson **/
271f7e22acaSJeremy L Thompson int CeedStringAllocCopy(const char *source, char **copy) {
272f7e22acaSJeremy L Thompson   size_t len = strlen(source);
2732b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(len + 1, copy));
274d602d780SJeremy L Thompson   memcpy(*copy, source, len);
275f7e22acaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
276f7e22acaSJeremy L Thompson }
277f7e22acaSJeremy L Thompson 
27834138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
27934138859Sjeremylt 
280ea61e9acSJeremy L Thompson   @param[in,out] p  address of pointer to memory.
281ea61e9acSJeremy L Thompson                       This argument is of type void* to avoid needing a cast, but is the address of the pointer (which is zeroed) rather than the
282ea61e9acSJeremy L Thompson pointer.
28334138859Sjeremylt **/
284d7b241e6Sjeremylt int CeedFree(void *p) {
285d7b241e6Sjeremylt   free(*(void **)p);
286d7b241e6Sjeremylt   *(void **)p = NULL;
287e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
288d7b241e6Sjeremylt }
289d7b241e6Sjeremylt 
290d7b241e6Sjeremylt /**
2917a982d89SJeremy L. Thompson   @brief Register a Ceed backend
292d7b241e6Sjeremylt 
293ea61e9acSJeremy L Thompson   @param[in] prefix   Prefix of resources for this backend to respond to.
294ea61e9acSJeremy L Thompson                         For example, the reference backend responds to "/cpu/self".
295ea61e9acSJeremy L Thompson   @param[in] init     Initialization function called by CeedInit() when the backend is selected to drive the requested resource.
296ea61e9acSJeremy L Thompson   @param[in] priority Integer priority.
297ea61e9acSJeremy L Thompson                         Lower values are preferred in case the resource requested by CeedInit() has non-unique best prefix match.
298b11c1e72Sjeremylt 
299b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
300dfdf5a53Sjeremylt 
3017a982d89SJeremy L. Thompson   @ref Backend
302b11c1e72Sjeremylt **/
3032b730f8bSJeremy L Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed), unsigned int priority) {
30410243053SJeremy L Thompson   CeedDebugEnv("Backend Register: %s", prefix);
3056a406739SJeremy L Thompson   CeedRegisterImpl(prefix, init, priority);
306e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
307d7b241e6Sjeremylt }
308d7b241e6Sjeremylt 
309b11c1e72Sjeremylt /**
31060f9e2d6SJeremy L Thompson   @brief Return debugging status flag
31160f9e2d6SJeremy L Thompson 
312ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to get debugging flag
313ea61e9acSJeremy L Thompson   @param[out] is_debug Variable to store debugging flag
31460f9e2d6SJeremy L Thompson 
31560f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
31660f9e2d6SJeremy L Thompson 
317d1d35e2fSjeremylt   @ref Backend
31860f9e2d6SJeremy L Thompson **/
319d1d35e2fSjeremylt int CeedIsDebug(Ceed ceed, bool *is_debug) {
3203f21f6b1SJeremy L Thompson   *is_debug = ceed->is_debug;
321e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
32260f9e2d6SJeremy L Thompson }
32360f9e2d6SJeremy L Thompson 
32460f9e2d6SJeremy L Thompson /**
325bc246734SJeremy L Thompson   @brief Get the root of the requested resource
326bc246734SJeremy L Thompson 
327bc246734SJeremy L Thompson   @param[in]  ceed          Ceed context to get resource name of
328bc246734SJeremy L Thompson   @param[in]  resource      ull user specified resource
329*1c66c397SJeremy L Thompson   @param[in]  delineator    Delineator to break resource_root and resource_spec
330bc246734SJeremy L Thompson   @param[out] resource_root Variable to store resource root
331bc246734SJeremy L Thompson 
332bc246734SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
333bc246734SJeremy L Thompson 
334bc246734SJeremy L Thompson   @ref Backend
335bc246734SJeremy L Thompson **/
336bc246734SJeremy L Thompson int CeedGetResourceRoot(Ceed ceed, const char *resource, const char *delineator, char **resource_root) {
337bc246734SJeremy L Thompson   char  *device_spec       = strstr(resource, delineator);
338bc246734SJeremy L Thompson   size_t resource_root_len = device_spec ? (size_t)(device_spec - resource) + 1 : strlen(resource) + 1;
339*1c66c397SJeremy L Thompson 
340bc246734SJeremy L Thompson   CeedCall(CeedCalloc(resource_root_len, resource_root));
341bc246734SJeremy L Thompson   memcpy(*resource_root, resource, resource_root_len - 1);
342bc246734SJeremy L Thompson   return CEED_ERROR_SUCCESS;
343bc246734SJeremy L Thompson }
344bc246734SJeremy L Thompson 
345bc246734SJeremy L Thompson /**
3467a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3477a982d89SJeremy L. Thompson 
348ea61e9acSJeremy L Thompson   @param[in]  ceed   Ceed context to retrieve parent of
3497a982d89SJeremy L. Thompson   @param[out] parent Address to save the parent to
3507a982d89SJeremy L. Thompson 
3517a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3527a982d89SJeremy L. Thompson 
3537a982d89SJeremy L. Thompson   @ref Backend
3547a982d89SJeremy L. Thompson **/
3557a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3567a982d89SJeremy L. Thompson   if (ceed->parent) {
3572b730f8bSJeremy L Thompson     CeedCall(CeedGetParent(ceed->parent, parent));
358e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
3597a982d89SJeremy L. Thompson   }
3607a982d89SJeremy L. Thompson   *parent = ceed;
361e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3627a982d89SJeremy L. Thompson }
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson /**
3657a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3667a982d89SJeremy L. Thompson 
367ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to retrieve delegate of
3687a982d89SJeremy L. Thompson   @param[out] delegate Address to save the delegate to
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3717a982d89SJeremy L. Thompson 
3727a982d89SJeremy L. Thompson   @ref Backend
3737a982d89SJeremy L. Thompson **/
3747a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3757a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
376e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3777a982d89SJeremy L. Thompson }
3787a982d89SJeremy L. Thompson 
3797a982d89SJeremy L. Thompson /**
3807a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
3817a982d89SJeremy L. Thompson 
382ea61e9acSJeremy L Thompson   This function allows a Ceed context to set a delegate Ceed context.
383ea61e9acSJeremy L Thompson   All backend implementations default to the delegate Ceed context, unless overridden.
3847a982d89SJeremy L. Thompson 
385ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to set delegate of
3867a982d89SJeremy L. Thompson   @param[out] delegate Address to set the delegate to
3877a982d89SJeremy L. Thompson 
3887a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson   @ref Backend
3917a982d89SJeremy L. Thompson **/
3927a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
3937a982d89SJeremy L. Thompson   ceed->delegate   = delegate;
3947a982d89SJeremy L. Thompson   delegate->parent = ceed;
395e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3967a982d89SJeremy L. Thompson }
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson /**
3997a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
4007a982d89SJeremy L. Thompson 
401ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to retrieve delegate of
4027a982d89SJeremy L. Thompson   @param[out] delegate Address to save the delegate to
403d1d35e2fSjeremylt   @param[in]  obj_name Name of the object type to retrieve delegate for
4047a982d89SJeremy L. Thompson 
4057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4067a982d89SJeremy L. Thompson 
4077a982d89SJeremy L. Thompson   @ref Backend
4087a982d89SJeremy L. Thompson **/
409d1d35e2fSjeremylt int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) {
4107a982d89SJeremy L. Thompson   // Check for object delegate
4112b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) {
412d1d35e2fSjeremylt     if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) {
413d1d35e2fSjeremylt       *delegate = ceed->obj_delegates->delegate;
414e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
4157a982d89SJeremy L. Thompson     }
4162b730f8bSJeremy L Thompson   }
4177a982d89SJeremy L. Thompson 
4187a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
4192b730f8bSJeremy L Thompson   CeedCall(CeedGetDelegate(ceed, delegate));
420e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4217a982d89SJeremy L. Thompson }
4227a982d89SJeremy L. Thompson 
4237a982d89SJeremy L. Thompson /**
4247a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
4257a982d89SJeremy L. Thompson 
426ea61e9acSJeremy L Thompson   This function allows a Ceed context to set a delegate Ceed context for a given type of Ceed object.
427ea61e9acSJeremy L Thompson   All backend implementations default to the delegate Ceed context for this object.
428*1c66c397SJeremy L Thompson   For example, CeedSetObjectDelegate(ceed, refceed, "Basis") uses delegate implementations for all CeedBasis backend functions.
4297a982d89SJeremy L. Thompson 
430ea61e9acSJeremy L Thompson   @param[in,out] ceed     Ceed context to set delegate of
431*1c66c397SJeremy L Thompson   @param[in]     delegate Ceed context to use for delegation
432d1d35e2fSjeremylt   @param[in]     obj_name Name of the object type to set delegate for
4337a982d89SJeremy L. Thompson 
4347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4357a982d89SJeremy L. Thompson 
4367a982d89SJeremy L. Thompson   @ref Backend
4377a982d89SJeremy L. Thompson **/
438d1d35e2fSjeremylt int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) {
439d1d35e2fSjeremylt   CeedInt count = ceed->obj_delegate_count;
4407a982d89SJeremy L. Thompson 
4417a982d89SJeremy L. Thompson   // Malloc or Realloc
4427a982d89SJeremy L. Thompson   if (count) {
4432b730f8bSJeremy L Thompson     CeedCall(CeedRealloc(count + 1, &ceed->obj_delegates));
4447a982d89SJeremy L. Thompson   } else {
4452b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &ceed->obj_delegates));
4467a982d89SJeremy L. Thompson   }
447d1d35e2fSjeremylt   ceed->obj_delegate_count++;
4487a982d89SJeremy L. Thompson 
4497a982d89SJeremy L. Thompson   // Set object delegate
450d1d35e2fSjeremylt   ceed->obj_delegates[count].delegate = delegate;
4512b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(obj_name, &ceed->obj_delegates[count].obj_name));
4527a982d89SJeremy L. Thompson 
4537a982d89SJeremy L. Thompson   // Set delegate parent
4547a982d89SJeremy L. Thompson   delegate->parent = ceed;
455e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4567a982d89SJeremy L. Thompson }
4577a982d89SJeremy L. Thompson 
4587a982d89SJeremy L. Thompson /**
4597a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4607a982d89SJeremy L. Thompson 
461ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context
4627a982d89SJeremy L. Thompson   @param[out] resource Variable to store fallback resource
4637a982d89SJeremy L. Thompson 
4647a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4657a982d89SJeremy L. Thompson 
4667a982d89SJeremy L. Thompson   @ref Backend
4677a982d89SJeremy L. Thompson **/
4687a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
469d1d35e2fSjeremylt   *resource = (const char *)ceed->op_fallback_resource;
470e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4717a982d89SJeremy L. Thompson }
4727a982d89SJeremy L. Thompson 
4737a982d89SJeremy L. Thompson /**
4748687e1d4SJeremy L Thompson   @brief Get the fallback Ceed for CeedOperators
4758687e1d4SJeremy L Thompson 
476ea61e9acSJeremy L Thompson   @param[in]  ceed          Ceed context
4778687e1d4SJeremy L Thompson   @param[out] fallback_ceed Variable to store fallback Ceed
4788687e1d4SJeremy L Thompson 
4798687e1d4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4808687e1d4SJeremy L Thompson 
4818687e1d4SJeremy L Thompson   @ref Backend
4828687e1d4SJeremy L Thompson **/
4838687e1d4SJeremy L Thompson int CeedGetOperatorFallbackCeed(Ceed ceed, Ceed *fallback_ceed) {
484d04bbc78SJeremy L Thompson   if (ceed->has_valid_op_fallback_resource) {
48523d4529eSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
4862b730f8bSJeremy L Thompson     CeedDebug(ceed, "Getting fallback from %s to %s\n", ceed->resource, ceed->op_fallback_resource);
487d04bbc78SJeremy L Thompson   }
4888687e1d4SJeremy L Thompson 
489d04bbc78SJeremy L Thompson   // Create fallback Ceed if uninitalized
490d04bbc78SJeremy L Thompson   if (!ceed->op_fallback_ceed && ceed->has_valid_op_fallback_resource) {
49113f886e9SJeremy L Thompson     CeedDebug(ceed, "Creating fallback Ceed");
492d04bbc78SJeremy L Thompson 
4938687e1d4SJeremy L Thompson     Ceed        fallback_ceed;
494d04bbc78SJeremy L Thompson     const char *fallback_resource;
495d04bbc78SJeremy L Thompson 
4962b730f8bSJeremy L Thompson     CeedCall(CeedGetOperatorFallbackResource(ceed, &fallback_resource));
4972b730f8bSJeremy L Thompson     CeedCall(CeedInit(fallback_resource, &fallback_ceed));
4988687e1d4SJeremy L Thompson     fallback_ceed->op_fallback_parent = ceed;
4998687e1d4SJeremy L Thompson     fallback_ceed->Error              = ceed->Error;
5008687e1d4SJeremy L Thompson     ceed->op_fallback_ceed            = fallback_ceed;
5018687e1d4SJeremy L Thompson   }
5028687e1d4SJeremy L Thompson   *fallback_ceed = ceed->op_fallback_ceed;
5038687e1d4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5048687e1d4SJeremy L Thompson }
5058687e1d4SJeremy L Thompson 
5068687e1d4SJeremy L Thompson /**
507ea61e9acSJeremy L Thompson   @brief Set the fallback resource for CeedOperators.
5084385fb7fSSebastian Grimberg 
509ea61e9acSJeremy L Thompson   The current resource, if any, is freed by calling this function.
510ea61e9acSJeremy L Thompson   This string is freed upon the destruction of the Ceed context.
5117a982d89SJeremy L. Thompson 
512ea61e9acSJeremy L Thompson   @param[in,out] ceed     Ceed context
513ea61e9acSJeremy L Thompson   @param[in]     resource Fallback resource to set
5147a982d89SJeremy L. Thompson 
5157a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5167a982d89SJeremy L. Thompson 
5177a982d89SJeremy L. Thompson   @ref Backend
5187a982d89SJeremy L. Thompson **/
5197a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
5207a982d89SJeremy L. Thompson   // Free old
5212b730f8bSJeremy L Thompson   CeedCall(CeedFree(&ceed->op_fallback_resource));
5227a982d89SJeremy L. Thompson 
5237a982d89SJeremy L. Thompson   // Set new
5242b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(resource, (char **)&ceed->op_fallback_resource));
525d04bbc78SJeremy L Thompson 
526d04bbc78SJeremy L Thompson   // Check validity
5272b730f8bSJeremy L Thompson   ceed->has_valid_op_fallback_resource = ceed->op_fallback_resource && ceed->resource && strcmp(ceed->op_fallback_resource, ceed->resource);
528e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5297a982d89SJeremy L. Thompson }
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson /**
532ea61e9acSJeremy L Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context for a CeedOperator
5337a982d89SJeremy L. Thompson 
534ea61e9acSJeremy L Thompson   @param[in]  ceed   Ceed context
5357a982d89SJeremy L. Thompson   @param[out] parent Variable to store parent Ceed context
5367a982d89SJeremy L. Thompson 
5377a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson   @ref Backend
5407a982d89SJeremy L. Thompson **/
5417a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
542d1d35e2fSjeremylt   *parent = ceed->op_fallback_parent;
543e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5447a982d89SJeremy L. Thompson }
5457a982d89SJeremy L. Thompson 
5467a982d89SJeremy L. Thompson /**
5479525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
5489525855cSJeremy L Thompson 
549ea61e9acSJeremy L Thompson   @param[in]  ceed             Ceed to flag as deterministic
55096b902e2Sjeremylt   @param[out] is_deterministic Deterministic status to set
5519525855cSJeremy L Thompson 
5529525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5539525855cSJeremy L Thompson 
5549525855cSJeremy L Thompson   @ref Backend
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 
564ea61e9acSJeremy L Thompson   This function is used for a backend to set the function associated with the Ceed objects.
565ea61e9acSJeremy L Thompson   For example, CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate) sets the backend implementation of 'CeedVectorCreate'
5669fd66db6SSebastian Grimberg and CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply) sets the backend implementation of 'CeedBasisApply'.
5679fd66db6SSebastian Grimberg   Note, the prefix 'Ceed' is not required for the object type ("Basis" vs "CeedBasis").
5687a982d89SJeremy L. Thompson 
569ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed context for error handling
570ea61e9acSJeremy L Thompson   @param[in]  type      Type of Ceed object to set function for
5717a982d89SJeremy L. Thompson   @param[out] object    Ceed object to set function for
572ea61e9acSJeremy L Thompson   @param[in]  func_name Name of function to set
573ea61e9acSJeremy L Thompson   @param[in]  f         Function to set
5747a982d89SJeremy L. Thompson 
5757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson   @ref Backend
5787a982d89SJeremy L. Thompson **/
5792b730f8bSJeremy L Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object, const char *func_name, int (*f)()) {
580d1d35e2fSjeremylt   char lookup_name[CEED_MAX_RESOURCE_LEN + 1] = "";
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   // Build lookup name
5832b730f8bSJeremy L Thompson   if (strcmp(type, "Ceed")) strncat(lookup_name, "Ceed", CEED_MAX_RESOURCE_LEN);
584d1d35e2fSjeremylt   strncat(lookup_name, type, CEED_MAX_RESOURCE_LEN);
585d1d35e2fSjeremylt   strncat(lookup_name, func_name, CEED_MAX_RESOURCE_LEN);
5867a982d89SJeremy L. Thompson 
5877a982d89SJeremy L. Thompson   // Find and use offset
5882b730f8bSJeremy L Thompson   for (CeedInt i = 0; ceed->f_offsets[i].func_name; i++) {
589d1d35e2fSjeremylt     if (!strcmp(ceed->f_offsets[i].func_name, lookup_name)) {
590d1d35e2fSjeremylt       size_t offset          = ceed->f_offsets[i].offset;
5917a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset);  // *NOPAD*
592*1c66c397SJeremy L Thompson 
5937a982d89SJeremy L. Thompson       *fpointer = f;
594e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
5957a982d89SJeremy L. Thompson     }
5962b730f8bSJeremy L Thompson   }
5977a982d89SJeremy L. Thompson 
5987a982d89SJeremy L. Thompson   // LCOV_EXCL_START
5992b730f8bSJeremy L Thompson   return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Requested function '%s' was not found for CEED object '%s'", func_name, type);
6007a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
6017a982d89SJeremy L. Thompson }
6027a982d89SJeremy L. Thompson 
6037a982d89SJeremy L. Thompson /**
6047a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
6057a982d89SJeremy L. Thompson 
606ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed context to retrieve data of
6077a982d89SJeremy L. Thompson   @param[out] data Address to save data to
6087a982d89SJeremy L. Thompson 
6097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6107a982d89SJeremy L. Thompson 
6117a982d89SJeremy L. Thompson   @ref Backend
6127a982d89SJeremy L. Thompson **/
613777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
614777ff853SJeremy L Thompson   *(void **)data = ceed->data;
615e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6167a982d89SJeremy L. Thompson }
6177a982d89SJeremy L. Thompson 
6187a982d89SJeremy L. Thompson /**
6197a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
6207a982d89SJeremy L. Thompson 
621ea61e9acSJeremy L Thompson   @param[in,out] ceed Ceed context to set data of
622ea61e9acSJeremy L Thompson   @param[in]     data Address of data to set
6237a982d89SJeremy L. Thompson 
6247a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6257a982d89SJeremy L. Thompson 
6267a982d89SJeremy L. Thompson   @ref Backend
6277a982d89SJeremy L. Thompson **/
628777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
629777ff853SJeremy L Thompson   ceed->data = data;
630e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6317a982d89SJeremy L. Thompson }
6327a982d89SJeremy L. Thompson 
63334359f16Sjeremylt /**
63434359f16Sjeremylt   @brief Increment the reference counter for a Ceed context
63534359f16Sjeremylt 
636ea61e9acSJeremy L Thompson   @param[in,out] ceed Ceed context to increment the reference counter
63734359f16Sjeremylt 
63834359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
63934359f16Sjeremylt 
64034359f16Sjeremylt   @ref Backend
64134359f16Sjeremylt **/
6429560d06aSjeremylt int CeedReference(Ceed ceed) {
64334359f16Sjeremylt   ceed->ref_count++;
64434359f16Sjeremylt   return CEED_ERROR_SUCCESS;
64534359f16Sjeremylt }
64634359f16Sjeremylt 
6477a982d89SJeremy L. Thompson /// @}
6487a982d89SJeremy L. Thompson 
6497a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6507a982d89SJeremy L. Thompson /// Ceed Public API
6517a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6527a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6537a982d89SJeremy L. Thompson /// @{
6547a982d89SJeremy L. Thompson 
6557a982d89SJeremy L. Thompson /**
65692ee7d1cSjeremylt   @brief Get the list of available resource names for Ceed contexts
6574385fb7fSSebastian Grimberg 
658ea61e9acSJeremy L Thompson   Note: The caller is responsible for `free()`ing the resources and priorities arrays, but should not `free()` the contents of the resources
659ea61e9acSJeremy L Thompson array.
66022e44211Sjeremylt 
66192ee7d1cSjeremylt   @param[out] n          Number of available resources
66292ee7d1cSjeremylt   @param[out] resources  List of available resource names
66322e44211Sjeremylt   @param[out] priorities Resource name prioritization values, lower is better
66422e44211Sjeremylt 
66522e44211Sjeremylt   @return An error code: 0 - success, otherwise - failure
66622e44211Sjeremylt 
66722e44211Sjeremylt   @ref User
66822e44211Sjeremylt **/
66922e44211Sjeremylt // LCOV_EXCL_START
6702b730f8bSJeremy L Thompson int CeedRegistryGetList(size_t *n, char ***const resources, CeedInt **priorities) {
671d0c91ce9Sjeremylt   *n         = 0;
6729ff86846Sjeremylt   *resources = malloc(num_backends * sizeof(**resources));
6736574a04fSJeremy L Thompson   CeedCheck(resources, NULL, CEED_ERROR_MAJOR, "malloc() failure");
6749ff86846Sjeremylt   if (priorities) {
6759ff86846Sjeremylt     *priorities = malloc(num_backends * sizeof(**priorities));
6766574a04fSJeremy L Thompson     CeedCheck(priorities, NULL, CEED_ERROR_MAJOR, "malloc() failure");
6779ff86846Sjeremylt   }
67822e44211Sjeremylt   for (size_t i = 0; i < num_backends; i++) {
679d0c91ce9Sjeremylt     // Only report compiled backends
680d0c91ce9Sjeremylt     if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) {
68122e44211Sjeremylt       *resources[i] = backends[i].prefix;
6829ff86846Sjeremylt       if (priorities) *priorities[i] = backends[i].priority;
683d0c91ce9Sjeremylt       *n += 1;
684d0c91ce9Sjeremylt     }
685d0c91ce9Sjeremylt   }
6866574a04fSJeremy L Thompson   CeedCheck(*n, NULL, CEED_ERROR_MAJOR, "No backends installed");
687d0c91ce9Sjeremylt   *resources = realloc(*resources, *n * sizeof(**resources));
6886574a04fSJeremy L Thompson   CeedCheck(resources, NULL, CEED_ERROR_MAJOR, "realloc() failure");
689d0c91ce9Sjeremylt   if (priorities) {
690d0c91ce9Sjeremylt     *priorities = realloc(*priorities, *n * sizeof(**priorities));
6916574a04fSJeremy L Thompson     CeedCheck(priorities, NULL, CEED_ERROR_MAJOR, "realloc() failure");
69222e44211Sjeremylt   }
69322e44211Sjeremylt   return CEED_ERROR_SUCCESS;
69445f1e315Sjeremylt }
69522e44211Sjeremylt // LCOV_EXCL_STOP
69622e44211Sjeremylt 
69722e44211Sjeremylt /**
698d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
6994385fb7fSSebastian Grimberg 
700ea61e9acSJeremy L Thompson   Note: Prefixing the resource with "help:" (e.g. "help:/cpu/self") will result in CeedInt printing the current libCEED version number and a
701ea61e9acSJeremy L Thompson list of current available backend resources to stderr.
702b11c1e72Sjeremylt 
703ea61e9acSJeremy L Thompson   @param[in]  resource Resource to use, e.g., "/cpu/self"
704ea61e9acSJeremy L Thompson   @param[out] ceed     The library context
705b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
706b11c1e72Sjeremylt 
707b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
708dfdf5a53Sjeremylt 
7097a982d89SJeremy L. Thompson   @ref User
710b11c1e72Sjeremylt **/
711d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
7122b730f8bSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX, match_priority = CEED_MAX_BACKEND_PRIORITY, priority;
713d7b241e6Sjeremylt 
714fe2413ffSjeremylt   // Find matching backend
7156574a04fSJeremy L Thompson   CeedCheck(resource, NULL, CEED_ERROR_MAJOR, "No resource provided");
7162b730f8bSJeremy L Thompson   CeedCall(CeedRegisterAll());
71713873f79Sjeremylt 
71822e44211Sjeremylt   // Check for help request
71922e44211Sjeremylt   const char *help_prefix = "help";
7202b730f8bSJeremy L Thompson   size_t      match_help  = 0;
7212b730f8bSJeremy L Thompson   while (match_help < 4 && resource[match_help] == help_prefix[match_help]) match_help++;
72222e44211Sjeremylt   if (match_help == 4) {
7232b730f8bSJeremy L Thompson     fprintf(stderr, "libCEED version: %d.%d%d%s\n", CEED_VERSION_MAJOR, CEED_VERSION_MINOR, CEED_VERSION_PATCH,
72422e44211Sjeremylt             CEED_VERSION_RELEASE ? "" : "+development");
72592ee7d1cSjeremylt     fprintf(stderr, "Available backend resources:\n");
72622e44211Sjeremylt     for (size_t i = 0; i < num_backends; i++) {
727d0c91ce9Sjeremylt       // Only report compiled backends
7282b730f8bSJeremy L Thompson       if (backends[i].priority < CEED_MAX_BACKEND_PRIORITY) fprintf(stderr, "  %s\n", backends[i].prefix);
72922e44211Sjeremylt     }
73022e44211Sjeremylt     fflush(stderr);
73122e44211Sjeremylt     match_help = 5;  // Delineating character expected
73222e44211Sjeremylt   } else {
73322e44211Sjeremylt     match_help = 0;
73422e44211Sjeremylt   }
73522e44211Sjeremylt 
736ea61e9acSJeremy L Thompson   // Find best match, computed as number of matching characters from requested resource stem
7372b730f8bSJeremy L Thompson   size_t stem_length = 0;
7382b730f8bSJeremy L Thompson   while (resource[stem_length + match_help] && resource[stem_length + match_help] != ':') stem_length++;
739d7b241e6Sjeremylt   for (size_t i = 0; i < num_backends; i++) {
7402b730f8bSJeremy L Thompson     size_t      n      = 0;
741d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
7422b730f8bSJeremy L Thompson     while (prefix[n] && prefix[n] == resource[n + match_help]) n++;
743d7b241e6Sjeremylt     priority = backends[i].priority;
744d1d35e2fSjeremylt     if (n > match_len || (n == match_len && match_priority > priority)) {
745d1d35e2fSjeremylt       match_len      = n;
746d1d35e2fSjeremylt       match_priority = priority;
747f7e22acaSJeremy L Thompson       match_index    = i;
748d7b241e6Sjeremylt     }
749d7b241e6Sjeremylt   }
7509c9a0587SLeila Ghaffari   // Using Levenshtein distance to find closest match
7519c9a0587SLeila Ghaffari   if (match_len <= 1 || match_len != stem_length) {
752203015caSLeila Ghaffari     // LCOV_EXCL_START
7539c9a0587SLeila Ghaffari     size_t lev_dis   = UINT_MAX;
754f7e22acaSJeremy L Thompson     size_t lev_index = UINT_MAX, lev_priority = CEED_MAX_BACKEND_PRIORITY;
7559c9a0587SLeila Ghaffari     for (size_t i = 0; i < num_backends; i++) {
7569c9a0587SLeila Ghaffari       const char *prefix        = backends[i].prefix;
7579c9a0587SLeila Ghaffari       size_t      prefix_length = strlen(backends[i].prefix);
7589c9a0587SLeila Ghaffari       size_t      min_len       = (prefix_length < stem_length) ? prefix_length : stem_length;
759092904ddSLeila Ghaffari       size_t      column[min_len + 1];
760092904ddSLeila Ghaffari       for (size_t j = 0; j <= min_len; j++) column[j] = j;
7619c9a0587SLeila Ghaffari       for (size_t j = 1; j <= min_len; j++) {
7629c9a0587SLeila Ghaffari         column[0] = j;
7639c9a0587SLeila Ghaffari         for (size_t k = 1, last_diag = j - 1; k <= min_len; k++) {
764092904ddSLeila Ghaffari           size_t old_diag = column[k];
7659c9a0587SLeila Ghaffari           size_t min_1    = (column[k] < column[k - 1]) ? column[k] + 1 : column[k - 1] + 1;
7669c9a0587SLeila Ghaffari           size_t min_2    = last_diag + (resource[k - 1] == prefix[j - 1] ? 0 : 1);
7679c9a0587SLeila Ghaffari           column[k]       = (min_1 < min_2) ? min_1 : min_2;
7689c9a0587SLeila Ghaffari           last_diag       = old_diag;
7699c9a0587SLeila Ghaffari         }
7709c9a0587SLeila Ghaffari       }
7719c9a0587SLeila Ghaffari       size_t n = column[min_len];
7729c9a0587SLeila Ghaffari       priority = backends[i].priority;
7732b730f8bSJeremy L Thompson       if (n < lev_dis || (n == lev_dis && lev_priority > priority)) {
7749c9a0587SLeila Ghaffari         lev_dis      = n;
7759c9a0587SLeila Ghaffari         lev_priority = priority;
776f7e22acaSJeremy L Thompson         lev_index    = i;
7779c9a0587SLeila Ghaffari       }
7789c9a0587SLeila Ghaffari     }
779f7e22acaSJeremy L Thompson     const char *prefix_lev = backends[lev_index].prefix;
7802b730f8bSJeremy L Thompson     size_t      lev_length = 0;
7812b730f8bSJeremy L Thompson     while (prefix_lev[lev_length] && prefix_lev[lev_length] != '\0') lev_length++;
7829c9a0587SLeila Ghaffari     size_t m = (lev_length < stem_length) ? lev_length : stem_length;
7836574a04fSJeremy L Thompson     if (lev_dis + 1 >= m) return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s", resource);
7846574a04fSJeremy L Thompson     else return CeedError(NULL, CEED_ERROR_MAJOR, "No suitable backend: %s\nClosest match: %s", resource, backends[lev_index].prefix);
785203015caSLeila Ghaffari     // LCOV_EXCL_STOP
7869c9a0587SLeila Ghaffari   }
787fe2413ffSjeremylt 
788fe2413ffSjeremylt   // Setup Ceed
7892b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, ceed));
7902b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &(*ceed)->jit_source_roots));
791bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
7922b730f8bSJeremy L Thompson   if (!ceed_error_handler) ceed_error_handler = "abort";
7932b730f8bSJeremy L Thompson   if (!strcmp(ceed_error_handler, "exit")) (*ceed)->Error = CeedErrorExit;
7942b730f8bSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store")) (*ceed)->Error = CeedErrorStore;
7952b730f8bSJeremy L Thompson   else (*ceed)->Error = CeedErrorAbort;
796d1d35e2fSjeremylt   memcpy((*ceed)->err_msg, "No error message stored", 24);
797d1d35e2fSjeremylt   (*ceed)->ref_count = 1;
798d7b241e6Sjeremylt   (*ceed)->data      = NULL;
799fe2413ffSjeremylt 
800fe2413ffSjeremylt   // Set lookup table
801d1d35e2fSjeremylt   FOffset f_offsets[] = {
8026e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, Error),
8035ae360d4SJeremy L Thompson       CEED_FTABLE_ENTRY(Ceed, SetStream),
8046e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
8056e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, Destroy),
806f8902d9eSjeremylt       CEED_FTABLE_ENTRY(Ceed, VectorCreate),
8076e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
8086e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
8096e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
8106e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
81150c301a5SRezgar Shakeri       CEED_FTABLE_ENTRY(Ceed, BasisCreateHdiv),
812c4e3f59bSSebastian Grimberg       CEED_FTABLE_ENTRY(Ceed, BasisCreateHcurl),
8136e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
8146e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
815777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
8166e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
8176e79d475Sjeremylt       CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
8189c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, HasValidArray),
8199c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, HasBorrowedArrayOfType),
8206e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, SetArray),
8216a6c615bSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, TakeArray),
8226e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, SetValue),
823f48ed27dSnbeams       CEED_FTABLE_ENTRY(CeedVector, SyncArray),
8246e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, GetArray),
8256e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
8269c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, GetArrayWrite),
8276e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
8286e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
829547d9b97Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, Norm),
830e0dd3b27Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, Scale),
8310f7fd0f8Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, AXPY),
8325fb68f37SKaren (Ren) Stengel       CEED_FTABLE_ENTRY(CeedVector, AXPBY),
8330f7fd0f8Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, PointwiseMult),
834d99fa3c5SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
8356e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedVector, Destroy),
8366e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
837f30b1135SSebastian Grimberg       CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyUnsigned),
8387c1dbaffSSebastian Grimberg       CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyUnoriented),
8396e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
840bd33150aSjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
84177d1c127SSebastian Grimberg       CEED_FTABLE_ENTRY(CeedElemRestriction, GetOrientations),
84277d1c127SSebastian Grimberg       CEED_FTABLE_ENTRY(CeedElemRestriction, GetCurlOrientations),
8436e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
8446e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedBasis, Apply),
845c8c3fa7dSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedBasis, ApplyAtPoints),
8466e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedBasis, Destroy),
8476e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
8486e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
8496e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, Apply),
8508c84ac63Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
8518c84ac63Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
8526e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
8539c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, HasValidData),
8549c774eddSJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, HasBorrowedDataOfType),
855777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
856891038deSjeremylt       CEED_FTABLE_ENTRY(CeedQFunctionContext, TakeData),
857777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
85828bfd0b7SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, GetDataRead),
859777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
86028bfd0b7SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreDataRead),
8612e64a2b9SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, DataDestroy),
862777ff853SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
86380ac2e43SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
86470a7ffb3SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunctionUpdate),
86580ac2e43SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
8669e9210b8SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
86780ac2e43SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
8689e9210b8SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
869e2f04181SAndrew T. Barker       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSymbolic),
870e2f04181SAndrew T. Barker       CEED_FTABLE_ENTRY(CeedOperator, LinearAssemble),
871cefa2673SJeremy L Thompson       CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleSingle),
872713f43c3Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
8736e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, Apply),
874250756a7Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
875cae8b89aSjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
876250756a7Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
8776e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
8786e79d475Sjeremylt       CEED_FTABLE_ENTRY(CeedOperator, Destroy),
8796e79d475Sjeremylt       {NULL, 0}  // End of lookup table - used in SetBackendFunction loop
8801dfeef1dSjeremylt   };
881fe2413ffSjeremylt 
8822b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(sizeof(f_offsets), &(*ceed)->f_offsets));
883d1d35e2fSjeremylt   memcpy((*ceed)->f_offsets, f_offsets, sizeof(f_offsets));
884fe2413ffSjeremylt 
8855107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
886e2f04181SAndrew T. Barker   const char fallbackresource[] = "";
8872b730f8bSJeremy L Thompson   CeedCall(CeedSetOperatorFallbackResource(*ceed, fallbackresource));
8885107b09fSJeremy L Thompson 
88960f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
890*1c66c397SJeremy L Thompson   (*ceed)->is_debug = getenv("CEED_DEBUG") || getenv("DEBUG") || getenv("DBG");
89160f9e2d6SJeremy L Thompson 
89222e44211Sjeremylt   // Copy resource prefix, if backend setup successful
8932b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(backends[match_index].prefix, (char **)&(*ceed)->resource));
894ee5a26f2SJeremy L Thompson 
895ee5a26f2SJeremy L Thompson   // Set default JiT source root
896ea61e9acSJeremy L Thompson   // Note: there will always be the default root for every Ceed but all additional paths are added to the top-most parent
8972b730f8bSJeremy L Thompson   CeedCall(CeedAddJitSourceRoot(*ceed, (char *)CeedJitSourceRootDefault));
898ee5a26f2SJeremy L Thompson 
899d04bbc78SJeremy L Thompson   // Backend specific setup
9002b730f8bSJeremy L Thompson   CeedCall(backends[match_index].init(&resource[match_help], *ceed));
901d04bbc78SJeremy L Thompson 
902e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
903d7b241e6Sjeremylt }
904d7b241e6Sjeremylt 
905d7b241e6Sjeremylt /**
9065ae360d4SJeremy L Thompson   @brief Set the GPU stream for a Ceed context
9075ae360d4SJeremy L Thompson 
9085ae360d4SJeremy L Thompson   @param[in,out] ceed   Ceed context to set the stream
9095ae360d4SJeremy L Thompson   @param[in]     handle Handle to GPU stream
9105ae360d4SJeremy L Thompson 
9115ae360d4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9125ae360d4SJeremy L Thompson 
9135ae360d4SJeremy L Thompson   @ref User
9145ae360d4SJeremy L Thompson **/
9155ae360d4SJeremy L Thompson int CeedSetStream(Ceed ceed, void *handle) {
9165ae360d4SJeremy L Thompson   CeedCheck(handle, ceed, CEED_ERROR_INCOMPATIBLE, "Stream handle must be non-null");
9179ffb25e0SJames Wright   if (ceed->SetStream) {
9185ae360d4SJeremy L Thompson     CeedCall(ceed->SetStream(ceed, handle));
9199ffb25e0SJames Wright   } else {
9209ffb25e0SJames Wright     Ceed delegate;
9219ffb25e0SJames Wright     CeedCall(CeedGetDelegate(ceed, &delegate));
9225ae360d4SJeremy L Thompson 
92328ce3d2aSJeremy L Thompson     if (delegate) CeedCall(CeedSetStream(delegate, handle));
92428ce3d2aSJeremy L Thompson     else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support setting stream");
9259ffb25e0SJames Wright   }
9265ae360d4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9275ae360d4SJeremy L Thompson }
9285ae360d4SJeremy L Thompson 
9295ae360d4SJeremy L Thompson /**
930ea61e9acSJeremy L Thompson   @brief Copy the pointer to a Ceed context.
9314385fb7fSSebastian Grimberg 
932512bb800SJeremy L Thompson   Both pointers should be destroyed with `CeedDestroy()`.
933512bb800SJeremy L Thompson 
934512bb800SJeremy L Thompson   Note: If the value of `ceed_copy` passed to this function is non-NULL, then it is assumed that `ceed_copy` is a pointer to a Ceed context.
935512bb800SJeremy L Thompson         This Ceed context will be destroyed if `ceed_copy` is the only reference to this Ceed context.
9369560d06aSjeremylt 
937ea61e9acSJeremy L Thompson   @param[in]     ceed      Ceed context to copy reference to
938ea61e9acSJeremy L Thompson   @param[in,out] ceed_copy Variable to store copied reference
9399560d06aSjeremylt 
9409560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
9419560d06aSjeremylt 
9429560d06aSjeremylt   @ref User
9439560d06aSjeremylt **/
9449560d06aSjeremylt int CeedReferenceCopy(Ceed ceed, Ceed *ceed_copy) {
9452b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
9462b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(ceed_copy));
9479560d06aSjeremylt   *ceed_copy = ceed;
9489560d06aSjeremylt   return CEED_ERROR_SUCCESS;
9499560d06aSjeremylt }
9509560d06aSjeremylt 
9519560d06aSjeremylt /**
9527a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
9532f86a920SJeremy L Thompson 
954ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to get resource name of
9557a982d89SJeremy L. Thompson   @param[out] resource Variable to store resource name
9562f86a920SJeremy L Thompson 
9572f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9582f86a920SJeremy L Thompson 
9597a982d89SJeremy L. Thompson   @ref User
9605107b09fSJeremy L Thompson **/
9617a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
9627a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
963e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9645107b09fSJeremy L Thompson }
9655107b09fSJeremy L Thompson 
9665107b09fSJeremy L Thompson /**
967d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
968c907536fSjeremylt 
969ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed context to get preferred memory type of
970d1d35e2fSjeremylt   @param[out] mem_type Address to save preferred memory type to
971c907536fSjeremylt 
972c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
973c907536fSjeremylt 
9747a982d89SJeremy L. Thompson   @ref User
975c907536fSjeremylt **/
976d1d35e2fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) {
977c907536fSjeremylt   if (ceed->GetPreferredMemType) {
9782b730f8bSJeremy L Thompson     CeedCall(ceed->GetPreferredMemType(mem_type));
979c907536fSjeremylt   } else {
980c263cd57Sjeremylt     Ceed delegate;
9812b730f8bSJeremy L Thompson     CeedCall(CeedGetDelegate(ceed, &delegate));
982c263cd57Sjeremylt 
983c263cd57Sjeremylt     if (delegate) {
9842b730f8bSJeremy L Thompson       CeedCall(CeedGetPreferredMemType(delegate, mem_type));
985c263cd57Sjeremylt     } else {
986d1d35e2fSjeremylt       *mem_type = CEED_MEM_HOST;
987c907536fSjeremylt     }
988c263cd57Sjeremylt   }
989e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
990c907536fSjeremylt }
991c907536fSjeremylt 
992c907536fSjeremylt /**
9939525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
9949525855cSJeremy L Thompson 
9959525855cSJeremy L Thompson   @param[in]  ceed             Ceed
996d1d35e2fSjeremylt   @param[out] is_deterministic Variable to store deterministic status
9979525855cSJeremy L Thompson 
9989525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9999525855cSJeremy L Thompson 
10009525855cSJeremy L Thompson   @ref User
10019525855cSJeremy L Thompson **/
1002d1d35e2fSjeremylt int CeedIsDeterministic(Ceed ceed, bool *is_deterministic) {
1003d1d35e2fSjeremylt   *is_deterministic = ceed->is_deterministic;
1004e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10059525855cSJeremy L Thompson }
10069525855cSJeremy L Thompson 
10079525855cSJeremy L Thompson /**
1008ee5a26f2SJeremy L Thompson   @brief Set additional JiT source root for Ceed
1009ee5a26f2SJeremy L Thompson 
1010ea61e9acSJeremy L Thompson   @param[in,out] ceed            Ceed
1011ee5a26f2SJeremy L Thompson   @param[in]     jit_source_root Absolute path to additional JiT source directory
1012ee5a26f2SJeremy L Thompson 
1013ee5a26f2SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1014ee5a26f2SJeremy L Thompson 
1015ee5a26f2SJeremy L Thompson   @ref User
1016ee5a26f2SJeremy L Thompson **/
1017ee5a26f2SJeremy L Thompson int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root) {
10186155f12fSJeremy L Thompson   Ceed ceed_parent;
1019ee5a26f2SJeremy L Thompson 
10202b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(ceed, &ceed_parent));
10216155f12fSJeremy L Thompson 
10226155f12fSJeremy L Thompson   CeedInt index       = ceed_parent->num_jit_source_roots;
1023ee5a26f2SJeremy L Thompson   size_t  path_length = strlen(jit_source_root);
1024*1c66c397SJeremy L Thompson 
10252b730f8bSJeremy L Thompson   CeedCall(CeedRealloc(index + 1, &ceed_parent->jit_source_roots));
10262b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(path_length + 1, &ceed_parent->jit_source_roots[index]));
1027d602d780SJeremy L Thompson   memcpy(ceed_parent->jit_source_roots[index], jit_source_root, path_length);
10286155f12fSJeremy L Thompson   ceed_parent->num_jit_source_roots++;
1029ee5a26f2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1030ee5a26f2SJeremy L Thompson }
1031ee5a26f2SJeremy L Thompson 
1032ee5a26f2SJeremy L Thompson /**
10330a0da059Sjeremylt   @brief View a Ceed
10340a0da059Sjeremylt 
10350a0da059Sjeremylt   @param[in] ceed   Ceed to view
10360a0da059Sjeremylt   @param[in] stream Filestream to write to
10370a0da059Sjeremylt 
10380a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
10390a0da059Sjeremylt 
10400a0da059Sjeremylt   @ref User
10410a0da059Sjeremylt **/
10420a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
1043d1d35e2fSjeremylt   CeedMemType mem_type;
10440a0da059Sjeremylt 
10452b730f8bSJeremy L Thompson   CeedCall(CeedGetPreferredMemType(ceed, &mem_type));
10460a0da059Sjeremylt 
10472b730f8bSJeremy L Thompson   fprintf(stream,
10482b730f8bSJeremy L Thompson           "Ceed\n"
10490a0da059Sjeremylt           "  Ceed Resource: %s\n"
10500a0da059Sjeremylt           "  Preferred MemType: %s\n",
1051d1d35e2fSjeremylt           ceed->resource, CeedMemTypes[mem_type]);
1052e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10530a0da059Sjeremylt }
10540a0da059Sjeremylt 
10550a0da059Sjeremylt /**
1056b11c1e72Sjeremylt   @brief Destroy a Ceed context
1057d7b241e6Sjeremylt 
1058ea61e9acSJeremy L Thompson   @param[in,out] ceed Address of Ceed context to destroy
1059b11c1e72Sjeremylt 
1060b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1061dfdf5a53Sjeremylt 
10627a982d89SJeremy L. Thompson   @ref User
1063b11c1e72Sjeremylt **/
1064d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
1065ad6481ceSJeremy L Thompson   if (!*ceed || --(*ceed)->ref_count > 0) {
1066ad6481ceSJeremy L Thompson     *ceed = NULL;
1067ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1068ad6481ceSJeremy L Thompson   }
10692b730f8bSJeremy L Thompson   if ((*ceed)->delegate) CeedCall(CeedDestroy(&(*ceed)->delegate));
10700ace9bf2Sjeremylt 
1071d1d35e2fSjeremylt   if ((*ceed)->obj_delegate_count > 0) {
107292ae7e47SJeremy L Thompson     for (CeedInt i = 0; i < (*ceed)->obj_delegate_count; i++) {
10732b730f8bSJeremy L Thompson       CeedCall(CeedDestroy(&((*ceed)->obj_delegates[i].delegate)));
10742b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*ceed)->obj_delegates[i].obj_name));
1075aefd8378Sjeremylt     }
10762b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ceed)->obj_delegates));
1077aefd8378Sjeremylt   }
10780ace9bf2Sjeremylt 
10792b730f8bSJeremy L Thompson   if ((*ceed)->Destroy) CeedCall((*ceed)->Destroy(*ceed));
10800ace9bf2Sjeremylt 
108192ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*ceed)->num_jit_source_roots; i++) {
10822b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ceed)->jit_source_roots[i]));
1083032e71eaSJeremy L Thompson   }
10842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->jit_source_roots));
1085032e71eaSJeremy L Thompson 
10862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->f_offsets));
10872b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->resource));
10882b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*ceed)->op_fallback_ceed));
10892b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ceed)->op_fallback_resource));
10902b730f8bSJeremy L Thompson   CeedCall(CeedFree(ceed));
1091e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1092d7b241e6Sjeremylt }
1093d7b241e6Sjeremylt 
1094f9982c62SWill Pazner // LCOV_EXCL_START
1095f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
10962b730f8bSJeremy L Thompson   if (ceed->parent) return CeedErrorFormat(ceed->parent, format, args);
10972b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedErrorFormat(ceed->op_fallback_parent, format, args);
109878464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
109978464608Sjeremylt   vsnprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, format, *args);  // NOLINT
1100d1d35e2fSjeremylt   return ceed->err_msg;
1101f9982c62SWill Pazner }
1102f9982c62SWill Pazner // LCOV_EXCL_STOP
1103f9982c62SWill Pazner 
11047a982d89SJeremy L. Thompson /**
11057a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
11067a982d89SJeremy L. Thompson 
11077a982d89SJeremy L. Thompson   @ref Developer
11087a982d89SJeremy L. Thompson **/
11092b730f8bSJeremy L Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func, int ecode, const char *format, ...) {
11107a982d89SJeremy L. Thompson   va_list args;
1111d1d35e2fSjeremylt   int     ret_val;
1112*1c66c397SJeremy L Thompson 
11137a982d89SJeremy L. Thompson   va_start(args, format);
11147a982d89SJeremy L. Thompson   if (ceed) {
1115d1d35e2fSjeremylt     ret_val = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
11167a982d89SJeremy L. Thompson   } else {
1117b0d62198Sjeremylt     // LCOV_EXCL_START
1118477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
11192b730f8bSJeremy L Thompson     if (!ceed_error_handler) ceed_error_handler = "abort";
11202b730f8bSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return")) ret_val = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
1121477729cfSJeremy L Thompson     else
1122477729cfSJeremy L Thompson       // This function will not return
1123d1d35e2fSjeremylt       ret_val = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
11247a982d89SJeremy L. Thompson   }
11257a982d89SJeremy L. Thompson   va_end(args);
1126d1d35e2fSjeremylt   return ret_val;
1127b0d62198Sjeremylt   // LCOV_EXCL_STOP
11287a982d89SJeremy L. Thompson }
11297a982d89SJeremy L. Thompson 
1130477729cfSJeremy L Thompson /**
1131477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
1132477729cfSJeremy L Thompson 
1133477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1134477729cfSJeremy L Thompson 
1135477729cfSJeremy L Thompson   @ref Developer
1136477729cfSJeremy L Thompson **/
1137477729cfSJeremy L Thompson // LCOV_EXCL_START
11382b730f8bSJeremy L Thompson int CeedErrorReturn(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
1139d1d35e2fSjeremylt   return err_code;
1140477729cfSJeremy L Thompson }
1141477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1142477729cfSJeremy L Thompson 
1143477729cfSJeremy L Thompson /**
1144ea61e9acSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns the error.
1145477729cfSJeremy L Thompson 
1146477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1147477729cfSJeremy L Thompson 
1148477729cfSJeremy L Thompson   @ref Developer
1149477729cfSJeremy L Thompson **/
1150477729cfSJeremy L Thompson // LCOV_EXCL_START
11512b730f8bSJeremy L Thompson int CeedErrorStore(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
11522b730f8bSJeremy L Thompson   if (ceed->parent) return CeedErrorStore(ceed->parent, filename, line_no, func, err_code, format, args);
11532b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedErrorStore(ceed->op_fallback_parent, filename, line_no, func, err_code, format, args);
1154477729cfSJeremy L Thompson 
1155477729cfSJeremy L Thompson   // Build message
1156*1c66c397SJeremy L Thompson   int len = snprintf(ceed->err_msg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ", filename, line_no, func);
115778464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
115878464608Sjeremylt   vsnprintf(ceed->err_msg + len, CEED_MAX_RESOURCE_LEN - len, format, *args);  // NOLINT
1159d1d35e2fSjeremylt   return err_code;
1160477729cfSJeremy L Thompson }
1161477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1162477729cfSJeremy L Thompson 
1163477729cfSJeremy L Thompson /**
1164477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
1165477729cfSJeremy L Thompson 
1166477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1167477729cfSJeremy L Thompson 
1168477729cfSJeremy L Thompson   @ref Developer
1169477729cfSJeremy L Thompson **/
1170477729cfSJeremy L Thompson // LCOV_EXCL_START
11712b730f8bSJeremy L Thompson int CeedErrorAbort(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
1172d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
1173f9982c62SWill Pazner   vfprintf(stderr, format, *args);
1174477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1175477729cfSJeremy L Thompson   abort();
1176d1d35e2fSjeremylt   return err_code;
1177477729cfSJeremy L Thompson }
1178477729cfSJeremy L Thompson // LCOV_EXCL_STOP
1179477729cfSJeremy L Thompson 
1180477729cfSJeremy L Thompson /**
1181477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
1182477729cfSJeremy L Thompson 
1183477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
1184477729cfSJeremy L Thompson 
1185ea61e9acSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit() handlers (e.g., as used by gcov) are run.
1186477729cfSJeremy L Thompson 
1187477729cfSJeremy L Thompson   @ref Developer
1188477729cfSJeremy L Thompson **/
11892b730f8bSJeremy L Thompson int CeedErrorExit(Ceed ceed, const char *filename, int line_no, const char *func, int err_code, const char *format, va_list *args) {
1190d1d35e2fSjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, line_no, func);
119178464608Sjeremylt   // Using pointer to va_list for better FFI, but clang-tidy can't verify va_list is initalized
119278464608Sjeremylt   vfprintf(stderr, format, *args);  // NOLINT
1193477729cfSJeremy L Thompson   fprintf(stderr, "\n");
1194d1d35e2fSjeremylt   exit(err_code);
1195d1d35e2fSjeremylt   return err_code;
1196477729cfSJeremy L Thompson }
1197477729cfSJeremy L Thompson 
1198477729cfSJeremy L Thompson /**
1199477729cfSJeremy L Thompson   @brief Set error handler
1200477729cfSJeremy L Thompson 
1201ea61e9acSJeremy L Thompson   A default error handler is set in CeedInit().
1202ea61e9acSJeremy L Thompson   Use this function to change the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined error handler.
1203477729cfSJeremy L Thompson 
1204477729cfSJeremy L Thompson   @ref Developer
1205477729cfSJeremy L Thompson **/
1206d1d35e2fSjeremylt int CeedSetErrorHandler(Ceed ceed, CeedErrorHandler handler) {
1207d1d35e2fSjeremylt   ceed->Error = handler;
1208d1d35e2fSjeremylt   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, handler);
12092b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) CeedSetErrorHandler(ceed->obj_delegates[i].delegate, handler);
1210e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1211477729cfSJeremy L Thompson }
1212477729cfSJeremy L Thompson 
1213477729cfSJeremy L Thompson /**
1214477729cfSJeremy L Thompson   @brief Get error message
1215477729cfSJeremy L Thompson 
1216ea61e9acSJeremy L Thompson   The error message is only stored when using the error handler CeedErrorStore()
1217477729cfSJeremy L Thompson 
1218ea61e9acSJeremy L Thompson   @param[in]  ceed    Ceed context to retrieve error message
1219d1d35e2fSjeremylt   @param[out] err_msg Char pointer to hold error message
1220477729cfSJeremy L Thompson 
1221477729cfSJeremy L Thompson   @ref Developer
1222477729cfSJeremy L Thompson **/
1223d1d35e2fSjeremylt int CeedGetErrorMessage(Ceed ceed, const char **err_msg) {
12242b730f8bSJeremy L Thompson   if (ceed->parent) return CeedGetErrorMessage(ceed->parent, err_msg);
12252b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedGetErrorMessage(ceed->op_fallback_parent, err_msg);
1226d1d35e2fSjeremylt   *err_msg = ceed->err_msg;
1227e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1228477729cfSJeremy L Thompson }
1229477729cfSJeremy L Thompson 
1230477729cfSJeremy L Thompson /**
1231477729cfSJeremy L Thompson   @brief Restore error message
1232477729cfSJeremy L Thompson 
1233ea61e9acSJeremy L Thompson   The error message is only stored when using the error handler CeedErrorStore()
1234477729cfSJeremy L Thompson 
1235ea61e9acSJeremy L Thompson   @param[in]  ceed    Ceed context 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) {
12412b730f8bSJeremy L Thompson   if (ceed->parent) return CeedResetErrorMessage(ceed->parent, err_msg);
12422b730f8bSJeremy L Thompson   if (ceed->op_fallback_parent) return CeedResetErrorMessage(ceed->op_fallback_parent, err_msg);
1243d1d35e2fSjeremylt   *err_msg = NULL;
1244d1d35e2fSjeremylt   memcpy(ceed->err_msg, "No error message stored", 24);
1245e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1246477729cfSJeremy L Thompson }
1247477729cfSJeremy L Thompson 
12481070991dSJed Brown /**
12491070991dSJed Brown   @brief Get libCEED library version info
12501070991dSJed Brown 
1251ea61e9acSJeremy L Thompson   libCEED version numbers have the form major.minor.patch.
1252ea61e9acSJeremy L Thompson   Non-release versions may contain unstable interfaces.
12531070991dSJed Brown 
12541070991dSJed Brown   @param[out] major   Major version of the library
12551070991dSJed Brown   @param[out] minor   Minor version of the library
12561070991dSJed Brown   @param[out] patch   Patch (subminor) version of the library
12571070991dSJed Brown   @param[out] release True for releases; false for development branches.
12581070991dSJed Brown 
12591070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
12601070991dSJed Brown 
12611070991dSJed Brown   @sa CEED_VERSION_GE()
12621070991dSJed Brown 
12631070991dSJed Brown   @ref Developer
12641070991dSJed Brown */
12651070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
12661070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
12671070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
12681070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
12691070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
12701070991dSJed Brown   return 0;
12711070991dSJed Brown }
12721070991dSJed Brown 
127380a9ef05SNatalie Beams int CeedGetScalarType(CeedScalarType *scalar_type) {
127480a9ef05SNatalie Beams   *scalar_type = CEED_SCALAR_TYPE;
127580a9ef05SNatalie Beams   return 0;
127680a9ef05SNatalie Beams }
127780a9ef05SNatalie Beams 
1278d7b241e6Sjeremylt /// @}
1279