xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-elemrestriction.c (revision 6b10039809512a52c0b4d656274c56ab7eb97a4b)
1 // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed-impl.h>
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 /// @file
16 /// Implementation of CeedElemRestriction interfaces
17 
18 /// ----------------------------------------------------------------------------
19 /// CeedElemRestriction Library Internal Functions
20 /// ----------------------------------------------------------------------------
21 /// @addtogroup CeedElemRestrictionDeveloper
22 /// @{
23 
24 /**
25   @brief Permute and pad offsets for a blocked `CeedElemRestriction`
26 
27   @param[in]  offsets       Array of shape `[num_elem, elem_size]`
28   @param[out] block_offsets Array of permuted and padded array values of shape `[num_block, elem_size, block_size]`
29   @param[in]  num_block     Number of blocks
30   @param[in]  num_elem      Number of elements
31   @param[in]  block_size    Number of elements in a block
32   @param[in]  elem_size     Size of each element
33 
34   @return An error code: 0 - success, otherwise - failure
35 
36   @ref Utility
37 **/
38 int CeedPermutePadOffsets(const CeedInt *offsets, CeedInt *block_offsets, CeedInt num_block, CeedInt num_elem, CeedInt block_size,
39                           CeedInt elem_size) {
40   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
41     for (CeedInt j = 0; j < block_size; j++) {
42       for (CeedInt k = 0; k < elem_size; k++) {
43         block_offsets[e * elem_size + k * block_size + j] = offsets[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
44       }
45     }
46   }
47   return CEED_ERROR_SUCCESS;
48 }
49 
50 /**
51   @brief Permute and pad orientations for a blocked `CeedElemRestriction`
52 
53   @param[in]  orients       Array of shape `[num_elem, elem_size]`
54   @param[out] block_orients Array of permuted and padded array values of shape `[num_block, elem_size, block_size]`
55   @param[in]  num_block     Number of blocks
56   @param[in]  num_elem      Number of elements
57   @param[in]  block_size    Number of elements in a block
58   @param[in]  elem_size     Size of each element
59 
60   @return An error code: 0 - success, otherwise - failure
61 
62   @ref Utility
63 **/
64 int CeedPermutePadOrients(const bool *orients, bool *block_orients, CeedInt num_block, CeedInt num_elem, CeedInt block_size, CeedInt elem_size) {
65   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
66     for (CeedInt j = 0; j < block_size; j++) {
67       for (CeedInt k = 0; k < elem_size; k++) {
68         block_orients[e * elem_size + k * block_size + j] = orients[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
69       }
70     }
71   }
72   return CEED_ERROR_SUCCESS;
73 }
74 
75 /**
76   @brief Permute and pad curl-conforming orientations for a blocked `CeedElemRestriction`
77 
78   @param[in]  curl_orients       Array of shape `[num_elem, elem_size]`
79   @param[out] block_curl_orients Array of permuted and padded array values of shape `[num_block, elem_size, block_size]`
80   @param[in]  num_block          Number of blocks
81   @param[in]  num_elem           Number of elements
82   @param[in]  block_size         Number of elements in a block
83   @param[in]  elem_size          Size of each element
84 
85   @return An error code: 0 - success, otherwise - failure
86 
87   @ref Utility
88 **/
89 int CeedPermutePadCurlOrients(const CeedInt8 *curl_orients, CeedInt8 *block_curl_orients, CeedInt num_block, CeedInt num_elem, CeedInt block_size,
90                               CeedInt elem_size) {
91   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
92     for (CeedInt j = 0; j < block_size; j++) {
93       for (CeedInt k = 0; k < elem_size; k++) {
94         block_curl_orients[e * elem_size + k * block_size + j] = curl_orients[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
95       }
96     }
97   }
98   return CEED_ERROR_SUCCESS;
99 }
100 
101 /// @}
102 
103 /// ----------------------------------------------------------------------------
104 /// CeedElemRestriction Backend API
105 /// ----------------------------------------------------------------------------
106 /// @addtogroup CeedElemRestrictionBackend
107 /// @{
108 
109 /**
110   @brief Get the type of a `CeedElemRestriction`
111 
112   @param[in]  rstr      `CeedElemRestriction`
113   @param[out] rstr_type Variable to store restriction type
114 
115   @return An error code: 0 - success, otherwise - failure
116 
117   @ref Backend
118 **/
119 int CeedElemRestrictionGetType(CeedElemRestriction rstr, CeedRestrictionType *rstr_type) {
120   *rstr_type = rstr->rstr_type;
121   return CEED_ERROR_SUCCESS;
122 }
123 
124 /**
125   @brief Get the strided status of a `CeedElemRestriction`
126 
127   @param[in]  rstr       `CeedElemRestriction`
128   @param[out] is_strided Variable to store strided status
129 
130   @return An error code: 0 - success, otherwise - failure
131 
132   @ref Backend
133 **/
134 int CeedElemRestrictionIsStrided(CeedElemRestriction rstr, bool *is_strided) {
135   *is_strided = (rstr->rstr_type == CEED_RESTRICTION_STRIDED);
136   return CEED_ERROR_SUCCESS;
137 }
138 
139 /**
140   @brief Get the points status of a `CeedElemRestriction`
141 
142   @param[in]  rstr      `CeedElemRestriction`
143   @param[out] is_points Variable to store points status
144 
145   @return An error code: 0 - success, otherwise - failure
146 
147   @ref Backend
148 **/
149 int CeedElemRestrictionIsAtPoints(CeedElemRestriction rstr, bool *is_points) {
150   *is_points = (rstr->rstr_type == CEED_RESTRICTION_POINTS);
151   return CEED_ERROR_SUCCESS;
152 }
153 
154 /**
155   @brief Check if two `CeedElemRestriction` created with @ref CeedElemRestrictionCreateAtPoints() and use the same points per element
156 
157   @param[in]  rstr_a         First `CeedElemRestriction`
158   @param[in]  rstr_b         Second `CeedElemRestriction`
159   @param[out] are_compatible Variable to store compatibility status
160 
161   @return An error code: 0 - success, otherwise - failure
162 
163   @ref Backend
164 **/
165 int CeedElemRestrictionAtPointsAreCompatible(CeedElemRestriction rstr_a, CeedElemRestriction rstr_b, bool *are_compatible) {
166   CeedInt num_elem_a, num_elem_b, num_points_a, num_points_b;
167   Ceed    ceed;
168 
169   CeedCall(CeedElemRestrictionGetCeed(rstr_a, &ceed));
170 
171   // Cannot compare non-points restrictions
172   CeedCheck(rstr_a->rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_UNSUPPORTED, "First CeedElemRestriction must be AtPoints");
173   CeedCheck(rstr_b->rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_UNSUPPORTED, "Second CeedElemRestriction must be AtPoints");
174 
175   CeedCall(CeedElemRestrictionGetNumElements(rstr_a, &num_elem_a));
176   CeedCall(CeedElemRestrictionGetNumElements(rstr_b, &num_elem_b));
177   CeedCall(CeedElemRestrictionGetNumPoints(rstr_a, &num_points_a));
178   CeedCall(CeedElemRestrictionGetNumPoints(rstr_b, &num_points_b));
179 
180   // Check size and contents of offsets arrays
181   *are_compatible = true;
182   if (num_elem_a != num_elem_b) *are_compatible = false;
183   if (num_points_a != num_points_b) *are_compatible = false;
184   if (*are_compatible) {
185     const CeedInt *offsets_a, *offsets_b;
186 
187     CeedCall(CeedElemRestrictionGetOffsets(rstr_a, CEED_MEM_HOST, &offsets_a));
188     CeedCall(CeedElemRestrictionGetOffsets(rstr_b, CEED_MEM_HOST, &offsets_b));
189     for (CeedInt i = 0; i < num_elem_a + 1 + num_points_a; i++) *are_compatible &= offsets_a[i] == offsets_b[i];
190     CeedCall(CeedElemRestrictionRestoreOffsets(rstr_a, &offsets_a));
191     CeedCall(CeedElemRestrictionRestoreOffsets(rstr_b, &offsets_b));
192   }
193   return CEED_ERROR_SUCCESS;
194 }
195 
196 /**
197   @brief Get the strides of a strided `CeedElemRestriction`
198 
199   @param[in]  rstr    `CeedElemRestriction`
200   @param[out] strides Variable to store strides array
201 
202   @return An error code: 0 - success, otherwise - failure
203 
204   @ref Backend
205 **/
206 int CeedElemRestrictionGetStrides(CeedElemRestriction rstr, CeedInt strides[3]) {
207   CeedCheck(rstr->strides, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no stride data");
208   for (CeedInt i = 0; i < 3; i++) strides[i] = rstr->strides[i];
209   return CEED_ERROR_SUCCESS;
210 }
211 
212 /**
213   @brief Get the backend stride status of a `CeedElemRestriction`
214 
215   @param[in]  rstr                 `CeedElemRestriction`
216   @param[out] has_backend_strides  Variable to store stride status
217 
218   @return An error code: 0 - success, otherwise - failure
219 
220   @ref Backend
221 **/
222 int CeedElemRestrictionHasBackendStrides(CeedElemRestriction rstr, bool *has_backend_strides) {
223   CeedCheck(rstr->strides, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no stride data");
224   *has_backend_strides = ((rstr->strides[0] == CEED_STRIDES_BACKEND[0]) && (rstr->strides[1] == CEED_STRIDES_BACKEND[1]) &&
225                           (rstr->strides[2] == CEED_STRIDES_BACKEND[2]));
226   return CEED_ERROR_SUCCESS;
227 }
228 
229 /**
230   @brief Get read-only access to a `CeedElemRestriction` offsets array by @ref CeedMemType
231 
232   @param[in]  rstr     `CeedElemRestriction` to retrieve offsets
233   @param[in]  mem_type Memory type on which to access the array.
234                          If the backend uses a different memory type, this will perform a copy (possibly cached).
235   @param[out] offsets  Array on memory type `mem_type`
236 
237   @return An error code: 0 - success, otherwise - failure
238 
239   @ref User
240 **/
241 int CeedElemRestrictionGetOffsets(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
242   if (rstr->rstr_base) {
243     CeedCall(CeedElemRestrictionGetOffsets(rstr->rstr_base, mem_type, offsets));
244   } else {
245     CeedCheck(rstr->GetOffsets, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
246               "Backend does not implement CeedElemRestrictionGetOffsets");
247     CeedCall(rstr->GetOffsets(rstr, mem_type, offsets));
248     rstr->num_readers++;
249   }
250   return CEED_ERROR_SUCCESS;
251 }
252 
253 /**
254   @brief Restore an offsets array obtained using @ref CeedElemRestrictionGetOffsets()
255 
256   @param[in] rstr    `CeedElemRestriction` to restore
257   @param[in] offsets Array of offset data
258 
259   @return An error code: 0 - success, otherwise - failure
260 
261   @ref User
262 **/
263 int CeedElemRestrictionRestoreOffsets(CeedElemRestriction rstr, const CeedInt **offsets) {
264   if (rstr->rstr_base) {
265     CeedCall(CeedElemRestrictionRestoreOffsets(rstr->rstr_base, offsets));
266   } else {
267     *offsets = NULL;
268     rstr->num_readers--;
269   }
270   return CEED_ERROR_SUCCESS;
271 }
272 
273 /**
274   @brief Get read-only access to a `CeedElemRestriction` orientations array by @ref CeedMemType
275 
276   @param[in]  rstr     `CeedElemRestriction` to retrieve orientations
277   @param[in]  mem_type Memory type on which to access the array.
278                          If the backend uses a different memory type, this will perform a copy (possibly cached).
279   @param[out] orients  Array on memory type `mem_type`
280 
281   @return An error code: 0 - success, otherwise - failure
282 
283   @ref User
284 **/
285 int CeedElemRestrictionGetOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
286   CeedCheck(rstr->GetOrientations, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
287             "Backend does not implement CeedElemRestrictionGetOrientations");
288   CeedCall(rstr->GetOrientations(rstr, mem_type, orients));
289   rstr->num_readers++;
290   return CEED_ERROR_SUCCESS;
291 }
292 
293 /**
294   @brief Restore an orientations array obtained using @ref CeedElemRestrictionGetOrientations()
295 
296   @param[in] rstr    `CeedElemRestriction` to restore
297   @param[in] orients Array of orientation data
298 
299   @return An error code: 0 - success, otherwise - failure
300 
301   @ref User
302 **/
303 int CeedElemRestrictionRestoreOrientations(CeedElemRestriction rstr, const bool **orients) {
304   *orients = NULL;
305   rstr->num_readers--;
306   return CEED_ERROR_SUCCESS;
307 }
308 
309 /**
310   @brief Get read-only access to a `CeedElemRestriction` curl-conforming orientations array by @ref CeedMemType
311 
312   @param[in]  rstr         `CeedElemRestriction` to retrieve curl-conforming orientations
313   @param[in]  mem_type     Memory type on which to access the array.
314                              If the backend uses a different memory type, this will perform a copy (possibly cached).
315   @param[out] curl_orients Array on memory type `mem_type`
316 
317   @return An error code: 0 - success, otherwise - failure
318 
319   @ref User
320 **/
321 int CeedElemRestrictionGetCurlOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
322   CeedCheck(rstr->GetCurlOrientations, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
323             "Backend does not implement CeedElemRestrictionGetCurlOrientations");
324   CeedCall(rstr->GetCurlOrientations(rstr, mem_type, curl_orients));
325   rstr->num_readers++;
326   return CEED_ERROR_SUCCESS;
327 }
328 
329 /**
330   @brief Restore an orientations array obtained using @ref CeedElemRestrictionGetCurlOrientations()
331 
332   @param[in] rstr         `CeedElemRestriction` to restore
333   @param[in] curl_orients Array of orientation data
334 
335   @return An error code: 0 - success, otherwise - failure
336 
337   @ref User
338 **/
339 int CeedElemRestrictionRestoreCurlOrientations(CeedElemRestriction rstr, const CeedInt8 **curl_orients) {
340   *curl_orients = NULL;
341   rstr->num_readers--;
342   return CEED_ERROR_SUCCESS;
343 }
344 
345 /**
346 
347   @brief Get the L-vector layout of a strided `CeedElemRestriction`
348 
349   @param[in]  rstr    `CeedElemRestriction`
350   @param[out] layout  Variable to store layout array, stored as `[nodes, components, elements]`.
351                         The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
352 
353   @return An error code: 0 - success, otherwise - failure
354 
355   @ref Backend
356 **/
357 int CeedElemRestrictionGetLLayout(CeedElemRestriction rstr, CeedInt layout[3]) {
358   bool                has_backend_strides;
359   CeedRestrictionType rstr_type;
360   Ceed                ceed;
361 
362   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
363   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
364   CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, ceed, CEED_ERROR_MINOR, "Only strided CeedElemRestriction have strided L-vector layout");
365   CeedCall(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
366   if (has_backend_strides) {
367     CeedCheck(rstr->l_layout[0], ceed, CEED_ERROR_MINOR, "CeedElemRestriction has no L-vector layout data");
368     for (CeedInt i = 0; i < 3; i++) layout[i] = rstr->l_layout[i];
369   } else {
370     CeedCall(CeedElemRestrictionGetStrides(rstr, layout));
371   }
372   return CEED_ERROR_SUCCESS;
373 }
374 
375 /**
376 
377   @brief Set the L-vector layout of a strided `CeedElemRestriction`
378 
379   @param[in] rstr   `CeedElemRestriction`
380   @param[in] layout Variable to containing layout array, stored as `[nodes, components, elements]`.
381                       The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
382 
383   @return An error code: 0 - success, otherwise - failure
384 
385   @ref Backend
386 **/
387 int CeedElemRestrictionSetLLayout(CeedElemRestriction rstr, CeedInt layout[3]) {
388   CeedRestrictionType rstr_type;
389 
390   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
391   CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR,
392             "Only strided CeedElemRestriction have strided L-vector layout");
393   for (CeedInt i = 0; i < 3; i++) rstr->l_layout[i] = layout[i];
394   return CEED_ERROR_SUCCESS;
395 }
396 
397 /**
398 
399   @brief Get the E-vector layout of a `CeedElemRestriction`
400 
401   @param[in]  rstr    `CeedElemRestriction`
402   @param[out] layout  Variable to store layout array, stored as `[nodes, components, elements]`.
403                         The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
404 
405   @return An error code: 0 - success, otherwise - failure
406 
407   @ref Backend
408 **/
409 int CeedElemRestrictionGetELayout(CeedElemRestriction rstr, CeedInt layout[3]) {
410   CeedCheck(rstr->e_layout[0], CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no E-vector layout data");
411   for (CeedInt i = 0; i < 3; i++) layout[i] = rstr->e_layout[i];
412   return CEED_ERROR_SUCCESS;
413 }
414 
415 /**
416 
417   @brief Set the E-vector layout of a `CeedElemRestriction`
418 
419   @param[in] rstr   `CeedElemRestriction`
420   @param[in] layout Variable to containing layout array, stored as `[nodes, components, elements]`.
421                       The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
422 
423   @return An error code: 0 - success, otherwise - failure
424 
425   @ref Backend
426 **/
427 int CeedElemRestrictionSetELayout(CeedElemRestriction rstr, CeedInt layout[3]) {
428   for (CeedInt i = 0; i < 3; i++) rstr->e_layout[i] = layout[i];
429   return CEED_ERROR_SUCCESS;
430 }
431 
432 /**
433 
434   @brief Get the E-vector element offset of a `CeedElemRestriction` at points
435 
436   @param[in]  rstr        `CeedElemRestriction`
437   @param[in]  elem        Element number index into E-vector for
438   @param[out] elem_offset Offset for element `elem` in the E-vector.
439                             The data for point `i`, component `j`, element `elem` in the E-vector is given by `i*e_layout[0] + j*e_layout[1] + elem_offset`.
440 
441   @return An error code: 0 - success, otherwise - failure
442 
443   @ref Backend
444 **/
445 int CeedElemRestrictionGetAtPointsElementOffset(CeedElemRestriction rstr, CeedInt elem, CeedSize *elem_offset) {
446   CeedInt             num_comp;
447   CeedRestrictionType rstr_type;
448 
449   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
450   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
451             "Can only compute offset for a points CeedElemRestriction");
452 
453   // Backend method
454   if (rstr->GetAtPointsElementOffset) {
455     CeedCall(rstr->GetAtPointsElementOffset(rstr, elem, elem_offset));
456     return CEED_ERROR_SUCCESS;
457   }
458 
459   // Default layout (CPU)
460   *elem_offset = 0;
461   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
462   for (CeedInt i = 0; i < elem; i++) {
463     CeedInt num_points;
464 
465     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, i, &num_points));
466     *elem_offset += num_points * num_comp;
467   }
468   return CEED_ERROR_SUCCESS;
469 }
470 
471 /**
472 
473   @brief Set the E-vector size of a `CeedElemRestriction` at points
474 
475   @param[in,out]  rstr   `CeedElemRestriction`
476   @param[in]      e_size New E-vector size; must be longer than the current E-vector size
477 
478   @return An error code: 0 - success, otherwise - failure
479 
480   @ref Backend
481 **/
482 int CeedElemRestrictionSetAtPointsEVectorSize(CeedElemRestriction rstr, CeedSize e_size) {
483   CeedRestrictionType rstr_type;
484   Ceed                ceed;
485 
486   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
487   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
488   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_INCOMPATIBLE, "Can only compute offset for a points CeedElemRestriction");
489   CeedCheck(e_size >= rstr->e_size, ceed, CEED_ERROR_INCOMPATIBLE,
490             "Can only increase the size of the E-vector for the CeedElemRestriction."
491             " Current size: %" CeedSize_FMT " New size: %" CeedSize_FMT,
492             rstr->e_size, e_size);
493   rstr->e_size = e_size;
494   return CEED_ERROR_SUCCESS;
495 }
496 
497 /**
498   @brief Get the backend data of a `CeedElemRestriction`
499 
500   @param[in]  rstr `CeedElemRestriction`
501   @param[out] data Variable to store data
502 
503   @return An error code: 0 - success, otherwise - failure
504 
505   @ref Backend
506 **/
507 int CeedElemRestrictionGetData(CeedElemRestriction rstr, void *data) {
508   *(void **)data = rstr->data;
509   return CEED_ERROR_SUCCESS;
510 }
511 
512 /**
513   @brief Set the backend data of a `CeedElemRestriction`
514 
515   @param[in,out] rstr `CeedElemRestriction`
516   @param[in]     data Data to set
517 
518   @return An error code: 0 - success, otherwise - failure
519 
520   @ref Backend
521 **/
522 int CeedElemRestrictionSetData(CeedElemRestriction rstr, void *data) {
523   rstr->data = data;
524   return CEED_ERROR_SUCCESS;
525 }
526 
527 /**
528   @brief Increment the reference counter for a `CeedElemRestriction`
529 
530   @param[in,out] rstr `CeedElemRestriction` to increment the reference counter
531 
532   @return An error code: 0 - success, otherwise - failure
533 
534   @ref Backend
535 **/
536 int CeedElemRestrictionReference(CeedElemRestriction rstr) {
537   rstr->ref_count++;
538   return CEED_ERROR_SUCCESS;
539 }
540 
541 /**
542   @brief Estimate number of FLOPs required to apply `CeedElemRestriction` in `t_mode`
543 
544   @param[in]  rstr   `CeedElemRestriction` to estimate FLOPs for
545   @param[in]  t_mode Apply restriction or transpose
546   @param[out] flops  Address of variable to hold FLOPs estimate
547 
548   @ref Backend
549 **/
550 int CeedElemRestrictionGetFlopsEstimate(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedSize *flops) {
551   CeedSize            e_size, scale = 0;
552   CeedRestrictionType rstr_type;
553 
554   CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &e_size));
555   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
556   if (t_mode == CEED_TRANSPOSE) {
557     switch (rstr_type) {
558       case CEED_RESTRICTION_POINTS:
559         scale = 0;
560         break;
561       case CEED_RESTRICTION_STRIDED:
562       case CEED_RESTRICTION_STANDARD:
563         scale = 1;
564         break;
565       case CEED_RESTRICTION_ORIENTED:
566         scale = 2;
567         break;
568       case CEED_RESTRICTION_CURL_ORIENTED:
569         scale = 6;
570         break;
571     }
572   } else {
573     switch (rstr_type) {
574       case CEED_RESTRICTION_STRIDED:
575       case CEED_RESTRICTION_STANDARD:
576       case CEED_RESTRICTION_POINTS:
577         scale = 0;
578         break;
579       case CEED_RESTRICTION_ORIENTED:
580         scale = 1;
581         break;
582       case CEED_RESTRICTION_CURL_ORIENTED:
583         scale = 5;
584         break;
585     }
586   }
587   *flops = e_size * scale;
588   return CEED_ERROR_SUCCESS;
589 }
590 
591 /// @}
592 
593 /// @cond DOXYGEN_SKIP
594 static struct CeedElemRestriction_private ceed_elemrestriction_none;
595 /// @endcond
596 
597 /// ----------------------------------------------------------------------------
598 /// CeedElemRestriction Public API
599 /// ----------------------------------------------------------------------------
600 /// @addtogroup CeedElemRestrictionUser
601 /// @{
602 
603 /// Indicate that the stride is determined by the backend
604 const CeedInt CEED_STRIDES_BACKEND[3] = {0};
605 
606 /// Argument for @ref CeedOperatorSetField() indicating that the field does not require a `CeedElemRestriction`
607 const CeedElemRestriction CEED_ELEMRESTRICTION_NONE = &ceed_elemrestriction_none;
608 
609 /**
610   @brief Create a `CeedElemRestriction`
611 
612   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
613   @param[in]  num_elem    Number of elements described in the `offsets` array
614   @param[in]  elem_size   Size (number of "nodes") per element
615   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
616   @param[in]  comp_stride Stride between components for the same L-vector "node".
617                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
618   @param[in]  l_size      The size of the L-vector.
619                             This vector may be larger than the elements and fields given by this restriction.
620   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
621   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
622   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
623                             Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where 0 <= i < @a num_elem.
624                             All offsets must be in the range `[0, l_size - 1]`.
625   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
626 
627   @return An error code: 0 - success, otherwise - failure
628 
629   @ref User
630 **/
631 int CeedElemRestrictionCreate(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
632                               CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
633   if (!ceed->ElemRestrictionCreate) {
634     Ceed delegate;
635 
636     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
637     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreate");
638     CeedCall(CeedElemRestrictionCreate(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, rstr));
639     return CEED_ERROR_SUCCESS;
640   }
641 
642   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
643   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
644   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
645   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
646 
647   CeedCall(CeedCalloc(1, rstr));
648   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
649   (*rstr)->ref_count   = 1;
650   (*rstr)->num_elem    = num_elem;
651   (*rstr)->elem_size   = elem_size;
652   (*rstr)->num_comp    = num_comp;
653   (*rstr)->comp_stride = comp_stride;
654   (*rstr)->l_size      = l_size;
655   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
656   (*rstr)->num_block   = num_elem;
657   (*rstr)->block_size  = 1;
658   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
659   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
660   return CEED_ERROR_SUCCESS;
661 }
662 
663 /**
664   @brief Create a `CeedElemRestriction` with orientation signs
665 
666   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
667   @param[in]  num_elem    Number of elements described in the `offsets` array
668   @param[in]  elem_size   Size (number of "nodes") per element
669   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
670   @param[in]  comp_stride Stride between components for the same L-vector "node".
671                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
672   @param[in]  l_size      The size of the L-vector.
673                             This vector may be larger than the elements and fields given by this restriction.
674   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
675   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
676   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
677                             Row i holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
678                             All offsets must be in the range `[0, l_size - 1]`.
679   @param[in]  orients     Boolean array of shape `[num_elem, elem_size]` with `false` for positively oriented and `true` to flip the orientation
680   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
681 
682   @return An error code: 0 - success, otherwise - failure
683 
684   @ref User
685 **/
686 int CeedElemRestrictionCreateOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
687                                       CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
688                                       CeedElemRestriction *rstr) {
689   if (!ceed->ElemRestrictionCreate) {
690     Ceed delegate;
691 
692     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
693     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateOriented");
694     CeedCall(
695         CeedElemRestrictionCreateOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, orients, rstr));
696     return CEED_ERROR_SUCCESS;
697   }
698 
699   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
700   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
701   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
702   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
703 
704   CeedCall(CeedCalloc(1, rstr));
705   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
706   (*rstr)->ref_count   = 1;
707   (*rstr)->num_elem    = num_elem;
708   (*rstr)->elem_size   = elem_size;
709   (*rstr)->num_comp    = num_comp;
710   (*rstr)->comp_stride = comp_stride;
711   (*rstr)->l_size      = l_size;
712   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
713   (*rstr)->num_block   = num_elem;
714   (*rstr)->block_size  = 1;
715   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
716   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, orients, NULL, *rstr));
717   return CEED_ERROR_SUCCESS;
718 }
719 
720 /**
721   @brief Create a `CeedElemRestriction` with a general tridiagonal transformation matrix for curl-conforming elements
722 
723   @param[in]  ceed         `Ceed` context used to create the `CeedElemRestriction`
724   @param[in]  num_elem     Number of elements described in the `offsets` array
725   @param[in]  elem_size    Size (number of "nodes") per element
726   @param[in]  num_comp     Number of field components per interpolation node (1 for scalar fields)
727   @param[in]  comp_stride  Stride between components for the same L-vector "node".
728                              Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
729   @param[in]  l_size       The size of the L-vector.
730                              This vector may be larger than the elements and fields given by this restriction.
731   @param[in]  mem_type     Memory type of the `offsets` array, see @ref CeedMemType
732   @param[in]  copy_mode    Copy mode for the `offsets` array, see @ref CeedCopyMode
733   @param[in]  offsets      Array of shape `[num_elem, elem_size]`.
734                              Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
735                              All offsets must be in the range `[0, l_size - 1]`.
736   @param[in]  curl_orients Array of shape `[num_elem, 3 * elem_size]` representing a row-major tridiagonal matrix (`curl_orients[i * 3 * elem_size] = curl_orients[(i + 1) * 3 * elem_size - 1] = 0`, where `0 <= i < num_elem`) which is applied to the element unknowns upon restriction.
737                              This orientation matrix allows for pairs of face degrees of freedom on elements for \f$H(\mathrm{curl})\f$ spaces to be coupled in the element restriction operation, which is a way to resolve face orientation issues for 3D meshes (https://dl.acm.org/doi/pdf/10.1145/3524456).
738   @param[out] rstr         Address of the variable where the newly created `CeedElemRestriction` will be stored
739 
740   @return An error code: 0 - success, otherwise - failure
741 
742   @ref User
743 **/
744 int CeedElemRestrictionCreateCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
745                                           CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const CeedInt8 *curl_orients,
746                                           CeedElemRestriction *rstr) {
747   if (!ceed->ElemRestrictionCreate) {
748     Ceed delegate;
749 
750     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
751     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateCurlOriented");
752     CeedCall(CeedElemRestrictionCreateCurlOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
753                                                    curl_orients, rstr));
754     return CEED_ERROR_SUCCESS;
755   }
756 
757   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
758   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
759   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
760   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
761 
762   CeedCall(CeedCalloc(1, rstr));
763   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
764   (*rstr)->ref_count   = 1;
765   (*rstr)->num_elem    = num_elem;
766   (*rstr)->elem_size   = elem_size;
767   (*rstr)->num_comp    = num_comp;
768   (*rstr)->comp_stride = comp_stride;
769   (*rstr)->l_size      = l_size;
770   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
771   (*rstr)->num_block   = num_elem;
772   (*rstr)->block_size  = 1;
773   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
774   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, curl_orients, *rstr));
775   return CEED_ERROR_SUCCESS;
776 }
777 
778 /**
779   @brief Create a strided `CeedElemRestriction`
780 
781   @param[in]  ceed      `Ceed` context used to create the `CeedElemRestriction`
782   @param[in]  num_elem  Number of elements described by the restriction
783   @param[in]  elem_size Size (number of "nodes") per element
784   @param[in]  num_comp  Number of field components per interpolation "node" (1 for scalar fields)
785   @param[in]  l_size    The size of the L-vector.
786                           This vector may be larger than the elements and fields given by this restriction.
787   @param[in]  strides   Array for strides between `[nodes, components, elements]`.
788                           Data for node `i`, component `j`, element `k` can be found in the L-vector at index `i*strides[0] + j*strides[1] + k*strides[2]`.
789                           @ref CEED_STRIDES_BACKEND may be used for `CeedVector` ordered by the same `Ceed` backend.
790                           `CEED_STRIDES_BACKEND` should only be used pass data between `CeedOperator` created with the same `Ceed` backend.
791                           The L-vector layout will, in general, be different between `Ceed` backends.
792   @param[out] rstr      Address of the variable where the newly created `CeedElemRestriction` will be stored
793 
794   @return An error code: 0 - success, otherwise - failure
795 
796   @ref User
797 **/
798 int CeedElemRestrictionCreateStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedSize l_size, const CeedInt strides[3],
799                                      CeedElemRestriction *rstr) {
800   if (!ceed->ElemRestrictionCreate) {
801     Ceed delegate;
802 
803     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
804     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateStrided");
805     CeedCall(CeedElemRestrictionCreateStrided(delegate, num_elem, elem_size, num_comp, l_size, strides, rstr));
806     return CEED_ERROR_SUCCESS;
807   }
808 
809   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
810   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
811   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
812   CeedCheck(l_size >= (CeedSize)num_elem * elem_size * num_comp, ceed, CEED_ERROR_DIMENSION,
813             "L-vector size must be at least num_elem * elem_size * num_comp");
814 
815   CeedCall(CeedCalloc(1, rstr));
816   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
817   (*rstr)->ref_count  = 1;
818   (*rstr)->num_elem   = num_elem;
819   (*rstr)->elem_size  = elem_size;
820   (*rstr)->num_comp   = num_comp;
821   (*rstr)->l_size     = l_size;
822   (*rstr)->e_size     = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
823   (*rstr)->num_block  = num_elem;
824   (*rstr)->block_size = 1;
825   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
826   CeedCall(CeedMalloc(3, &(*rstr)->strides));
827   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
828   CeedCall(ceed->ElemRestrictionCreate(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
829   return CEED_ERROR_SUCCESS;
830 }
831 
832 /**
833   @brief Create a points `CeedElemRestriction`, for restricting for restricting from a all local points to the current element in which they are located.
834 
835   The offsets array is arranged as
836 
837   element_0_start_index
838   element_1_start_index
839   ...
840   element_n_start_index
841   element_n_stop_index
842   element_0_point_0
843   element_0_point_1
844   ...
845 
846   @param[in]  ceed       `Ceed` context used to create the `CeedElemRestriction`
847   @param[in]  num_elem   Number of elements described in the `offsets` array
848   @param[in]  num_points Number of points described in the `offsets` array
849   @param[in]  num_comp   Number of field components per interpolation node (1 for scalar fields).
850                            Components are assumed to be contiguous by point.
851   @param[in]  l_size     The size of the L-vector.
852                            This vector may be larger than the elements and fields given by this restriction.
853   @param[in]  mem_type   Memory type of the `offsets` array, see @ref CeedMemType
854   @param[in]  copy_mode  Copy mode for the `offsets` array, see @ref CeedCopyMode
855   @param[in]  offsets    Array of size `num_elem + 1 + num_points`.
856                            The first portion of the offsets array holds the ranges of indices corresponding to each element.
857                            The second portion holds the indices for each element.
858   @param[out] rstr       Address of the variable where the newly created `CeedElemRestriction` will be stored
859 
860   @return An error code: 0 - success, otherwise - failure
861 
862   @ref Backend
863  **/
864 int CeedElemRestrictionCreateAtPoints(Ceed ceed, CeedInt num_elem, CeedInt num_points, CeedInt num_comp, CeedSize l_size, CeedMemType mem_type,
865                                       CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
866   if (!ceed->ElemRestrictionCreateAtPoints) {
867     Ceed delegate;
868 
869     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
870     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateAtPoints");
871     CeedCall(CeedElemRestrictionCreateAtPoints(delegate, num_elem, num_points, num_comp, l_size, mem_type, copy_mode, offsets, rstr));
872     return CEED_ERROR_SUCCESS;
873   }
874 
875   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
876   CeedCheck(num_points >= 0, ceed, CEED_ERROR_DIMENSION, "Number of points must be non-negative");
877   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
878   CeedCheck(l_size >= (CeedSize)num_points * num_comp, ceed, CEED_ERROR_DIMENSION, "L-vector must be at least num_points * num_comp");
879 
880   CeedCall(CeedCalloc(1, rstr));
881   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
882   (*rstr)->ref_count   = 1;
883   (*rstr)->num_elem    = num_elem;
884   (*rstr)->num_points  = num_points;
885   (*rstr)->num_comp    = num_comp;
886   (*rstr)->comp_stride = 1;
887   (*rstr)->l_size      = l_size;
888   (*rstr)->e_size      = (CeedSize)num_points * (CeedSize)num_comp;
889   (*rstr)->num_block   = num_elem;
890   (*rstr)->block_size  = 1;
891   (*rstr)->rstr_type   = CEED_RESTRICTION_POINTS;
892   CeedCall(ceed->ElemRestrictionCreateAtPoints(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
893   return CEED_ERROR_SUCCESS;
894 }
895 
896 /**
897   @brief Create a blocked `CeedElemRestriction`, typically only used by backends
898 
899   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
900   @param[in]  num_elem    Number of elements described in the `offsets` array
901   @param[in]  elem_size   Size (number of unknowns) per element
902   @param[in]  block_size  Number of elements in a block
903   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
904   @param[in]  comp_stride Stride between components for the same L-vector "node".
905                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
906   @param[in]  l_size      The size of the L-vector.
907                             This vector may be larger than the elements and fields given by this restriction.
908   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
909   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
910   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
911                             Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
912                             All offsets must be in the range `[0, l_size - 1]`.
913                             The backend will permute and pad this array to the desired ordering for the blocksize, which is typically given by the backend.
914                             The default reordering is to interlace elements.
915   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
916 
917   @return An error code: 0 - success, otherwise - failure
918 
919   @ref Backend
920  **/
921 int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedInt comp_stride,
922                                      CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets,
923                                      CeedElemRestriction *rstr) {
924   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
925 
926   if (!ceed->ElemRestrictionCreateBlocked) {
927     Ceed delegate;
928 
929     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
930     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlocked");
931     CeedCall(CeedElemRestrictionCreateBlocked(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
932                                               rstr));
933     return CEED_ERROR_SUCCESS;
934   }
935 
936   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
937   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
938   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
939   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
940   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
941 
942   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
943   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
944 
945   CeedCall(CeedCalloc(1, rstr));
946   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
947   (*rstr)->ref_count   = 1;
948   (*rstr)->num_elem    = num_elem;
949   (*rstr)->elem_size   = elem_size;
950   (*rstr)->num_comp    = num_comp;
951   (*rstr)->comp_stride = comp_stride;
952   (*rstr)->l_size      = l_size;
953   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
954   (*rstr)->num_block   = num_block;
955   (*rstr)->block_size  = block_size;
956   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
957   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL, NULL, *rstr));
958   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
959   return CEED_ERROR_SUCCESS;
960 }
961 
962 /**
963   @brief Create a blocked oriented `CeedElemRestriction`, typically only used by backends
964 
965   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
966   @param[in]  num_elem    Number of elements described in the `offsets` array.
967   @param[in]  elem_size   Size (number of unknowns) per element
968   @param[in]  block_size  Number of elements in a block
969   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
970   @param[in]  comp_stride Stride between components for the same L-vector "node".
971                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
972   @param[in]  l_size      The size of the L-vector.
973                             This vector may be larger than the elements and fields given by this restriction.
974   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
975   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
976   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
977                             Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
978                             All offsets must be in the range `[0, l_size - 1]`.
979                             The backend will permute and pad this array to the desired ordering for the blocksize, which is typically given by the backend.
980                             The default reordering is to interlace elements.
981   @param[in]  orients     Boolean array of shape `[num_elem, elem_size]` with `false` for positively oriented and `true` to flip the orientation.
982                             Will also be permuted and padded similarly to `offsets`.
983   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
984 
985   @return An error code: 0 - success, otherwise - failure
986 
987   @ref Backend
988  **/
989 int CeedElemRestrictionCreateBlockedOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
990                                              CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
991                                              const CeedInt *offsets, const bool *orients, CeedElemRestriction *rstr) {
992   bool    *block_orients;
993   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
994 
995   if (!ceed->ElemRestrictionCreateBlocked) {
996     Ceed delegate;
997 
998     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
999     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedOriented");
1000     CeedCall(CeedElemRestrictionCreateBlockedOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode,
1001                                                       offsets, orients, rstr));
1002     return CEED_ERROR_SUCCESS;
1003   }
1004 
1005   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1006   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1007   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1008   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
1009 
1010   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
1011   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_orients));
1012   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
1013   CeedCall(CeedPermutePadOrients(orients, block_orients, num_block, num_elem, block_size, elem_size));
1014 
1015   CeedCall(CeedCalloc(1, rstr));
1016   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
1017   (*rstr)->ref_count   = 1;
1018   (*rstr)->num_elem    = num_elem;
1019   (*rstr)->elem_size   = elem_size;
1020   (*rstr)->num_comp    = num_comp;
1021   (*rstr)->comp_stride = comp_stride;
1022   (*rstr)->l_size      = l_size;
1023   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1024   (*rstr)->num_block   = num_block;
1025   (*rstr)->block_size  = block_size;
1026   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
1027   CeedCall(
1028       ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, (const bool *)block_orients, NULL, *rstr));
1029   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
1030   return CEED_ERROR_SUCCESS;
1031 }
1032 
1033 /**
1034   @brief Create a blocked curl-oriented `CeedElemRestriction`, typically only used by backends
1035 
1036   @param[in]  ceed         `Ceed` context used to create the `CeedElemRestriction`
1037   @param[in]  num_elem     Number of elements described in the `offsets` array.
1038   @param[in]  elem_size    Size (number of unknowns) per element
1039   @param[in]  block_size   Number of elements in a block
1040   @param[in]  num_comp     Number of field components per interpolation node (1 for scalar fields)
1041   @param[in]  comp_stride  Stride between components for the same L-vector "node".
1042                              Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
1043   @param[in]  l_size       The size of the L-vector.
1044                              This vector may be larger than the elements and fields given by this restriction.
1045   @param[in]  mem_type     Memory type of the `offsets` array, see @ref CeedMemType
1046   @param[in]  copy_mode    Copy mode for the `offsets` array, see @ref CeedCopyMode
1047   @param[in]  offsets      Array of shape `[num_elem, elem_size]`.
1048                              Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
1049                              All offsets must be in the range `[0, l_size - 1]`.
1050                              The backend will permute and pad this array to the desired  ordering for the blocksize, which is typically given by the backend.
1051                              The default reordering is to interlace elements.
1052   @param[in]  curl_orients Array of shape `[num_elem, 3 * elem_size]` representing a row-major tridiagonal matrix (`curl_orients[i * 3 * elem_size] = curl_orients[(i + 1) * 3 * elem_size - 1] = 0`, where `0 <= i < num_elem`) which is applied to the element unknowns upon restriction.
1053                              This orientation matrix allows for pairs of face degrees of freedom on elements for \f$H(\mathrm{curl})\f$ spaces to be coupled in the element restriction  operation, which is a way to resolve face orientation issues for 3D meshes (https://dl.acm.org/doi/pdf/10.1145/3524456).
1054                              Will also be permuted and padded similarly to offsets.
1055   @param[out] rstr         Address of the variable where the newly created `CeedElemRestriction` will be stored
1056 
1057   @return An error code: 0 - success, otherwise - failure
1058 
1059   @ref Backend
1060  **/
1061 int CeedElemRestrictionCreateBlockedCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
1062                                                  CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
1063                                                  const CeedInt *offsets, const CeedInt8 *curl_orients, CeedElemRestriction *rstr) {
1064   CeedInt8 *block_curl_orients;
1065   CeedInt  *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
1066 
1067   if (!ceed->ElemRestrictionCreateBlocked) {
1068     Ceed delegate;
1069 
1070     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
1071     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedCurlOriented");
1072     CeedCall(CeedElemRestrictionCreateBlockedCurlOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type,
1073                                                           copy_mode, offsets, curl_orients, rstr));
1074     return CEED_ERROR_SUCCESS;
1075   }
1076 
1077   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
1078   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1079   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1080   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1081   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
1082 
1083   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
1084   CeedCall(CeedCalloc(num_block * block_size * 3 * elem_size, &block_curl_orients));
1085   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
1086   CeedCall(CeedPermutePadCurlOrients(curl_orients, block_curl_orients, num_block, num_elem, block_size, 3 * elem_size));
1087 
1088   CeedCall(CeedCalloc(1, rstr));
1089   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
1090   (*rstr)->ref_count   = 1;
1091   (*rstr)->num_elem    = num_elem;
1092   (*rstr)->elem_size   = elem_size;
1093   (*rstr)->num_comp    = num_comp;
1094   (*rstr)->comp_stride = comp_stride;
1095   (*rstr)->l_size      = l_size;
1096   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1097   (*rstr)->num_block   = num_block;
1098   (*rstr)->block_size  = block_size;
1099   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
1100   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL,
1101                                               (const CeedInt8 *)block_curl_orients, *rstr));
1102   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
1103   return CEED_ERROR_SUCCESS;
1104 }
1105 
1106 /**
1107   @brief Create a blocked strided `CeedElemRestriction`, typically only used by backends
1108 
1109   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
1110   @param[in]  num_elem    Number of elements described by the restriction
1111   @param[in]  elem_size   Size (number of "nodes") per element
1112   @param[in]  block_size  Number of elements in a block
1113   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
1114   @param[in]  l_size      The size of the L-vector.
1115                             This vector may be larger than the elements and fields given by this restriction.
1116   @param[in]  strides     Array for strides between `[nodes, components, elements]`.
1117                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `i*strides[0] + j*strides[1] +k*strides[2]`.
1118                             @ref CEED_STRIDES_BACKEND may be used for `CeedVector` ordered by the same `Ceed` backend.
1119   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
1120 
1121   @return An error code: 0 - success, otherwise - failure
1122 
1123   @ref User
1124 **/
1125 int CeedElemRestrictionCreateBlockedStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedSize l_size,
1126                                             const CeedInt strides[3], CeedElemRestriction *rstr) {
1127   CeedInt num_block = (num_elem / block_size) + !!(num_elem % block_size);
1128 
1129   if (!ceed->ElemRestrictionCreateBlocked) {
1130     Ceed delegate;
1131 
1132     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
1133     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedStrided");
1134     CeedCall(CeedElemRestrictionCreateBlockedStrided(delegate, num_elem, elem_size, block_size, num_comp, l_size, strides, rstr));
1135     return CEED_ERROR_SUCCESS;
1136   }
1137 
1138   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
1139   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1140   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1141   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1142   CeedCheck(l_size >= (CeedSize)num_elem * elem_size * num_comp, ceed, CEED_ERROR_DIMENSION,
1143             "L-vector size must be at least num_elem * elem_size * num_comp");
1144 
1145   CeedCall(CeedCalloc(1, rstr));
1146   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
1147   (*rstr)->ref_count  = 1;
1148   (*rstr)->num_elem   = num_elem;
1149   (*rstr)->elem_size  = elem_size;
1150   (*rstr)->num_comp   = num_comp;
1151   (*rstr)->l_size     = l_size;
1152   (*rstr)->e_size     = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1153   (*rstr)->num_block  = num_block;
1154   (*rstr)->block_size = block_size;
1155   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
1156   CeedCall(CeedMalloc(3, &(*rstr)->strides));
1157   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
1158   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
1159   return CEED_ERROR_SUCCESS;
1160 }
1161 
1162 /**
1163   @brief Copy the pointer to a `CeedElemRestriction` and set @ref CeedElemRestrictionApply() implementation to use the unsigned version.
1164 
1165   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
1166 
1167   @param[in]     rstr          `CeedElemRestriction` to create unsigned reference to
1168   @param[in,out] rstr_unsigned Variable to store unsigned `CeedElemRestriction`
1169 
1170   @return An error code: 0 - success, otherwise - failure
1171 
1172   @ref User
1173 **/
1174 int CeedElemRestrictionCreateUnsignedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unsigned) {
1175   CeedCall(CeedCalloc(1, rstr_unsigned));
1176 
1177   // Copy old rstr
1178   memcpy(*rstr_unsigned, rstr, sizeof(struct CeedElemRestriction_private));
1179   (*rstr_unsigned)->ceed = NULL;
1180   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unsigned)->ceed));
1181   (*rstr_unsigned)->ref_count = 1;
1182   (*rstr_unsigned)->strides   = NULL;
1183   if (rstr->strides) {
1184     CeedCall(CeedMalloc(3, &(*rstr_unsigned)->strides));
1185     for (CeedInt i = 0; i < 3; i++) (*rstr_unsigned)->strides[i] = rstr->strides[i];
1186   }
1187   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unsigned)->rstr_base));
1188 
1189   // Override Apply
1190   (*rstr_unsigned)->Apply = rstr->ApplyUnsigned;
1191   return CEED_ERROR_SUCCESS;
1192 }
1193 
1194 /**
1195   @brief Copy the pointer to a `CeedElemRestriction` and set @ref CeedElemRestrictionApply() implementation to use the unoriented version.
1196 
1197   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
1198 
1199   @param[in]     rstr            `CeedElemRestriction` to create unoriented reference to
1200   @param[in,out] rstr_unoriented Variable to store unoriented `CeedElemRestriction`
1201 
1202   @return An error code: 0 - success, otherwise - failure
1203 
1204   @ref User
1205 **/
1206 int CeedElemRestrictionCreateUnorientedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unoriented) {
1207   CeedCall(CeedCalloc(1, rstr_unoriented));
1208 
1209   // Copy old rstr
1210   memcpy(*rstr_unoriented, rstr, sizeof(struct CeedElemRestriction_private));
1211   (*rstr_unoriented)->ceed = NULL;
1212   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unoriented)->ceed));
1213   (*rstr_unoriented)->ref_count = 1;
1214   (*rstr_unoriented)->strides   = NULL;
1215   if (rstr->strides) {
1216     CeedCall(CeedMalloc(3, &(*rstr_unoriented)->strides));
1217     for (CeedInt i = 0; i < 3; i++) (*rstr_unoriented)->strides[i] = rstr->strides[i];
1218   }
1219   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unoriented)->rstr_base));
1220 
1221   // Override Apply
1222   (*rstr_unoriented)->Apply = rstr->ApplyUnoriented;
1223   return CEED_ERROR_SUCCESS;
1224 }
1225 
1226 /**
1227   @brief Copy the pointer to a `CeedElemRestriction`.
1228 
1229   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
1230 
1231   Note: If the value of `*rstr_copy` passed into this function is non-`NULL`, then it is assumed that `*rstr_copy` is a pointer to a `CeedElemRestriction`.
1232         This `CeedElemRestriction` will be destroyed if `*rstr_copy` is the only reference to this `CeedElemRestriction`.
1233 
1234   @param[in]     rstr      `CeedElemRestriction` to copy reference to
1235   @param[in,out] rstr_copy Variable to store copied reference
1236 
1237   @return An error code: 0 - success, otherwise - failure
1238 
1239   @ref User
1240 **/
1241 int CeedElemRestrictionReferenceCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_copy) {
1242   if (rstr != CEED_ELEMRESTRICTION_NONE) CeedCall(CeedElemRestrictionReference(rstr));
1243   CeedCall(CeedElemRestrictionDestroy(rstr_copy));
1244   *rstr_copy = rstr;
1245   return CEED_ERROR_SUCCESS;
1246 }
1247 
1248 /**
1249   @brief Create `CeedVector` associated with a `CeedElemRestriction`
1250 
1251   @param[in]  rstr  `CeedElemRestriction`
1252   @param[out] l_vec The address of the L-vector to be created, or `NULL`
1253   @param[out] e_vec The address of the E-vector to be created, or `NULL`
1254 
1255   @return An error code: 0 - success, otherwise - failure
1256 
1257   @ref User
1258 **/
1259 int CeedElemRestrictionCreateVector(CeedElemRestriction rstr, CeedVector *l_vec, CeedVector *e_vec) {
1260   CeedSize e_size, l_size;
1261   Ceed     ceed;
1262 
1263   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1264   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1265   CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &e_size));
1266   if (l_vec) CeedCall(CeedVectorCreate(ceed, l_size, l_vec));
1267   if (e_vec) CeedCall(CeedVectorCreate(ceed, e_size, e_vec));
1268   return CEED_ERROR_SUCCESS;
1269 }
1270 
1271 /**
1272   @brief Restrict an L-vector to an E-vector or apply its transpose
1273 
1274   @param[in]  rstr    `CeedElemRestriction`
1275   @param[in]  t_mode  Apply restriction or transpose
1276   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1277   @param[out] ru      Output vector (of shape `[num_elem * elem_size]` when `t_mode` = @ref CEED_NOTRANSPOSE).
1278                         Ordering of the e-vector is decided by the backend.
1279   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1280 
1281   @return An error code: 0 - success, otherwise - failure
1282 
1283   @ref User
1284 **/
1285 int CeedElemRestrictionApply(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector ru, CeedRequest *request) {
1286   CeedSize min_u_len, min_ru_len, len;
1287   CeedInt  num_elem;
1288   Ceed     ceed;
1289 
1290   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1291   if (t_mode == CEED_NOTRANSPOSE) {
1292     CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &min_ru_len));
1293     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1294   } else {
1295     CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &min_u_len));
1296     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
1297   }
1298   CeedCall(CeedVectorGetLength(u, &len));
1299   CeedCheck(min_u_len <= len, ceed, CEED_ERROR_DIMENSION,
1300             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len,
1301             min_u_len);
1302   CeedCall(CeedVectorGetLength(ru, &len));
1303   CeedCheck(min_ru_len <= len, ceed, CEED_ERROR_DIMENSION,
1304             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len,
1305             min_ru_len);
1306   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1307   if (num_elem > 0) CeedCall(rstr->Apply(rstr, t_mode, u, ru, request));
1308   return CEED_ERROR_SUCCESS;
1309 }
1310 
1311 /**
1312   @brief Restrict an L-vector of points to a single element or apply its transpose
1313 
1314   @param[in]  rstr    `CeedElemRestriction`
1315   @param[in]  elem    Element number in range `[0, num_elem)`
1316   @param[in]  t_mode  Apply restriction or transpose
1317   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1318   @param[out] ru      Output vector (of shape [`num_points * num_comp]` when `t_mode` = @ref CEED_NOTRANSPOSE).
1319                         Ordering of the e-vector is decided by the backend.
1320   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1321 
1322   @return An error code: 0 - success, otherwise - failure
1323 
1324   @ref User
1325 **/
1326 int CeedElemRestrictionApplyAtPointsInElement(CeedElemRestriction rstr, CeedInt elem, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
1327                                               CeedRequest *request) {
1328   CeedSize min_u_len, min_ru_len, len;
1329   CeedInt  num_elem;
1330   Ceed     ceed;
1331 
1332   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1333   CeedCheck(rstr->ApplyAtPointsInElement, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionApplyAtPointsInElement");
1334 
1335   if (t_mode == CEED_NOTRANSPOSE) {
1336     CeedInt num_points, num_comp;
1337 
1338     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1339     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &num_points));
1340     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1341     min_ru_len = (CeedSize)num_points * (CeedSize)num_comp;
1342   } else {
1343     CeedInt num_points, num_comp;
1344 
1345     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &num_points));
1346     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1347     min_u_len = (CeedSize)num_points * (CeedSize)num_comp;
1348     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
1349   }
1350   CeedCall(CeedVectorGetLength(u, &len));
1351   CeedCheck(min_u_len <= len, ceed, CEED_ERROR_DIMENSION,
1352             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
1353             ") for element %" CeedInt_FMT,
1354             len, min_ru_len, min_u_len, elem);
1355   CeedCall(CeedVectorGetLength(ru, &len));
1356   CeedCheck(min_ru_len <= len, ceed, CEED_ERROR_DIMENSION,
1357             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
1358             ") for element %" CeedInt_FMT,
1359             len, min_ru_len, min_u_len, elem);
1360   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1361   CeedCheck(elem < num_elem, ceed, CEED_ERROR_DIMENSION,
1362             "Cannot retrieve element %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", elem, elem, num_elem);
1363   if (num_elem > 0) CeedCall(rstr->ApplyAtPointsInElement(rstr, elem, t_mode, u, ru, request));
1364   return CEED_ERROR_SUCCESS;
1365 }
1366 
1367 /**
1368   @brief Restrict an L-vector to a block of an E-vector or apply its transpose
1369 
1370   @param[in]  rstr    `CeedElemRestriction`
1371   @param[in]  block   Block number to restrict to/from, i.e. `block = 0` will handle elements `[0 : block_size]` and `block = 3` will handle elements `[3*block_size : 4*block_size]`
1372   @param[in]  t_mode  Apply restriction or transpose
1373   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1374   @param[out] ru      Output vector (of shape `[block_size * elem_size]` when `t_mode` = @ref CEED_NOTRANSPOSE).
1375                         Ordering of the e-vector is decided by the backend.
1376   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1377 
1378   @return An error code: 0 - success, otherwise - failure
1379 
1380   @ref Backend
1381 **/
1382 int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
1383                                   CeedRequest *request) {
1384   CeedSize min_u_len, min_ru_len, len;
1385   CeedInt  block_size, num_elem;
1386   Ceed     ceed;
1387 
1388   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1389   CeedCheck(rstr->ApplyBlock, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionApplyBlock");
1390 
1391   CeedCall(CeedElemRestrictionGetBlockSize(rstr, &block_size));
1392   if (t_mode == CEED_NOTRANSPOSE) {
1393     CeedInt elem_size, num_comp;
1394 
1395     CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1396     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1397     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1398     min_ru_len = (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1399   } else {
1400     CeedInt elem_size, num_comp;
1401 
1402     CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1403     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1404     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
1405     min_u_len = (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1406   }
1407   CeedCall(CeedVectorGetLength(u, &len));
1408   CeedCheck(min_u_len == len, ceed, CEED_ERROR_DIMENSION,
1409             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len,
1410             min_ru_len);
1411   CeedCall(CeedVectorGetLength(ru, &len));
1412   CeedCheck(min_ru_len == len, ceed, CEED_ERROR_DIMENSION,
1413             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len,
1414             min_u_len);
1415   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1416   CeedCheck(block_size * block <= num_elem, ceed, CEED_ERROR_DIMENSION,
1417             "Cannot retrieve block %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", block, block_size * block,
1418             num_elem);
1419   CeedCall(rstr->ApplyBlock(rstr, block, t_mode, u, ru, request));
1420   return CEED_ERROR_SUCCESS;
1421 }
1422 
1423 /**
1424   @brief Get the `Ceed` associated with a `CeedElemRestriction`
1425 
1426   @param[in]  rstr `CeedElemRestriction`
1427   @param[out] ceed Variable to store `Ceed`
1428 
1429   @return An error code: 0 - success, otherwise - failure
1430 
1431   @ref Advanced
1432 **/
1433 int CeedElemRestrictionGetCeed(CeedElemRestriction rstr, Ceed *ceed) {
1434   *ceed = CeedElemRestrictionReturnCeed(rstr);
1435   return CEED_ERROR_SUCCESS;
1436 }
1437 
1438 /**
1439   @brief Return the `Ceed` associated with a `CeedElemRestriction`
1440 
1441   @param[in]  rstr `CeedElemRestriction`
1442 
1443   @return `Ceed` associated with the `rstr`
1444 
1445   @ref Advanced
1446 **/
1447 Ceed CeedElemRestrictionReturnCeed(CeedElemRestriction rstr) { return rstr->ceed; }
1448 
1449 /**
1450   @brief Get the L-vector component stride
1451 
1452   @param[in]  rstr        `CeedElemRestriction`
1453   @param[out] comp_stride Variable to store component stride
1454 
1455   @return An error code: 0 - success, otherwise - failure
1456 
1457   @ref Advanced
1458 **/
1459 int CeedElemRestrictionGetCompStride(CeedElemRestriction rstr, CeedInt *comp_stride) {
1460   *comp_stride = rstr->comp_stride;
1461   return CEED_ERROR_SUCCESS;
1462 }
1463 
1464 /**
1465   @brief Get the total number of elements in the range of a `CeedElemRestriction`
1466 
1467   @param[in] rstr      `CeedElemRestriction`
1468   @param[out] num_elem Variable to store number of elements
1469 
1470   @return An error code: 0 - success, otherwise - failure
1471 
1472   @ref Advanced
1473 **/
1474 int CeedElemRestrictionGetNumElements(CeedElemRestriction rstr, CeedInt *num_elem) {
1475   *num_elem = rstr->num_elem;
1476   return CEED_ERROR_SUCCESS;
1477 }
1478 
1479 /**
1480   @brief Get the size of elements in the `CeedElemRestriction`
1481 
1482   @param[in]  rstr      `CeedElemRestriction`
1483   @param[out] elem_size Variable to store size of elements
1484 
1485   @return An error code: 0 - success, otherwise - failure
1486 
1487   @ref Advanced
1488 **/
1489 int CeedElemRestrictionGetElementSize(CeedElemRestriction rstr, CeedInt *elem_size) {
1490   *elem_size = rstr->elem_size;
1491   return CEED_ERROR_SUCCESS;
1492 }
1493 
1494 /**
1495 
1496   @brief Get the number of points in the offsets array for a points `CeedElemRestriction`
1497 
1498   @param[in]  rstr       `CeedElemRestriction`
1499   @param[out] num_points The number of points in the offsets array
1500 
1501   @return An error code: 0 - success, otherwise - failure
1502 
1503   @ref User
1504 **/
1505 int CeedElemRestrictionGetNumPoints(CeedElemRestriction rstr, CeedInt *num_points) {
1506   CeedRestrictionType rstr_type;
1507 
1508   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1509   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
1510             "Can only retrieve the number of points for a points CeedElemRestriction");
1511 
1512   *num_points = rstr->num_points;
1513   return CEED_ERROR_SUCCESS;
1514 }
1515 
1516 /**
1517 
1518   @brief Get the number of points in an element of a `CeedElemRestriction` at points
1519 
1520   @param[in]  rstr       `CeedElemRestriction`
1521   @param[in]  elem       Index number of element to retrieve the number of points for
1522   @param[out] num_points The number of points in the element at index elem
1523 
1524   @return An error code: 0 - success, otherwise - failure
1525 
1526   @ref User
1527 **/
1528 int CeedElemRestrictionGetNumPointsInElement(CeedElemRestriction rstr, CeedInt elem, CeedInt *num_points) {
1529   CeedRestrictionType rstr_type;
1530   const CeedInt      *offsets;
1531 
1532   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1533   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
1534             "Can only retrieve the number of points for a points CeedElemRestriction");
1535 
1536   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1537   *num_points = offsets[elem + 1] - offsets[elem];
1538   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
1539   return CEED_ERROR_SUCCESS;
1540 }
1541 
1542 /**
1543   @brief Get the maximum number of points in an element for a `CeedElemRestriction` at points
1544 
1545   @param[in]  rstr       `CeedElemRestriction`
1546   @param[out] max_points Variable to store size of elements
1547 
1548   @return An error code: 0 - success, otherwise - failure
1549 
1550   @ref Advanced
1551 **/
1552 int CeedElemRestrictionGetMaxPointsInElement(CeedElemRestriction rstr, CeedInt *max_points) {
1553   CeedInt             num_elem;
1554   CeedRestrictionType rstr_type;
1555 
1556   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1557   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
1558             "Cannot compute max points for a CeedElemRestriction that does not use points");
1559 
1560   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1561   *max_points = 0;
1562   for (CeedInt e = 0; e < num_elem; e++) {
1563     CeedInt num_points;
1564 
1565     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, e, &num_points));
1566     *max_points = CeedIntMax(num_points, *max_points);
1567   }
1568   return CEED_ERROR_SUCCESS;
1569 }
1570 
1571 /**
1572   @brief Get the size of the l-vector for a `CeedElemRestriction`
1573 
1574   @param[in]  rstr   `CeedElemRestriction`
1575   @param[out] l_size Variable to store l-vector size
1576 
1577   @return An error code: 0 - success, otherwise - failure
1578 
1579   @ref Advanced
1580 **/
1581 int CeedElemRestrictionGetLVectorSize(CeedElemRestriction rstr, CeedSize *l_size) {
1582   *l_size = rstr->l_size;
1583   return CEED_ERROR_SUCCESS;
1584 }
1585 
1586 /**
1587   @brief Get the size of the e-vector for a `CeedElemRestriction`
1588 
1589   @param[in]  rstr   `CeedElemRestriction`
1590   @param[out] e_size Variable to store e-vector size
1591 
1592   @return An error code: 0 - success, otherwise - failure
1593 
1594   @ref Advanced
1595 **/
1596 int CeedElemRestrictionGetEVectorSize(CeedElemRestriction rstr, CeedSize *e_size) {
1597   *e_size = rstr->e_size;
1598   return CEED_ERROR_SUCCESS;
1599 }
1600 
1601 /**
1602   @brief Get the number of components in the elements of a `CeedElemRestriction`
1603 
1604   @param[in]  rstr     `CeedElemRestriction`
1605   @param[out] num_comp Variable to store number of components
1606 
1607   @return An error code: 0 - success, otherwise - failure
1608 
1609   @ref Advanced
1610 **/
1611 int CeedElemRestrictionGetNumComponents(CeedElemRestriction rstr, CeedInt *num_comp) {
1612   *num_comp = rstr->num_comp;
1613   return CEED_ERROR_SUCCESS;
1614 }
1615 
1616 /**
1617   @brief Get the number of blocks in a `CeedElemRestriction`
1618 
1619   @param[in]  rstr      `CeedElemRestriction`
1620   @param[out] num_block Variable to store number of blocks
1621 
1622   @return An error code: 0 - success, otherwise - failure
1623 
1624   @ref Advanced
1625 **/
1626 int CeedElemRestrictionGetNumBlocks(CeedElemRestriction rstr, CeedInt *num_block) {
1627   *num_block = rstr->num_block;
1628   return CEED_ERROR_SUCCESS;
1629 }
1630 
1631 /**
1632   @brief Get the size of blocks in the `CeedElemRestriction`
1633 
1634   @param[in]  rstr       `CeedElemRestriction`
1635   @param[out] block_size Variable to store size of blocks
1636 
1637   @return An error code: 0 - success, otherwise - failure
1638 
1639   @ref Advanced
1640 **/
1641 int CeedElemRestrictionGetBlockSize(CeedElemRestriction rstr, CeedInt *block_size) {
1642   *block_size = rstr->block_size;
1643   return CEED_ERROR_SUCCESS;
1644 }
1645 
1646 /**
1647   @brief Get the multiplicity of nodes in a `CeedElemRestriction`
1648 
1649   @param[in]  rstr `CeedElemRestriction`
1650   @param[out] mult Vector to store multiplicity (of size `l_size`)
1651 
1652   @return An error code: 0 - success, otherwise - failure
1653 
1654   @ref User
1655 **/
1656 int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr, CeedVector mult) {
1657   CeedVector e_vec;
1658 
1659   // Create e_vec to hold intermediate computation in E^T (E 1)
1660   CeedCall(CeedElemRestrictionCreateVector(rstr, NULL, &e_vec));
1661 
1662   // Compute e_vec = E * 1
1663   CeedCall(CeedVectorSetValue(mult, 1.0));
1664   CeedCall(CeedElemRestrictionApply(rstr, CEED_NOTRANSPOSE, mult, e_vec, CEED_REQUEST_IMMEDIATE));
1665   // Compute multiplicity, mult = E^T * e_vec = E^T (E 1)
1666   CeedCall(CeedVectorSetValue(mult, 0.0));
1667   CeedCall(CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, e_vec, mult, CEED_REQUEST_IMMEDIATE));
1668   // Cleanup
1669   CeedCall(CeedVectorDestroy(&e_vec));
1670   return CEED_ERROR_SUCCESS;
1671 }
1672 
1673 /**
1674   @brief View a `CeedElemRestriction`
1675 
1676   @param[in] rstr   `CeedElemRestriction` to view
1677   @param[in] stream Stream to write; typically `stdout` or a file
1678 
1679   @return Error code: 0 - success, otherwise - failure
1680 
1681   @ref User
1682 **/
1683 int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream) {
1684   CeedRestrictionType rstr_type;
1685 
1686   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1687   if (rstr_type == CEED_RESTRICTION_POINTS) {
1688     CeedInt max_points;
1689 
1690     CeedCall(CeedElemRestrictionGetMaxPointsInElement(rstr, &max_points));
1691     fprintf(stream,
1692             "CeedElemRestriction at points from (%" CeedSize_FMT ", %" CeedInt_FMT ") to %" CeedInt_FMT " elements with a maximum of %" CeedInt_FMT
1693             " points on an element\n",
1694             rstr->l_size, rstr->num_comp, rstr->num_elem, max_points);
1695   } else {
1696     char strides_str[500];
1697 
1698     if (rstr->strides) {
1699       sprintf(strides_str, "[%" CeedInt_FMT ", %" CeedInt_FMT ", %" CeedInt_FMT "]", rstr->strides[0], rstr->strides[1], rstr->strides[2]);
1700     } else {
1701       sprintf(strides_str, "%" CeedInt_FMT, rstr->comp_stride);
1702     }
1703     fprintf(stream,
1704             "%sCeedElemRestriction from (%" CeedSize_FMT ", %" CeedInt_FMT ") to %" CeedInt_FMT " elements with %" CeedInt_FMT
1705             " nodes each and %s %s\n",
1706             rstr->block_size > 1 ? "Blocked " : "", rstr->l_size, rstr->num_comp, rstr->num_elem, rstr->elem_size,
1707             rstr->strides ? "strides" : "component stride", strides_str);
1708   }
1709   return CEED_ERROR_SUCCESS;
1710 }
1711 
1712 /**
1713   @brief Destroy a `CeedElemRestriction`
1714 
1715   @param[in,out] rstr `CeedElemRestriction` to destroy
1716 
1717   @return An error code: 0 - success, otherwise - failure
1718 
1719   @ref User
1720 **/
1721 int CeedElemRestrictionDestroy(CeedElemRestriction *rstr) {
1722   if (!*rstr || *rstr == CEED_ELEMRESTRICTION_NONE || --(*rstr)->ref_count > 0) {
1723     *rstr = NULL;
1724     return CEED_ERROR_SUCCESS;
1725   }
1726   CeedCheck((*rstr)->num_readers == 0, (*rstr)->ceed, CEED_ERROR_ACCESS,
1727             "Cannot destroy CeedElemRestriction, a process has read access to the offset data");
1728 
1729   // Only destroy backend data once between rstr and unsigned copy
1730   if ((*rstr)->rstr_base) CeedCall(CeedElemRestrictionDestroy(&(*rstr)->rstr_base));
1731   else if ((*rstr)->Destroy) CeedCall((*rstr)->Destroy(*rstr));
1732 
1733   CeedCall(CeedFree(&(*rstr)->strides));
1734   CeedCall(CeedDestroy(&(*rstr)->ceed));
1735   CeedCall(CeedFree(rstr));
1736   return CEED_ERROR_SUCCESS;
1737 }
1738 
1739 /// @}
1740