xref: /libCEED/rust/libceed-sys/c-src/interface/ceed.c (revision 9525855cad8c4d11c92d8690c1fd31b3722ac1da)
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 /**
200b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
201b11c1e72Sjeremylt 
202b11c1e72Sjeremylt   Memory usage can be tracked by the library.  This ensures sufficient
203b11c1e72Sjeremylt     alignment for vectorization and should be used for large allocations.
204b11c1e72Sjeremylt 
205b11c1e72Sjeremylt   @param n Number of units to allocate
206b11c1e72Sjeremylt   @param unit Size of each unit
207b11c1e72Sjeremylt   @param p Address of pointer to hold the result.
208b11c1e72Sjeremylt 
209b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
210b11c1e72Sjeremylt 
211b11c1e72Sjeremylt   @sa CeedFree()
212dfdf5a53Sjeremylt 
2137a982d89SJeremy L. Thompson   @ref Backend
214b11c1e72Sjeremylt **/
215d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
216d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit);
217d7b241e6Sjeremylt   if (ierr)
218c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2191d102b48SJeremy L Thompson     return CeedError(NULL, ierr, "posix_memalign failed to allocate %zd "
2201d102b48SJeremy L Thompson                      "members of size %zd\n", n, unit);
221c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
222c042f62fSJeremy L Thompson 
223d7b241e6Sjeremylt   return 0;
224d7b241e6Sjeremylt }
225d7b241e6Sjeremylt 
226b11c1e72Sjeremylt /**
227b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
228b11c1e72Sjeremylt 
229b11c1e72Sjeremylt   Memory usage can be tracked by the library.
230b11c1e72Sjeremylt 
231b11c1e72Sjeremylt   @param n    Number of units to allocate
232b11c1e72Sjeremylt   @param unit Size of each unit
233b11c1e72Sjeremylt   @param p    Address of pointer to hold the result.
234b11c1e72Sjeremylt 
235b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
236b11c1e72Sjeremylt 
237b11c1e72Sjeremylt   @sa CeedFree()
238dfdf5a53Sjeremylt 
2397a982d89SJeremy L. Thompson   @ref Backend
240b11c1e72Sjeremylt **/
241d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
242d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
243d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
244c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2451d102b48SJeremy L Thompson     return CeedError(NULL, 1, "calloc failed to allocate %zd members of size "
2461d102b48SJeremy L Thompson                      "%zd\n", n, unit);
247c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
248c042f62fSJeremy L Thompson 
249d7b241e6Sjeremylt   return 0;
250d7b241e6Sjeremylt }
251d7b241e6Sjeremylt 
252b11c1e72Sjeremylt /**
253b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
254b11c1e72Sjeremylt 
255b11c1e72Sjeremylt   Memory usage can be tracked by the library.
256b11c1e72Sjeremylt 
257b11c1e72Sjeremylt   @param n    Number of units to allocate
258b11c1e72Sjeremylt   @param unit Size of each unit
259b11c1e72Sjeremylt   @param p    Address of pointer to hold the result.
260b11c1e72Sjeremylt 
261b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
262b11c1e72Sjeremylt 
263b11c1e72Sjeremylt   @sa CeedFree()
264dfdf5a53Sjeremylt 
2657a982d89SJeremy L. Thompson   @ref Backend
266b11c1e72Sjeremylt **/
267d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
268d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n*unit);
269d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
270c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2711d102b48SJeremy L Thompson     return CeedError(NULL, 1, "realloc failed to allocate %zd members of size "
2721d102b48SJeremy L Thompson                      "%zd\n", n, unit);
273c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
274c042f62fSJeremy L Thompson 
275d7b241e6Sjeremylt   return 0;
276d7b241e6Sjeremylt }
277d7b241e6Sjeremylt 
27834138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
27934138859Sjeremylt 
28034138859Sjeremylt   @param p address of pointer to memory.  This argument is of type void* to
28134138859Sjeremylt              avoid needing a cast, but is the address of the pointer (which is
28234138859Sjeremylt              zeroed) rather than the pointer.
28334138859Sjeremylt **/
284d7b241e6Sjeremylt int CeedFree(void *p) {
285d7b241e6Sjeremylt   free(*(void **)p);
286d7b241e6Sjeremylt   *(void **)p = NULL;
287d7b241e6Sjeremylt   return 0;
288d7b241e6Sjeremylt }
289d7b241e6Sjeremylt 
290d7b241e6Sjeremylt /**
2917a982d89SJeremy L. Thompson   @brief Register a Ceed backend
292d7b241e6Sjeremylt 
2937a982d89SJeremy L. Thompson   @param prefix   Prefix of resources for this backend to respond to.  For
2947a982d89SJeremy L. Thompson                     example, the reference backend responds to "/cpu/self".
2957a982d89SJeremy L. Thompson   @param init     Initialization function called by CeedInit() when the backend
2967a982d89SJeremy L. Thompson                     is selected to drive the requested resource.
2977a982d89SJeremy L. Thompson   @param priority Integer priority.  Lower values are preferred in case the
2987a982d89SJeremy L. Thompson                     resource requested by CeedInit() has non-unique best prefix
2997a982d89SJeremy L. Thompson                     match.
300b11c1e72Sjeremylt 
301b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
302dfdf5a53Sjeremylt 
3037a982d89SJeremy L. Thompson   @ref Backend
304b11c1e72Sjeremylt **/
3057a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed),
3067a982d89SJeremy L. Thompson                  unsigned int priority) {
3077a982d89SJeremy L. Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0]))
3087a982d89SJeremy L. Thompson     // LCOV_EXCL_START
3097a982d89SJeremy L. Thompson     return CeedError(NULL, 1, "Too many backends");
3107a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
3117a982d89SJeremy L. Thompson 
3127a982d89SJeremy L. Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
3137a982d89SJeremy L. Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0;
3147a982d89SJeremy L. Thompson   backends[num_backends].init = init;
3157a982d89SJeremy L. Thompson   backends[num_backends].priority = priority;
3167a982d89SJeremy L. Thompson   num_backends++;
3171d102b48SJeremy L Thompson   return 0;
318d7b241e6Sjeremylt }
319d7b241e6Sjeremylt 
320b11c1e72Sjeremylt /**
3217a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3227a982d89SJeremy L. Thompson 
3237a982d89SJeremy L. Thompson   @param ceed        Ceed context to retrieve parent of
3247a982d89SJeremy L. Thompson   @param[out] parent Address to save the parent to
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3277a982d89SJeremy L. Thompson 
3287a982d89SJeremy L. Thompson   @ref Backend
3297a982d89SJeremy L. Thompson **/
3307a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3317a982d89SJeremy L. Thompson   int ierr;
3327a982d89SJeremy L. Thompson   if (ceed->parent) {
3337a982d89SJeremy L. Thompson     ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr);
3347a982d89SJeremy L. Thompson     return 0;
3357a982d89SJeremy L. Thompson   }
3367a982d89SJeremy L. Thompson   *parent = ceed;
3377a982d89SJeremy L. Thompson   return 0;
3387a982d89SJeremy L. Thompson }
3397a982d89SJeremy L. Thompson 
3407a982d89SJeremy L. Thompson /**
3417a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3427a982d89SJeremy L. Thompson 
3437a982d89SJeremy L. Thompson   @param ceed          Ceed context to retrieve delegate of
3447a982d89SJeremy L. Thompson   @param[out] delegate Address to save the delegate to
3457a982d89SJeremy L. Thompson 
3467a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3477a982d89SJeremy L. Thompson 
3487a982d89SJeremy L. Thompson   @ref Backend
3497a982d89SJeremy L. Thompson **/
3507a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3517a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
3527a982d89SJeremy L. Thompson   return 0;
3537a982d89SJeremy L. Thompson }
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson /**
3567a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
3577a982d89SJeremy L. Thompson 
3587a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context. All
3597a982d89SJeremy L. Thompson     backend implementations default to the delegate Ceed context, unless
3607a982d89SJeremy L. Thompson     overridden.
3617a982d89SJeremy L. Thompson 
3627a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
3637a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3667a982d89SJeremy L. Thompson 
3677a982d89SJeremy L. Thompson   @ref Backend
3687a982d89SJeremy L. Thompson **/
3697a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
3707a982d89SJeremy L. Thompson   ceed->delegate = delegate;
3717a982d89SJeremy L. Thompson   delegate->parent = ceed;
3727a982d89SJeremy L. Thompson   return 0;
3737a982d89SJeremy L. Thompson }
3747a982d89SJeremy L. Thompson 
3757a982d89SJeremy L. Thompson /**
3767a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
3777a982d89SJeremy L. Thompson 
3787a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3797a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
3807a982d89SJeremy L. Thompson   @param[in] objname    Name of the object type to retrieve delegate for
3817a982d89SJeremy L. Thompson 
3827a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson   @ref Backend
3857a982d89SJeremy L. Thompson **/
3867a982d89SJeremy L. Thompson int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *objname) {
3877a982d89SJeremy L. Thompson   CeedInt ierr;
3887a982d89SJeremy L. Thompson 
3897a982d89SJeremy L. Thompson   // Check for object delegate
3907a982d89SJeremy L. Thompson   for (CeedInt i=0; i<ceed->objdelegatecount; i++)
3917a982d89SJeremy L. Thompson     if (!strcmp(objname, ceed->objdelegates->objname)) {
3927a982d89SJeremy L. Thompson       *delegate = ceed->objdelegates->delegate;
3937a982d89SJeremy L. Thompson       return 0;
3947a982d89SJeremy L. Thompson     }
3957a982d89SJeremy L. Thompson 
3967a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
3977a982d89SJeremy L. Thompson   ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr);
3987a982d89SJeremy L. Thompson 
3997a982d89SJeremy L. Thompson   return 0;
4007a982d89SJeremy L. Thompson }
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson /**
4037a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
4047a982d89SJeremy L. Thompson 
4057a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context for a
4067a982d89SJeremy L. Thompson     given type of Ceed object. All backend implementations default to the
4077a982d89SJeremy L. Thompson     delegate Ceed context for this object. For example,
4087a982d89SJeremy L. Thompson     CeedSetObjectDelegate(ceed, refceed, "Basis")
4097a982d89SJeremy L. Thompson   uses refceed implementations for all CeedBasis backend functions.
4107a982d89SJeremy L. Thompson 
4117a982d89SJeremy L. Thompson   @param ceed          Ceed context to set delegate of
4127a982d89SJeremy L. Thompson   @param[out] delegate Address to set the delegate to
4137a982d89SJeremy L. Thompson   @param[in] objname   Name of the object type to set delegate for
4147a982d89SJeremy L. Thompson 
4157a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4167a982d89SJeremy L. Thompson 
4177a982d89SJeremy L. Thompson   @ref Backend
4187a982d89SJeremy L. Thompson **/
4197a982d89SJeremy L. Thompson int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *objname) {
4207a982d89SJeremy L. Thompson   CeedInt ierr;
4217a982d89SJeremy L. Thompson   CeedInt count = ceed->objdelegatecount;
4227a982d89SJeremy L. Thompson 
4237a982d89SJeremy L. Thompson   // Malloc or Realloc
4247a982d89SJeremy L. Thompson   if (count) {
4257a982d89SJeremy L. Thompson     ierr = CeedRealloc(count+1, &ceed->objdelegates); CeedChk(ierr);
4267a982d89SJeremy L. Thompson   } else {
4277a982d89SJeremy L. Thompson     ierr = CeedCalloc(1, &ceed->objdelegates); CeedChk(ierr);
4287a982d89SJeremy L. Thompson   }
4297a982d89SJeremy L. Thompson   ceed->objdelegatecount++;
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson   // Set object delegate
4327a982d89SJeremy L. Thompson   ceed->objdelegates[count].delegate = delegate;
4337a982d89SJeremy L. Thompson   size_t slen = strlen(objname) + 1;
4347a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &ceed->objdelegates[count].objname); CeedChk(ierr);
4357a982d89SJeremy L. Thompson   memcpy(ceed->objdelegates[count].objname, objname, slen);
4367a982d89SJeremy L. Thompson 
4377a982d89SJeremy L. Thompson   // Set delegate parent
4387a982d89SJeremy L. Thompson   delegate->parent = ceed;
4397a982d89SJeremy L. Thompson 
4407a982d89SJeremy L. Thompson   return 0;
4417a982d89SJeremy L. Thompson }
4427a982d89SJeremy L. Thompson 
4437a982d89SJeremy L. Thompson /**
4447a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4457a982d89SJeremy L. Thompson 
4467a982d89SJeremy L. Thompson   @param ceed          Ceed context
4477a982d89SJeremy L. Thompson   @param[out] resource Variable to store fallback resource
4487a982d89SJeremy L. Thompson 
4497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson   @ref Backend
4527a982d89SJeremy L. Thompson **/
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
4557a982d89SJeremy L. Thompson   *resource = (const char *)ceed->opfallbackresource;
4567a982d89SJeremy L. Thompson   return 0;
4577a982d89SJeremy L. Thompson }
4587a982d89SJeremy L. Thompson 
4597a982d89SJeremy L. Thompson /**
4607a982d89SJeremy L. Thompson   @brief Set the fallback resource for CeedOperators. The current resource, if
4617a982d89SJeremy L. Thompson            any, is freed by calling this function. This string is freed upon the
4627a982d89SJeremy L. Thompson            destruction of the Ceed context.
4637a982d89SJeremy L. Thompson 
4647a982d89SJeremy L. Thompson   @param[out] ceed Ceed context
4657a982d89SJeremy L. Thompson   @param resource  Fallback resource to set
4667a982d89SJeremy L. Thompson 
4677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson   @ref Backend
4707a982d89SJeremy L. Thompson **/
4717a982d89SJeremy L. Thompson 
4727a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
4737a982d89SJeremy L. Thompson   int ierr;
4747a982d89SJeremy L. Thompson 
4757a982d89SJeremy L. Thompson   // Free old
4767a982d89SJeremy L. Thompson   ierr = CeedFree(&ceed->opfallbackresource); CeedChk(ierr);
4777a982d89SJeremy L. Thompson 
4787a982d89SJeremy L. Thompson   // Set new
4797a982d89SJeremy L. Thompson   size_t len = strlen(resource);
4807a982d89SJeremy L. Thompson   char *tmp;
4817a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
4827a982d89SJeremy L. Thompson   memcpy(tmp, resource, len+1);
4837a982d89SJeremy L. Thompson   ceed->opfallbackresource = tmp;
4847a982d89SJeremy L. Thompson 
4857a982d89SJeremy L. Thompson   return 0;
4867a982d89SJeremy L. Thompson }
4877a982d89SJeremy L. Thompson 
4887a982d89SJeremy L. Thompson /**
4897a982d89SJeremy L. Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context
4907a982d89SJeremy L. Thompson            for a CeedOperator
4917a982d89SJeremy L. Thompson 
4927a982d89SJeremy L. Thompson   @param ceed         Ceed context
4937a982d89SJeremy L. Thompson   @param[out] parent  Variable to store parent Ceed context
4947a982d89SJeremy L. Thompson 
4957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4967a982d89SJeremy L. Thompson 
4977a982d89SJeremy L. Thompson   @ref Backend
4987a982d89SJeremy L. Thompson **/
4997a982d89SJeremy L. Thompson 
5007a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
5017a982d89SJeremy L. Thompson   *parent = ceed->opfallbackparent;
5027a982d89SJeremy L. Thompson   return 0;
5037a982d89SJeremy L. Thompson }
5047a982d89SJeremy L. Thompson 
5057a982d89SJeremy L. Thompson /**
506*9525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
507*9525855cSJeremy L Thompson 
508*9525855cSJeremy L Thompson   @param ceed     Ceed to flag as deterministic
509*9525855cSJeremy L Thompson 
510*9525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
511*9525855cSJeremy L Thompson 
512*9525855cSJeremy L Thompson   @ref Backend
513*9525855cSJeremy L Thompson **/
514*9525855cSJeremy L Thompson 
515*9525855cSJeremy L Thompson int CeedSetDeterministic(Ceed ceed, bool isDeterministic) {
516*9525855cSJeremy L Thompson   ceed->isDeterministic = isDeterministic;
517*9525855cSJeremy L Thompson   return 0;
518*9525855cSJeremy L Thompson }
519*9525855cSJeremy L Thompson 
520*9525855cSJeremy L Thompson /**
5217a982d89SJeremy L. Thompson   @brief Set a backend function
5227a982d89SJeremy L. Thompson 
5237a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5247a982d89SJeremy L. Thompson   the Ceed objects. For example,
5257a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5267a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5277a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5287a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
5297a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson   @param ceed           Ceed context for error handling
5327a982d89SJeremy L. Thompson   @param type           Type of Ceed object to set function for
5337a982d89SJeremy L. Thompson   @param[out] object    Ceed object to set function for
5347a982d89SJeremy L. Thompson   @param fname          Name of function to set
5357a982d89SJeremy L. Thompson   @param f              Function to set
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 CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
5427a982d89SJeremy L. Thompson                            const char *fname, int (*f)()) {
5437a982d89SJeremy L. Thompson   char lookupname[CEED_MAX_RESOURCE_LEN+1] = "";
5447a982d89SJeremy L. Thompson 
5457a982d89SJeremy L. Thompson   // Build lookup name
5467a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
5477a982d89SJeremy L. Thompson     strncat (lookupname, "Ceed", CEED_MAX_RESOURCE_LEN);
5487a982d89SJeremy L. Thompson   strncat(lookupname, type, CEED_MAX_RESOURCE_LEN);
5497a982d89SJeremy L. Thompson   strncat(lookupname, fname, CEED_MAX_RESOURCE_LEN);
5507a982d89SJeremy L. Thompson 
5517a982d89SJeremy L. Thompson   // Find and use offset
5527a982d89SJeremy L. Thompson   for (CeedInt i = 0; ceed->foffsets[i].fname; i++)
5537a982d89SJeremy L. Thompson     if (!strcmp(ceed->foffsets[i].fname, lookupname)) {
5547a982d89SJeremy L. Thompson       size_t offset = ceed->foffsets[i].offset;
5557a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
5567a982d89SJeremy L. Thompson       *fpointer = f;
5577a982d89SJeremy L. Thompson       return 0;
5587a982d89SJeremy L. Thompson     }
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson   // LCOV_EXCL_START
5617a982d89SJeremy L. Thompson   return CeedError(ceed, 1, "Requested function '%s' was not found for CEED "
5627a982d89SJeremy L. Thompson                    "object '%s'", fname, type);
5637a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
5647a982d89SJeremy L. Thompson }
5657a982d89SJeremy L. Thompson 
5667a982d89SJeremy L. Thompson /**
5677a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson   @param ceed      Ceed context to retrieve data of
5707a982d89SJeremy L. Thompson   @param[out] data Address to save data to
5717a982d89SJeremy L. Thompson 
5727a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5737a982d89SJeremy L. Thompson 
5747a982d89SJeremy L. Thompson   @ref Backend
5757a982d89SJeremy L. Thompson **/
5767a982d89SJeremy L. Thompson int CeedGetData(Ceed ceed, void **data) {
5777a982d89SJeremy L. Thompson   *data = ceed->data;
5787a982d89SJeremy L. Thompson   return 0;
5797a982d89SJeremy L. Thompson }
5807a982d89SJeremy L. Thompson 
5817a982d89SJeremy L. Thompson /**
5827a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
5837a982d89SJeremy L. Thompson 
5847a982d89SJeremy L. Thompson   @param ceed           Ceed context to set data of
5857a982d89SJeremy L. Thompson   @param data           Address of data to set
5867a982d89SJeremy L. Thompson 
5877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5887a982d89SJeremy L. Thompson 
5897a982d89SJeremy L. Thompson   @ref Backend
5907a982d89SJeremy L. Thompson **/
5917a982d89SJeremy L. Thompson int CeedSetData(Ceed ceed, void **data) {
5927a982d89SJeremy L. Thompson   ceed->data = *data;
5937a982d89SJeremy L. Thompson   return 0;
5947a982d89SJeremy L. Thompson }
5957a982d89SJeremy L. Thompson 
5967a982d89SJeremy L. Thompson /// @}
5977a982d89SJeremy L. Thompson 
5987a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5997a982d89SJeremy L. Thompson /// Ceed Public API
6007a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6017a982d89SJeremy L. Thompson /// @addtogroup CeedUser
6027a982d89SJeremy L. Thompson /// @{
6037a982d89SJeremy L. Thompson 
6047a982d89SJeremy L. Thompson /**
605d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
606b11c1e72Sjeremylt 
607b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
608b11c1e72Sjeremylt   @param ceed      The library context
609b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
610b11c1e72Sjeremylt 
611b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
612dfdf5a53Sjeremylt 
6137a982d89SJeremy L. Thompson   @ref User
614b11c1e72Sjeremylt **/
615d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
616d7b241e6Sjeremylt   int ierr;
617aedaa0e5Sjeremylt   size_t matchlen = 0, matchidx = UINT_MAX, matchpriority = UINT_MAX, priority;
618d7b241e6Sjeremylt 
619fe2413ffSjeremylt   // Find matching backend
6201d102b48SJeremy L Thompson   if (!resource)
62113873f79Sjeremylt     // LCOV_EXCL_START
6221d102b48SJeremy L Thompson     return CeedError(NULL, 1, "No resource provided");
62313873f79Sjeremylt   // LCOV_EXCL_STOP
62413873f79Sjeremylt 
625d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
626d7b241e6Sjeremylt     size_t n;
627d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
628d7b241e6Sjeremylt     for (n = 0; prefix[n] && prefix[n] == resource[n]; n++) {}
629d7b241e6Sjeremylt     priority = backends[i].priority;
630d7b241e6Sjeremylt     if (n > matchlen || (n == matchlen && matchpriority > priority)) {
631d7b241e6Sjeremylt       matchlen = n;
632d7b241e6Sjeremylt       matchpriority = priority;
633d7b241e6Sjeremylt       matchidx = i;
634d7b241e6Sjeremylt     }
635d7b241e6Sjeremylt   }
6361d102b48SJeremy L Thompson   if (!matchlen)
63713873f79Sjeremylt     // LCOV_EXCL_START
6388079b1e4SJed Brown     return CeedError(NULL, 1, "No suitable backend: %s", resource);
63913873f79Sjeremylt   // LCOV_EXCL_STOP
640fe2413ffSjeremylt 
641fe2413ffSjeremylt   // Setup Ceed
642d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
643bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
6441d102b48SJeremy L Thompson   if (!ceed_error_handler)
6451d102b48SJeremy L Thompson     ceed_error_handler = "abort";
646bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
64756e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
64856e866f4SJed Brown   else
649d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
650d7b241e6Sjeremylt   (*ceed)->refcount = 1;
651d7b241e6Sjeremylt   (*ceed)->data = NULL;
652fe2413ffSjeremylt 
653fe2413ffSjeremylt   // Set lookup table
6546e79d475Sjeremylt   foffset foffsets[] = {
6556e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
6566e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
6576e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
658f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
6596e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
6606e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
6616e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
6626e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
6636e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
6646e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
6656e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
6666e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
6676e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
6686e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
6696e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
6706e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
6716e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
6726e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
673547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
6746e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Destroy),
6756e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Apply),
6766e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, ApplyBlock),
677bd33150aSjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, GetOffsets),
6786e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedElemRestriction, Destroy),
6796e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Apply),
6806e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedBasis, Destroy),
6816e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Apply),
6826e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedTensorContract, Destroy),
6836e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Apply),
6846e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
68580ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
68680ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
6879e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
68880ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
6899e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
690713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
6916e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
692250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
693cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
694250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
6956e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
6966e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
6976e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
6981dfeef1dSjeremylt   };
699fe2413ffSjeremylt 
7006e79d475Sjeremylt   ierr = CeedCalloc(sizeof(foffsets), &(*ceed)->foffsets); CeedChk(ierr);
7016e79d475Sjeremylt   memcpy((*ceed)->foffsets, foffsets, sizeof(foffsets));
702fe2413ffSjeremylt 
7035107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
7045107b09fSJeremy L Thompson   const char fallbackresource[] = "/cpu/self/ref/serial";
7055107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
7065107b09fSJeremy L Thompson   CeedChk(ierr);
7075107b09fSJeremy L Thompson 
708fe2413ffSjeremylt   // Backend specific setup
709d7b241e6Sjeremylt   ierr = backends[matchidx].init(resource, *ceed); CeedChk(ierr);
710fe2413ffSjeremylt 
711e07206deSjeremylt   // Copy resource prefix, if backend setup sucessful
712e07206deSjeremylt   size_t len = strlen(backends[matchidx].prefix);
713e07206deSjeremylt   char *tmp;
714e07206deSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
715e07206deSjeremylt   memcpy(tmp, backends[matchidx].prefix, len+1);
716e07206deSjeremylt   (*ceed)->resource = tmp;
717e07206deSjeremylt 
718d7b241e6Sjeremylt   return 0;
719d7b241e6Sjeremylt }
720d7b241e6Sjeremylt 
721d7b241e6Sjeremylt /**
7227a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
7232f86a920SJeremy L Thompson 
7247a982d89SJeremy L. Thompson   @param ceed            Ceed context to get resource name of
7257a982d89SJeremy L. Thompson   @param[out] resource   Variable to store resource name
7262f86a920SJeremy L Thompson 
7272f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7282f86a920SJeremy L Thompson 
7297a982d89SJeremy L. Thompson   @ref User
7305107b09fSJeremy L Thompson **/
7315107b09fSJeremy L Thompson 
7327a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
7337a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
7345107b09fSJeremy L Thompson   return 0;
7355107b09fSJeremy L Thompson }
7365107b09fSJeremy L Thompson 
7375107b09fSJeremy L Thompson /**
738d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
739c907536fSjeremylt 
740d79b80ecSjeremylt   @param ceed      Ceed context to get preferred memory type of
741288c0443SJeremy L Thompson   @param[out] type Address to save preferred memory type to
742c907536fSjeremylt 
743c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
744c907536fSjeremylt 
7457a982d89SJeremy L. Thompson   @ref User
746c907536fSjeremylt **/
747c907536fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type) {
748c907536fSjeremylt   int ierr;
749c263cd57Sjeremylt 
750c907536fSjeremylt   if (ceed->GetPreferredMemType) {
751c907536fSjeremylt     ierr = ceed->GetPreferredMemType(type); CeedChk(ierr);
752c907536fSjeremylt   } else {
753c263cd57Sjeremylt     Ceed delegate;
754c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
755c263cd57Sjeremylt 
756c263cd57Sjeremylt     if (delegate) {
757c263cd57Sjeremylt       ierr = CeedGetPreferredMemType(delegate, type); CeedChk(ierr);
758c263cd57Sjeremylt     } else {
759c907536fSjeremylt       *type = CEED_MEM_HOST;
760c907536fSjeremylt     }
761c263cd57Sjeremylt   }
762c907536fSjeremylt 
763c907536fSjeremylt   return 0;
764c907536fSjeremylt }
765c907536fSjeremylt 
766c907536fSjeremylt /**
767*9525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
768*9525855cSJeremy L Thompson 
769*9525855cSJeremy L Thompson   @param[in] ceed              Ceed
770*9525855cSJeremy L Thompson   @param[out] isDeterministic  Variable to store deterministic status
771*9525855cSJeremy L Thompson 
772*9525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
773*9525855cSJeremy L Thompson 
774*9525855cSJeremy L Thompson   @ref User
775*9525855cSJeremy L Thompson **/
776*9525855cSJeremy L Thompson int CeedIsDeterministic(Ceed ceed, bool *isDeterministic) {
777*9525855cSJeremy L Thompson   *isDeterministic = ceed->isDeterministic;
778*9525855cSJeremy L Thompson   return 0;
779*9525855cSJeremy L Thompson }
780*9525855cSJeremy L Thompson 
781*9525855cSJeremy L Thompson /**
7820a0da059Sjeremylt   @brief View a Ceed
7830a0da059Sjeremylt 
7840a0da059Sjeremylt   @param[in] ceed          Ceed to view
7850a0da059Sjeremylt   @param[in] stream        Filestream to write to
7860a0da059Sjeremylt 
7870a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
7880a0da059Sjeremylt 
7890a0da059Sjeremylt   @ref User
7900a0da059Sjeremylt **/
7910a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
7920a0da059Sjeremylt   int ierr;
7930a0da059Sjeremylt   CeedMemType memtype;
7940a0da059Sjeremylt 
7950a0da059Sjeremylt   ierr = CeedGetPreferredMemType(ceed, &memtype); CeedChk(ierr);
7960a0da059Sjeremylt 
7970a0da059Sjeremylt   fprintf(stream, "Ceed\n"
7980a0da059Sjeremylt           "  Ceed Resource: %s\n"
7990a0da059Sjeremylt           "  Preferred MemType: %s\n",
8000a0da059Sjeremylt           ceed->resource, CeedMemTypes[memtype]);
8010a0da059Sjeremylt 
8020a0da059Sjeremylt   return 0;
8030a0da059Sjeremylt }
8040a0da059Sjeremylt 
8050a0da059Sjeremylt /**
806b11c1e72Sjeremylt   @brief Destroy a Ceed context
807d7b241e6Sjeremylt 
808d7b241e6Sjeremylt   @param ceed Address of Ceed context to destroy
809b11c1e72Sjeremylt 
810b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
811dfdf5a53Sjeremylt 
8127a982d89SJeremy L. Thompson   @ref User
813b11c1e72Sjeremylt **/
814d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
815d7b241e6Sjeremylt   int ierr;
8161d102b48SJeremy L Thompson   if (!*ceed || --(*ceed)->refcount > 0)
8171d102b48SJeremy L Thompson     return 0;
8180ace9bf2Sjeremylt 
8195fe0d4faSjeremylt   if ((*ceed)->delegate) {
8205fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
8215fe0d4faSjeremylt   }
8220ace9bf2Sjeremylt 
823aefd8378Sjeremylt   if ((*ceed)->objdelegatecount > 0) {
824aefd8378Sjeremylt     for (int i=0; i<(*ceed)->objdelegatecount; i++) {
825aefd8378Sjeremylt       ierr = CeedDestroy(&((*ceed)->objdelegates[i].delegate)); CeedChk(ierr);
826aefd8378Sjeremylt       ierr = CeedFree(&(*ceed)->objdelegates[i].objname); CeedChk(ierr);
827aefd8378Sjeremylt     }
828aefd8378Sjeremylt     ierr = CeedFree(&(*ceed)->objdelegates); CeedChk(ierr);
829aefd8378Sjeremylt   }
8300ace9bf2Sjeremylt 
831d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
832d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
833d7b241e6Sjeremylt   }
8340ace9bf2Sjeremylt 
8356e79d475Sjeremylt   ierr = CeedFree(&(*ceed)->foffsets); CeedChk(ierr);
836e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
8375107b09fSJeremy L Thompson   ierr = CeedDestroy(&(*ceed)->opfallbackceed); CeedChk(ierr);
8385107b09fSJeremy L Thompson   ierr = CeedFree(&(*ceed)->opfallbackresource); CeedChk(ierr);
839d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
840d7b241e6Sjeremylt   return 0;
841d7b241e6Sjeremylt }
842d7b241e6Sjeremylt 
8437a982d89SJeremy L. Thompson /**
8447a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
8457a982d89SJeremy L. Thompson 
8467a982d89SJeremy L. Thompson   @ref Developer
8477a982d89SJeremy L. Thompson **/
8487a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
8497a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
8507a982d89SJeremy L. Thompson   va_list args;
8517a982d89SJeremy L. Thompson   int retval;
8527a982d89SJeremy L. Thompson   va_start(args, format);
8537a982d89SJeremy L. Thompson   if (ceed) {
8547a982d89SJeremy L. Thompson     retval = ceed->Error(ceed, filename, lineno, func, ecode, format, args);
8557a982d89SJeremy L. Thompson   } else {
8567a982d89SJeremy L. Thompson     // This function doesn't actually return
857b0d62198Sjeremylt     // LCOV_EXCL_START
8587a982d89SJeremy L. Thompson     retval = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, args);
8597a982d89SJeremy L. Thompson   }
8607a982d89SJeremy L. Thompson   va_end(args);
8617a982d89SJeremy L. Thompson   return retval;
862b0d62198Sjeremylt   // LCOV_EXCL_STOP
8637a982d89SJeremy L. Thompson }
8647a982d89SJeremy L. Thompson 
865d7b241e6Sjeremylt /// @}
866