xref: /libCEED/rust/libceed-sys/c-src/interface/ceed.c (revision 1070991d337d0be0525e3d5540009010c9db64b9)
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 
1227a982d89SJeremy L. Thompson /// @}
123d7b241e6Sjeremylt 
1247a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1257a982d89SJeremy L. Thompson /// Ceed Backend API
1267a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1277a982d89SJeremy L. Thompson /// @addtogroup CeedBackend
1287a982d89SJeremy L. Thompson /// @{
129d7b241e6Sjeremylt 
130b11c1e72Sjeremylt /**
13160f9e2d6SJeremy L Thompson   @brief Print Ceed debugging information
13260f9e2d6SJeremy L Thompson 
13360f9e2d6SJeremy L Thompson   @param ceed    Ceed context
13460f9e2d6SJeremy L Thompson   @param format  Printing format
13560f9e2d6SJeremy L Thompson 
13660f9e2d6SJeremy L Thompson   @return None
13760f9e2d6SJeremy L Thompson 
13860f9e2d6SJeremy L Thompson   @ref Backend
13960f9e2d6SJeremy L Thompson **/
140fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
14160f9e2d6SJeremy L Thompson void CeedDebugImpl(const Ceed ceed, const char *format,...) {
14260f9e2d6SJeremy L Thompson   if (!ceed->debug) return;
14360f9e2d6SJeremy L Thompson   va_list args;
14460f9e2d6SJeremy L Thompson   va_start(args, format);
14560f9e2d6SJeremy L Thompson   CeedDebugImpl256(ceed, 0, format, args);
14660f9e2d6SJeremy L Thompson   va_end(args);
14760f9e2d6SJeremy L Thompson }
148fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
14960f9e2d6SJeremy L Thompson 
15060f9e2d6SJeremy L Thompson /**
15160f9e2d6SJeremy L Thompson   @brief Print Ceed debugging information in color
15260f9e2d6SJeremy L Thompson 
15360f9e2d6SJeremy L Thompson   @param ceed    Ceed context
15460f9e2d6SJeremy L Thompson   @param color   Color to print
15560f9e2d6SJeremy L Thompson   @param format  Printing format
15660f9e2d6SJeremy L Thompson 
15760f9e2d6SJeremy L Thompson   @return None
15860f9e2d6SJeremy L Thompson 
15960f9e2d6SJeremy L Thompson   @ref Backend
16060f9e2d6SJeremy L Thompson **/
161fc6bbcedSJeremy L Thompson // LCOV_EXCL_START
16260f9e2d6SJeremy L Thompson void CeedDebugImpl256(const Ceed ceed, const unsigned char color,
16360f9e2d6SJeremy L Thompson                       const char *format,...) {
16460f9e2d6SJeremy L Thompson   if (!ceed->debug) return;
16560f9e2d6SJeremy L Thompson   va_list args;
16660f9e2d6SJeremy L Thompson   va_start(args, format);
16760f9e2d6SJeremy L Thompson   fflush(stdout);
16860f9e2d6SJeremy L Thompson   fprintf(stdout, "\033[38;5;%dm", color);
16960f9e2d6SJeremy L Thompson   vfprintf(stdout, format, args);
17060f9e2d6SJeremy L Thompson   fprintf(stdout, "\033[m");
17160f9e2d6SJeremy L Thompson   fprintf(stdout, "\n");
17260f9e2d6SJeremy L Thompson   fflush(stdout);
17360f9e2d6SJeremy L Thompson   va_end(args);
17460f9e2d6SJeremy L Thompson }
175fc6bbcedSJeremy L Thompson // LCOV_EXCL_STOP
17660f9e2d6SJeremy L Thompson 
17760f9e2d6SJeremy L Thompson /**
178b11c1e72Sjeremylt   @brief Allocate an array on the host; use CeedMalloc()
179b11c1e72Sjeremylt 
180b11c1e72Sjeremylt   Memory usage can be tracked by the library.  This ensures sufficient
181b11c1e72Sjeremylt     alignment for vectorization and should be used for large allocations.
182b11c1e72Sjeremylt 
183b11c1e72Sjeremylt   @param n Number of units to allocate
184b11c1e72Sjeremylt   @param unit Size of each unit
185b11c1e72Sjeremylt   @param p Address of pointer to hold the result.
186b11c1e72Sjeremylt 
187b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
188b11c1e72Sjeremylt 
189b11c1e72Sjeremylt   @sa CeedFree()
190dfdf5a53Sjeremylt 
1917a982d89SJeremy L. Thompson   @ref Backend
192b11c1e72Sjeremylt **/
193d7b241e6Sjeremylt int CeedMallocArray(size_t n, size_t unit, void *p) {
194d7b241e6Sjeremylt   int ierr = posix_memalign((void **)p, CEED_ALIGN, n*unit);
195d7b241e6Sjeremylt   if (ierr)
196c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1971d102b48SJeremy L Thompson     return CeedError(NULL, ierr, "posix_memalign failed to allocate %zd "
1981d102b48SJeremy L Thompson                      "members of size %zd\n", n, unit);
199c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
200c042f62fSJeremy L Thompson 
201d7b241e6Sjeremylt   return 0;
202d7b241e6Sjeremylt }
203d7b241e6Sjeremylt 
204b11c1e72Sjeremylt /**
205b11c1e72Sjeremylt   @brief Allocate a cleared (zeroed) array on the host; use CeedCalloc()
206b11c1e72Sjeremylt 
207b11c1e72Sjeremylt   Memory usage can be tracked by the library.
208b11c1e72Sjeremylt 
209b11c1e72Sjeremylt   @param n    Number of units to allocate
210b11c1e72Sjeremylt   @param unit Size of each unit
211b11c1e72Sjeremylt   @param p    Address of pointer to hold the result.
212b11c1e72Sjeremylt 
213b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
214b11c1e72Sjeremylt 
215b11c1e72Sjeremylt   @sa CeedFree()
216dfdf5a53Sjeremylt 
2177a982d89SJeremy L. Thompson   @ref Backend
218b11c1e72Sjeremylt **/
219d7b241e6Sjeremylt int CeedCallocArray(size_t n, size_t unit, void *p) {
220d7b241e6Sjeremylt   *(void **)p = calloc(n, unit);
221d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
222c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2231d102b48SJeremy L Thompson     return CeedError(NULL, 1, "calloc failed to allocate %zd members of size "
2241d102b48SJeremy L Thompson                      "%zd\n", n, unit);
225c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
226c042f62fSJeremy L Thompson 
227d7b241e6Sjeremylt   return 0;
228d7b241e6Sjeremylt }
229d7b241e6Sjeremylt 
230b11c1e72Sjeremylt /**
231b11c1e72Sjeremylt   @brief Reallocate an array on the host; use CeedRealloc()
232b11c1e72Sjeremylt 
233b11c1e72Sjeremylt   Memory usage can be tracked by the library.
234b11c1e72Sjeremylt 
235b11c1e72Sjeremylt   @param n    Number of units to allocate
236b11c1e72Sjeremylt   @param unit Size of each unit
237b11c1e72Sjeremylt   @param p    Address of pointer to hold the result.
238b11c1e72Sjeremylt 
239b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
240b11c1e72Sjeremylt 
241b11c1e72Sjeremylt   @sa CeedFree()
242dfdf5a53Sjeremylt 
2437a982d89SJeremy L. Thompson   @ref Backend
244b11c1e72Sjeremylt **/
245d7b241e6Sjeremylt int CeedReallocArray(size_t n, size_t unit, void *p) {
246d7b241e6Sjeremylt   *(void **)p = realloc(*(void **)p, n*unit);
247d7b241e6Sjeremylt   if (n && unit && !*(void **)p)
248c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2491d102b48SJeremy L Thompson     return CeedError(NULL, 1, "realloc failed to allocate %zd members of size "
2501d102b48SJeremy L Thompson                      "%zd\n", n, unit);
251c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
252c042f62fSJeremy L Thompson 
253d7b241e6Sjeremylt   return 0;
254d7b241e6Sjeremylt }
255d7b241e6Sjeremylt 
25634138859Sjeremylt /** Free memory allocated using CeedMalloc() or CeedCalloc()
25734138859Sjeremylt 
25834138859Sjeremylt   @param p address of pointer to memory.  This argument is of type void* to
25934138859Sjeremylt              avoid needing a cast, but is the address of the pointer (which is
26034138859Sjeremylt              zeroed) rather than the pointer.
26134138859Sjeremylt **/
262d7b241e6Sjeremylt int CeedFree(void *p) {
263d7b241e6Sjeremylt   free(*(void **)p);
264d7b241e6Sjeremylt   *(void **)p = NULL;
265d7b241e6Sjeremylt   return 0;
266d7b241e6Sjeremylt }
267d7b241e6Sjeremylt 
268d7b241e6Sjeremylt /**
2697a982d89SJeremy L. Thompson   @brief Register a Ceed backend
270d7b241e6Sjeremylt 
2717a982d89SJeremy L. Thompson   @param prefix   Prefix of resources for this backend to respond to.  For
2727a982d89SJeremy L. Thompson                     example, the reference backend responds to "/cpu/self".
2737a982d89SJeremy L. Thompson   @param init     Initialization function called by CeedInit() when the backend
2747a982d89SJeremy L. Thompson                     is selected to drive the requested resource.
2757a982d89SJeremy L. Thompson   @param priority Integer priority.  Lower values are preferred in case the
2767a982d89SJeremy L. Thompson                     resource requested by CeedInit() has non-unique best prefix
2777a982d89SJeremy L. Thompson                     match.
278b11c1e72Sjeremylt 
279b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
280dfdf5a53Sjeremylt 
2817a982d89SJeremy L. Thompson   @ref Backend
282b11c1e72Sjeremylt **/
2837a982d89SJeremy L. Thompson int CeedRegister(const char *prefix, int (*init)(const char *, Ceed),
2847a982d89SJeremy L. Thompson                  unsigned int priority) {
2857a982d89SJeremy L. Thompson   if (num_backends >= sizeof(backends) / sizeof(backends[0]))
2867a982d89SJeremy L. Thompson     // LCOV_EXCL_START
2877a982d89SJeremy L. Thompson     return CeedError(NULL, 1, "Too many backends");
2887a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
2897a982d89SJeremy L. Thompson 
2907a982d89SJeremy L. Thompson   strncpy(backends[num_backends].prefix, prefix, CEED_MAX_RESOURCE_LEN);
2917a982d89SJeremy L. Thompson   backends[num_backends].prefix[CEED_MAX_RESOURCE_LEN-1] = 0;
2927a982d89SJeremy L. Thompson   backends[num_backends].init = init;
2937a982d89SJeremy L. Thompson   backends[num_backends].priority = priority;
2947a982d89SJeremy L. Thompson   num_backends++;
2951d102b48SJeremy L Thompson   return 0;
296d7b241e6Sjeremylt }
297d7b241e6Sjeremylt 
298b11c1e72Sjeremylt /**
29960f9e2d6SJeremy L Thompson   @brief Return debugging status flag
30060f9e2d6SJeremy L Thompson 
30160f9e2d6SJeremy L Thompson   @param ceed     Ceed context to get debugging flag
30260f9e2d6SJeremy L Thompson   @param isDebug  Variable to store debugging flag
30360f9e2d6SJeremy L Thompson 
30460f9e2d6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
30560f9e2d6SJeremy L Thompson 
30660f9e2d6SJeremy L Thompson   @ref Bcakend
30760f9e2d6SJeremy L Thompson **/
30860f9e2d6SJeremy L Thompson int CeedIsDebug(Ceed ceed, bool *isDebug) {
30960f9e2d6SJeremy L Thompson   *isDebug = ceed->debug;
31060f9e2d6SJeremy L Thompson   return 0;
31160f9e2d6SJeremy L Thompson }
31260f9e2d6SJeremy L Thompson 
31360f9e2d6SJeremy L Thompson /**
3147a982d89SJeremy L. Thompson   @brief Retrieve a parent Ceed context
3157a982d89SJeremy L. Thompson 
3167a982d89SJeremy L. Thompson   @param ceed        Ceed context to retrieve parent of
3177a982d89SJeremy L. Thompson   @param[out] parent Address to save the parent to
3187a982d89SJeremy L. Thompson 
3197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3207a982d89SJeremy L. Thompson 
3217a982d89SJeremy L. Thompson   @ref Backend
3227a982d89SJeremy L. Thompson **/
3237a982d89SJeremy L. Thompson int CeedGetParent(Ceed ceed, Ceed *parent) {
3247a982d89SJeremy L. Thompson   int ierr;
3257a982d89SJeremy L. Thompson   if (ceed->parent) {
3267a982d89SJeremy L. Thompson     ierr = CeedGetParent(ceed->parent, parent); CeedChk(ierr);
3277a982d89SJeremy L. Thompson     return 0;
3287a982d89SJeremy L. Thompson   }
3297a982d89SJeremy L. Thompson   *parent = ceed;
3307a982d89SJeremy L. Thompson   return 0;
3317a982d89SJeremy L. Thompson }
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson /**
3347a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context
3357a982d89SJeremy L. Thompson 
3367a982d89SJeremy L. Thompson   @param ceed          Ceed context to retrieve delegate of
3377a982d89SJeremy L. Thompson   @param[out] delegate Address to save the delegate to
3387a982d89SJeremy L. Thompson 
3397a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3407a982d89SJeremy L. Thompson 
3417a982d89SJeremy L. Thompson   @ref Backend
3427a982d89SJeremy L. Thompson **/
3437a982d89SJeremy L. Thompson int CeedGetDelegate(Ceed ceed, Ceed *delegate) {
3447a982d89SJeremy L. Thompson   *delegate = ceed->delegate;
3457a982d89SJeremy L. Thompson   return 0;
3467a982d89SJeremy L. Thompson }
3477a982d89SJeremy L. Thompson 
3487a982d89SJeremy L. Thompson /**
3497a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context
3507a982d89SJeremy L. Thompson 
3517a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context. All
3527a982d89SJeremy L. Thompson     backend implementations default to the delegate Ceed context, unless
3537a982d89SJeremy L. Thompson     overridden.
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson   @param ceed           Ceed context to set delegate of
3567a982d89SJeremy L. Thompson   @param[out] delegate  Address to set the delegate to
3577a982d89SJeremy L. Thompson 
3587a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3597a982d89SJeremy L. Thompson 
3607a982d89SJeremy L. Thompson   @ref Backend
3617a982d89SJeremy L. Thompson **/
3627a982d89SJeremy L. Thompson int CeedSetDelegate(Ceed ceed, Ceed delegate) {
3637a982d89SJeremy L. Thompson   ceed->delegate = delegate;
3647a982d89SJeremy L. Thompson   delegate->parent = ceed;
3657a982d89SJeremy L. Thompson   return 0;
3667a982d89SJeremy L. Thompson }
3677a982d89SJeremy L. Thompson 
3687a982d89SJeremy L. Thompson /**
3697a982d89SJeremy L. Thompson   @brief Retrieve a delegate Ceed context for a specific object type
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson   @param ceed           Ceed context to retrieve delegate of
3727a982d89SJeremy L. Thompson   @param[out] delegate  Address to save the delegate to
3737a982d89SJeremy L. Thompson   @param[in] objname    Name of the object type to retrieve delegate for
3747a982d89SJeremy L. Thompson 
3757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3767a982d89SJeremy L. Thompson 
3777a982d89SJeremy L. Thompson   @ref Backend
3787a982d89SJeremy L. Thompson **/
3797a982d89SJeremy L. Thompson int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *objname) {
3807a982d89SJeremy L. Thompson   CeedInt ierr;
3817a982d89SJeremy L. Thompson 
3827a982d89SJeremy L. Thompson   // Check for object delegate
3837a982d89SJeremy L. Thompson   for (CeedInt i=0; i<ceed->objdelegatecount; i++)
3847a982d89SJeremy L. Thompson     if (!strcmp(objname, ceed->objdelegates->objname)) {
3857a982d89SJeremy L. Thompson       *delegate = ceed->objdelegates->delegate;
3867a982d89SJeremy L. Thompson       return 0;
3877a982d89SJeremy L. Thompson     }
3887a982d89SJeremy L. Thompson 
3897a982d89SJeremy L. Thompson   // Use default delegate if no object delegate
3907a982d89SJeremy L. Thompson   ierr = CeedGetDelegate(ceed, delegate); CeedChk(ierr);
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson   return 0;
3937a982d89SJeremy L. Thompson }
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson /**
3967a982d89SJeremy L. Thompson   @brief Set a delegate Ceed context for a specific object type
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson   This function allows a Ceed context to set a delegate Ceed context for a
3997a982d89SJeremy L. Thompson     given type of Ceed object. All backend implementations default to the
4007a982d89SJeremy L. Thompson     delegate Ceed context for this object. For example,
4017a982d89SJeremy L. Thompson     CeedSetObjectDelegate(ceed, refceed, "Basis")
4027a982d89SJeremy L. Thompson   uses refceed implementations for all CeedBasis backend functions.
4037a982d89SJeremy L. Thompson 
4047a982d89SJeremy L. Thompson   @param ceed          Ceed context to set delegate of
4057a982d89SJeremy L. Thompson   @param[out] delegate Address to set the delegate to
4067a982d89SJeremy L. Thompson   @param[in] objname   Name of the object type to set delegate for
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 CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *objname) {
4137a982d89SJeremy L. Thompson   CeedInt ierr;
4147a982d89SJeremy L. Thompson   CeedInt count = ceed->objdelegatecount;
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   // Malloc or Realloc
4177a982d89SJeremy L. Thompson   if (count) {
4187a982d89SJeremy L. Thompson     ierr = CeedRealloc(count+1, &ceed->objdelegates); CeedChk(ierr);
4197a982d89SJeremy L. Thompson   } else {
4207a982d89SJeremy L. Thompson     ierr = CeedCalloc(1, &ceed->objdelegates); CeedChk(ierr);
4217a982d89SJeremy L. Thompson   }
4227a982d89SJeremy L. Thompson   ceed->objdelegatecount++;
4237a982d89SJeremy L. Thompson 
4247a982d89SJeremy L. Thompson   // Set object delegate
4257a982d89SJeremy L. Thompson   ceed->objdelegates[count].delegate = delegate;
4267a982d89SJeremy L. Thompson   size_t slen = strlen(objname) + 1;
4277a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &ceed->objdelegates[count].objname); CeedChk(ierr);
4287a982d89SJeremy L. Thompson   memcpy(ceed->objdelegates[count].objname, objname, slen);
4297a982d89SJeremy L. Thompson 
4307a982d89SJeremy L. Thompson   // Set delegate parent
4317a982d89SJeremy L. Thompson   delegate->parent = ceed;
4327a982d89SJeremy L. Thompson 
4337a982d89SJeremy L. Thompson   return 0;
4347a982d89SJeremy L. Thompson }
4357a982d89SJeremy L. Thompson 
4367a982d89SJeremy L. Thompson /**
4377a982d89SJeremy L. Thompson   @brief Get the fallback resource for CeedOperators
4387a982d89SJeremy L. Thompson 
4397a982d89SJeremy L. Thompson   @param ceed          Ceed context
4407a982d89SJeremy L. Thompson   @param[out] resource Variable to store fallback resource
4417a982d89SJeremy L. Thompson 
4427a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4437a982d89SJeremy L. Thompson 
4447a982d89SJeremy L. Thompson   @ref Backend
4457a982d89SJeremy L. Thompson **/
4467a982d89SJeremy L. Thompson 
4477a982d89SJeremy L. Thompson int CeedGetOperatorFallbackResource(Ceed ceed, const char **resource) {
4487a982d89SJeremy L. Thompson   *resource = (const char *)ceed->opfallbackresource;
4497a982d89SJeremy L. Thompson   return 0;
4507a982d89SJeremy L. Thompson }
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson /**
4537a982d89SJeremy L. Thompson   @brief Set the fallback resource for CeedOperators. The current resource, if
4547a982d89SJeremy L. Thompson            any, is freed by calling this function. This string is freed upon the
4557a982d89SJeremy L. Thompson            destruction of the Ceed context.
4567a982d89SJeremy L. Thompson 
4577a982d89SJeremy L. Thompson   @param[out] ceed Ceed context
4587a982d89SJeremy L. Thompson   @param resource  Fallback resource to set
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4617a982d89SJeremy L. Thompson 
4627a982d89SJeremy L. Thompson   @ref Backend
4637a982d89SJeremy L. Thompson **/
4647a982d89SJeremy L. Thompson 
4657a982d89SJeremy L. Thompson int CeedSetOperatorFallbackResource(Ceed ceed, const char *resource) {
4667a982d89SJeremy L. Thompson   int ierr;
4677a982d89SJeremy L. Thompson 
4687a982d89SJeremy L. Thompson   // Free old
4697a982d89SJeremy L. Thompson   ierr = CeedFree(&ceed->opfallbackresource); CeedChk(ierr);
4707a982d89SJeremy L. Thompson 
4717a982d89SJeremy L. Thompson   // Set new
4727a982d89SJeremy L. Thompson   size_t len = strlen(resource);
4737a982d89SJeremy L. Thompson   char *tmp;
4747a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
4757a982d89SJeremy L. Thompson   memcpy(tmp, resource, len+1);
4767a982d89SJeremy L. Thompson   ceed->opfallbackresource = tmp;
4777a982d89SJeremy L. Thompson 
4787a982d89SJeremy L. Thompson   return 0;
4797a982d89SJeremy L. Thompson }
4807a982d89SJeremy L. Thompson 
4817a982d89SJeremy L. Thompson /**
4827a982d89SJeremy L. Thompson   @brief Get the parent Ceed context associated with a fallback Ceed context
4837a982d89SJeremy L. Thompson            for a CeedOperator
4847a982d89SJeremy L. Thompson 
4857a982d89SJeremy L. Thompson   @param ceed         Ceed context
4867a982d89SJeremy L. Thompson   @param[out] parent  Variable to store parent Ceed context
4877a982d89SJeremy L. Thompson 
4887a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4897a982d89SJeremy L. Thompson 
4907a982d89SJeremy L. Thompson   @ref Backend
4917a982d89SJeremy L. Thompson **/
4927a982d89SJeremy L. Thompson 
4937a982d89SJeremy L. Thompson int CeedGetOperatorFallbackParentCeed(Ceed ceed, Ceed *parent) {
4947a982d89SJeremy L. Thompson   *parent = ceed->opfallbackparent;
4957a982d89SJeremy L. Thompson   return 0;
4967a982d89SJeremy L. Thompson }
4977a982d89SJeremy L. Thompson 
4987a982d89SJeremy L. Thompson /**
4999525855cSJeremy L Thompson   @brief Flag Ceed context as deterministic
5009525855cSJeremy L Thompson 
5019525855cSJeremy L Thompson   @param ceed     Ceed to flag as deterministic
5029525855cSJeremy L Thompson 
5039525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5049525855cSJeremy L Thompson 
5059525855cSJeremy L Thompson   @ref Backend
5069525855cSJeremy L Thompson **/
5079525855cSJeremy L Thompson 
5089525855cSJeremy L Thompson int CeedSetDeterministic(Ceed ceed, bool isDeterministic) {
5099525855cSJeremy L Thompson   ceed->isDeterministic = isDeterministic;
5109525855cSJeremy L Thompson   return 0;
5119525855cSJeremy L Thompson }
5129525855cSJeremy L Thompson 
5139525855cSJeremy L Thompson /**
5147a982d89SJeremy L. Thompson   @brief Set a backend function
5157a982d89SJeremy L. Thompson 
5167a982d89SJeremy L. Thompson   This function is used for a backend to set the function associated with
5177a982d89SJeremy L. Thompson   the Ceed objects. For example,
5187a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", BackendVectorCreate)
5197a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedVectorCreate' and
5207a982d89SJeremy L. Thompson     CeedSetBackendFunction(ceed, "Basis", basis, "Apply", BackendBasisApply)
5217a982d89SJeremy L. Thompson   sets the backend implementation of 'CeedBasisApply'. Note, the prefix 'Ceed'
5227a982d89SJeremy L. Thompson   is not required for the object type ("Basis" vs "CeedBasis").
5237a982d89SJeremy L. Thompson 
5247a982d89SJeremy L. Thompson   @param ceed           Ceed context for error handling
5257a982d89SJeremy L. Thompson   @param type           Type of Ceed object to set function for
5267a982d89SJeremy L. Thompson   @param[out] object    Ceed object to set function for
5277a982d89SJeremy L. Thompson   @param fname          Name of function to set
5287a982d89SJeremy L. Thompson   @param f              Function to set
5297a982d89SJeremy L. Thompson 
5307a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5317a982d89SJeremy L. Thompson 
5327a982d89SJeremy L. Thompson   @ref Backend
5337a982d89SJeremy L. Thompson **/
5347a982d89SJeremy L. Thompson int CeedSetBackendFunction(Ceed ceed, const char *type, void *object,
5357a982d89SJeremy L. Thompson                            const char *fname, int (*f)()) {
5367a982d89SJeremy L. Thompson   char lookupname[CEED_MAX_RESOURCE_LEN+1] = "";
5377a982d89SJeremy L. Thompson 
5387a982d89SJeremy L. Thompson   // Build lookup name
5397a982d89SJeremy L. Thompson   if (strcmp(type, "Ceed"))
5407a982d89SJeremy L. Thompson     strncat (lookupname, "Ceed", CEED_MAX_RESOURCE_LEN);
5417a982d89SJeremy L. Thompson   strncat(lookupname, type, CEED_MAX_RESOURCE_LEN);
5427a982d89SJeremy L. Thompson   strncat(lookupname, fname, CEED_MAX_RESOURCE_LEN);
5437a982d89SJeremy L. Thompson 
5447a982d89SJeremy L. Thompson   // Find and use offset
5457a982d89SJeremy L. Thompson   for (CeedInt i = 0; ceed->foffsets[i].fname; i++)
5467a982d89SJeremy L. Thompson     if (!strcmp(ceed->foffsets[i].fname, lookupname)) {
5477a982d89SJeremy L. Thompson       size_t offset = ceed->foffsets[i].offset;
5487a982d89SJeremy L. Thompson       int (**fpointer)(void) = (int (**)(void))((char *)object + offset); // *NOPAD*
5497a982d89SJeremy L. Thompson       *fpointer = f;
5507a982d89SJeremy L. Thompson       return 0;
5517a982d89SJeremy L. Thompson     }
5527a982d89SJeremy L. Thompson 
5537a982d89SJeremy L. Thompson   // LCOV_EXCL_START
5547a982d89SJeremy L. Thompson   return CeedError(ceed, 1, "Requested function '%s' was not found for CEED "
5557a982d89SJeremy L. Thompson                    "object '%s'", fname, type);
5567a982d89SJeremy L. Thompson   // LCOV_EXCL_STOP
5577a982d89SJeremy L. Thompson }
5587a982d89SJeremy L. Thompson 
5597a982d89SJeremy L. Thompson /**
5607a982d89SJeremy L. Thompson   @brief Retrieve backend data for a Ceed context
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson   @param ceed      Ceed context to retrieve data of
5637a982d89SJeremy L. Thompson   @param[out] data Address to save data to
5647a982d89SJeremy L. Thompson 
5657a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5667a982d89SJeremy L. Thompson 
5677a982d89SJeremy L. Thompson   @ref Backend
5687a982d89SJeremy L. Thompson **/
569777ff853SJeremy L Thompson int CeedGetData(Ceed ceed, void *data) {
570777ff853SJeremy L Thompson   *(void **)data = ceed->data;
5717a982d89SJeremy L. Thompson   return 0;
5727a982d89SJeremy L. Thompson }
5737a982d89SJeremy L. Thompson 
5747a982d89SJeremy L. Thompson /**
5757a982d89SJeremy L. Thompson   @brief Set backend data for a Ceed context
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson   @param ceed           Ceed context to set data of
5787a982d89SJeremy L. Thompson   @param data           Address of data to set
5797a982d89SJeremy L. Thompson 
5807a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   @ref Backend
5837a982d89SJeremy L. Thompson **/
584777ff853SJeremy L Thompson int CeedSetData(Ceed ceed, void *data) {
585777ff853SJeremy L Thompson   ceed->data = data;
5867a982d89SJeremy L. Thompson   return 0;
5877a982d89SJeremy L. Thompson }
5887a982d89SJeremy L. Thompson 
5897a982d89SJeremy L. Thompson /// @}
5907a982d89SJeremy L. Thompson 
5917a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5927a982d89SJeremy L. Thompson /// Ceed Public API
5937a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5947a982d89SJeremy L. Thompson /// @addtogroup CeedUser
5957a982d89SJeremy L. Thompson /// @{
5967a982d89SJeremy L. Thompson 
5977a982d89SJeremy L. Thompson /**
598d79b80ecSjeremylt   @brief Initialize a \ref Ceed context to use the specified resource.
599b11c1e72Sjeremylt 
600b11c1e72Sjeremylt   @param resource  Resource to use, e.g., "/cpu/self"
601b11c1e72Sjeremylt   @param ceed      The library context
602b11c1e72Sjeremylt   @sa CeedRegister() CeedDestroy()
603b11c1e72Sjeremylt 
604b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
605dfdf5a53Sjeremylt 
6067a982d89SJeremy L. Thompson   @ref User
607b11c1e72Sjeremylt **/
608d7b241e6Sjeremylt int CeedInit(const char *resource, Ceed *ceed) {
609d7b241e6Sjeremylt   int ierr;
610aedaa0e5Sjeremylt   size_t matchlen = 0, matchidx = UINT_MAX, matchpriority = UINT_MAX, priority;
611d7b241e6Sjeremylt 
612fe2413ffSjeremylt   // Find matching backend
6131d102b48SJeremy L Thompson   if (!resource)
61413873f79Sjeremylt     // LCOV_EXCL_START
6151d102b48SJeremy L Thompson     return CeedError(NULL, 1, "No resource provided");
61613873f79Sjeremylt   // LCOV_EXCL_STOP
6171d013790SJed Brown   ierr = CeedRegisterAll(); CeedChk(ierr);
61813873f79Sjeremylt 
619d7b241e6Sjeremylt   for (size_t i=0; i<num_backends; i++) {
620d7b241e6Sjeremylt     size_t n;
621d7b241e6Sjeremylt     const char *prefix = backends[i].prefix;
622d7b241e6Sjeremylt     for (n = 0; prefix[n] && prefix[n] == resource[n]; n++) {}
623d7b241e6Sjeremylt     priority = backends[i].priority;
624d7b241e6Sjeremylt     if (n > matchlen || (n == matchlen && matchpriority > priority)) {
625d7b241e6Sjeremylt       matchlen = n;
626d7b241e6Sjeremylt       matchpriority = priority;
627d7b241e6Sjeremylt       matchidx = i;
628d7b241e6Sjeremylt     }
629d7b241e6Sjeremylt   }
6308f7c06a6Sjeremylt   if (matchlen <= 1)
63113873f79Sjeremylt     // LCOV_EXCL_START
6328079b1e4SJed Brown     return CeedError(NULL, 1, "No suitable backend: %s", resource);
63313873f79Sjeremylt   // LCOV_EXCL_STOP
634fe2413ffSjeremylt 
635fe2413ffSjeremylt   // Setup Ceed
636d7b241e6Sjeremylt   ierr = CeedCalloc(1, ceed); CeedChk(ierr);
637bc81ce41Sjeremylt   const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
6381d102b48SJeremy L Thompson   if (!ceed_error_handler)
6391d102b48SJeremy L Thompson     ceed_error_handler = "abort";
640bc81ce41Sjeremylt   if (!strcmp(ceed_error_handler, "exit"))
64156e866f4SJed Brown     (*ceed)->Error = CeedErrorExit;
642477729cfSJeremy L Thompson   else if (!strcmp(ceed_error_handler, "store"))
643477729cfSJeremy L Thompson     (*ceed)->Error = CeedErrorStore;
64456e866f4SJed Brown   else
645d7b241e6Sjeremylt     (*ceed)->Error = CeedErrorAbort;
646477729cfSJeremy L Thompson   memcpy((*ceed)->errmsg, "No error message stored", 24);
647d7b241e6Sjeremylt   (*ceed)->refcount = 1;
648d7b241e6Sjeremylt   (*ceed)->data = NULL;
649fe2413ffSjeremylt 
650fe2413ffSjeremylt   // Set lookup table
6516e79d475Sjeremylt   foffset foffsets[] = {
6526e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Error),
6536e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, GetPreferredMemType),
6546e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, Destroy),
655f8902d9eSjeremylt     CEED_FTABLE_ENTRY(Ceed, VectorCreate),
6566e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreate),
6576e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, ElemRestrictionCreateBlocked),
6586e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateTensorH1),
6596e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, BasisCreateH1),
6606e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, TensorContractCreate),
6616e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, QFunctionCreate),
662777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(Ceed, QFunctionContextCreate),
6636e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, OperatorCreate),
6646e79d475Sjeremylt     CEED_FTABLE_ENTRY(Ceed, CompositeOperatorCreate),
6656e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetArray),
6666a6c615bSJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, TakeArray),
6676e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, SetValue),
6686e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArray),
6696e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, GetArrayRead),
6706e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArray),
6716e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, RestoreArrayRead),
672547d9b97Sjeremylt     CEED_FTABLE_ENTRY(CeedVector, Norm),
673d99fa3c5SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedVector, Reciprocal),
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),
6848c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetCUDAUserFunction),
6858c84ac63Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, SetHIPUserFunction),
6866e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedQFunction, Destroy),
687777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, SetData),
688777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, GetData),
689777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, RestoreData),
690777ff853SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedQFunctionContext, Destroy),
69180ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleQFunction),
69280ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleDiagonal),
6939e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddDiagonal),
69480ac2e43SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssemblePointBlockDiagonal),
6959e9210b8SJeremy L Thompson     CEED_FTABLE_ENTRY(CeedOperator, LinearAssembleAddPointBlockDiagonal),
696713f43c3Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, CreateFDMElementInverse),
6976e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Apply),
698250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyComposite),
699cae8b89aSjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAdd),
700250756a7Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyAddComposite),
7016e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, ApplyJacobian),
7026e79d475Sjeremylt     CEED_FTABLE_ENTRY(CeedOperator, Destroy),
7036e79d475Sjeremylt     {NULL, 0} // End of lookup table - used in SetBackendFunction loop
7041dfeef1dSjeremylt   };
705fe2413ffSjeremylt 
7066e79d475Sjeremylt   ierr = CeedCalloc(sizeof(foffsets), &(*ceed)->foffsets); CeedChk(ierr);
7076e79d475Sjeremylt   memcpy((*ceed)->foffsets, foffsets, sizeof(foffsets));
708fe2413ffSjeremylt 
7095107b09fSJeremy L Thompson   // Set fallback for advanced CeedOperator functions
7105107b09fSJeremy L Thompson   const char fallbackresource[] = "/cpu/self/ref/serial";
7115107b09fSJeremy L Thompson   ierr = CeedSetOperatorFallbackResource(*ceed, fallbackresource);
7125107b09fSJeremy L Thompson   CeedChk(ierr);
7135107b09fSJeremy L Thompson 
71460f9e2d6SJeremy L Thompson   // Record env variables CEED_DEBUG or DBG
71560f9e2d6SJeremy L Thompson   (*ceed)->debug = !!getenv("CEED_DEBUG") || !!getenv("DBG");
71660f9e2d6SJeremy L Thompson 
717fe2413ffSjeremylt   // Backend specific setup
718d7b241e6Sjeremylt   ierr = backends[matchidx].init(resource, *ceed); CeedChk(ierr);
719fe2413ffSjeremylt 
720e07206deSjeremylt   // Copy resource prefix, if backend setup sucessful
721e07206deSjeremylt   size_t len = strlen(backends[matchidx].prefix);
722e07206deSjeremylt   char *tmp;
723e07206deSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
724e07206deSjeremylt   memcpy(tmp, backends[matchidx].prefix, len+1);
725e07206deSjeremylt   (*ceed)->resource = tmp;
726e07206deSjeremylt 
727d7b241e6Sjeremylt   return 0;
728d7b241e6Sjeremylt }
729d7b241e6Sjeremylt 
730d7b241e6Sjeremylt /**
7317a982d89SJeremy L. Thompson   @brief Get the full resource name for a Ceed context
7322f86a920SJeremy L Thompson 
7337a982d89SJeremy L. Thompson   @param ceed            Ceed context to get resource name of
7347a982d89SJeremy L. Thompson   @param[out] resource   Variable to store resource name
7352f86a920SJeremy L Thompson 
7362f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7372f86a920SJeremy L Thompson 
7387a982d89SJeremy L. Thompson   @ref User
7395107b09fSJeremy L Thompson **/
7405107b09fSJeremy L Thompson 
7417a982d89SJeremy L. Thompson int CeedGetResource(Ceed ceed, const char **resource) {
7427a982d89SJeremy L. Thompson   *resource = (const char *)ceed->resource;
7435107b09fSJeremy L Thompson   return 0;
7445107b09fSJeremy L Thompson }
7455107b09fSJeremy L Thompson 
7465107b09fSJeremy L Thompson /**
747d79b80ecSjeremylt   @brief Return Ceed context preferred memory type
748c907536fSjeremylt 
749d79b80ecSjeremylt   @param ceed      Ceed context to get preferred memory type of
750288c0443SJeremy L Thompson   @param[out] type Address to save preferred memory type to
751c907536fSjeremylt 
752c907536fSjeremylt   @return An error code: 0 - success, otherwise - failure
753c907536fSjeremylt 
7547a982d89SJeremy L. Thompson   @ref User
755c907536fSjeremylt **/
756c907536fSjeremylt int CeedGetPreferredMemType(Ceed ceed, CeedMemType *type) {
757c907536fSjeremylt   int ierr;
758c263cd57Sjeremylt 
759c907536fSjeremylt   if (ceed->GetPreferredMemType) {
760c907536fSjeremylt     ierr = ceed->GetPreferredMemType(type); CeedChk(ierr);
761c907536fSjeremylt   } else {
762c263cd57Sjeremylt     Ceed delegate;
763c263cd57Sjeremylt     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
764c263cd57Sjeremylt 
765c263cd57Sjeremylt     if (delegate) {
766c263cd57Sjeremylt       ierr = CeedGetPreferredMemType(delegate, type); CeedChk(ierr);
767c263cd57Sjeremylt     } else {
768c907536fSjeremylt       *type = CEED_MEM_HOST;
769c907536fSjeremylt     }
770c263cd57Sjeremylt   }
771c907536fSjeremylt 
772c907536fSjeremylt   return 0;
773c907536fSjeremylt }
774c907536fSjeremylt 
775c907536fSjeremylt /**
7769525855cSJeremy L Thompson   @brief Get deterministic status of Ceed
7779525855cSJeremy L Thompson 
7789525855cSJeremy L Thompson   @param[in] ceed              Ceed
7799525855cSJeremy L Thompson   @param[out] isDeterministic  Variable to store deterministic status
7809525855cSJeremy L Thompson 
7819525855cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7829525855cSJeremy L Thompson 
7839525855cSJeremy L Thompson   @ref User
7849525855cSJeremy L Thompson **/
7859525855cSJeremy L Thompson int CeedIsDeterministic(Ceed ceed, bool *isDeterministic) {
7869525855cSJeremy L Thompson   *isDeterministic = ceed->isDeterministic;
7879525855cSJeremy L Thompson   return 0;
7889525855cSJeremy L Thompson }
7899525855cSJeremy L Thompson 
7909525855cSJeremy L Thompson /**
7910a0da059Sjeremylt   @brief View a Ceed
7920a0da059Sjeremylt 
7930a0da059Sjeremylt   @param[in] ceed          Ceed to view
7940a0da059Sjeremylt   @param[in] stream        Filestream to write to
7950a0da059Sjeremylt 
7960a0da059Sjeremylt   @return An error code: 0 - success, otherwise - failure
7970a0da059Sjeremylt 
7980a0da059Sjeremylt   @ref User
7990a0da059Sjeremylt **/
8000a0da059Sjeremylt int CeedView(Ceed ceed, FILE *stream) {
8010a0da059Sjeremylt   int ierr;
8020a0da059Sjeremylt   CeedMemType memtype;
8030a0da059Sjeremylt 
8040a0da059Sjeremylt   ierr = CeedGetPreferredMemType(ceed, &memtype); CeedChk(ierr);
8050a0da059Sjeremylt 
8060a0da059Sjeremylt   fprintf(stream, "Ceed\n"
8070a0da059Sjeremylt           "  Ceed Resource: %s\n"
8080a0da059Sjeremylt           "  Preferred MemType: %s\n",
8090a0da059Sjeremylt           ceed->resource, CeedMemTypes[memtype]);
8100a0da059Sjeremylt 
8110a0da059Sjeremylt   return 0;
8120a0da059Sjeremylt }
8130a0da059Sjeremylt 
8140a0da059Sjeremylt /**
815b11c1e72Sjeremylt   @brief Destroy a Ceed context
816d7b241e6Sjeremylt 
817d7b241e6Sjeremylt   @param ceed Address of Ceed context to destroy
818b11c1e72Sjeremylt 
819b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
820dfdf5a53Sjeremylt 
8217a982d89SJeremy L. Thompson   @ref User
822b11c1e72Sjeremylt **/
823d7b241e6Sjeremylt int CeedDestroy(Ceed *ceed) {
824d7b241e6Sjeremylt   int ierr;
825752c3701SJeremy L Thompson   if (!*ceed || --(*ceed)->refcount > 0) return 0;
8265fe0d4faSjeremylt   if ((*ceed)->delegate) {
8275fe0d4faSjeremylt     ierr = CeedDestroy(&(*ceed)->delegate); CeedChk(ierr);
8285fe0d4faSjeremylt   }
8290ace9bf2Sjeremylt 
830aefd8378Sjeremylt   if ((*ceed)->objdelegatecount > 0) {
831aefd8378Sjeremylt     for (int i=0; i<(*ceed)->objdelegatecount; i++) {
832aefd8378Sjeremylt       ierr = CeedDestroy(&((*ceed)->objdelegates[i].delegate)); CeedChk(ierr);
833aefd8378Sjeremylt       ierr = CeedFree(&(*ceed)->objdelegates[i].objname); CeedChk(ierr);
834aefd8378Sjeremylt     }
835aefd8378Sjeremylt     ierr = CeedFree(&(*ceed)->objdelegates); CeedChk(ierr);
836aefd8378Sjeremylt   }
8370ace9bf2Sjeremylt 
838d7b241e6Sjeremylt   if ((*ceed)->Destroy) {
839d7b241e6Sjeremylt     ierr = (*ceed)->Destroy(*ceed); CeedChk(ierr);
840d7b241e6Sjeremylt   }
8410ace9bf2Sjeremylt 
8426e79d475Sjeremylt   ierr = CeedFree(&(*ceed)->foffsets); CeedChk(ierr);
843e07206deSjeremylt   ierr = CeedFree(&(*ceed)->resource); CeedChk(ierr);
8445107b09fSJeremy L Thompson   ierr = CeedDestroy(&(*ceed)->opfallbackceed); CeedChk(ierr);
8455107b09fSJeremy L Thompson   ierr = CeedFree(&(*ceed)->opfallbackresource); CeedChk(ierr);
846d7b241e6Sjeremylt   ierr = CeedFree(ceed); CeedChk(ierr);
847d7b241e6Sjeremylt   return 0;
848d7b241e6Sjeremylt }
849d7b241e6Sjeremylt 
850f9982c62SWill Pazner // LCOV_EXCL_START
851f9982c62SWill Pazner const char *CeedErrorFormat(Ceed ceed, const char *format, va_list *args) {
852f9982c62SWill Pazner   vsnprintf(ceed->errmsg, CEED_MAX_RESOURCE_LEN, format, *args);
853f9982c62SWill Pazner   return ceed->errmsg;
854f9982c62SWill Pazner }
855f9982c62SWill Pazner // LCOV_EXCL_STOP
856f9982c62SWill Pazner 
8577a982d89SJeremy L. Thompson /**
8587a982d89SJeremy L. Thompson   @brief Error handling implementation; use \ref CeedError instead.
8597a982d89SJeremy L. Thompson 
8607a982d89SJeremy L. Thompson   @ref Developer
8617a982d89SJeremy L. Thompson **/
8627a982d89SJeremy L. Thompson int CeedErrorImpl(Ceed ceed, const char *filename, int lineno, const char *func,
8637a982d89SJeremy L. Thompson                   int ecode, const char *format, ...) {
8647a982d89SJeremy L. Thompson   va_list args;
8657a982d89SJeremy L. Thompson   int retval;
8667a982d89SJeremy L. Thompson   va_start(args, format);
8677a982d89SJeremy L. Thompson   if (ceed) {
868f9982c62SWill Pazner     retval = ceed->Error(ceed, filename, lineno, func, ecode, format, &args);
8697a982d89SJeremy L. Thompson   } else {
870b0d62198Sjeremylt     // LCOV_EXCL_START
871477729cfSJeremy L Thompson     const char *ceed_error_handler = getenv("CEED_ERROR_HANDLER");
872477729cfSJeremy L Thompson     if (!ceed_error_handler)
873477729cfSJeremy L Thompson       ceed_error_handler = "abort";
874477729cfSJeremy L Thompson     if (!strcmp(ceed_error_handler, "return"))
875f9982c62SWill Pazner       retval = CeedErrorReturn(ceed, filename, lineno, func, ecode, format, &args);
876477729cfSJeremy L Thompson     else
877477729cfSJeremy L Thompson       // This function will not return
878f9982c62SWill Pazner       retval = CeedErrorAbort(ceed, filename, lineno, func, ecode, format, &args);
8797a982d89SJeremy L. Thompson   }
8807a982d89SJeremy L. Thompson   va_end(args);
8817a982d89SJeremy L. Thompson   return retval;
882b0d62198Sjeremylt   // LCOV_EXCL_STOP
8837a982d89SJeremy L. Thompson }
8847a982d89SJeremy L. Thompson 
885477729cfSJeremy L Thompson /**
886477729cfSJeremy L Thompson   @brief Error handler that returns without printing anything.
887477729cfSJeremy L Thompson 
888477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
889477729cfSJeremy L Thompson 
890477729cfSJeremy L Thompson   @ref Developer
891477729cfSJeremy L Thompson **/
892477729cfSJeremy L Thompson // LCOV_EXCL_START
893477729cfSJeremy L Thompson int CeedErrorReturn(Ceed ceed, const char *filename, int lineno,
894477729cfSJeremy L Thompson                     const char *func, int ecode, const char *format,
895f9982c62SWill Pazner                     va_list *args) {
896477729cfSJeremy L Thompson   return ecode;
897477729cfSJeremy L Thompson }
898477729cfSJeremy L Thompson // LCOV_EXCL_STOP
899477729cfSJeremy L Thompson 
900477729cfSJeremy L Thompson /**
901477729cfSJeremy L Thompson   @brief Error handler that stores the error message for future use and returns
902477729cfSJeremy L Thompson            the error.
903477729cfSJeremy L Thompson 
904477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
905477729cfSJeremy L Thompson 
906477729cfSJeremy L Thompson   @ref Developer
907477729cfSJeremy L Thompson **/
908477729cfSJeremy L Thompson // LCOV_EXCL_START
909477729cfSJeremy L Thompson int CeedErrorStore(Ceed ceed, const char *filename, int lineno,
910477729cfSJeremy L Thompson                    const char *func, int ecode, const char *format,
911f9982c62SWill Pazner                    va_list *args) {
912477729cfSJeremy L Thompson   if (ceed->parent)
913187168c7SJeremy L Thompson     return CeedErrorStore(ceed->parent, filename, lineno, func, ecode, format,
914187168c7SJeremy L Thompson                           args);
915fc164cf7SWill Pazner   if (ceed->opfallbackparent)
916fc164cf7SWill Pazner     return CeedErrorStore(ceed->opfallbackparent, filename, lineno, func, ecode,
917fc164cf7SWill Pazner                           format, args);
918477729cfSJeremy L Thompson 
919477729cfSJeremy L Thompson   // Build message
920477729cfSJeremy L Thompson   CeedInt len;
921477729cfSJeremy L Thompson   len = snprintf(ceed->errmsg, CEED_MAX_RESOURCE_LEN, "%s:%d in %s(): ",
922477729cfSJeremy L Thompson                  filename, lineno, func);
923f9982c62SWill Pazner   vsnprintf(ceed->errmsg + len, CEED_MAX_RESOURCE_LEN - len, format, *args);
924477729cfSJeremy L Thompson   return ecode;
925477729cfSJeremy L Thompson }
926477729cfSJeremy L Thompson // LCOV_EXCL_STOP
927477729cfSJeremy L Thompson 
928477729cfSJeremy L Thompson /**
929477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and aborts
930477729cfSJeremy L Thompson 
931477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
932477729cfSJeremy L Thompson 
933477729cfSJeremy L Thompson   @ref Developer
934477729cfSJeremy L Thompson **/
935477729cfSJeremy L Thompson // LCOV_EXCL_START
936477729cfSJeremy L Thompson int CeedErrorAbort(Ceed ceed, const char *filename, int lineno,
937477729cfSJeremy L Thompson                    const char *func, int ecode, const char *format,
938f9982c62SWill Pazner                    va_list *args) {
939477729cfSJeremy L Thompson   fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func);
940f9982c62SWill Pazner   vfprintf(stderr, format, *args);
941477729cfSJeremy L Thompson   fprintf(stderr, "\n");
942477729cfSJeremy L Thompson   abort();
943477729cfSJeremy L Thompson   return ecode;
944477729cfSJeremy L Thompson }
945477729cfSJeremy L Thompson // LCOV_EXCL_STOP
946477729cfSJeremy L Thompson 
947477729cfSJeremy L Thompson /**
948477729cfSJeremy L Thompson   @brief Error handler that prints to stderr and exits
949477729cfSJeremy L Thompson 
950477729cfSJeremy L Thompson   Pass this to CeedSetErrorHandler() to obtain this error handling behavior.
951477729cfSJeremy L Thompson 
952477729cfSJeremy L Thompson   In contrast to CeedErrorAbort(), this exits without a signal, so atexit()
953477729cfSJeremy L Thompson   handlers (e.g., as used by gcov) are run.
954477729cfSJeremy L Thompson 
955477729cfSJeremy L Thompson   @ref Developer
956477729cfSJeremy L Thompson **/
957477729cfSJeremy L Thompson int CeedErrorExit(Ceed ceed, const char *filename, int lineno, const char *func,
958f9982c62SWill Pazner                   int ecode, const char *format, va_list *args) {
959477729cfSJeremy L Thompson   fprintf(stderr, "%s:%d in %s(): ", filename, lineno, func);
960f9982c62SWill Pazner   vfprintf(stderr, format, *args);
961477729cfSJeremy L Thompson   fprintf(stderr, "\n");
962477729cfSJeremy L Thompson   exit(ecode);
963477729cfSJeremy L Thompson   return ecode;
964477729cfSJeremy L Thompson }
965477729cfSJeremy L Thompson 
966477729cfSJeremy L Thompson /**
967477729cfSJeremy L Thompson   @brief Set error handler
968477729cfSJeremy L Thompson 
969477729cfSJeremy L Thompson   A default error handler is set in CeedInit().  Use this function to change
970477729cfSJeremy L Thompson   the error handler to CeedErrorReturn(), CeedErrorAbort(), or a user-defined
971477729cfSJeremy L Thompson   error handler.
972477729cfSJeremy L Thompson 
973477729cfSJeremy L Thompson   @ref Developer
974477729cfSJeremy L Thompson **/
975477729cfSJeremy L Thompson int CeedSetErrorHandler(Ceed ceed,
976477729cfSJeremy L Thompson                         int (*eh)(Ceed, const char *, int, const char *,
977f9982c62SWill Pazner                                   int, const char *, va_list *)) {
978477729cfSJeremy L Thompson   ceed->Error = eh;
979477729cfSJeremy L Thompson   if (ceed->delegate) CeedSetErrorHandler(ceed->delegate, eh);
980477729cfSJeremy L Thompson   for (int i=0; i<ceed->objdelegatecount; i++)
981477729cfSJeremy L Thompson     CeedSetErrorHandler(ceed->objdelegates[i].delegate, eh);
982477729cfSJeremy L Thompson   return 0;
983477729cfSJeremy L Thompson }
984477729cfSJeremy L Thompson 
985477729cfSJeremy L Thompson /**
986477729cfSJeremy L Thompson   @brief Get error message
987477729cfSJeremy L Thompson 
988477729cfSJeremy L Thompson   The error message is only stored when using the error handler
989477729cfSJeremy L Thompson     CeedErrorStore()
990477729cfSJeremy L Thompson 
991477729cfSJeremy L Thompson   @param[in] ceed     Ceed contex to retrieve error message
992477729cfSJeremy L Thompson   @param[out] errmsg  Char pointer to hold error message
993477729cfSJeremy L Thompson 
994477729cfSJeremy L Thompson   @ref Developer
995477729cfSJeremy L Thompson **/
996477729cfSJeremy L Thompson int CeedGetErrorMessage(Ceed ceed, const char **errmsg) {
997477729cfSJeremy L Thompson   *errmsg = ceed->errmsg;
998477729cfSJeremy L Thompson   return 0;
999477729cfSJeremy L Thompson }
1000477729cfSJeremy L Thompson 
1001477729cfSJeremy L Thompson /**
1002477729cfSJeremy L Thompson   @brief Restore error message
1003477729cfSJeremy L Thompson 
1004477729cfSJeremy L Thompson   The error message is only stored when using the error handler
1005477729cfSJeremy L Thompson     CeedErrorStore()
1006477729cfSJeremy L Thompson 
1007477729cfSJeremy L Thompson   @param[in] ceed     Ceed contex to restore error message
1008477729cfSJeremy L Thompson   @param[out] errmsg  Char pointer that holds error message
1009477729cfSJeremy L Thompson 
1010477729cfSJeremy L Thompson   @ref Developer
1011477729cfSJeremy L Thompson **/
1012477729cfSJeremy L Thompson int CeedResetErrorMessage(Ceed ceed, const char **errmsg) {
1013477729cfSJeremy L Thompson   *errmsg = NULL;
1014477729cfSJeremy L Thompson   memcpy(ceed->errmsg, "No error message stored", 24);
1015477729cfSJeremy L Thompson   return 0;
1016477729cfSJeremy L Thompson }
1017477729cfSJeremy L Thompson 
1018*1070991dSJed Brown /**
1019*1070991dSJed Brown   @brief Get libCEED library version info
1020*1070991dSJed Brown 
1021*1070991dSJed Brown   libCEED version numbers have the form major.minor.patch. Non-release versions
1022*1070991dSJed Brown   may contain unstable interfaces.
1023*1070991dSJed Brown 
1024*1070991dSJed Brown   @param[out] major    Major version of the library
1025*1070991dSJed Brown   @param[out] minor    Minor version of the library
1026*1070991dSJed Brown   @param[out] patch    Patch (subminor) version of the library
1027*1070991dSJed Brown   @param[out] release  True for releases; false for development branches.
1028*1070991dSJed Brown 
1029*1070991dSJed Brown   The caller may pass NULL for any arguments that are not needed.
1030*1070991dSJed Brown 
1031*1070991dSJed Brown   @sa CEED_VERSION_GE()
1032*1070991dSJed Brown 
1033*1070991dSJed Brown   @ref Developer
1034*1070991dSJed Brown */
1035*1070991dSJed Brown int CeedGetVersion(int *major, int *minor, int *patch, bool *release) {
1036*1070991dSJed Brown   if (major) *major = CEED_VERSION_MAJOR;
1037*1070991dSJed Brown   if (minor) *minor = CEED_VERSION_MINOR;
1038*1070991dSJed Brown   if (patch) *patch = CEED_VERSION_PATCH;
1039*1070991dSJed Brown   if (release) *release = CEED_VERSION_RELEASE;
1040*1070991dSJed Brown   return 0;
1041*1070991dSJed Brown }
1042*1070991dSJed Brown 
1043d7b241e6Sjeremylt /// @}
1044