xref: /libCEED/interface/ceed-elemrestriction.c (revision 2c7e74134068de5221afb7e7255d8033016bfd12)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
13c17ec2beSJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
157a982d89SJeremy L. Thompson /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedElemRestriction interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedElemRestriction Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedElemRestrictionDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
25d979a051Sjeremylt   @brief Permute and pad offsets for a blocked restriction
267a982d89SJeremy L. Thompson 
27fcbe8c06SSebastian Grimberg   @param[in]  offsets        Array of shape [@a num_elem, @a elem_size].
28e7f679fcSJeremy L Thompson   @param[out] block_offsets  Array of permuted and padded array values of shape [@a num_block, @a elem_size, @a block_size].
29e7f679fcSJeremy L Thompson   @param[in]  num_block      Number of blocks
30ea61e9acSJeremy L Thompson   @param[in]  num_elem       Number of elements
31e7f679fcSJeremy L Thompson   @param[in]  block_size     Number of elements in a block
32ea61e9acSJeremy L Thompson   @param[in]  elem_size      Size of each element
337a982d89SJeremy L. Thompson 
347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
357a982d89SJeremy L. Thompson 
367a982d89SJeremy L. Thompson   @ref Utility
377a982d89SJeremy L. Thompson **/
38e7f679fcSJeremy L Thompson int CeedPermutePadOffsets(const CeedInt *offsets, CeedInt *block_offsets, CeedInt num_block, CeedInt num_elem, CeedInt block_size,
39e7f679fcSJeremy L Thompson                           CeedInt elem_size) {
40e7f679fcSJeremy L Thompson   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
41e7f679fcSJeremy L Thompson     for (CeedInt j = 0; j < block_size; j++) {
422b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < elem_size; k++) {
43e7f679fcSJeremy L Thompson         block_offsets[e * elem_size + k * block_size + j] = offsets[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
442b730f8bSJeremy L Thompson       }
452b730f8bSJeremy L Thompson     }
462b730f8bSJeremy L Thompson   }
47e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
487a982d89SJeremy L. Thompson }
497a982d89SJeremy L. Thompson 
5077d1c127SSebastian Grimberg /**
5177d1c127SSebastian Grimberg   @brief Permute and pad orientations for a blocked restriction
5277d1c127SSebastian Grimberg 
53fcbe8c06SSebastian Grimberg   @param[in]  orients       Array of shape [@a num_elem, @a elem_size].
54e7f679fcSJeremy L Thompson   @param[out] block_orients Array of permuted and padded array values of shape [@a num_block, @a elem_size, @a block_size].
55e7f679fcSJeremy L Thompson   @param[in]  num_block     Number of blocks
5677d1c127SSebastian Grimberg   @param[in]  num_elem      Number of elements
57e7f679fcSJeremy L Thompson   @param[in]  block_size    Number of elements in a block
5877d1c127SSebastian Grimberg   @param[in]  elem_size     Size of each element
5977d1c127SSebastian Grimberg 
6077d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
6177d1c127SSebastian Grimberg 
6277d1c127SSebastian Grimberg   @ref Utility
6377d1c127SSebastian Grimberg **/
64e7f679fcSJeremy L Thompson int CeedPermutePadOrients(const bool *orients, bool *block_orients, CeedInt num_block, CeedInt num_elem, CeedInt block_size, CeedInt elem_size) {
65e7f679fcSJeremy L Thompson   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
66e7f679fcSJeremy L Thompson     for (CeedInt j = 0; j < block_size; j++) {
6777d1c127SSebastian Grimberg       for (CeedInt k = 0; k < elem_size; k++) {
68e7f679fcSJeremy L Thompson         block_orients[e * elem_size + k * block_size + j] = orients[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
6977d1c127SSebastian Grimberg       }
7077d1c127SSebastian Grimberg     }
7177d1c127SSebastian Grimberg   }
7277d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
7377d1c127SSebastian Grimberg }
7477d1c127SSebastian Grimberg 
750c73c039SSebastian Grimberg /**
760c73c039SSebastian Grimberg   @brief Permute and pad curl-conforming orientations for a blocked restriction
770c73c039SSebastian Grimberg 
780c73c039SSebastian Grimberg   @param[in]  curl_orients       Array of shape [@a num_elem, @a 3 * elem_size].
79e7f679fcSJeremy L Thompson   @param[out] block_curl_orients Array of permuted and padded array values of shape [@a num_block, @a elem_size, @a block_size].
80e7f679fcSJeremy L Thompson   @param[in]  num_block          Number of blocks
810c73c039SSebastian Grimberg   @param[in]  num_elem           Number of elements
82e7f679fcSJeremy L Thompson   @param[in]  block_size         Number of elements in a block
830c73c039SSebastian Grimberg   @param[in]  elem_size          Size of each element
840c73c039SSebastian Grimberg 
850c73c039SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
860c73c039SSebastian Grimberg 
870c73c039SSebastian Grimberg   @ref Utility
880c73c039SSebastian Grimberg **/
89e7f679fcSJeremy L Thompson int CeedPermutePadCurlOrients(const CeedInt8 *curl_orients, CeedInt8 *block_curl_orients, CeedInt num_block, CeedInt num_elem, CeedInt block_size,
900c73c039SSebastian Grimberg                               CeedInt elem_size) {
91e7f679fcSJeremy L Thompson   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
92e7f679fcSJeremy L Thompson     for (CeedInt j = 0; j < block_size; j++) {
930c73c039SSebastian Grimberg       for (CeedInt k = 0; k < elem_size; k++) {
94e7f679fcSJeremy L Thompson         block_curl_orients[e * elem_size + k * block_size + j] = curl_orients[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
950c73c039SSebastian Grimberg       }
960c73c039SSebastian Grimberg     }
970c73c039SSebastian Grimberg   }
980c73c039SSebastian Grimberg   return CEED_ERROR_SUCCESS;
990c73c039SSebastian Grimberg }
1000c73c039SSebastian Grimberg 
1017a982d89SJeremy L. Thompson /// @}
1027a982d89SJeremy L. Thompson 
1037a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1047a982d89SJeremy L. Thompson /// CeedElemRestriction Backend API
1057a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1067a982d89SJeremy L. Thompson /// @addtogroup CeedElemRestrictionBackend
1077a982d89SJeremy L. Thompson /// @{
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson /**
110fcbe8c06SSebastian Grimberg   @brief Get the type of a CeedElemRestriction
111a681ae63Sjeremylt 
112fcbe8c06SSebastian Grimberg   @param[in]  rstr      CeedElemRestriction
113fcbe8c06SSebastian Grimberg   @param[out] rstr_type Variable to store restriction type
114fcbe8c06SSebastian Grimberg 
115fcbe8c06SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
116fcbe8c06SSebastian Grimberg 
117fcbe8c06SSebastian Grimberg   @ref Backend
118fcbe8c06SSebastian Grimberg **/
119fcbe8c06SSebastian Grimberg int CeedElemRestrictionGetType(CeedElemRestriction rstr, CeedRestrictionType *rstr_type) {
120fcbe8c06SSebastian Grimberg   *rstr_type = rstr->rstr_type;
121fcbe8c06SSebastian Grimberg   return CEED_ERROR_SUCCESS;
122fcbe8c06SSebastian Grimberg }
123fcbe8c06SSebastian Grimberg 
124fcbe8c06SSebastian Grimberg /**
125fcbe8c06SSebastian Grimberg   @brief Get the strided status of a CeedElemRestriction
126fcbe8c06SSebastian Grimberg 
127fcbe8c06SSebastian Grimberg   @param[in]  rstr       CeedElemRestriction
128fcbe8c06SSebastian Grimberg   @param[out] is_strided Variable to store strided status, 1 if strided else 0
129fcbe8c06SSebastian Grimberg **/
130fcbe8c06SSebastian Grimberg int CeedElemRestrictionIsStrided(CeedElemRestriction rstr, bool *is_strided) {
131fcbe8c06SSebastian Grimberg   *is_strided = (rstr->rstr_type == CEED_RESTRICTION_STRIDED);
132fcbe8c06SSebastian Grimberg   return CEED_ERROR_SUCCESS;
133fcbe8c06SSebastian Grimberg }
134fcbe8c06SSebastian Grimberg 
135fcbe8c06SSebastian Grimberg /**
1363ac8f562SJeremy L Thompson   @brief Get the points status of a CeedElemRestriction
1373ac8f562SJeremy L Thompson 
1383ac8f562SJeremy L Thompson   @param[in]  rstr      CeedElemRestriction
1393ac8f562SJeremy L Thompson   @param[out] is_points Variable to store points status, 1 if points else 0
1403ac8f562SJeremy L Thompson **/
1413ac8f562SJeremy L Thompson int CeedElemRestrictionIsPoints(CeedElemRestriction rstr, bool *is_points) {
1423ac8f562SJeremy L Thompson   *is_points = (rstr->rstr_type == CEED_RESTRICTION_POINTS);
1433ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1443ac8f562SJeremy L Thompson }
1453ac8f562SJeremy L Thompson 
1463ac8f562SJeremy L Thompson /**
147a681ae63Sjeremylt   @brief Get the strides of a strided CeedElemRestriction
1487a982d89SJeremy L. Thompson 
149ea61e9acSJeremy L Thompson   @param[in]  rstr    CeedElemRestriction
150a681ae63Sjeremylt   @param[out] strides Variable to store strides array
1517a982d89SJeremy L. Thompson 
1527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1537a982d89SJeremy L. Thompson 
1547a982d89SJeremy L. Thompson   @ref Backend
1557a982d89SJeremy L. Thompson **/
1562b730f8bSJeremy L Thompson int CeedElemRestrictionGetStrides(CeedElemRestriction rstr, CeedInt (*strides)[3]) {
1576574a04fSJeremy L Thompson   CeedCheck(rstr->strides, rstr->ceed, CEED_ERROR_MINOR, "ElemRestriction has no stride data");
1582b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*strides)[i] = rstr->strides[i];
159e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1607a982d89SJeremy L. Thompson }
1617a982d89SJeremy L. Thompson 
1627a982d89SJeremy L. Thompson /**
16377d1c127SSebastian Grimberg   @brief Get the backend stride status of a CeedElemRestriction
16477d1c127SSebastian Grimberg 
16577d1c127SSebastian Grimberg   @param[in]  rstr                 CeedElemRestriction
16677d1c127SSebastian Grimberg   @param[out] has_backend_strides  Variable to store stride status
16777d1c127SSebastian Grimberg 
16877d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
16977d1c127SSebastian Grimberg 
17077d1c127SSebastian Grimberg   @ref Backend
17177d1c127SSebastian Grimberg **/
17277d1c127SSebastian Grimberg int CeedElemRestrictionHasBackendStrides(CeedElemRestriction rstr, bool *has_backend_strides) {
17377d1c127SSebastian Grimberg   CeedCheck(rstr->strides, rstr->ceed, CEED_ERROR_MINOR, "ElemRestriction has no stride data");
17477d1c127SSebastian Grimberg   *has_backend_strides = ((rstr->strides[0] == CEED_STRIDES_BACKEND[0]) && (rstr->strides[1] == CEED_STRIDES_BACKEND[1]) &&
17577d1c127SSebastian Grimberg                           (rstr->strides[2] == CEED_STRIDES_BACKEND[2]));
17677d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17777d1c127SSebastian Grimberg }
17877d1c127SSebastian Grimberg 
17977d1c127SSebastian Grimberg /**
180bd33150aSjeremylt   @brief Get read-only access to a CeedElemRestriction offsets array by memtype
181bd33150aSjeremylt 
182ea61e9acSJeremy L Thompson   @param[in]  rstr     CeedElemRestriction to retrieve offsets
183fcbe8c06SSebastian Grimberg   @param[in]  mem_type Memory type on which to access the array.
184fcbe8c06SSebastian Grimberg                          If the backend uses a different memory type, this will perform a copy (possibly cached).
185d1d35e2fSjeremylt   @param[out] offsets  Array on memory type mem_type
186bd33150aSjeremylt 
187bd33150aSjeremylt   @return An error code: 0 - success, otherwise - failure
188bd33150aSjeremylt 
189bd33150aSjeremylt   @ref User
190bd33150aSjeremylt **/
1912b730f8bSJeremy L Thompson int CeedElemRestrictionGetOffsets(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
1927c1dbaffSSebastian Grimberg   if (rstr->rstr_base) {
1937c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(rstr->rstr_base, mem_type, offsets));
194c17ec2beSJeremy L Thompson   } else {
1956574a04fSJeremy L Thompson     CeedCheck(rstr->GetOffsets, rstr->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetOffsets");
1962b730f8bSJeremy L Thompson     CeedCall(rstr->GetOffsets(rstr, mem_type, offsets));
197d1d35e2fSjeremylt     rstr->num_readers++;
198c17ec2beSJeremy L Thompson   }
199e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
200430758c8SJeremy L Thompson }
201430758c8SJeremy L Thompson 
202430758c8SJeremy L Thompson /**
203430758c8SJeremy L Thompson   @brief Restore an offsets array obtained using CeedElemRestrictionGetOffsets()
204430758c8SJeremy L Thompson 
205ea61e9acSJeremy L Thompson   @param[in] rstr    CeedElemRestriction to restore
206ea61e9acSJeremy L Thompson   @param[in] offsets Array of offset data
207430758c8SJeremy L Thompson 
208430758c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
209430758c8SJeremy L Thompson 
210430758c8SJeremy L Thompson   @ref User
211430758c8SJeremy L Thompson **/
2122b730f8bSJeremy L Thompson int CeedElemRestrictionRestoreOffsets(CeedElemRestriction rstr, const CeedInt **offsets) {
2137c1dbaffSSebastian Grimberg   if (rstr->rstr_base) {
2147c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(rstr->rstr_base, offsets));
215c17ec2beSJeremy L Thompson   } else {
216430758c8SJeremy L Thompson     *offsets = NULL;
217d1d35e2fSjeremylt     rstr->num_readers--;
218c17ec2beSJeremy L Thompson   }
219e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
220bd33150aSjeremylt }
221bd33150aSjeremylt 
222bd33150aSjeremylt /**
22377d1c127SSebastian Grimberg   @brief Get read-only access to a CeedElemRestriction orientations array by memtype
2243ac43b2cSJeremy L Thompson 
22577d1c127SSebastian Grimberg   @param[in]  rstr     CeedElemRestriction to retrieve orientations
226fcbe8c06SSebastian Grimberg   @param[in]  mem_type Memory type on which to access the array.
227fcbe8c06SSebastian Grimberg                          If the backend uses a different memory type, this will perform a copy (possibly cached).
22877d1c127SSebastian Grimberg   @param[out] orients  Array on memory type mem_type
2293ac43b2cSJeremy L Thompson 
2303ac43b2cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2313ac43b2cSJeremy L Thompson 
23277d1c127SSebastian Grimberg   @ref User
2333ac43b2cSJeremy L Thompson **/
23477d1c127SSebastian Grimberg int CeedElemRestrictionGetOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
235fcbe8c06SSebastian Grimberg   CeedCheck(rstr->GetOrientations, rstr->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetOrientations");
23677d1c127SSebastian Grimberg   CeedCall(rstr->GetOrientations(rstr, mem_type, orients));
23777d1c127SSebastian Grimberg   rstr->num_readers++;
238e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2393ac43b2cSJeremy L Thompson }
2403ac43b2cSJeremy L Thompson 
2413ac43b2cSJeremy L Thompson /**
24277d1c127SSebastian Grimberg   @brief Restore an orientations array obtained using CeedElemRestrictionGetOrientations()
243b435c5a6Srezgarshakeri 
24477d1c127SSebastian Grimberg   @param[in] rstr    CeedElemRestriction to restore
24577d1c127SSebastian Grimberg   @param[in] orients Array of orientation data
246b435c5a6Srezgarshakeri 
247b435c5a6Srezgarshakeri   @return An error code: 0 - success, otherwise - failure
248b435c5a6Srezgarshakeri 
24977d1c127SSebastian Grimberg   @ref User
250b435c5a6Srezgarshakeri **/
25177d1c127SSebastian Grimberg int CeedElemRestrictionRestoreOrientations(CeedElemRestriction rstr, const bool **orients) {
25277d1c127SSebastian Grimberg   *orients = NULL;
25377d1c127SSebastian Grimberg   rstr->num_readers--;
254b435c5a6Srezgarshakeri   return CEED_ERROR_SUCCESS;
255b435c5a6Srezgarshakeri }
256b435c5a6Srezgarshakeri 
257b435c5a6Srezgarshakeri /**
25877d1c127SSebastian Grimberg   @brief Get read-only access to a CeedElemRestriction curl-conforming orientations array by memtype
2597a982d89SJeremy L. Thompson 
26077d1c127SSebastian Grimberg   @param[in]  rstr         CeedElemRestriction to retrieve curl-conforming orientations
261fcbe8c06SSebastian Grimberg   @param[in]  mem_type     Memory type on which to access the array.
262fcbe8c06SSebastian Grimberg                              If the backend uses a different memory type, this will perform a copy (possibly cached).
26377d1c127SSebastian Grimberg   @param[out] curl_orients Array on memory type mem_type
2647a982d89SJeremy L. Thompson 
2657a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2667a982d89SJeremy L. Thompson 
26777d1c127SSebastian Grimberg   @ref User
2687a982d89SJeremy L. Thompson **/
2690c73c039SSebastian Grimberg int CeedElemRestrictionGetCurlOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
270fcbe8c06SSebastian Grimberg   CeedCheck(rstr->GetCurlOrientations, rstr->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetCurlOrientations");
27177d1c127SSebastian Grimberg   CeedCall(rstr->GetCurlOrientations(rstr, mem_type, curl_orients));
27277d1c127SSebastian Grimberg   rstr->num_readers++;
27377d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
27477d1c127SSebastian Grimberg }
27577d1c127SSebastian Grimberg 
27677d1c127SSebastian Grimberg /**
27777d1c127SSebastian Grimberg   @brief Restore an orientations array obtained using CeedElemRestrictionGetCurlOrientations()
27877d1c127SSebastian Grimberg 
27977d1c127SSebastian Grimberg   @param[in] rstr         CeedElemRestriction to restore
28077d1c127SSebastian Grimberg   @param[in] curl_orients Array of orientation data
28177d1c127SSebastian Grimberg 
28277d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
28377d1c127SSebastian Grimberg 
28477d1c127SSebastian Grimberg   @ref User
28577d1c127SSebastian Grimberg **/
2860c73c039SSebastian Grimberg int CeedElemRestrictionRestoreCurlOrientations(CeedElemRestriction rstr, const CeedInt8 **curl_orients) {
28777d1c127SSebastian Grimberg   *curl_orients = NULL;
28877d1c127SSebastian Grimberg   rstr->num_readers--;
289e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2907a982d89SJeremy L. Thompson }
2917a982d89SJeremy L. Thompson 
2927a982d89SJeremy L. Thompson /**
29349fd234cSJeremy L Thompson 
2943ac8f562SJeremy L Thompson   @brief Get the number of points in an element of a points CeedElemRestriction
2953ac8f562SJeremy L Thompson 
2963ac8f562SJeremy L Thompson   @param[in]  rstr       CeedElemRestriction
2973ac8f562SJeremy L Thompson   @param[in]  elem       Index number of element to retrieve the number of points for
2983ac8f562SJeremy L Thompson   @param[out] num_points The number of points in the element at index elem
2993ac8f562SJeremy L Thompson 
3003ac8f562SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3013ac8f562SJeremy L Thompson 
3023ac8f562SJeremy L Thompson   @ref Backend
3033ac8f562SJeremy L Thompson **/
3043ac8f562SJeremy L Thompson int CeedElemRestrictionGetNumPointsInElement(CeedElemRestriction rstr, CeedInt elem, CeedInt *num_points) {
3053ac8f562SJeremy L Thompson   Ceed           ceed;
3063ac8f562SJeremy L Thompson   const CeedInt *offsets;
3073ac8f562SJeremy L Thompson 
3083ac8f562SJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
3093ac8f562SJeremy L Thompson   CeedCheck(rstr->rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_INCOMPATIBLE,
3103ac8f562SJeremy L Thompson             "Can only retrieve the number of points for a points CeedElemRestriction");
3113ac8f562SJeremy L Thompson 
3123ac8f562SJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
3133ac8f562SJeremy L Thompson   *num_points = offsets[elem - 1] - offsets[elem];
3143ac8f562SJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
3153ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3163ac8f562SJeremy L Thompson }
3173ac8f562SJeremy L Thompson 
3183ac8f562SJeremy L Thompson /**
3193ac8f562SJeremy L Thompson 
32049fd234cSJeremy L Thompson   @brief Get the E-vector layout of a CeedElemRestriction
32149fd234cSJeremy L Thompson 
322ea61e9acSJeremy L Thompson   @param[in]  rstr    CeedElemRestriction
323fcbe8c06SSebastian Grimberg   @param[out] layout  Variable to store layout array, stored as [nodes, components, elements].
324fcbe8c06SSebastian Grimberg                         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]
32549fd234cSJeremy L Thompson 
32649fd234cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
32749fd234cSJeremy L Thompson 
32849fd234cSJeremy L Thompson   @ref Backend
32949fd234cSJeremy L Thompson **/
3302b730f8bSJeremy L Thompson int CeedElemRestrictionGetELayout(CeedElemRestriction rstr, CeedInt (*layout)[3]) {
3316574a04fSJeremy L Thompson   CeedCheck(rstr->layout[0], rstr->ceed, CEED_ERROR_MINOR, "ElemRestriction has no layout data");
3322b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*layout)[i] = rstr->layout[i];
333e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
33449fd234cSJeremy L Thompson }
33549fd234cSJeremy L Thompson 
33649fd234cSJeremy L Thompson /**
33749fd234cSJeremy L Thompson 
33849fd234cSJeremy L Thompson   @brief Set the E-vector layout of a CeedElemRestriction
33949fd234cSJeremy L Thompson 
340ea61e9acSJeremy L Thompson   @param[in] rstr   CeedElemRestriction
341ea61e9acSJeremy L Thompson   @param[in] layout Variable to containing layout array, stored as [nodes, components, elements].
342ea61e9acSJeremy L Thompson                       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]
34349fd234cSJeremy L Thompson 
34449fd234cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
34549fd234cSJeremy L Thompson 
34649fd234cSJeremy L Thompson   @ref Backend
34749fd234cSJeremy L Thompson **/
3482b730f8bSJeremy L Thompson int CeedElemRestrictionSetELayout(CeedElemRestriction rstr, CeedInt layout[3]) {
3492b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) rstr->layout[i] = layout[i];
350e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
35149fd234cSJeremy L Thompson }
35249fd234cSJeremy L Thompson 
35349fd234cSJeremy L Thompson /**
3547a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedElemRestriction
3557a982d89SJeremy L. Thompson 
356ea61e9acSJeremy L Thompson   @param[in]  rstr CeedElemRestriction
3577a982d89SJeremy L. Thompson   @param[out] data Variable to store data
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3607a982d89SJeremy L. Thompson 
3617a982d89SJeremy L. Thompson   @ref Backend
3627a982d89SJeremy L. Thompson **/
363777ff853SJeremy L Thompson int CeedElemRestrictionGetData(CeedElemRestriction rstr, void *data) {
364777ff853SJeremy L Thompson   *(void **)data = rstr->data;
365e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3667a982d89SJeremy L. Thompson }
3677a982d89SJeremy L. Thompson 
3687a982d89SJeremy L. Thompson /**
3697a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedElemRestriction
3707a982d89SJeremy L. Thompson 
371ea61e9acSJeremy L Thompson   @param[in,out] rstr CeedElemRestriction
372ea61e9acSJeremy L Thompson   @param[in]     data Data to set
3737a982d89SJeremy L. Thompson 
3747a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3757a982d89SJeremy L. Thompson 
3767a982d89SJeremy L. Thompson   @ref Backend
3777a982d89SJeremy L. Thompson **/
378777ff853SJeremy L Thompson int CeedElemRestrictionSetData(CeedElemRestriction rstr, void *data) {
379777ff853SJeremy L Thompson   rstr->data = data;
380e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3817a982d89SJeremy L. Thompson }
3827a982d89SJeremy L. Thompson 
38334359f16Sjeremylt /**
38434359f16Sjeremylt   @brief Increment the reference counter for a CeedElemRestriction
38534359f16Sjeremylt 
386ea61e9acSJeremy L Thompson   @param[in,out] rstr ElemRestriction to increment the reference counter
38734359f16Sjeremylt 
38834359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
38934359f16Sjeremylt 
39034359f16Sjeremylt   @ref Backend
39134359f16Sjeremylt **/
3929560d06aSjeremylt int CeedElemRestrictionReference(CeedElemRestriction rstr) {
39334359f16Sjeremylt   rstr->ref_count++;
39434359f16Sjeremylt   return CEED_ERROR_SUCCESS;
39534359f16Sjeremylt }
39634359f16Sjeremylt 
3976e15d496SJeremy L Thompson /**
3986e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedElemRestriction in t_mode
3996e15d496SJeremy L Thompson 
400ea61e9acSJeremy L Thompson   @param[in]  rstr   ElemRestriction to estimate FLOPs for
401ea61e9acSJeremy L Thompson   @param[in]  t_mode Apply restriction or transpose
402ea61e9acSJeremy L Thompson   @param[out] flops  Address of variable to hold FLOPs estimate
4036e15d496SJeremy L Thompson 
4046e15d496SJeremy L Thompson   @ref Backend
4056e15d496SJeremy L Thompson **/
4062b730f8bSJeremy L Thompson int CeedElemRestrictionGetFlopsEstimate(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedSize *flops) {
407e7f679fcSJeremy L Thompson   CeedInt             e_size = rstr->num_block * rstr->block_size * rstr->elem_size * rstr->num_comp, scale = 0;
40889edb9e3SSebastian Grimberg   CeedRestrictionType rstr_type;
4091c66c397SJeremy L Thompson 
41089edb9e3SSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
4113ac8f562SJeremy L Thompson   if (rstr_type == CEED_RESTRICTION_POINTS) e_size = rstr->num_points * rstr->num_comp;
41277d1c127SSebastian Grimberg   if (t_mode == CEED_TRANSPOSE) {
41389edb9e3SSebastian Grimberg     switch (rstr_type) {
4143ac8f562SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4153ac8f562SJeremy L Thompson         scale = 0;
4163ac8f562SJeremy L Thompson         break;
41789edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
41889edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
41977d1c127SSebastian Grimberg         scale = 1;
42089edb9e3SSebastian Grimberg         break;
42189edb9e3SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
42277d1c127SSebastian Grimberg         scale = 2;
42389edb9e3SSebastian Grimberg         break;
42489edb9e3SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
42577d1c127SSebastian Grimberg         scale = 6;
42689edb9e3SSebastian Grimberg         break;
4276e15d496SJeremy L Thompson     }
42877d1c127SSebastian Grimberg   } else {
42989edb9e3SSebastian Grimberg     switch (rstr_type) {
43089edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
43189edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
4323ac8f562SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
43377d1c127SSebastian Grimberg         scale = 0;
43489edb9e3SSebastian Grimberg         break;
43589edb9e3SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
43677d1c127SSebastian Grimberg         scale = 1;
43789edb9e3SSebastian Grimberg         break;
43889edb9e3SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
43977d1c127SSebastian Grimberg         scale = 5;
44089edb9e3SSebastian Grimberg         break;
44177d1c127SSebastian Grimberg     }
44277d1c127SSebastian Grimberg   }
4436e15d496SJeremy L Thompson   *flops = e_size * scale;
4446e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4456e15d496SJeremy L Thompson }
4466e15d496SJeremy L Thompson 
4477a982d89SJeremy L. Thompson /// @}
4487a982d89SJeremy L. Thompson 
44915910d16Sjeremylt /// @cond DOXYGEN_SKIP
45015910d16Sjeremylt static struct CeedElemRestriction_private ceed_elemrestriction_none;
45115910d16Sjeremylt /// @endcond
45215910d16Sjeremylt 
4537a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4547a982d89SJeremy L. Thompson /// CeedElemRestriction Public API
4557a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4567a982d89SJeremy L. Thompson /// @addtogroup CeedElemRestrictionUser
457d7b241e6Sjeremylt /// @{
458d7b241e6Sjeremylt 
4597a982d89SJeremy L. Thompson /// Indicate that the stride is determined by the backend
46045f1e315Sjeremylt const CeedInt CEED_STRIDES_BACKEND[3] = {0};
4617a982d89SJeremy L. Thompson 
462356036faSJeremy L Thompson /// Argument for CeedOperatorSetField indicating that the field does not requre a CeedElemRestriction
4632b730f8bSJeremy L Thompson const CeedElemRestriction CEED_ELEMRESTRICTION_NONE = &ceed_elemrestriction_none;
4647a982d89SJeremy L. Thompson 
465d7b241e6Sjeremylt /**
466b11c1e72Sjeremylt   @brief Create a CeedElemRestriction
467d7b241e6Sjeremylt 
468ea61e9acSJeremy L Thompson   @param[in]  ceed        Ceed object where the CeedElemRestriction will be created
469ea61e9acSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the @a offsets array
470ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
471ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
472fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
473fcbe8c06SSebastian Grimberg                             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.
474fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
475fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
476ea61e9acSJeremy L Thompson   @param[in]  mem_type    Memory type of the @a offsets array, see CeedMemType
477ea61e9acSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the @a offsets array, see CeedCopyMode
478fcbe8c06SSebastian Grimberg   @param[in]  offsets     Array of shape [@a num_elem, @a elem_size].
479fcbe8c06SSebastian Grimberg                             Row i holds the ordered list of the offsets (into the input CeedVector) for the unknowns corresponding to element i, where
480fcbe8c06SSebastian Grimberg 0 <= i < @a num_elem. All offsets must be in the range [0, @a l_size - 1].
481ea61e9acSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created CeedElemRestriction will be stored
482d7b241e6Sjeremylt 
483b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
484dfdf5a53Sjeremylt 
4857a982d89SJeremy L. Thompson   @ref User
486b11c1e72Sjeremylt **/
4872b730f8bSJeremy L Thompson int CeedElemRestrictionCreate(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
4882b730f8bSJeremy L Thompson                               CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
4895fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreate) {
4905fe0d4faSjeremylt     Ceed delegate;
4916574a04fSJeremy L Thompson 
4922b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
49377d1c127SSebastian Grimberg     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreate");
4942b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreate(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, rstr));
495e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4965fe0d4faSjeremylt   }
4975fe0d4faSjeremylt 
498e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
4996574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
5006574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
5016574a04fSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction component stride must be at least 1");
502e022e1f8SJeremy L Thompson 
5032b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
504db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
505d1d35e2fSjeremylt   (*rstr)->ref_count   = 1;
506d1d35e2fSjeremylt   (*rstr)->num_elem    = num_elem;
507d1d35e2fSjeremylt   (*rstr)->elem_size   = elem_size;
508d1d35e2fSjeremylt   (*rstr)->num_comp    = num_comp;
509d1d35e2fSjeremylt   (*rstr)->comp_stride = comp_stride;
510d1d35e2fSjeremylt   (*rstr)->l_size      = l_size;
511e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
512e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
51361a27d74SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
514fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
515e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
516d7b241e6Sjeremylt }
517d7b241e6Sjeremylt 
518d7b241e6Sjeremylt /**
51977d1c127SSebastian Grimberg   @brief Create a CeedElemRestriction with orientation signs
520fc0567d9Srezgarshakeri 
521ea61e9acSJeremy L Thompson   @param[in]  ceed        Ceed object where the CeedElemRestriction will be created
522ea61e9acSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the @a offsets array
523ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
524ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
525fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
526fcbe8c06SSebastian Grimberg                             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.
527fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
528fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
529ea61e9acSJeremy L Thompson   @param[in]  mem_type    Memory type of the @a offsets array, see CeedMemType
530ea61e9acSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the @a offsets array, see CeedCopyMode
531fcbe8c06SSebastian Grimberg   @param[in]  offsets     Array of shape [@a num_elem, @a elem_size].
532fcbe8c06SSebastian Grimberg                             Row i holds the ordered list of the offsets (into the input CeedVector) for the unknowns corresponding to element i, where
533fcbe8c06SSebastian Grimberg 0 <= i < @a num_elem. All offsets must be in the range [0, @a l_size - 1].
53477d1c127SSebastian Grimberg   @param[in]  orients     Array of shape [@a num_elem, @a elem_size] with bool false for positively oriented and true to flip the orientation.
535ea61e9acSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created CeedElemRestriction will be stored
536fc0567d9Srezgarshakeri 
537fc0567d9Srezgarshakeri   @return An error code: 0 - success, otherwise - failure
538fc0567d9Srezgarshakeri 
539fc0567d9Srezgarshakeri   @ref User
540fc0567d9Srezgarshakeri **/
5412b730f8bSJeremy L Thompson int CeedElemRestrictionCreateOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
54277d1c127SSebastian Grimberg                                       CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
543fc0567d9Srezgarshakeri                                       CeedElemRestriction *rstr) {
544fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreate) {
545fc0567d9Srezgarshakeri     Ceed delegate;
5466574a04fSJeremy L Thompson 
5472b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
548fcbe8c06SSebastian Grimberg     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreate");
5492b730f8bSJeremy L Thompson     CeedCall(
55077d1c127SSebastian Grimberg         CeedElemRestrictionCreateOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, orients, rstr));
551fc0567d9Srezgarshakeri     return CEED_ERROR_SUCCESS;
552fc0567d9Srezgarshakeri   }
553fc0567d9Srezgarshakeri 
554e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
5556574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
5566574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
5576574a04fSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction component stride must be at least 1");
558e022e1f8SJeremy L Thompson 
5592b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
560db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
561fc0567d9Srezgarshakeri   (*rstr)->ref_count   = 1;
562fc0567d9Srezgarshakeri   (*rstr)->num_elem    = num_elem;
563fc0567d9Srezgarshakeri   (*rstr)->elem_size   = elem_size;
564fc0567d9Srezgarshakeri   (*rstr)->num_comp    = num_comp;
565fc0567d9Srezgarshakeri   (*rstr)->comp_stride = comp_stride;
566fc0567d9Srezgarshakeri   (*rstr)->l_size      = l_size;
567e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
568e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
569fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
570fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, orients, NULL, *rstr));
57177d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
57277d1c127SSebastian Grimberg }
57377d1c127SSebastian Grimberg 
57477d1c127SSebastian Grimberg /**
57577d1c127SSebastian Grimberg   @brief Create a CeedElemRestriction with a general tridiagonal transformation matrix for curl-conforming elements
57677d1c127SSebastian Grimberg 
57777d1c127SSebastian Grimberg   @param[in]  ceed         Ceed object where the CeedElemRestriction will be created
57877d1c127SSebastian Grimberg   @param[in]  num_elem     Number of elements described in the @a offsets array
57977d1c127SSebastian Grimberg   @param[in]  elem_size    Size (number of "nodes") per element
58077d1c127SSebastian Grimberg   @param[in]  num_comp     Number of field components per interpolation node (1 for scalar fields)
581fcbe8c06SSebastian Grimberg   @param[in]  comp_stride  Stride between components for the same L-vector "node".
582fcbe8c06SSebastian Grimberg                              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.
583fcbe8c06SSebastian Grimberg   @param[in]  l_size       The size of the L-vector.
584fcbe8c06SSebastian Grimberg                              This vector may be larger than the elements and fields given by this restriction.
58577d1c127SSebastian Grimberg   @param[in]  mem_type     Memory type of the @a offsets array, see CeedMemType
58677d1c127SSebastian Grimberg   @param[in]  copy_mode    Copy mode for the @a offsets array, see CeedCopyMode
587fcbe8c06SSebastian Grimberg   @param[in]  offsets      Array of shape [@a num_elem, @a elem_size].
588fcbe8c06SSebastian Grimberg                              Row i holds the ordered list of the offsets (into the input CeedVector) for the unknowns corresponding to element i,
589fcbe8c06SSebastian Grimberg where 0 <= i < @a num_elem. All offsets must be in the range [0, @a l_size - 1].
5907c1dbaffSSebastian Grimberg   @param[in]  curl_orients Array of shape [@a num_elem, @a 3 * elem_size] representing a row-major tridiagonal matrix (curl_orients[i * 3 * elem_size]
5917c1dbaffSSebastian Grimberg = curl_orients[(i + 1) * 3 * elem_size - 1] = 0, where 0 <= i < @a num_elem) which is applied to the element unknowns upon restriction. This
5927c1dbaffSSebastian Grimberg orientation matrix allows for pairs of face degrees of freedom on elements for H(curl) spaces to be coupled in the element restriction operation,
5937c1dbaffSSebastian Grimberg which is a way to resolve face orientation issues for 3D meshes (https://dl.acm.org/doi/pdf/10.1145/3524456).
59477d1c127SSebastian Grimberg   @param[out] rstr         Address of the variable where the newly created CeedElemRestriction will be stored
59577d1c127SSebastian Grimberg 
59677d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
59777d1c127SSebastian Grimberg 
59877d1c127SSebastian Grimberg   @ref User
59977d1c127SSebastian Grimberg **/
60077d1c127SSebastian Grimberg int CeedElemRestrictionCreateCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
6010c73c039SSebastian Grimberg                                           CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const CeedInt8 *curl_orients,
60277d1c127SSebastian Grimberg                                           CeedElemRestriction *rstr) {
603fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreate) {
60477d1c127SSebastian Grimberg     Ceed delegate;
60577d1c127SSebastian Grimberg 
60677d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
607fcbe8c06SSebastian Grimberg     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreate");
60877d1c127SSebastian Grimberg     CeedCall(CeedElemRestrictionCreateCurlOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
60977d1c127SSebastian Grimberg                                                    curl_orients, rstr));
61077d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
61177d1c127SSebastian Grimberg   }
61277d1c127SSebastian Grimberg 
613e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
61477d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
61577d1c127SSebastian Grimberg   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
61677d1c127SSebastian Grimberg   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction component stride must be at least 1");
61777d1c127SSebastian Grimberg 
61877d1c127SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
619fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
62077d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
62177d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
62277d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
62377d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
62477d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
62577d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
626e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
627e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
628fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
629fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, curl_orients, *rstr));
630fc0567d9Srezgarshakeri   return CEED_ERROR_SUCCESS;
631fc0567d9Srezgarshakeri }
632fc0567d9Srezgarshakeri 
633fc0567d9Srezgarshakeri /**
6347509a596Sjeremylt   @brief Create a strided CeedElemRestriction
635d7b241e6Sjeremylt 
636ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed object where the CeedElemRestriction will be created
637ea61e9acSJeremy L Thompson   @param[in]  num_elem  Number of elements described by the restriction
638ea61e9acSJeremy L Thompson   @param[in]  elem_size Size (number of "nodes") per element
639ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components per interpolation "node" (1 for scalar fields)
640fcbe8c06SSebastian Grimberg   @param[in]  l_size    The size of the L-vector.
641fcbe8c06SSebastian Grimberg                           This vector may be larger than the elements and fields given by this restriction.
642fcbe8c06SSebastian Grimberg   @param[in]  strides   Array for strides between [nodes, components, elements].
643fcbe8c06SSebastian Grimberg                           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].
644fcbe8c06SSebastian Grimberg                           @a CEED_STRIDES_BACKEND may be used with vectors created by a Ceed backend.
645ea61e9acSJeremy L Thompson   @param[out] rstr      Address of the variable where the newly created CeedElemRestriction will be stored
646d7b241e6Sjeremylt 
647b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
648dfdf5a53Sjeremylt 
6497a982d89SJeremy L. Thompson   @ref User
650b11c1e72Sjeremylt **/
6512b730f8bSJeremy L Thompson int CeedElemRestrictionCreateStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedSize l_size, const CeedInt strides[3],
652f90c8643Sjeremylt                                      CeedElemRestriction *rstr) {
6535fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreate) {
6545fe0d4faSjeremylt     Ceed delegate;
655b04eb3d9SSebastian Grimberg 
6562b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
657fcbe8c06SSebastian Grimberg     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreate");
6582b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateStrided(delegate, num_elem, elem_size, num_comp, l_size, strides, rstr));
659e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6605fe0d4faSjeremylt   }
6615fe0d4faSjeremylt 
662e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
6636574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
6646574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
665e7f679fcSJeremy L Thompson   CeedCheck(l_size >= num_elem * elem_size * num_comp, ceed, CEED_ERROR_DIMENSION, "L-vector size must be at least num_elem * elem_size * num_comp");
666e022e1f8SJeremy L Thompson 
6672b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
668db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
669d1d35e2fSjeremylt   (*rstr)->ref_count  = 1;
670d1d35e2fSjeremylt   (*rstr)->num_elem   = num_elem;
671d1d35e2fSjeremylt   (*rstr)->elem_size  = elem_size;
672d1d35e2fSjeremylt   (*rstr)->num_comp   = num_comp;
673d1d35e2fSjeremylt   (*rstr)->l_size     = l_size;
674e7f679fcSJeremy L Thompson   (*rstr)->num_block  = num_elem;
675e7f679fcSJeremy L Thompson   (*rstr)->block_size = 1;
676fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
6772b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(3, &(*rstr)->strides));
6782b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
679fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
680e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
681d7b241e6Sjeremylt }
682d7b241e6Sjeremylt 
683d7b241e6Sjeremylt /**
6843ac8f562SJeremy L Thompson   @brief Create a points CeedElemRestriction, for restricting for restricting from a all local points to the current element in which they are
6853ac8f562SJeremy L Thompson  located.
6863ac8f562SJeremy L Thompson 
6873ac8f562SJeremy L Thompson   The offsets array is arranged as
6883ac8f562SJeremy L Thompson 
6893ac8f562SJeremy L Thompson   element_0_start_index
6903ac8f562SJeremy L Thompson   element_1_start_index
6913ac8f562SJeremy L Thompson   ...
6923ac8f562SJeremy L Thompson   element_n_start_index
6933ac8f562SJeremy L Thompson   element_n_stop_index
6943ac8f562SJeremy L Thompson   element_0_point_0
6953ac8f562SJeremy L Thompson   element_0_point_1
6963ac8f562SJeremy L Thompson   ...
6973ac8f562SJeremy L Thompson 
6983ac8f562SJeremy L Thompson   @param[in]  ceed          Ceed object where the CeedElemRestriction will be created
6993ac8f562SJeremy L Thompson   @param[in]  num_elem      Number of elements described in the @a offsets array
7003ac8f562SJeremy L Thompson   @param[in]  num_points    Number of points described in the @a offsets array
7013ac8f562SJeremy L Thompson   @param[in]  num_comp      Number of field components per interpolation node (1 for scalar fields).
7023ac8f562SJeremy L Thompson                               Components are assumed to be contiguous by point.
7033ac8f562SJeremy L Thompson   @param[in]  l_size        The size of the L-vector.
7043ac8f562SJeremy L Thompson                               This vector may be larger than the elements and fields given by this restriction.
7053ac8f562SJeremy L Thompson   @param[in]  mem_type      Memory type of the @a offsets array, see CeedMemType
7063ac8f562SJeremy L Thompson   @param[in]  copy_mode     Copy mode for the @a offsets array, see CeedCopyMode
7073ac8f562SJeremy L Thompson   @param[in]  offsets       Array of size num_elem + 1 + num_points.
7083ac8f562SJeremy L Thompson                               The first portion of the offsets array holds the ranges of indices corresponding to each element.
7093ac8f562SJeremy L Thompson                               The second portion holds the indices for each element.
7103ac8f562SJeremy L Thompson   @param[out] rstr          Address of the variable where the newly created CeedElemRestriction will be stored
7113ac8f562SJeremy L Thompson 
7123ac8f562SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7133ac8f562SJeremy L Thompson 
7143ac8f562SJeremy L Thompson   @ref Backend
7153ac8f562SJeremy L Thompson  **/
7163ac8f562SJeremy L Thompson int CeedElemRestrictionCreateAtPoints(Ceed ceed, CeedInt num_elem, CeedInt num_points, CeedInt num_comp, CeedSize l_size, CeedMemType mem_type,
7173ac8f562SJeremy L Thompson                                       CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
7183ac8f562SJeremy L Thompson   if (!ceed->ElemRestrictionCreateAtPoints) {
7193ac8f562SJeremy L Thompson     Ceed delegate;
7203ac8f562SJeremy L Thompson 
7213ac8f562SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
7223ac8f562SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreateAtPoints");
7233ac8f562SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateAtPoints(delegate, num_elem, num_points, num_comp, l_size, mem_type, copy_mode, offsets, rstr));
7243ac8f562SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7253ac8f562SJeremy L Thompson   }
7263ac8f562SJeremy L Thompson 
7273ac8f562SJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
7283ac8f562SJeremy L Thompson   CeedCheck(num_points >= 0, ceed, CEED_ERROR_DIMENSION, "Number of points must be non-negative");
7293ac8f562SJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
7303ac8f562SJeremy L Thompson   CeedCheck(l_size >= num_points * num_comp, ceed, CEED_ERROR_DIMENSION, "L-vector must be at least num_points * num_comp");
7313ac8f562SJeremy L Thompson 
7323ac8f562SJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
7333ac8f562SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
7343ac8f562SJeremy L Thompson   (*rstr)->ref_count  = 1;
7353ac8f562SJeremy L Thompson   (*rstr)->num_elem   = num_elem;
7363ac8f562SJeremy L Thompson   (*rstr)->num_points = num_points;
7373ac8f562SJeremy L Thompson   (*rstr)->num_comp   = num_comp;
7383ac8f562SJeremy L Thompson   (*rstr)->l_size     = l_size;
7393ac8f562SJeremy L Thompson   (*rstr)->block_size = 1;
7403ac8f562SJeremy L Thompson   (*rstr)->rstr_type  = CEED_RESTRICTION_POINTS;
7413ac8f562SJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateAtPoints(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
7423ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7433ac8f562SJeremy L Thompson }
7443ac8f562SJeremy L Thompson 
7453ac8f562SJeremy L Thompson /**
746b11c1e72Sjeremylt   @brief Create a blocked CeedElemRestriction, typically only called by backends
747d7b241e6Sjeremylt 
7483ac8f562SJeremy L Thompson   @param[in]  ceed          Ceed object where the CeedElemRestriction will be created
7493ac8f562SJeremy L Thompson   @param[in]  num_elem      Number of elements described in the @a offsets array
750ea61e9acSJeremy L Thompson   @param[in]  elem_size     Size (number of unknowns) per element
751e7f679fcSJeremy L Thompson   @param[in]  block_size    Number of elements in a block
752ea61e9acSJeremy L Thompson   @param[in]  num_comp      Number of field components per interpolation node (1 for scalar fields)
753fcbe8c06SSebastian Grimberg   @param[in]  comp_stride   Stride between components for the same L-vector "node".
754fcbe8c06SSebastian Grimberg                               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.
755fcbe8c06SSebastian Grimberg   @param[in]  l_size        The size of the L-vector.
756fcbe8c06SSebastian Grimberg                               This vector may be larger than the elements and fields given by this restriction.
757ea61e9acSJeremy L Thompson   @param[in]  mem_type      Memory type of the @a offsets array, see CeedMemType
758ea61e9acSJeremy L Thompson   @param[in]  copy_mode     Copy mode for the @a offsets array, see CeedCopyMode
759fcbe8c06SSebastian Grimberg   @param[in]  offsets       Array of shape [@a num_elem, @a elem_size].
760e7f679fcSJeremy L Thompson                               Row i holds the ordered list of the offsets (into the input CeedVector) for the unknowns corresponding to element i,
761e7f679fcSJeremy L Thompson  where 0 <= i < @a num_elem. All offsets must be in the range [0, @a l_size - 1]. The backend will permute and pad this array to the desired ordering
762e7f679fcSJeremy L Thompson  for the blocksize, which is typically given by the backend. The default reordering is to interlace elements.
763ea61e9acSJeremy L Thompson   @param[out] rstr          Address of the variable where the newly created CeedElemRestriction will be stored
764d7b241e6Sjeremylt 
765b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
766dfdf5a53Sjeremylt 
7677a982d89SJeremy L. Thompson   @ref Backend
768b11c1e72Sjeremylt  **/
769e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedInt comp_stride,
7702b730f8bSJeremy L Thompson                                      CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets,
7714ce2993fSjeremylt                                      CeedElemRestriction *rstr) {
7721c66c397SJeremy L Thompson   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
773d7b241e6Sjeremylt 
7745fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreateBlocked) {
7755fe0d4faSjeremylt     Ceed delegate;
7766574a04fSJeremy L Thompson 
7772b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
7786402da51SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreateBlocked");
779e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlocked(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
780e7f679fcSJeremy L Thompson                                               rstr));
781e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7825fe0d4faSjeremylt   }
783d7b241e6Sjeremylt 
784e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
7856574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
786e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
7876574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
7886574a04fSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction component stride must be at least 1");
789e022e1f8SJeremy L Thompson 
790e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
791e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
792d7b241e6Sjeremylt 
793db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
794db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
795d1d35e2fSjeremylt   (*rstr)->ref_count   = 1;
796d1d35e2fSjeremylt   (*rstr)->num_elem    = num_elem;
797d1d35e2fSjeremylt   (*rstr)->elem_size   = elem_size;
798d1d35e2fSjeremylt   (*rstr)->num_comp    = num_comp;
799d1d35e2fSjeremylt   (*rstr)->comp_stride = comp_stride;
800d1d35e2fSjeremylt   (*rstr)->l_size      = l_size;
801e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
802e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
80361a27d74SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
804e7f679fcSJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL, NULL, *rstr));
8051c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
806e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
807d7b241e6Sjeremylt }
808d7b241e6Sjeremylt 
809b11c1e72Sjeremylt /**
81077d1c127SSebastian Grimberg   @brief Create a blocked oriented CeedElemRestriction, typically only called by backends
81177d1c127SSebastian Grimberg 
81277d1c127SSebastian Grimberg   @param[in]  ceed          Ceed object where the CeedElemRestriction will be created.
81377d1c127SSebastian Grimberg   @param[in]  num_elem      Number of elements described in the @a offsets array.
81477d1c127SSebastian Grimberg   @param[in]  elem_size     Size (number of unknowns) per element
815e7f679fcSJeremy L Thompson   @param[in]  block_size    Number of elements in a block
81677d1c127SSebastian Grimberg   @param[in]  num_comp      Number of field components per interpolation node (1 for scalar fields)
817fcbe8c06SSebastian Grimberg   @param[in]  comp_stride   Stride between components for the same L-vector "node".
818fcbe8c06SSebastian Grimberg                               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.
819fcbe8c06SSebastian Grimberg   @param[in]  l_size        The size of the L-vector.
820fcbe8c06SSebastian Grimberg                               This vector may be larger than the elements and fields given by this restriction.
82177d1c127SSebastian Grimberg   @param[in]  mem_type      Memory type of the @a offsets array, see CeedMemType
82277d1c127SSebastian Grimberg   @param[in]  copy_mode     Copy mode for the @a offsets array, see CeedCopyMode
823fcbe8c06SSebastian Grimberg   @param[in]  offsets       Array of shape [@a num_elem, @a elem_size].
824fcbe8c06SSebastian Grimberg                             Row i holds the ordered list of the offsets (into the input CeedVector) for the unknowns corresponding to element i, where
825fcbe8c06SSebastian Grimberg  0 <= i < @a num_elem. All offsets must be in the range [0, @a l_size - 1]. The backend will permute and pad this array to the desired ordering for
826fcbe8c06SSebastian Grimberg  the blocksize, which is typically given by the backend. The default reordering is to interlace elements.
827fcbe8c06SSebastian Grimberg   @param[in]  orients       Array of shape [@a num_elem, @a elem_size] with bool false for positively oriented and true to flip the orientation.
828fcbe8c06SSebastian Grimberg                               Will also be permuted and padded similarly to @a offsets.
82977d1c127SSebastian Grimberg   @param[out] rstr          Address of the variable where the newly created CeedElemRestriction will be stored
83077d1c127SSebastian Grimberg 
83177d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
83277d1c127SSebastian Grimberg 
83377d1c127SSebastian Grimberg   @ref Backend
83477d1c127SSebastian Grimberg  **/
835e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
836e7f679fcSJeremy L Thompson                                              CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
837e7f679fcSJeremy L Thompson                                              const CeedInt *offsets, const bool *orients, CeedElemRestriction *rstr) {
838e7f679fcSJeremy L Thompson   bool    *block_orients;
8391c66c397SJeremy L Thompson   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
84077d1c127SSebastian Grimberg 
841fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreateBlocked) {
84277d1c127SSebastian Grimberg     Ceed delegate;
84377d1c127SSebastian Grimberg 
84477d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
845fcbe8c06SSebastian Grimberg     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreateBlocked");
846e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode,
84777d1c127SSebastian Grimberg                                                       offsets, orients, rstr));
84877d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
84977d1c127SSebastian Grimberg   }
85077d1c127SSebastian Grimberg 
85177d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
852e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
85377d1c127SSebastian Grimberg   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
85477d1c127SSebastian Grimberg   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction component stride must be at least 1");
85577d1c127SSebastian Grimberg 
856e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
857e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_orients));
858e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
859e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOrients(orients, block_orients, num_block, num_elem, block_size, elem_size));
86077d1c127SSebastian Grimberg 
861fcbe8c06SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
862fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
86377d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
86477d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
86577d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
86677d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
86777d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
86877d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
869e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
870e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
871fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
872e7f679fcSJeremy L Thompson   CeedCall(
873e7f679fcSJeremy L Thompson       ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, (const bool *)block_orients, NULL, *rstr));
8741c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
87577d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
87677d1c127SSebastian Grimberg }
87777d1c127SSebastian Grimberg 
87877d1c127SSebastian Grimberg /**
87977d1c127SSebastian Grimberg   @brief Create a blocked curl-oriented CeedElemRestriction, typically only called by backends
88077d1c127SSebastian Grimberg 
88177d1c127SSebastian Grimberg   @param[in]  ceed           Ceed object where the CeedElemRestriction will be created.
88277d1c127SSebastian Grimberg   @param[in]  num_elem       Number of elements described in the @a offsets array.
88377d1c127SSebastian Grimberg   @param[in]  elem_size      Size (number of unknowns) per element
884e7f679fcSJeremy L Thompson   @param[in]  block_size     Number of elements in a block
88577d1c127SSebastian Grimberg   @param[in]  num_comp       Number of field components per interpolation node (1 for scalar fields)
886fcbe8c06SSebastian Grimberg   @param[in]  comp_stride    Stride between components for the same L-vector "node".
887fcbe8c06SSebastian Grimberg                                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.
888fcbe8c06SSebastian Grimberg   @param[in]  l_size         The size of the L-vector.
889fcbe8c06SSebastian Grimberg                                This vector may be larger than the elements and fields given by this restriction.
89077d1c127SSebastian Grimberg   @param[in]  mem_type       Memory type of the @a offsets array, see CeedMemType
89177d1c127SSebastian Grimberg   @param[in]  copy_mode      Copy mode for the @a offsets array, see CeedCopyMode
892fcbe8c06SSebastian Grimberg   @param[in]  offsets        Array of shape [@a num_elem, @a elem_size].
893fcbe8c06SSebastian Grimberg                              Row i holds the ordered list of the offsets (into the input CeedVector) for the unknowns corresponding to element i,
894fcbe8c06SSebastian Grimberg where 0 <= i < @a num_elem. All offsets must be in the range [0, @a l_size - 1]. The backend will permute and pad this array to the desired ordering
895fcbe8c06SSebastian Grimberg for the blocksize, which is typically given by the backend. The default reordering is to interlace elements.
8967c1dbaffSSebastian Grimberg   @param[in]  curl_orients Array of shape [@a num_elem, @a 3 * elem_size] representing a row-major tridiagonal matrix (curl_orients[i * 3 * elem_size]
8977c1dbaffSSebastian Grimberg = curl_orients[(i + 1) * 3 * elem_size - 1] = 0, where 0 <= i < @a num_elem) which is applied to the element unknowns upon restriction. This
8987c1dbaffSSebastian Grimberg orientation matrix allows for pairs of face degrees of freedom on elements for H(curl) spaces to be coupled in the element restriction operation,
8997c1dbaffSSebastian Grimberg which is a way to resolve face orientation issues for 3D meshes (https://dl.acm.org/doi/pdf/10.1145/3524456). Will also be permuted and padded
9007c1dbaffSSebastian Grimberg similarly to @a offsets.
90177d1c127SSebastian Grimberg   @param[out] rstr           Address of the variable where the newly created CeedElemRestriction will be stored
90277d1c127SSebastian Grimberg 
90377d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
90477d1c127SSebastian Grimberg 
90577d1c127SSebastian Grimberg   @ref Backend
90677d1c127SSebastian Grimberg  **/
907e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
90877d1c127SSebastian Grimberg                                                  CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
9090c73c039SSebastian Grimberg                                                  const CeedInt *offsets, const CeedInt8 *curl_orients, CeedElemRestriction *rstr) {
910e7f679fcSJeremy L Thompson   CeedInt8 *block_curl_orients;
9111c66c397SJeremy L Thompson   CeedInt  *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
91277d1c127SSebastian Grimberg 
913fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreateBlocked) {
91477d1c127SSebastian Grimberg     Ceed delegate;
91577d1c127SSebastian Grimberg 
91677d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
917fcbe8c06SSebastian Grimberg     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreateBlocked");
918e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedCurlOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type,
919e7f679fcSJeremy L Thompson                                                           copy_mode, offsets, curl_orients, rstr));
92077d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
92177d1c127SSebastian Grimberg   }
92277d1c127SSebastian Grimberg 
923e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
92477d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
925e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
92677d1c127SSebastian Grimberg   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
92777d1c127SSebastian Grimberg   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction component stride must be at least 1");
92877d1c127SSebastian Grimberg 
929e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
930e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * 3 * elem_size, &block_curl_orients));
931e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
932e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadCurlOrients(curl_orients, block_curl_orients, num_block, num_elem, block_size, 3 * elem_size));
93377d1c127SSebastian Grimberg 
934fcbe8c06SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
935fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
93677d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
93777d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
93877d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
93977d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
94077d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
94177d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
942e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
943e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
944fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
945e7f679fcSJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL,
946e7f679fcSJeremy L Thompson                                               (const CeedInt8 *)block_curl_orients, *rstr));
9471c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
94877d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
94977d1c127SSebastian Grimberg }
95077d1c127SSebastian Grimberg 
95177d1c127SSebastian Grimberg /**
95277d1c127SSebastian Grimberg   @brief Create a blocked strided CeedElemRestriction, typically only called by backends
9537509a596Sjeremylt 
954ea61e9acSJeremy L Thompson   @param[in]  ceed        Ceed object where the CeedElemRestriction will be created
955ea61e9acSJeremy L Thompson   @param[in]  num_elem    Number of elements described by the restriction
956ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
957e7f679fcSJeremy L Thompson   @param[in]  block_size  Number of elements in a block
958ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
959fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
960fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
961fcbe8c06SSebastian Grimberg   @param[in]  strides     Array for strides between [nodes, components, elements].
962fcbe8c06SSebastian Grimberg                             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].
963fcbe8c06SSebastian Grimberg                             @a CEED_STRIDES_BACKEND may be used with vectors created by a Ceed backend.
964ea61e9acSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created CeedElemRestriction will be stored
9657509a596Sjeremylt 
9667509a596Sjeremylt   @return An error code: 0 - success, otherwise - failure
9677509a596Sjeremylt 
9687a982d89SJeremy L. Thompson   @ref User
9697509a596Sjeremylt **/
970e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedSize l_size,
9718621c6c6SJeremy L Thompson                                             const CeedInt strides[3], CeedElemRestriction *rstr) {
972e7f679fcSJeremy L Thompson   CeedInt num_block = (num_elem / block_size) + !!(num_elem % block_size);
9737509a596Sjeremylt 
9747509a596Sjeremylt   if (!ceed->ElemRestrictionCreateBlocked) {
9757509a596Sjeremylt     Ceed delegate;
9766574a04fSJeremy L Thompson 
9772b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
978fcbe8c06SSebastian Grimberg     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionCreateBlocked");
979e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedStrided(delegate, num_elem, elem_size, block_size, num_comp, l_size, strides, rstr));
980e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
9817509a596Sjeremylt   }
9827509a596Sjeremylt 
983e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
9846574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
985e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
9866574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "ElemRestriction must have at least 1 component");
987e7f679fcSJeremy L Thompson   CeedCheck(l_size >= num_elem * elem_size * num_comp, ceed, CEED_ERROR_DIMENSION, "L-vector size must be at least num_elem * elem_size * num_comp");
988e022e1f8SJeremy L Thompson 
9892b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
990db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
991d1d35e2fSjeremylt   (*rstr)->ref_count  = 1;
992d1d35e2fSjeremylt   (*rstr)->num_elem   = num_elem;
993d1d35e2fSjeremylt   (*rstr)->elem_size  = elem_size;
994d1d35e2fSjeremylt   (*rstr)->num_comp   = num_comp;
995d1d35e2fSjeremylt   (*rstr)->l_size     = l_size;
996e7f679fcSJeremy L Thompson   (*rstr)->num_block  = num_block;
997e7f679fcSJeremy L Thompson   (*rstr)->block_size = block_size;
998fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
9992b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(3, &(*rstr)->strides));
10002b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
1001fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
1002e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10037509a596Sjeremylt }
10047509a596Sjeremylt 
10057509a596Sjeremylt /**
10067c1dbaffSSebastian Grimberg   @brief Copy the pointer to a CeedElemRestriction and set `CeedElemRestrictionApply()` implementation to use the unsigned version.
1007c17ec2beSJeremy L Thompson 
1008c17ec2beSJeremy L Thompson   Both pointers should be destroyed with `CeedElemRestrictionDestroy()`.
1009c17ec2beSJeremy L Thompson 
1010c17ec2beSJeremy L Thompson   @param[in]     rstr          CeedElemRestriction to create unsigned reference to
1011c17ec2beSJeremy L Thompson   @param[in,out] rstr_unsigned Variable to store unsigned CeedElemRestriction
1012c17ec2beSJeremy L Thompson 
1013c17ec2beSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1014c17ec2beSJeremy L Thompson 
1015c17ec2beSJeremy L Thompson   @ref User
1016c17ec2beSJeremy L Thompson **/
1017c17ec2beSJeremy L Thompson int CeedElemRestrictionCreateUnsignedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unsigned) {
1018c17ec2beSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr_unsigned));
1019c17ec2beSJeremy L Thompson 
1020c17ec2beSJeremy L Thompson   // Copy old rstr
1021c17ec2beSJeremy L Thompson   memcpy(*rstr_unsigned, rstr, sizeof(struct CeedElemRestriction_private));
1022c17ec2beSJeremy L Thompson   (*rstr_unsigned)->ceed = NULL;
1023c17ec2beSJeremy L Thompson   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unsigned)->ceed));
1024c17ec2beSJeremy L Thompson   (*rstr_unsigned)->ref_count = 1;
1025c17ec2beSJeremy L Thompson   (*rstr_unsigned)->strides   = NULL;
1026c17ec2beSJeremy L Thompson   if (rstr->strides) {
1027c17ec2beSJeremy L Thompson     CeedCall(CeedMalloc(3, &(*rstr_unsigned)->strides));
1028c17ec2beSJeremy L Thompson     for (CeedInt i = 0; i < 3; i++) (*rstr_unsigned)->strides[i] = rstr->strides[i];
1029c17ec2beSJeremy L Thompson   }
10307c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unsigned)->rstr_base));
1031c17ec2beSJeremy L Thompson 
1032c17ec2beSJeremy L Thompson   // Override Apply
1033c17ec2beSJeremy L Thompson   (*rstr_unsigned)->Apply = rstr->ApplyUnsigned;
1034c17ec2beSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1035c17ec2beSJeremy L Thompson }
1036c17ec2beSJeremy L Thompson 
1037c17ec2beSJeremy L Thompson /**
10387c1dbaffSSebastian Grimberg   @brief Copy the pointer to a CeedElemRestriction and set `CeedElemRestrictionApply()` implementation to use the unoriented version.
10397c1dbaffSSebastian Grimberg 
10407c1dbaffSSebastian Grimberg   Both pointers should be destroyed with `CeedElemRestrictionDestroy()`.
10417c1dbaffSSebastian Grimberg 
10427c1dbaffSSebastian Grimberg   @param[in]     rstr            CeedElemRestriction to create unoriented reference to
10437c1dbaffSSebastian Grimberg   @param[in,out] rstr_unoriented Variable to store unoriented CeedElemRestriction
10447c1dbaffSSebastian Grimberg 
10457c1dbaffSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
10467c1dbaffSSebastian Grimberg 
10477c1dbaffSSebastian Grimberg   @ref User
10487c1dbaffSSebastian Grimberg **/
10497c1dbaffSSebastian Grimberg int CeedElemRestrictionCreateUnorientedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unoriented) {
10507c1dbaffSSebastian Grimberg   CeedCall(CeedCalloc(1, rstr_unoriented));
10517c1dbaffSSebastian Grimberg 
10527c1dbaffSSebastian Grimberg   // Copy old rstr
10537c1dbaffSSebastian Grimberg   memcpy(*rstr_unoriented, rstr, sizeof(struct CeedElemRestriction_private));
10547c1dbaffSSebastian Grimberg   (*rstr_unoriented)->ceed = NULL;
10557c1dbaffSSebastian Grimberg   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unoriented)->ceed));
10567c1dbaffSSebastian Grimberg   (*rstr_unoriented)->ref_count = 1;
10577c1dbaffSSebastian Grimberg   (*rstr_unoriented)->strides   = NULL;
10587c1dbaffSSebastian Grimberg   if (rstr->strides) {
10597c1dbaffSSebastian Grimberg     CeedCall(CeedMalloc(3, &(*rstr_unoriented)->strides));
10607c1dbaffSSebastian Grimberg     for (CeedInt i = 0; i < 3; i++) (*rstr_unoriented)->strides[i] = rstr->strides[i];
10617c1dbaffSSebastian Grimberg   }
10627c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unoriented)->rstr_base));
10637c1dbaffSSebastian Grimberg 
10647c1dbaffSSebastian Grimberg   // Override Apply
10657c1dbaffSSebastian Grimberg   (*rstr_unoriented)->Apply = rstr->ApplyUnoriented;
10667c1dbaffSSebastian Grimberg   return CEED_ERROR_SUCCESS;
10677c1dbaffSSebastian Grimberg }
10687c1dbaffSSebastian Grimberg 
10697c1dbaffSSebastian Grimberg /**
1070ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedElemRestriction.
10719fd66db6SSebastian Grimberg 
1072ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedElemRestrictionDestroy()`.
10739560d06aSjeremylt 
10749fd66db6SSebastian Grimberg   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.
10759fd66db6SSebastian Grimberg         This CeedElemRestriction will be destroyed if `rstr_copy` is the only reference to this CeedElemRestriction.
1076ea61e9acSJeremy L Thompson 
1077ea61e9acSJeremy L Thompson   @param[in]     rstr      CeedElemRestriction to copy reference to
1078ea61e9acSJeremy L Thompson   @param[in,out] rstr_copy Variable to store copied reference
10799560d06aSjeremylt 
10809560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
10819560d06aSjeremylt 
10829560d06aSjeremylt   @ref User
10839560d06aSjeremylt **/
10842b730f8bSJeremy L Thompson int CeedElemRestrictionReferenceCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_copy) {
1085393ac2cdSJeremy L Thompson   if (rstr != CEED_ELEMRESTRICTION_NONE) CeedCall(CeedElemRestrictionReference(rstr));
10862b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(rstr_copy));
10879560d06aSjeremylt   *rstr_copy = rstr;
10889560d06aSjeremylt   return CEED_ERROR_SUCCESS;
10899560d06aSjeremylt }
10909560d06aSjeremylt 
10919560d06aSjeremylt /**
1092b11c1e72Sjeremylt   @brief Create CeedVectors associated with a CeedElemRestriction
1093b11c1e72Sjeremylt 
1094ea61e9acSJeremy L Thompson   @param[in]  rstr  CeedElemRestriction
1095ea61e9acSJeremy L Thompson   @param[out] l_vec The address of the L-vector to be created, or NULL
1096ea61e9acSJeremy L Thompson   @param[out] e_vec The address of the E-vector to be created, or NULL
1097b11c1e72Sjeremylt 
1098b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1099dfdf5a53Sjeremylt 
11007a982d89SJeremy L. Thompson   @ref User
1101b11c1e72Sjeremylt **/
11022b730f8bSJeremy L Thompson int CeedElemRestrictionCreateVector(CeedElemRestriction rstr, CeedVector *l_vec, CeedVector *e_vec) {
1103d2643443SJeremy L Thompson   CeedSize e_size, l_size;
1104d1d35e2fSjeremylt   l_size = rstr->l_size;
1105e7f679fcSJeremy L Thompson   e_size = rstr->num_block * rstr->block_size * rstr->elem_size * rstr->num_comp;
11062b730f8bSJeremy L Thompson   if (l_vec) CeedCall(CeedVectorCreate(rstr->ceed, l_size, l_vec));
11072b730f8bSJeremy L Thompson   if (e_vec) CeedCall(CeedVectorCreate(rstr->ceed, e_size, e_vec));
1108e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1109d7b241e6Sjeremylt }
1110d7b241e6Sjeremylt 
1111d7b241e6Sjeremylt /**
1112d9e1f99aSValeria Barra   @brief Restrict an L-vector to an E-vector or apply its transpose
1113d7b241e6Sjeremylt 
1114ea61e9acSJeremy L Thompson   @param[in]  rstr    CeedElemRestriction
1115ea61e9acSJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1116ea61e9acSJeremy L Thompson   @param[in]  u       Input vector (of size @a l_size when t_mode=@ref CEED_NOTRANSPOSE)
1117fcbe8c06SSebastian Grimberg   @param[out] ru      Output vector (of shape [@a num_elem * @a elem_size] when t_mode=@ref CEED_NOTRANSPOSE).
1118fcbe8c06SSebastian Grimberg                         Ordering of the e-vector is decided by the backend.
1119ea61e9acSJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1120b11c1e72Sjeremylt 
1121b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1122dfdf5a53Sjeremylt 
11237a982d89SJeremy L. Thompson   @ref User
1124b11c1e72Sjeremylt **/
11252b730f8bSJeremy L Thompson int CeedElemRestrictionApply(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector ru, CeedRequest *request) {
1126d7b241e6Sjeremylt   CeedInt m, n;
1127d7b241e6Sjeremylt 
1128d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1129e7f679fcSJeremy L Thompson     m = rstr->num_block * rstr->block_size * rstr->elem_size * rstr->num_comp;
1130d1d35e2fSjeremylt     n = rstr->l_size;
1131d7b241e6Sjeremylt   } else {
1132d1d35e2fSjeremylt     m = rstr->l_size;
1133e7f679fcSJeremy L Thompson     n = rstr->num_block * rstr->block_size * rstr->elem_size * rstr->num_comp;
1134d7b241e6Sjeremylt   }
11356574a04fSJeremy L Thompson   CeedCheck(n == u->length, rstr->ceed, CEED_ERROR_DIMENSION,
11366574a04fSJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", u->length, m, n);
11376574a04fSJeremy L Thompson   CeedCheck(m == ru->length, rstr->ceed, CEED_ERROR_DIMENSION,
11386574a04fSJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", ru->length, m, n);
11392b730f8bSJeremy L Thompson   if (rstr->num_elem > 0) CeedCall(rstr->Apply(rstr, t_mode, u, ru, request));
1140e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1141d7b241e6Sjeremylt }
1142d7b241e6Sjeremylt 
1143d7b241e6Sjeremylt /**
11443ac8f562SJeremy L Thompson   @brief Restrict an L-vector of points to a single element or apply its transpose
11453ac8f562SJeremy L Thompson 
11463ac8f562SJeremy L Thompson   @param[in]  rstr    CeedElemRestriction
11473ac8f562SJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
11483ac8f562SJeremy L Thompson   @param[in]  u       Input vector (of size @a l_size when t_mode=@ref CEED_NOTRANSPOSE)
11493ac8f562SJeremy L Thompson   @param[out] ru      Output vector (of shape [@a num_elem * @a elem_size] when t_mode=@ref CEED_NOTRANSPOSE).
11503ac8f562SJeremy L Thompson                         Ordering of the e-vector is decided by the backend.
11513ac8f562SJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
11523ac8f562SJeremy L Thompson 
11533ac8f562SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11543ac8f562SJeremy L Thompson 
11553ac8f562SJeremy L Thompson   @ref User
11563ac8f562SJeremy L Thompson **/
11573ac8f562SJeremy L Thompson int CeedElemRestrictionApplyAtPoints(CeedElemRestriction rstr, CeedInt elem, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
11583ac8f562SJeremy L Thompson                                      CeedRequest *request) {
11593ac8f562SJeremy L Thompson   CeedInt m, n;
11603ac8f562SJeremy L Thompson 
11613ac8f562SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
11623ac8f562SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &m));
11633ac8f562SJeremy L Thompson     n = rstr->l_size;
11643ac8f562SJeremy L Thompson   } else {
11653ac8f562SJeremy L Thompson     m = rstr->l_size;
11663ac8f562SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &n));
11673ac8f562SJeremy L Thompson   }
11683ac8f562SJeremy L Thompson   CeedCheck(n <= u->length, rstr->ceed, CEED_ERROR_DIMENSION,
11693ac8f562SJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
11703ac8f562SJeremy L Thompson             ") for element %" CeedInt_FMT,
11713ac8f562SJeremy L Thompson             u->length, m, n, elem);
11723ac8f562SJeremy L Thompson   CeedCheck(m <= ru->length, rstr->ceed, CEED_ERROR_DIMENSION,
11733ac8f562SJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
11743ac8f562SJeremy L Thompson             ") for element %" CeedInt_FMT,
11753ac8f562SJeremy L Thompson             ru->length, m, n, elem);
11763ac8f562SJeremy L Thompson   CeedCheck(elem <= rstr->num_elem, rstr->ceed, CEED_ERROR_DIMENSION,
11773ac8f562SJeremy L Thompson             "Cannot retrieve element %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", elem, elem, rstr->num_elem);
11783ac8f562SJeremy L Thompson   if (rstr->num_elem > 0) CeedCall(rstr->ApplyAtPoints(rstr, elem, t_mode, u, ru, request));
11793ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11803ac8f562SJeremy L Thompson }
11813ac8f562SJeremy L Thompson 
11823ac8f562SJeremy L Thompson /**
1183d9e1f99aSValeria Barra   @brief Restrict an L-vector to a block of an E-vector or apply its transpose
1184be9261b7Sjeremylt 
1185ea61e9acSJeremy L Thompson   @param[in]  rstr    CeedElemRestriction
1186e7f679fcSJeremy L Thompson   @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
1187e7f679fcSJeremy L Thompson [3*block_size : 4*block_size]
1188ea61e9acSJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1189ea61e9acSJeremy L Thompson   @param[in]  u       Input vector (of size @a l_size when t_mode=@ref CEED_NOTRANSPOSE)
1190e7f679fcSJeremy L Thompson   @param[out] ru      Output vector (of shape [@a block_size * @a elem_size] when t_mode=@ref CEED_NOTRANSPOSE).
1191fcbe8c06SSebastian Grimberg                         Ordering of the e-vector is decided by the backend.
1192ea61e9acSJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1193be9261b7Sjeremylt 
1194be9261b7Sjeremylt   @return An error code: 0 - success, otherwise - failure
1195be9261b7Sjeremylt 
11967a982d89SJeremy L. Thompson   @ref Backend
1197be9261b7Sjeremylt **/
11982b730f8bSJeremy L Thompson int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
11992b730f8bSJeremy L Thompson                                   CeedRequest *request) {
1200be9261b7Sjeremylt   CeedInt m, n;
1201be9261b7Sjeremylt 
12026402da51SJeremy L Thompson   CeedCheck(rstr->ApplyBlock, rstr->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement ElemRestrictionApplyBlock");
12036402da51SJeremy L Thompson 
1204d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1205e7f679fcSJeremy L Thompson     m = rstr->block_size * rstr->elem_size * rstr->num_comp;
1206d1d35e2fSjeremylt     n = rstr->l_size;
1207be9261b7Sjeremylt   } else {
1208d1d35e2fSjeremylt     m = rstr->l_size;
1209e7f679fcSJeremy L Thompson     n = rstr->block_size * rstr->elem_size * rstr->num_comp;
1210be9261b7Sjeremylt   }
12116574a04fSJeremy L Thompson   CeedCheck(n == u->length, rstr->ceed, CEED_ERROR_DIMENSION,
12126574a04fSJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", u->length, m, n);
12136574a04fSJeremy L Thompson   CeedCheck(m == ru->length, rstr->ceed, CEED_ERROR_DIMENSION,
12146574a04fSJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", ru->length, m, n);
1215e7f679fcSJeremy L Thompson   CeedCheck(rstr->block_size * block <= rstr->num_elem, rstr->ceed, CEED_ERROR_DIMENSION,
1216e7f679fcSJeremy L Thompson             "Cannot retrieve block %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", block, rstr->block_size * block,
12176574a04fSJeremy L Thompson             rstr->num_elem);
12182b730f8bSJeremy L Thompson   CeedCall(rstr->ApplyBlock(rstr, block, t_mode, u, ru, request));
1219e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1220be9261b7Sjeremylt }
1221be9261b7Sjeremylt 
1222be9261b7Sjeremylt /**
1223b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedElemRestriction
1224b7c9bbdaSJeremy L Thompson 
1225ea61e9acSJeremy L Thompson   @param[in]  rstr CeedElemRestriction
1226b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1227b7c9bbdaSJeremy L Thompson 
1228b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1229b7c9bbdaSJeremy L Thompson 
1230b7c9bbdaSJeremy L Thompson   @ref Advanced
1231b7c9bbdaSJeremy L Thompson **/
1232b7c9bbdaSJeremy L Thompson int CeedElemRestrictionGetCeed(CeedElemRestriction rstr, Ceed *ceed) {
1233b7c9bbdaSJeremy L Thompson   *ceed = rstr->ceed;
1234b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1235b7c9bbdaSJeremy L Thompson }
1236b7c9bbdaSJeremy L Thompson 
1237b7c9bbdaSJeremy L Thompson /**
1238d979a051Sjeremylt   @brief Get the L-vector component stride
1239a681ae63Sjeremylt 
1240ea61e9acSJeremy L Thompson   @param[in]  rstr        CeedElemRestriction
1241d1d35e2fSjeremylt   @param[out] comp_stride Variable to store component stride
1242a681ae63Sjeremylt 
1243a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1244a681ae63Sjeremylt 
1245b7c9bbdaSJeremy L Thompson   @ref Advanced
1246a681ae63Sjeremylt **/
12472b730f8bSJeremy L Thompson int CeedElemRestrictionGetCompStride(CeedElemRestriction rstr, CeedInt *comp_stride) {
1248d1d35e2fSjeremylt   *comp_stride = rstr->comp_stride;
1249e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1250a681ae63Sjeremylt }
1251a681ae63Sjeremylt 
1252a681ae63Sjeremylt /**
1253a681ae63Sjeremylt   @brief Get the total number of elements in the range of a CeedElemRestriction
1254a681ae63Sjeremylt 
1255ea61e9acSJeremy L Thompson   @param[in] rstr      CeedElemRestriction
1256d1d35e2fSjeremylt   @param[out] num_elem Variable to store number of elements
1257a681ae63Sjeremylt 
1258a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1259a681ae63Sjeremylt 
1260b7c9bbdaSJeremy L Thompson   @ref Advanced
1261a681ae63Sjeremylt **/
12622b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumElements(CeedElemRestriction rstr, CeedInt *num_elem) {
1263d1d35e2fSjeremylt   *num_elem = rstr->num_elem;
1264e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1265a681ae63Sjeremylt }
1266a681ae63Sjeremylt 
1267a681ae63Sjeremylt /**
1268a681ae63Sjeremylt   @brief Get the size of elements in the CeedElemRestriction
1269a681ae63Sjeremylt 
1270ea61e9acSJeremy L Thompson   @param[in]  rstr      CeedElemRestriction
1271d1d35e2fSjeremylt   @param[out] elem_size Variable to store size of elements
1272a681ae63Sjeremylt 
1273a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1274a681ae63Sjeremylt 
1275b7c9bbdaSJeremy L Thompson   @ref Advanced
1276a681ae63Sjeremylt **/
12772b730f8bSJeremy L Thompson int CeedElemRestrictionGetElementSize(CeedElemRestriction rstr, CeedInt *elem_size) {
1278d1d35e2fSjeremylt   *elem_size = rstr->elem_size;
1279*2c7e7413SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1280*2c7e7413SJeremy L Thompson }
1281*2c7e7413SJeremy L Thompson 
1282*2c7e7413SJeremy L Thompson /**
1283*2c7e7413SJeremy L Thompson   @brief Get the maximum number of points in an element for a CeedElemRestriction at points
1284*2c7e7413SJeremy L Thompson 
1285*2c7e7413SJeremy L Thompson   @param[in]  rstr       CeedElemRestriction
1286*2c7e7413SJeremy L Thompson   @param[out] max_points Variable to store size of elements
1287*2c7e7413SJeremy L Thompson 
1288*2c7e7413SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1289*2c7e7413SJeremy L Thompson 
1290*2c7e7413SJeremy L Thompson   @ref Advanced
1291*2c7e7413SJeremy L Thompson **/
1292*2c7e7413SJeremy L Thompson int CeedElemRestrictionGetMaxPointsInElement(CeedElemRestriction rstr, CeedInt *max_points) {
1293*2c7e7413SJeremy L Thompson   Ceed                ceed;
1294*2c7e7413SJeremy L Thompson   CeedInt             num_elem;
1295*2c7e7413SJeremy L Thompson   CeedRestrictionType rstr_type;
1296*2c7e7413SJeremy L Thompson 
1297*2c7e7413SJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1298*2c7e7413SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1299*2c7e7413SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_INCOMPATIBLE,
1300*2c7e7413SJeremy L Thompson             "Cannot compute max points for a CeedElemRestriction that does not use points");
1301*2c7e7413SJeremy L Thompson 
1302*2c7e7413SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1303*2c7e7413SJeremy L Thompson   *max_points = 0;
1304*2c7e7413SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
1305*2c7e7413SJeremy L Thompson     CeedInt num_points;
1306*2c7e7413SJeremy L Thompson 
1307*2c7e7413SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, e, &num_points));
1308*2c7e7413SJeremy L Thompson     *max_points = CeedIntMax(num_points, *max_points);
1309*2c7e7413SJeremy L Thompson   }
1310e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1311a681ae63Sjeremylt }
1312a681ae63Sjeremylt 
1313a681ae63Sjeremylt /**
1314d979a051Sjeremylt   @brief Get the size of the l-vector for a CeedElemRestriction
1315a681ae63Sjeremylt 
1316ea61e9acSJeremy L Thompson   @param[in]  rstr   CeedElemRestriction
1317d1d35e2fSjeremylt   @param[out] l_size Variable to store number of nodes
1318a681ae63Sjeremylt 
1319a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1320a681ae63Sjeremylt 
1321b7c9bbdaSJeremy L Thompson   @ref Advanced
1322a681ae63Sjeremylt **/
13232b730f8bSJeremy L Thompson int CeedElemRestrictionGetLVectorSize(CeedElemRestriction rstr, CeedSize *l_size) {
1324d1d35e2fSjeremylt   *l_size = rstr->l_size;
1325e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1326a681ae63Sjeremylt }
1327a681ae63Sjeremylt 
1328a681ae63Sjeremylt /**
1329ea61e9acSJeremy L Thompson   @brief Get the number of components in the elements of a CeedElemRestriction
1330a681ae63Sjeremylt 
1331ea61e9acSJeremy L Thompson   @param[in]  rstr     CeedElemRestriction
1332d1d35e2fSjeremylt   @param[out] num_comp Variable to store number of components
1333a681ae63Sjeremylt 
1334a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1335a681ae63Sjeremylt 
1336b7c9bbdaSJeremy L Thompson   @ref Advanced
1337a681ae63Sjeremylt **/
13382b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumComponents(CeedElemRestriction rstr, CeedInt *num_comp) {
1339d1d35e2fSjeremylt   *num_comp = rstr->num_comp;
1340e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1341a681ae63Sjeremylt }
1342a681ae63Sjeremylt 
1343a681ae63Sjeremylt /**
1344a681ae63Sjeremylt   @brief Get the number of blocks in a CeedElemRestriction
1345a681ae63Sjeremylt 
1346ea61e9acSJeremy L Thompson   @param[in]  rstr      CeedElemRestriction
1347d1d35e2fSjeremylt   @param[out] num_block Variable to store number of blocks
1348a681ae63Sjeremylt 
1349a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1350a681ae63Sjeremylt 
1351b7c9bbdaSJeremy L Thompson   @ref Advanced
1352a681ae63Sjeremylt **/
13532b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumBlocks(CeedElemRestriction rstr, CeedInt *num_block) {
1354e7f679fcSJeremy L Thompson   *num_block = rstr->num_block;
1355e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1356a681ae63Sjeremylt }
1357a681ae63Sjeremylt 
1358a681ae63Sjeremylt /**
1359a681ae63Sjeremylt   @brief Get the size of blocks in the CeedElemRestriction
1360a681ae63Sjeremylt 
1361ea61e9acSJeremy L Thompson   @param[in]  rstr       CeedElemRestriction
1362e7f679fcSJeremy L Thompson   @param[out] block_size Variable to store size of blocks
1363a681ae63Sjeremylt 
1364a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1365a681ae63Sjeremylt 
1366b7c9bbdaSJeremy L Thompson   @ref Advanced
1367a681ae63Sjeremylt **/
1368e7f679fcSJeremy L Thompson int CeedElemRestrictionGetBlockSize(CeedElemRestriction rstr, CeedInt *block_size) {
1369e7f679fcSJeremy L Thompson   *block_size = rstr->block_size;
1370e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1371a681ae63Sjeremylt }
1372a681ae63Sjeremylt 
1373a681ae63Sjeremylt /**
1374d9e1f99aSValeria Barra   @brief Get the multiplicity of nodes in a CeedElemRestriction
13751469ee4dSjeremylt 
1376ea61e9acSJeremy L Thompson   @param[in]  rstr CeedElemRestriction
1377d1d35e2fSjeremylt   @param[out] mult Vector to store multiplicity (of size l_size)
13781469ee4dSjeremylt 
13791469ee4dSjeremylt   @return An error code: 0 - success, otherwise - failure
13801469ee4dSjeremylt 
13817a982d89SJeremy L. Thompson   @ref User
13821469ee4dSjeremylt **/
13832b730f8bSJeremy L Thompson int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr, CeedVector mult) {
1384d1d35e2fSjeremylt   CeedVector e_vec;
13851469ee4dSjeremylt 
138625509ebbSRezgar Shakeri   // Create e_vec to hold intermediate computation in E^T (E 1)
13872b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateVector(rstr, NULL, &e_vec));
13881469ee4dSjeremylt 
138925509ebbSRezgar Shakeri   // Compute e_vec = E * 1
13902b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 1.0));
13912b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr, CEED_NOTRANSPOSE, mult, e_vec, CEED_REQUEST_IMMEDIATE));
139225509ebbSRezgar Shakeri   // Compute multiplicity, mult = E^T * e_vec = E^T (E 1)
13932b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
13942b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, e_vec, mult, CEED_REQUEST_IMMEDIATE));
13951469ee4dSjeremylt   // Cleanup
13962b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&e_vec));
1397e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13981469ee4dSjeremylt }
13991469ee4dSjeremylt 
14001469ee4dSjeremylt /**
1401f02ca4a2SJed Brown   @brief View a CeedElemRestriction
1402f02ca4a2SJed Brown 
1403f02ca4a2SJed Brown   @param[in] rstr   CeedElemRestriction to view
1404f02ca4a2SJed Brown   @param[in] stream Stream to write; typically stdout/stderr or a file
1405f02ca4a2SJed Brown 
1406f02ca4a2SJed Brown   @return Error code: 0 - success, otherwise - failure
1407f02ca4a2SJed Brown 
14087a982d89SJeremy L. Thompson   @ref User
1409f02ca4a2SJed Brown **/
1410f02ca4a2SJed Brown int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream) {
14117509a596Sjeremylt   char stridesstr[500];
14121c66c397SJeremy L Thompson 
14132b730f8bSJeremy L Thompson   if (rstr->strides) {
14142b730f8bSJeremy L Thompson     sprintf(stridesstr, "[%" CeedInt_FMT ", %" CeedInt_FMT ", %" CeedInt_FMT "]", rstr->strides[0], rstr->strides[1], rstr->strides[2]);
14152b730f8bSJeremy L Thompson   } else {
1416990fdeb6SJeremy L Thompson     sprintf(stridesstr, "%" CeedInt_FMT, rstr->comp_stride);
14172b730f8bSJeremy L Thompson   }
14187509a596Sjeremylt 
14192b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedElemRestriction from (%td, %" CeedInt_FMT ") to %" CeedInt_FMT " elements with %" CeedInt_FMT " nodes each and %s %s\n",
1420e7f679fcSJeremy L Thompson           rstr->block_size > 1 ? "Blocked " : "", rstr->l_size, rstr->num_comp, rstr->num_elem, rstr->elem_size,
1421d979a051Sjeremylt           rstr->strides ? "strides" : "component stride", stridesstr);
1422e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1423f02ca4a2SJed Brown }
1424f02ca4a2SJed Brown 
1425f02ca4a2SJed Brown /**
1426b11c1e72Sjeremylt   @brief Destroy a CeedElemRestriction
1427b11c1e72Sjeremylt 
1428ea61e9acSJeremy L Thompson   @param[in,out] rstr CeedElemRestriction to destroy
1429b11c1e72Sjeremylt 
1430b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1431dfdf5a53Sjeremylt 
14327a982d89SJeremy L. Thompson   @ref User
1433b11c1e72Sjeremylt **/
14344ce2993fSjeremylt int CeedElemRestrictionDestroy(CeedElemRestriction *rstr) {
1435393ac2cdSJeremy L Thompson   if (!*rstr || *rstr == CEED_ELEMRESTRICTION_NONE || --(*rstr)->ref_count > 0) {
1436ad6481ceSJeremy L Thompson     *rstr = NULL;
1437ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1438ad6481ceSJeremy L Thompson   }
14396574a04fSJeremy L Thompson   CeedCheck((*rstr)->num_readers == 0, (*rstr)->ceed, CEED_ERROR_ACCESS,
14406574a04fSJeremy L Thompson             "Cannot destroy CeedElemRestriction, a process has read access to the offset data");
1441c17ec2beSJeremy L Thompson 
1442c17ec2beSJeremy L Thompson   // Only destroy backend data once between rstr and unsigned copy
14437c1dbaffSSebastian Grimberg   if ((*rstr)->rstr_base) CeedCall(CeedElemRestrictionDestroy(&(*rstr)->rstr_base));
1444c17ec2beSJeremy L Thompson   else if ((*rstr)->Destroy) CeedCall((*rstr)->Destroy(*rstr));
1445c17ec2beSJeremy L Thompson 
14462b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*rstr)->strides));
14472b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*rstr)->ceed));
14482b730f8bSJeremy L Thompson   CeedCall(CeedFree(rstr));
1449e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1450d7b241e6Sjeremylt }
1451d7b241e6Sjeremylt 
1452d7b241e6Sjeremylt /// @}
1453