xref: /libCEED/rust/libceed-sys/c-src/interface/ceed.c (revision fc6bbced940bdfff573ee2079e5cb2caff7bebe9)
1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details.
4d7b241e6Sjeremylt //
5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and
8d7b241e6Sjeremylt // source code availability see http://github.com/ceed.
9d7b241e6Sjeremylt //
10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early
15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16d7b241e6Sjeremylt 
17d7b241e6Sjeremylt #define _POSIX_C_SOURCE 200112
18d7b241e6Sjeremylt #include <ceed-impl.h>
19d863ab9bSjeremylt #include <ceed-backend.h>
20aedaa0e5Sjeremylt #include <limits.h>
21d7b241e6Sjeremylt #include <stdarg.h>
226e79d475Sjeremylt #include <stddef.h>
23d7b241e6Sjeremylt #include <stdio.h>
24d7b241e6Sjeremylt #include <stdlib.h>
25d7b241e6Sjeremylt #include <string.h>
26d7b241e6Sjeremylt 
27d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP
28d7b241e6Sjeremylt static CeedRequest ceed_request_immediate;
29d7b241e6Sjeremylt static CeedRequest ceed_request_ordered;
30d7b241e6Sjeremylt 
31d7b241e6Sjeremylt static struct {
32d7b241e6Sjeremylt   char prefix[CEED_MAX_RESOURCE_LEN];
33d7b241e6Sjeremylt   int (*init)(const char *resource, Ceed f);
34d7b241e6Sjeremylt   unsigned int priority;
35d7b241e6Sjeremylt } backends[32];
36d7b241e6Sjeremylt static size_t num_backends;
37fe2413ffSjeremylt 
386e79d475Sjeremylt #define CEED_FTABLE_ENTRY(class, method) \
396e79d475Sjeremylt   {#class #method, offsetof(struct class ##_private, method)}
40d7b241e6Sjeremylt /// @endcond
41d7b241e6Sjeremylt 
42d7b241e6Sjeremylt /// @file
43d7b241e6Sjeremylt /// Implementation of core components of Ceed library
447a982d89SJeremy L. Thompson 
457a982d89SJeremy L. Thompson /// @addtogroup CeedUser
46d7b241e6Sjeremylt /// @{
47d7b241e6Sjeremylt 
48dfdf5a53Sjeremylt /**
49dfdf5a53Sjeremylt   @brief Request immediate completion
50dfdf5a53Sjeremylt 
51dfdf5a53Sjeremylt   This predefined constant is passed as the \ref CeedRequest argument to
52dfdf5a53Sjeremylt   interfaces when the caller wishes for the operation to be performed
53dfdf5a53Sjeremylt   immediately.  The code
54dfdf5a53Sjeremylt 
55dfdf5a53Sjeremylt   @code
56dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., CEED_REQUEST_IMMEDIATE);
57dfdf5a53Sjeremylt   @endcode
58dfdf5a53Sjeremylt 
59dfdf5a53Sjeremylt   is semantically equivalent to
60dfdf5a53Sjeremylt 
61dfdf5a53Sjeremylt   @code
62dfdf5a53Sjeremylt     CeedRequest request;
63dfdf5a53Sjeremylt     CeedOperatorApply(op, ..., &request);
64dfdf5a53Sjeremylt     CeedRequestWait(&request);
65dfdf5a53Sjeremylt   @endcode
66dfdf5a53Sjeremylt 
67dfdf5a53Sjeremylt   @sa CEED_REQUEST_ORDERED
68dfdf5a53Sjeremylt **/
69d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_IMMEDIATE = &ceed_request_immediate;
70d7b241e6Sjeremylt 
71d7b241e6Sjeremylt /**
72b11c1e72Sjeremylt   @brief Request ordered completion
73d7b241e6Sjeremylt 
74d7b241e6Sjeremylt   This predefined constant is passed as the \ref CeedRequest argument to
75d7b241e6Sjeremylt   interfaces when the caller wishes for the operation to be completed in the
76d7b241e6Sjeremylt   order that it is submitted to the device.  It is typically used in a construct
77d7b241e6Sjeremylt   such as
78d7b241e6Sjeremylt 
79d7b241e6Sjeremylt   @code
80d7b241e6Sjeremylt     CeedRequest request;
81d7b241e6Sjeremylt     CeedOperatorApply(op1, ..., CEED_REQUEST_ORDERED);
82d7b241e6Sjeremylt     CeedOperatorApply(op2, ..., &request);
83d7b241e6Sjeremylt     // other optional work
84d7b241e6Sjeremylt     CeedWait(&request);
85d7b241e6Sjeremylt   @endcode
86d7b241e6Sjeremylt 
87d7b241e6Sjeremylt   which allows the sequence to complete asynchronously but does not start
88d7b241e6Sjeremylt   `op2` until `op1` has completed.
89d7b241e6Sjeremylt 
90288c0443SJeremy L Thompson   @todo The current implementation is overly strict, offering equivalent
914cc79fe7SJed Brown   semantics to @ref CEED_REQUEST_IMMEDIATE.
92d7b241e6Sjeremylt 
93d7b241e6Sjeremylt   @sa CEED_REQUEST_IMMEDIATE
94d7b241e6Sjeremylt  */
95d7b241e6Sjeremylt CeedRequest *const CEED_REQUEST_ORDERED = &ceed_request_ordered;
96d7b241e6Sjeremylt 
97b11c1e72Sjeremylt /**
987a982d89SJeremy L. Thompson   @brief Wait for a CeedRequest to complete.
99dfdf5a53Sjeremylt 
1007a982d89SJeremy L. Thompson   Calling CeedRequestWait on a NULL request is a no-op.
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson   @param req Address of CeedRequest to wait for; zeroed on completion.
1037a982d89SJeremy L. Thompson 
1047a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1057a982d89SJeremy L. Thompson 
1067a982d89SJeremy L. Thompson   @ref User
107b11c1e72Sjeremylt **/
1087a982d89SJeremy L. Thompson int CeedRequestWait(CeedRequest *req) {
1097a982d89SJeremy L. Thompson   if (!*req)
1107a982d89SJeremy L. Thompson     return 0;
1117a982d89SJeremy L. Thompson   return CeedError(NULL, 2, "CeedRequestWait not implemented");
112683faae0SJed Brown }
1137a982d89SJeremy L. Thompson 
1147a982d89SJeremy L. Thompson /// @}
1157a982d89SJeremy L. Thompson 
1167a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1177a982d89SJeremy L. Thompson /// Ceed Library Internal Functions
1187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1197a982d89SJeremy L. Thompson /// @addtogroup CeedDeveloper
1207a982d89SJeremy L. Thompson /// @{
121d7b241e6Sjeremylt 
122b11c1e72Sjeremylt /**
123b11c1e72Sjeremylt   @brief Error handler that returns without printing anything.
124b11c1e72Sjeremylt 
125b11c1e72Sjeremylt   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
126dfdf5a53Sjeremylt 
127dfdf5a53Sjeremylt   @ref Developer
128b11c1e72Sjeremylt **/
12913873f79Sjeremylt // LCOV_EXCL_START
130d7b241e6Sjeremylt int CeedErrorReturn(Ceed ceed, const char *filename, int lineno,
131d7b241e6Sjeremylt                     const char *func, int ecode, const char *format,
132d7b241e6Sjeremylt                     va_list args) {
133d7b241e6Sjeremylt   return ecode;
134d7b241e6Sjeremylt }
13513873f79Sjeremylt // LCOV_EXCL_STOP
136d7b241e6Sjeremylt 
137b11c1e72Sjeremylt /**
138b11c1e72Sjeremylt   @brief Error handler that prints to stderr and aborts
139b11c1e72Sjeremylt 
140b11c1e72Sjeremylt   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
141dfdf5a53Sjeremylt 
142dfdf5a53Sjeremylt   @ref Developer
143b11c1e72Sjeremylt **/
14413873f79Sjeremylt // LCOV_EXCL_START
145d7b241e6Sjeremylt int CeedErrorAbort(Ceed ceed, const char *filename, int lineno,
1461d102b48SJeremy L Thompson                    const char *func, int ecode, const char *format,
1471d102b48SJeremy L Thompson                    va_list args) {
148d7b241e6Sjeremylt   fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func);
149d7b241e6Sjeremylt   vfprintf(stderr, format, args);
150d7b241e6Sjeremylt   fprintf(stderr, "\n");
151d7b241e6Sjeremylt   abort();
152d7b241e6Sjeremylt   return ecode;
153d7b241e6Sjeremylt }
15413873f79Sjeremylt // LCOV_EXCL_STOP
155d7b241e6Sjeremylt 
156b11c1e72Sjeremylt /**
15756e866f4SJed Brown   @brief Error handler that prints to stderr and exits
15856e866f4SJed Brown 
15956e866f4SJed Brown   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
16056e866f4SJed Brown 
16156e866f4SJed Brown   In contrast to CeedErrorAbort(), this exits without a signal, so atexit()
16256e866f4SJed Brown   handlers (e.g., as used by gcov) are run.
16356e866f4SJed Brown 
16456e866f4SJed Brown   @ref Developer
16556e866f4SJed Brown **/
166692c2638Sjeremylt int CeedErrorExit(Ceed ceed, const char *filename, int lineno, const char *func,
167692c2638Sjeremylt                   int ecode, const char *format, va_list args) {
16856e866f4SJed Brown   fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func);
16956e866f4SJed Brown   vfprintf(stderr, format, args);
17056e866f4SJed Brown   fprintf(stderr, "\n");
17156e866f4SJed Brown   exit(ecode);
17256e866f4SJed Brown   return ecode;
17356e866f4SJed Brown }
17456e866f4SJed Brown 
17556e866f4SJed Brown /**
176dfdf5a53Sjeremylt   @brief Set error handler
177b11c1e72Sjeremylt 
178b11c1e72Sjeremylt   A default error handler is set in CeedInit().  Use this function to change
179b11c1e72Sjeremylt   the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined
180b11c1e72Sjeremylt   error handler.
181dfdf5a53Sjeremylt 
182dfdf5a53Sjeremylt   @ref Developer
183b11c1e72Sjeremylt **/
184d7b241e6Sjeremylt int CeedSetErrorHandler(Ceed ceed,
1857a982d89SJeremy L. Thompson                         int (*eh)(Ceed, const char *, int, const char *,
186d7b241e6Sjeremylt                                   int, const char *, va_list)) {
187d7b241e6Sjeremylt   ceed->Error = eh;
188d7b241e6Sjeremylt   return 0;
189d7b241e6Sjeremylt }
190d7b241e6Sjeremylt 
1917a982d89SJeremy L. Thompson /// @}
192d7b241e6Sjeremylt 
1937a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1947a982d89SJeremy L. Thompson /// Ceed Backend API
1957a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1967a982d89SJeremy L. Thompson /// @addtogroup CeedBackend
1977a982d89SJeremy L. Thompson /// @{
198d7b241e6Sjeremylt 
199b11c1e72Sjeremylt /**
20060f9e2d6SJeremy L Thompson   @brief Print Ceed debugging information
20160f9e2d6SJeremy L Thompson 
20260f9e2d6SJeremy L Thompson   @param ceed    Ceed context
20360f9e2d6SJeremy L Thompson   @param format  Printing format
20460f9e2d6SJeremy L Thompson 
20560f9e2d6SJeremy L Thompson   @return None
20660f9e2d6SJeremy L Thompson 
20760f9e2d6SJeremy L Thompson   @ref Backend
20860f9e2d6SJeremy L Thompson **/
209*fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
21060f9e2d6SJeremy L Thompson void CeedDebugImpl(const Ceed ceed, const char *format,...) {
21160f9e2d6SJeremy L Thompson   if (!ceed->debug) return;
21260f9e2d6SJeremy L Thompson   va_list args;
21360f9e2d6SJeremy L Thompson   va_start(args, format);
21460f9e2d6SJeremy L Thompson   CeedDebugImpl256(ceed, 0, format, args);
21560f9e2d6SJeremy L Thompson   va_end(args);
21660f9e2d6SJeremy L Thompson }
217*fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
21860f9e2d6SJeremy L Thompson 
21960f9e2d6SJeremy L Thompson /**
22060f9e2d6SJeremy L Thompson   @brief Print Ceed debugging information in color
22160f9e2d6SJeremy L Thompson 
22260f9e2d6SJeremy L Thompson   @param ceed    Ceed context
22360f9e2d6SJeremy L Thompson   @param color   Color to print
22460f9e2d6SJeremy L Thompson   @param format  Printing format
22560f9e2d6SJeremy L Thompson 
22660f9e2d6SJeremy L Thompson   @return None
22760f9e2d6SJeremy L Thompson 
22860f9e2d6SJeremy L Thompson   @ref Backend
22960f9e2d6SJeremy L Thompson **/
230*fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
23160f9e2d6SJeremy L Thompson void CeedDebugImpl256(const Ceed ceed, const unsigned char color,
23260f9e2d6SJeremy L Thompson                       const char *format,...) {
23360f9e2d6SJeremy L Thompson   if (!ceed->debug) return;
23460f9e2d6SJeremy L Thompson   va_list args;
23560f9e2d6SJeremy L Thompson   va_start(args, format);
23660f9e2d6SJeremy L Thompson   fflush(stdout);
23760f9e2d6SJeremy L Thompson   fprintf(stdout, "\033[38;5;%dm", color);
23860f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
23960f9e2d6SJeremy L Thompson   fprintf(stdout, "\033[m");
24060f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
24160f9e2d6SJeremy L Thompson   fflush(stdout);
24260f9e2d6SJeremy L Thompson   va_end(args);
24360f9e2d6SJeremy L Thompson }
244*fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
24560f9e2d6SJeremy L Thompson 
24660f9e2d6SJeremy L Thompson /**
247b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
248b11c1e72Sjeremylt 
249b11c1e72Sjeremylt   Memory usage can be tracked by the library.  This ensures sufficient
250b11c1e72Sjeremylt     alignment for vectorization and should be used for large allocations.
251b11c1e72Sjeremylt 
252b11c1e72Sjeremylt   @param n Number of units to allocate
253b11c1e72Sjeremylt   @param unit Size of each unit
254b11c1e72Sjeremylt   @param p Address of pointer to hold the result.
255b11c1e72Sjeremylt 
256b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
257b11c1e72Sjeremylt 
258b11c1e72Sjeremylt   @sa CeedFree()
259dfdf5a53Sjeremylt 
2607a982d89SJeremy L. Thompson   @ref Backend
261b11c1e72Sjeremylt **/
262d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
263d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit);
264d7b241e6Sjeremylt   if (ierr)
265c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2661d102b48SJeremy L Thompson     return CeedError(NULL, ierr, "posix_memalign failed to allocate %zd "
2671d102b48SJeremy L Thompson                      "members of size %zd\n", n, unit);
268c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
269c042f62fSJeremy L Thompson 
270d7b241e6Sjeremylt   return 0;
271d7b241e6Sjeremylt }
272d7b241e6Sjeremylt 
273b11c1e72Sjeremylt /**
274b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
275b11c1e72Sjeremylt 
276b11c1e72Sjeremylt   Memory usage can be tracked by the library.
277b11c1e72Sjeremylt 
278b11c1e72Sjeremylt   @param n    Number of units to allocate
279b11c1e72Sjeremylt   @param unit Size of each unit
280b11c1e72Sjeremylt   @param p    Address of pointer to hold the result.
281b11c1e72Sjeremylt 
282b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
283b11c1e72Sjeremylt 
284b11c1e72Sjeremylt   @sa CeedFree()
285dfdf5a53Sjeremylt 
2867a982d89SJeremy L. Thompson   @ref Backend
287b11c1e72Sjeremylt **/
288d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
289d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
290d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
291c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2921d102b48SJeremy L Thompson     return CeedError(NULL, 1, "calloc failed to allocate %zd members of size "
2931d102b48SJeremy L Thompson                      "%zd\n", n, unit);
294c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
295c042f62fSJeremy L Thompson 
296d7b241e6Sjeremylt   return 0;
297d7b241e6Sjeremylt }
298d7b241e6Sjeremylt 
299b11c1e72Sjeremylt /**
300b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
301b11c1e72Sjeremylt 
302b11c1e72Sjeremylt   Memory usage can be tracked by the library.
303b11c1e72Sjeremylt 
304b11c1e72Sjeremylt   @param n    Number of units to allocate
305b11c1e72Sjeremylt   @param unit Size of each unit
306b11c1e72Sjeremylt   @param p    Address of pointer to hold the result.
307b11c1e72Sjeremylt 
308b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
309b11c1e72Sjeremylt 
310b11c1e72Sjeremylt   @sa CeedFree()
311dfdf5a53Sjeremylt 
3127a982d89SJeremy L. Thompson   @ref Backend
313b11c1e72Sjeremylt **/
314d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
315d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n*unit);
316d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
317c042f62fSJeremy L Thompson     // LCOV_EXCL_START
3181d102b48SJeremy L Thompson     return CeedError(NULL, 1, "realloc failed to allocate %zd members of size "
3191d102b48SJeremy L Thompson                      "%zd\n", n, unit);
320c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
321c042f62fSJeremy L Thompson 
322d7b241e6Sjeremylt   return 0;
323d7b241e6Sjeremylt }
324d7b241e6Sjeremylt 
32534138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
32634138859Sjeremylt 
32734138859Sjeremylt   @param p address of pointer to memory.  This argument is of type void* to
32834138859Sjeremylt              avoid needing a cast, but is the address of the pointer (which is
32934138859Sjeremylt              zeroed) rather than the pointer.
33034138859Sjeremylt **/
331d7b241e6Sjeremylt int CeedFree(void *p) {
332d7b241e6Sjeremylt   free(*(void **)p);
333d7b241e6Sjeremylt   *(void **)p = NULL;
334d7b241e6Sjeremylt   return 0;
335d7b241e6Sjeremylt }
336d7b241e6Sjeremylt 
337d7b241e6Sjeremylt /**
3387a982d89SJeremy L. Thompson   @brief Register a Ceed backend
339d7b241e6Sjeremylt 
3407a982d89SJeremy L. Thompson   @param prefix   Prefix of resources for this backend to respond to.  For
3417a982d89SJeremy L. Thompson                     example, the reference backend responds to "/cpu/self".
3427a982d89SJeremy L. Thompson   @param init     Initialization function called by CeedInit() when the backend
3437a982d89SJeremy L. Thompson                     is selected to drive the requested resource.
3447a982d89SJeremy L. Thompson   @param priority Integer priority.  Lower values are preferred in case the
3457a982d89SJeremy L. Thompson                     resource requested by CeedInit() has non-unique best prefix
3467a982d89SJeremy L. Thompson                     match.
347b11c1e72Sjeremylt 
348b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
349dfdf5a53Sjeremylt 
3507a982d89SJeremy L. Thompson   @ref Backend
351b11c1e72Sjeremylt **/
3527a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed),
3537a982d89SJeremy L. Thompson                  unsigned int priority) {
3547a982d89SJeremy L. Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0]))
3557a982d89SJeremy L. Thompson     // LCOV_EXCL_START
3567a982d89SJeremy L. Thompson     return CeedError(NULL, 1, "Too many backends");
3577a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
3607a982d89SJeremy L. Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0;
3617a982d89SJeremy L. Thompson   backends[num_backends].init = init;
3627a982d89SJeremy L. Thompson   backends[num_backends].priority = priority;
3637a982d89SJeremy L. Thompson   num_backends++;
3641d102b48SJeremy L Thompson   return 0;
365d7b241e6Sjeremylt }
366d7b241e6Sjeremylt 
367b11c1e72Sjeremylt /**
36860f9e2d6SJeremy L Thompson   @brief Return debugging status flag
36960f9e2d6SJeremy L Thompson 
37060f9e2d6SJeremy L Thompson   @param ceed     Ceed context to get debugging flag
37160f9e2d6SJeremy L Thompson   @param isDebug  Variable to store debugging flag
37260f9e2d6SJeremy L Thompson 
37360f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
37460f9e2d6SJeremy L Thompson 
37560f9e2d6SJeremy L Thompson   @ref Bcakend
37660f9e2d6SJeremy L Thompson **/
37760f9e2d6SJeremy L Thompson int CeedIsDebug(Ceed ceed, bool *isDebug) {
37860f9e2d6SJeremy L Thompson   *isDebug = ceed->debug;
37960f9e2d6SJeremy L Thompson   return 0;
38060f9e2d6SJeremy L Thompson }
38160f9e2d6SJeremy L Thompson 
38260f9e2d6SJeremy L Thompson /**
3837a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3847a982d89SJeremy L. Thompson 
3857a982d89SJeremy L. Thompson   @param ceed        Ceed context to retrieve parent of
3867a982d89SJeremy L. Thompson   @param[out] parent Address to save the parent 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 CeedGetParent(Ceed ceed, Ceed *parent) {
3937a982d89SJeremy L. Thompson   int ierr;
3947a982d89SJeremy L. Thompson   if (ceed->parent) {
3957a982d89SJeremy L. Thompson     ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr);
3967a982d89SJeremy L. Thompson     return 0;
3977a982d89SJeremy L. Thompson   }
3987a982d89SJeremy L. Thompson   *parent = ceed;
3997a982d89SJeremy L. Thompson   return 0;
4007a982d89SJeremy L. Thompson }
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson /**
4037a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
4047a982d89SJeremy L. Thompson 
4057a982d89SJeremy L. Thompson   @param ceed          Ceed context to retrieve delegate of
4067a982d89SJeremy L. Thompson   @param[out] delegate Address to save the delegate to
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4097a982d89SJeremy L. Thompson 
4107a982d89SJeremy L. Thompson   @ref Backend
4117a982d89SJeremy L. Thompson **/
4127a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
4137a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
4147a982d89SJeremy L. Thompson   return 0;
4157a982d89SJeremy L. Thompson }
4167a982d89SJeremy L. Thompson 
4177a982d89SJeremy L. Thompson /**
4187a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
4197a982d89SJeremy L. Thompson 
4207a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context. All
4217a982d89SJeremy L. Thompson     backend implementations default to the delegate Ceed context, unless
4227a982d89SJeremy L. Thompson     overridden.
4237a982d89SJeremy L. Thompson 
4247a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
4257a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
4267a982d89SJeremy L. Thompson 
4277a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4287a982d89SJeremy L. Thompson 
4297a982d89SJeremy L. Thompson   @ref Backend
4307a982d89SJeremy L. Thompson **/
4317a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
4327a982d89SJeremy L. Thompson   ceed->delegate = delegate;
4337a982d89SJeremy L. Thompson   delegate->parent = ceed;
4347a982d89SJeremy L. Thompson   return 0;
4357a982d89SJeremy L. Thompson }
4367a982d89SJeremy L. Thompson 
4377a982d89SJeremy L. Thompson /**
4387a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
4397a982d89SJeremy L. Thompson 
4407a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
4417a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
4427a982d89SJeremy L. Thompson   @param[in] objname    Name of the object type to retrieve delegate for
4437a982d89SJeremy L. Thompson 
4447a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4457a982d89SJeremy L. Thompson 
4467a982d89SJeremy L. Thompson   @ref Backend
4477a982d89SJeremy L. Thompson **/
4487a982d89SJeremy L. Thompson int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *objname) {
4497a982d89SJeremy L. Thompson   CeedInt ierr;
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson   // Check for object delegate
4527a982d89SJeremy L. Thompson   for (CeedInt i=0; i<ceed->objdelegatecount; i++)
4537a982d89SJeremy L. Thompson     if (!strcmp(objname, ceed->objdelegates->objname)) {
4547a982d89SJeremy L. Thompson       *delegate = ceed->objdelegates->delegate;
4557a982d89SJeremy L. Thompson       return 0;
4567a982d89SJeremy L. Thompson     }
4577a982d89SJeremy L. Thompson 
4587a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
4597a982d89SJeremy L. Thompson   ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr);
4607a982d89SJeremy L. Thompson 
4617a982d89SJeremy L. Thompson   return 0;
4627a982d89SJeremy L. Thompson }
4637a982d89SJeremy L. Thompson 
4647a982d89SJeremy L. Thompson /**
4657a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
4667a982d89SJeremy L. Thompson 
4677a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context for a
4687a982d89SJeremy L. Thompson     given type of Ceed object. All backend implementations default to the
4697a982d89SJeremy L. Thompson     delegate Ceed context for this object. For example,
4707a982d89SJeremy L. Thompson     CeedSetObjectDelegate(ceed, refceed, "Basis")
4717a982d89SJeremy L. Thompson   uses refceed implementations for all CeedBasis backend functions.
4727a982d89SJeremy L. Thompson 
4737a982d89SJeremy L. Thompson   @param ceed          Ceed context to set delegate of
4747a982d89SJeremy L. Thompson   @param[out] delegate Address to set the delegate to
4757a982d89SJeremy L. Thompson   @param[in] objname   Name of the object type to set delegate for
4767a982d89SJeremy L. Thompson 
4777a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4787a982d89SJeremy L. Thompson 
4797a982d89SJeremy L. Thompson   @ref Backend
4807a982d89SJeremy L. Thompson **/
4817a982d89SJeremy L. Thompson int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *objname) {
4827a982d89SJeremy L. Thompson   CeedInt ierr;
4837a982d89SJeremy L. Thompson   CeedInt count = ceed->objdelegatecount;
4847a982d89SJeremy L. Thompson 
4857a982d89SJeremy L. Thompson   // Malloc or Realloc
4867a982d89SJeremy L. Thompson   if (count) {
4877a982d89SJeremy L. Thompson     ierr = CeedRealloc(count+1, &ceed->objdelegates); CeedChk(ierr);
4887a982d89SJeremy L. Thompson   } else {
4897a982d89SJeremy L. Thompson     ierr = CeedCalloc(1, &ceed->objdelegates); CeedChk(ierr);
4907a982d89SJeremy L. Thompson   }
4917a982d89SJeremy L. Thompson   ceed->objdelegatecount++;
4927a982d89SJeremy L. Thompson 
4937a982d89SJeremy L. Thompson   // Set object delegate
4947a982d89SJeremy L. Thompson   ceed->objdelegates[count].delegate = delegate;
4957a982d89SJeremy L. Thompson   size_t slen = strlen(objname) + 1;
4967a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &ceed->objdelegates[count].objname); CeedChk(ierr);
4977a982d89SJeremy L. Thompson   memcpy(ceed->objdelegates[count].objname, objname, slen);
4987a982d89SJeremy L. Thompson 
4997a982d89SJeremy L. Thompson   // Set delegate parent
5007a982d89SJeremy L. Thompson   delegate->parent = ceed;
5017a982d89SJeremy L. Thompson 
5027a982d89SJeremy L. Thompson   return 0;
5037a982d89SJeremy L. Thompson }
5047a982d89SJeremy L. Thompson 
5057a982d89SJeremy L. Thompson /**
5067a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
5077a982d89SJeremy L. Thompson 
5087a982d89SJeremy L. Thompson   @param ceed          Ceed context
5097a982d89SJeremy L. Thompson   @param[out] resource Variable to store fallback resource
5107a982d89SJeremy L. Thompson 
5117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5127a982d89SJeremy L. Thompson 
5137a982d89SJeremy L. Thompson   @ref Backend
5147a982d89SJeremy L. Thompson **/
5157a982d89SJeremy L. Thompson 
5167a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
5177a982d89SJeremy L. Thompson   *resource = (const char *)ceed->opfallbackresource;
5187a982d89SJeremy L. Thompson   return 0;
5197a982d89SJeremy L. Thompson }
5207a982d89SJeremy L. Thompson 
5217a982d89SJeremy L. Thompson /**
5227a982d89SJeremy L. Thompson   @brief Set the fallback resource for CeedOperators. The current resource, if
5237a982d89SJeremy L. Thompson            any, is freed by calling this function. This string is freed upon the
5247a982d89SJeremy L. Thompson            destruction of the Ceed context.
5257a982d89SJeremy L. Thompson 
5267a982d89SJeremy L. Thompson   @param[out] ceed Ceed context
5277a982d89SJeremy L. Thompson   @param resource  Fallback resource to set
5287a982d89SJeremy L. Thompson 
5297a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson   @ref Backend
5327a982d89SJeremy L. Thompson **/
5337a982d89SJeremy L. Thompson 
5347a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
5357a982d89SJeremy L. Thompson   int ierr;
5367a982d89SJeremy L. Thompson 
5377a982d89SJeremy L. Thompson   // Free old
5387a982d89SJeremy L. Thompson   ierr = CeedFree(&ceed->opfallbackresource); CeedChk(ierr);
5397a982d89SJeremy L. Thompson 
5407a982d89SJeremy L. Thompson   // Set new
5417a982d89SJeremy L. Thompson   size_t len = strlen(resource);
5427a982d89SJeremy L. Thompson   char *tmp;
5437a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
5447a982d89SJeremy L. Thompson   memcpy(tmp, resource, len+1);
5457a982d89SJeremy L. Thompson   ceed->opfallbackresource = tmp;
5467a982d89SJeremy L. Thompson 
5477a982d89SJeremy L. Thompson   return 0;
5487a982d89SJeremy L. Thompson }
5497a982d89SJeremy L. Thompson 
5507a982d89SJeremy L. Thompson /**
5517a982d89SJeremy L. Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context
5527a982d89SJeremy L. Thompson            for a CeedOperator
5537a982d89SJeremy L. Thompson 
5547a982d89SJeremy L. Thompson   @param ceed         Ceed context
5557a982d89SJeremy L. Thompson   @param[out] parent  Variable to store parent Ceed context
5567a982d89SJeremy L. Thompson 
5577a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5587a982d89SJeremy L. Thompson 
5597a982d89SJeremy L. Thompson   @ref Backend
5607a982d89SJeremy L. Thompson **/
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
5637a982d89SJeremy L. Thompson   *parent = ceed->opfallbackparent;
5647a982d89SJeremy L. Thompson   return 0;
5657a982d89SJeremy L. Thompson }
5667a982d89SJeremy L. Thompson 
5677a982d89SJeremy L. Thompson /**
5689525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
5699525855cSJeremy L Thompson 
5709525855cSJeremy L Thompson   @param ceed     Ceed to flag as deterministic
5719525855cSJeremy L Thompson 
5729525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5739525855cSJeremy L Thompson 
5749525855cSJeremy L Thompson   @ref Backend
5759525855cSJeremy L Thompson **/
5769525855cSJeremy L Thompson 
5779525855cSJeremy L Thompson int CeedSetDeterministic(Ceed ceed, bool isDeterministic) {
5789525855cSJeremy L Thompson   ceed->isDeterministic = isDeterministic;
5799525855cSJeremy L Thompson   return 0;
5809525855cSJeremy L Thompson }
5819525855cSJeremy L Thompson 
5829525855cSJeremy L Thompson /**
5837a982d89SJeremy L. Thompson   @brief Set a backend function
5847a982d89SJeremy L. Thompson 
5857a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5867a982d89SJeremy L. Thompson   the Ceed objects. For example,
5877a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5887a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5897a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5907a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
5917a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
5927a982d89SJeremy L. Thompson 
5937a982d89SJeremy L. Thompson   @param ceed           Ceed context for error handling
5947a982d89SJeremy L. Thompson   @param type           Type of Ceed object to set function for
5957a982d89SJeremy L. Thompson   @param[out] object    Ceed object to set function for
5967a982d89SJeremy L. Thompson   @param fname          Name of function to set
5977a982d89SJeremy L. Thompson   @param f              Function to set
5987a982d89SJeremy L. Thompson 
5997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6007a982d89SJeremy L. Thompson 
6017a982d89SJeremy L. Thompson   @ref Backend
6027a982d89SJeremy L. Thompson **/
6037a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
6047a982d89SJeremy L. Thompson                            const char *fname, int (*f)()) {
6057a982d89SJeremy L. Thompson   char lookupname[CEED_MAX_RESOURCE_LEN+1] = "";
6067a982d89SJeremy L. Thompson 
6077a982d89SJeremy L. Thompson   // Build lookup name
6087a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
6097a982d89SJeremy L. Thompson     strncat (lookupname, "Ceed", CEED_MAX_RESOURCE_LEN);
6107a982d89SJeremy L. Thompson   strncat(lookupname, type, CEED_MAX_RESOURCE_LEN);
6117a982d89SJeremy L. Thompson   strncat(lookupname, fname, CEED_MAX_RESOURCE_LEN);
6127a982d89SJeremy L. Thompson 
6137a982d89SJeremy L. Thompson   // Find and use offset
6147a982d89SJeremy L. Thompson   for (CeedInt i = 0; ceed->foffsets[i].fname; i++)
6157a982d89SJeremy L. Thompson     if (!strcmp(ceed->foffsets[i].fname, lookupname)) {
6167a982d89SJeremy L. Thompson       size_t offset = ceed->foffsets[i].offset;
6177a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
6187a982d89SJeremy L. Thompson       *fpointer = f;
6197a982d89SJeremy L. Thompson       return 0;
6207a982d89SJeremy L. Thompson     }
6217a982d89SJeremy L. Thompson 
6227a982d89SJeremy L. Thompson   // LCOV_EXCL_START
6237a982d89SJeremy L. Thompson   return CeedError(ceed, 1, "Requested function '%s' was not found for CEED "
6247a982d89SJeremy L. Thompson                    "object '%s'", fname, type);
6257a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
6267a982d89SJeremy L. Thompson }
6277a982d89SJeremy L. Thompson 
6287a982d89SJeremy L. Thompson /**
6297a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
6307a982d89SJeremy L. Thompson 
6317a982d89SJeremy L. Thompson   @param ceed      Ceed context to retrieve data of
6327a982d89SJeremy L. Thompson   @param[out] data Address to save data to
6337a982d89SJeremy L. Thompson 
6347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6357a982d89SJeremy L. Thompson 
6367a982d89SJeremy L. Thompson   @ref Backend
6377a982d89SJeremy L. Thompson **/
6387a982d89SJeremy L. Thompson int CeedGetData(Ceed ceed, void **data) {
6397a982d89SJeremy L. Thompson   *data = ceed->data;
6407a982d89SJeremy L. Thompson   return 0;
6417a982d89SJeremy L. Thompson }
6427a982d89SJeremy L. Thompson 
6437a982d89SJeremy L. Thompson /**
6447a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
6457a982d89SJeremy L. Thompson 
6467a982d89SJeremy L. Thompson   @param ceed           Ceed context to set data of
6477a982d89SJeremy L. Thompson   @param data           Address of data to set
6487a982d89SJeremy L. Thompson 
6497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6507a982d89SJeremy L. Thompson 
6517a982d89SJeremy L. Thompson   @ref Backend
6527a982d89SJeremy L. Thompson **/
6537a982d89SJeremy L. Thompson int CeedSetData(Ceed ceed, void **data) {
6547a982d89SJeremy L. Thompson   ceed->data = *data;
6557a982d89SJeremy L. Thompson   return 0;
6567a982d89SJeremy L. Thompson }
6577a982d89SJeremy L. Thompson 
6587a982d89SJeremy L. Thompson /// @}
6597a982d89SJeremy L. Thompson 
6607a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6617a982d89SJeremy L. Thompson /// Ceed Public API
6627a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6637a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6647a982d89SJeremy L. Thompson /// @{
6657a982d89SJeremy L. Thompson 
6667a982d89SJeremy L. Thompson /**
667d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
668b11c1e72Sjeremylt 
669b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
670b11c1e72Sjeremylt   @param ceed      The library context
671b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
672b11c1e72Sjeremylt 
673b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
674dfdf5a53Sjeremylt 
6757a982d89SJeremy L. Thompson   @ref User
676b11c1e72Sjeremylt **/
677d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
678d7b241e6Sjeremylt   int ierr;
679aedaa0e5Sjeremylt   size_t matchlen = 0, matchidx = UINT_MAX, matchpriority = UINT_MAX, priority;
680d7b241e6Sjeremylt 
681fe2413ffSjeremylt   // Find matching backend
6821d102b48SJeremy L Thompson   if (!resource)
68313873f79Sjeremylt     // LCOV_EXCL_START
6841d102b48SJeremy L Thompson     return CeedError(NULL, 1, "No resource provided");
68513873f79Sjeremylt   // LCOV_EXCL_STOP
68613873f79Sjeremylt 
687d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
688d7b241e6Sjeremylt     size_t n;
689d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
690d7b241e6Sjeremylt     for (n = 0; prefix[n] && prefix[n] == resource[n]; n++) {}
691d7b241e6Sjeremylt     priority = backends[i].priority;
692d7b241e6Sjeremylt     if (n > matchlen || (n == matchlen && matchpriority > priority)) {
693d7b241e6Sjeremylt       matchlen = n;
694d7b241e6Sjeremylt       matchpriority = priority;
695d7b241e6Sjeremylt       matchidx = i;
696d7b241e6Sjeremylt     }
697d7b241e6Sjeremylt   }
6981d102b48SJeremy L Thompson   if (!matchlen)
69913873f79Sjeremylt     // LCOV_EXCL_START
7008079b1e4SJed Brown     return CeedError(NULL, 1, "No suitable backend: %s", resource);
70113873f79Sjeremylt   // LCOV_EXCL_STOP
702fe2413ffSjeremylt 
703fe2413ffSjeremylt   // Setup Ceed
704d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
705bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
7061d102b48SJeremy L Thompson   if (!ceed_error_handler)
7071d102b48SJeremy L Thompson     ceed_error_handler = "abort";
708bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
70956e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
71056e866f4SJed Brown   else
711d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
712d7b241e6Sjeremylt   (*ceed)->refcount = 1;
713d7b241e6Sjeremylt   (*ceed)->data = NULL;
714fe2413ffSjeremylt 
715fe2413ffSjeremylt   // Set lookup table
7166e79d475Sjeremylt   foffset foffsets[] = {
7176e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
7186e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
7196e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
720f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
7216e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
7226e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
7236e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
7246e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
7256e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
7266e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
7276e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
7286e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
7296e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
7306e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
7316e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
7326e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
7336e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
7346e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
735547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
7366e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Destroy),
7376e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
7386e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
739bd33150aSjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
7406e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
7416e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Apply),
7426e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Destroy),
7436e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
7446e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
7456e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Apply),
7466e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
74780ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
74880ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
7499e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
75080ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
7519e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
752713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
7536e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
754250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
755cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
756250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
7576e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
7586e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
7596e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
7601dfeef1dSjeremylt   };
761fe2413ffSjeremylt 
7626e79d475Sjeremylt   ierr = CeedCalloc(sizeof(foffsets), &(*ceed)->foffsets); CeedChk(ierr);
7636e79d475Sjeremylt   memcpy((*ceed)->foffsets, foffsets, sizeof(foffsets));
764fe2413ffSjeremylt 
7655107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
7665107b09fSJeremy L Thompson   const char fallbackresource[] = "/cpu/self/ref/serial";
7675107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
7685107b09fSJeremy L Thompson   CeedChk(ierr);
7695107b09fSJeremy L Thompson 
77060f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
77160f9e2d6SJeremy L Thompson   (*ceed)->debug = !!getenv("CEED_DEBUG") || !!getenv("DBG");
77260f9e2d6SJeremy L Thompson 
773fe2413ffSjeremylt   // Backend specific setup
774d7b241e6Sjeremylt   ierr = backends[matchidx].init(resource, *ceed); CeedChk(ierr);
775fe2413ffSjeremylt 
776e07206deSjeremylt   // Copy resource prefix, if backend setup sucessful
777e07206deSjeremylt   size_t len = strlen(backends[matchidx].prefix);
778e07206deSjeremylt   char *tmp;
779e07206deSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
780e07206deSjeremylt   memcpy(tmp, backends[matchidx].prefix, len+1);
781e07206deSjeremylt   (*ceed)->resource = tmp;
782e07206deSjeremylt 
783d7b241e6Sjeremylt   return 0;
784d7b241e6Sjeremylt }
785d7b241e6Sjeremylt 
786d7b241e6Sjeremylt /**
7877a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
7882f86a920SJeremy L Thompson 
7897a982d89SJeremy L. Thompson   @param ceed            Ceed context to get resource name of
7907a982d89SJeremy L. Thompson   @param[out] resource   Variable to store resource name
7912f86a920SJeremy L Thompson 
7922f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7932f86a920SJeremy L Thompson 
7947a982d89SJeremy L. Thompson   @ref User
7955107b09fSJeremy L Thompson **/
7965107b09fSJeremy L Thompson 
7977a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
7987a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
7995107b09fSJeremy L Thompson   return 0;
8005107b09fSJeremy L Thompson }
8015107b09fSJeremy L Thompson 
8025107b09fSJeremy L Thompson /**
803d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
804c907536fSjeremylt 
805d79b80ecSjeremylt   @param ceed      Ceed context to get preferred memory type of
806288c0443SJeremy L Thompson   @param[out] type Address to save preferred memory type to
807c907536fSjeremylt 
808c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
809c907536fSjeremylt 
8107a982d89SJeremy L. Thompson   @ref User
811c907536fSjeremylt **/
812c907536fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type) {
813c907536fSjeremylt   int ierr;
814c263cd57Sjeremylt 
815c907536fSjeremylt   if (ceed->GetPreferredMemType) {
816c907536fSjeremylt     ierr = ceed->GetPreferredMemType(type); CeedChk(ierr);
817c907536fSjeremylt   } else {
818c263cd57Sjeremylt     Ceed delegate;
819c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
820c263cd57Sjeremylt 
821c263cd57Sjeremylt     if (delegate) {
822c263cd57Sjeremylt       ierr = CeedGetPreferredMemType(delegate, type); CeedChk(ierr);
823c263cd57Sjeremylt     } else {
824c907536fSjeremylt       *type = CEED_MEM_HOST;
825c907536fSjeremylt     }
826c263cd57Sjeremylt   }
827c907536fSjeremylt 
828c907536fSjeremylt   return 0;
829c907536fSjeremylt }
830c907536fSjeremylt 
831c907536fSjeremylt /**
8329525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
8339525855cSJeremy L Thompson 
8349525855cSJeremy L Thompson   @param[in] ceed              Ceed
8359525855cSJeremy L Thompson   @param[out] isDeterministic  Variable to store deterministic status
8369525855cSJeremy L Thompson 
8379525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
8389525855cSJeremy L Thompson 
8399525855cSJeremy L Thompson   @ref User
8409525855cSJeremy L Thompson **/
8419525855cSJeremy L Thompson int CeedIsDeterministic(Ceed ceed, bool *isDeterministic) {
8429525855cSJeremy L Thompson   *isDeterministic = ceed->isDeterministic;
8439525855cSJeremy L Thompson   return 0;
8449525855cSJeremy L Thompson }
8459525855cSJeremy L Thompson 
8469525855cSJeremy L Thompson /**
8470a0da059Sjeremylt   @brief View a Ceed
8480a0da059Sjeremylt 
8490a0da059Sjeremylt   @param[in] ceed          Ceed to view
8500a0da059Sjeremylt   @param[in] stream        Filestream to write to
8510a0da059Sjeremylt 
8520a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
8530a0da059Sjeremylt 
8540a0da059Sjeremylt   @ref User
8550a0da059Sjeremylt **/
8560a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
8570a0da059Sjeremylt   int ierr;
8580a0da059Sjeremylt   CeedMemType memtype;
8590a0da059Sjeremylt 
8600a0da059Sjeremylt   ierr = CeedGetPreferredMemType(ceed, &memtype); CeedChk(ierr);
8610a0da059Sjeremylt 
8620a0da059Sjeremylt   fprintf(stream, "Ceed\n"
8630a0da059Sjeremylt           "  Ceed Resource: %s\n"
8640a0da059Sjeremylt           "  Preferred MemType: %s\n",
8650a0da059Sjeremylt           ceed->resource, CeedMemTypes[memtype]);
8660a0da059Sjeremylt 
8670a0da059Sjeremylt   return 0;
8680a0da059Sjeremylt }
8690a0da059Sjeremylt 
8700a0da059Sjeremylt /**
871b11c1e72Sjeremylt   @brief Destroy a Ceed context
872d7b241e6Sjeremylt 
873d7b241e6Sjeremylt   @param ceed Address of Ceed context to destroy
874b11c1e72Sjeremylt 
875b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
876dfdf5a53Sjeremylt 
8777a982d89SJeremy L. Thompson   @ref User
878b11c1e72Sjeremylt **/
879d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
880d7b241e6Sjeremylt   int ierr;
8811d102b48SJeremy L Thompson   if (!*ceed || --(*ceed)->refcount > 0)
8821d102b48SJeremy L Thompson     return 0;
8830ace9bf2Sjeremylt 
8845fe0d4faSjeremylt   if ((*ceed)->delegate) {
8855fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
8865fe0d4faSjeremylt   }
8870ace9bf2Sjeremylt 
888aefd8378Sjeremylt   if ((*ceed)->objdelegatecount > 0) {
889aefd8378Sjeremylt     for (int i=0; i<(*ceed)->objdelegatecount; i++) {
890aefd8378Sjeremylt       ierr = CeedDestroy(&((*ceed)->objdelegates[i].delegate)); CeedChk(ierr);
891aefd8378Sjeremylt       ierr = CeedFree(&(*ceed)->objdelegates[i].objname); CeedChk(ierr);
892aefd8378Sjeremylt     }
893aefd8378Sjeremylt     ierr = CeedFree(&(*ceed)->objdelegates); CeedChk(ierr);
894aefd8378Sjeremylt   }
8950ace9bf2Sjeremylt 
896d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
897d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
898d7b241e6Sjeremylt   }
8990ace9bf2Sjeremylt 
9006e79d475Sjeremylt   ierr = CeedFree(&(*ceed)->foffsets); CeedChk(ierr);
901e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
9025107b09fSJeremy L Thompson   ierr = CeedDestroy(&(*ceed)->opfallbackceed); CeedChk(ierr);
9035107b09fSJeremy L Thompson   ierr = CeedFree(&(*ceed)->opfallbackresource); CeedChk(ierr);
904d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
905d7b241e6Sjeremylt   return 0;
906d7b241e6Sjeremylt }
907d7b241e6Sjeremylt 
9087a982d89SJeremy L. Thompson /**
9097a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
9107a982d89SJeremy L. Thompson 
9117a982d89SJeremy L. Thompson   @ref Developer
9127a982d89SJeremy L. Thompson **/
9137a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
9147a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
9157a982d89SJeremy L. Thompson   va_list args;
9167a982d89SJeremy L. Thompson   int retval;
9177a982d89SJeremy L. Thompson   va_start(args, format);
9187a982d89SJeremy L. Thompson   if (ceed) {
9197a982d89SJeremy L. Thompson     retval = ceed->Error(ceed, filename, lineno, func, ecode, format, args);
9207a982d89SJeremy L. Thompson   } else {
9217a982d89SJeremy L. Thompson     // This function doesn't actually return
922b0d62198Sjeremylt     // LCOV_EXCL_START
9237a982d89SJeremy L. Thompson     retval = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, args);
9247a982d89SJeremy L. Thompson   }
9257a982d89SJeremy L. Thompson   va_end(args);
9267a982d89SJeremy L. Thompson   return retval;
927b0d62198Sjeremylt   // LCOV_EXCL_STOP
9287a982d89SJeremy L. Thompson }
9297a982d89SJeremy L. Thompson 
930d7b241e6Sjeremylt /// @}
931