xref: /libCEED/backends/ref/ceed-ref-restriction.c (revision 22eb13854768cd7db9fa223351f183dc3d3dc7a1)
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.
321617c04Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
521617c04Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
721617c04Sjeremylt 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <stdbool.h>
11fcbe8c06SSebastian Grimberg #include <stdlib.h>
123d576824SJeremy L Thompson #include <string.h>
132b730f8bSJeremy L Thompson 
1421617c04Sjeremylt #include "ceed-ref.h"
1521617c04Sjeremylt 
16f10650afSjeremylt //------------------------------------------------------------------------------
17f10650afSjeremylt // Core ElemRestriction Apply Code
18f10650afSjeremylt //------------------------------------------------------------------------------
191cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyStridedNoTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
2094648b7dSSebastian Grimberg                                                                       CeedInt start, CeedInt stop, CeedInt num_elem, CeedInt elem_size,
21eda0adbcSSebastian Grimberg                                                                       CeedInt v_offset, const CeedScalar *__restrict__ uu,
22eda0adbcSSebastian Grimberg                                                                       CeedScalar *__restrict__ vv) {
2394648b7dSSebastian Grimberg   // No offsets provided, identity restriction
24d1d35e2fSjeremylt   bool has_backend_strides;
25ad70ee2cSJeremy L Thompson 
261cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
27d1d35e2fSjeremylt   if (has_backend_strides) {
28d1d35e2fSjeremylt     // CPU backend strides are {1, elem_size, elem_size*num_comp}
297f90ec76Sjeremylt     // This if branch is left separate to allow better inlining
30ad70ee2cSJeremy L Thompson     for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
312b730f8bSJeremy L Thompson       CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
322b730f8bSJeremy L Thompson         CeedPragmaSIMD for (CeedInt n = 0; n < elem_size; n++) {
33ad70ee2cSJeremy L Thompson           CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
34ad70ee2cSJeremy L Thompson             vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
352b730f8bSJeremy L Thompson                 uu[n + k * elem_size + CeedIntMin(e + j, num_elem - 1) * elem_size * num_comp];
362b730f8bSJeremy L Thompson           }
372b730f8bSJeremy L Thompson         }
382b730f8bSJeremy L Thompson       }
392b730f8bSJeremy L Thompson     }
407f90ec76Sjeremylt   } else {
417f90ec76Sjeremylt     // User provided strides
427f90ec76Sjeremylt     CeedInt strides[3];
43ad70ee2cSJeremy L Thompson 
441cc2c60dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetStrides(rstr, &strides));
45ad70ee2cSJeremy L Thompson     for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
462b730f8bSJeremy L Thompson       CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
472b730f8bSJeremy L Thompson         CeedPragmaSIMD for (CeedInt n = 0; n < elem_size; n++) {
48ad70ee2cSJeremy L Thompson           CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
49ad70ee2cSJeremy L Thompson             vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
502b730f8bSJeremy L Thompson                 uu[n * strides[0] + k * strides[1] + CeedIntMin(e + j, num_elem - 1) * strides[2]];
512b730f8bSJeremy L Thompson           }
522b730f8bSJeremy L Thompson         }
532b730f8bSJeremy L Thompson       }
542b730f8bSJeremy L Thompson     }
557509a596Sjeremylt   }
5694648b7dSSebastian Grimberg   return CEED_ERROR_SUCCESS;
5794648b7dSSebastian Grimberg }
5894648b7dSSebastian Grimberg 
59eda0adbcSSebastian Grimberg static inline int CeedElemRestrictionApplyOffsetNoTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
6094648b7dSSebastian Grimberg                                                                      const CeedInt comp_stride, CeedInt start, CeedInt stop, CeedInt num_elem,
61eda0adbcSSebastian Grimberg                                                                      CeedInt elem_size, CeedInt v_offset, const CeedScalar *__restrict__ uu,
62eda0adbcSSebastian Grimberg                                                                      CeedScalar *__restrict__ vv) {
63fcbe8c06SSebastian Grimberg   // Default restriction with offsets
6494648b7dSSebastian Grimberg   CeedElemRestriction_Ref *impl;
65ad70ee2cSJeremy L Thompson 
661cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
67ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
682b730f8bSJeremy L Thompson     CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
69ad70ee2cSJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < elem_size * block_size; i++) {
70ad70ee2cSJeremy L Thompson         vv[elem_size * (k * block_size + e * num_comp) + i - v_offset] = uu[impl->offsets[i + e * elem_size] + k * comp_stride];
71fcbe8c06SSebastian Grimberg       }
72fcbe8c06SSebastian Grimberg     }
73fcbe8c06SSebastian Grimberg   }
7494648b7dSSebastian Grimberg   return CEED_ERROR_SUCCESS;
7594648b7dSSebastian Grimberg }
7694648b7dSSebastian Grimberg 
771cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyOrientedNoTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
7894648b7dSSebastian Grimberg                                                                        const CeedInt comp_stride, CeedInt start, CeedInt stop, CeedInt num_elem,
79eda0adbcSSebastian Grimberg                                                                        CeedInt elem_size, CeedInt v_offset, const CeedScalar *__restrict__ uu,
80eda0adbcSSebastian Grimberg                                                                        CeedScalar *__restrict__ vv) {
81fcbe8c06SSebastian Grimberg   // Restriction with orientations
8294648b7dSSebastian Grimberg   CeedElemRestriction_Ref *impl;
83ad70ee2cSJeremy L Thompson 
841cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
85ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
86fcbe8c06SSebastian Grimberg     CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
87ad70ee2cSJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < elem_size * block_size; i++) {
88ad70ee2cSJeremy L Thompson         vv[elem_size * (k * block_size + e * num_comp) + i - v_offset] =
897c1dbaffSSebastian Grimberg             uu[impl->offsets[i + e * elem_size] + k * comp_stride] * (impl->orients[i + e * elem_size] ? -1.0 : 1.0);
90fcbe8c06SSebastian Grimberg       }
91fcbe8c06SSebastian Grimberg     }
92fcbe8c06SSebastian Grimberg   }
9394648b7dSSebastian Grimberg   return CEED_ERROR_SUCCESS;
9494648b7dSSebastian Grimberg }
9594648b7dSSebastian Grimberg 
961cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyCurlOrientedNoTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
9794648b7dSSebastian Grimberg                                                                            const CeedInt comp_stride, CeedInt start, CeedInt stop, CeedInt num_elem,
98eda0adbcSSebastian Grimberg                                                                            CeedInt elem_size, CeedInt v_offset, const CeedScalar *__restrict__ uu,
99eda0adbcSSebastian Grimberg                                                                            CeedScalar *__restrict__ vv) {
10077d1c127SSebastian Grimberg   // Restriction with tridiagonal transformation
10194648b7dSSebastian Grimberg   CeedElemRestriction_Ref *impl;
102ad70ee2cSJeremy L Thompson 
1031cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
104ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
105fcbe8c06SSebastian Grimberg     CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
1060c73c039SSebastian Grimberg       CeedInt n = 0;
1075c7e0f51SSebastian Grimberg 
108ad70ee2cSJeremy L Thompson       CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
109ad70ee2cSJeremy L Thompson         vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
110ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] *
111ad70ee2cSJeremy L Thompson                 impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size] +
112ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + (n + 1) * block_size + e * elem_size] + k * comp_stride] *
113ad70ee2cSJeremy L Thompson                 impl->curl_orients[j + (3 * n + 2) * block_size + e * 3 * elem_size];
1140c73c039SSebastian Grimberg       }
1155c7e0f51SSebastian Grimberg       CeedPragmaSIMD for (n = 1; n < elem_size - 1; n++) {
116ad70ee2cSJeremy L Thompson         CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
117ad70ee2cSJeremy L Thompson           vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
118ad70ee2cSJeremy L Thompson               uu[impl->offsets[j + (n - 1) * block_size + e * elem_size] + k * comp_stride] *
119ad70ee2cSJeremy L Thompson                   impl->curl_orients[j + (3 * n + 0) * block_size + e * 3 * elem_size] +
120ad70ee2cSJeremy L Thompson               uu[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] *
121ad70ee2cSJeremy L Thompson                   impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size] +
122ad70ee2cSJeremy L Thompson               uu[impl->offsets[j + (n + 1) * block_size + e * elem_size] + k * comp_stride] *
123ad70ee2cSJeremy L Thompson                   impl->curl_orients[j + (3 * n + 2) * block_size + e * 3 * elem_size];
1240c73c039SSebastian Grimberg         }
1250c73c039SSebastian Grimberg       }
126ad70ee2cSJeremy L Thompson       CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
127ad70ee2cSJeremy L Thompson         vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
128ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + (n - 1) * block_size + e * elem_size] + k * comp_stride] *
129ad70ee2cSJeremy L Thompson                 impl->curl_orients[j + (3 * n + 0) * block_size + e * 3 * elem_size] +
130ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] *
131ad70ee2cSJeremy L Thompson                 impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size];
1322b730f8bSJeremy L Thompson       }
1332b730f8bSJeremy L Thompson     }
1342b730f8bSJeremy L Thompson   }
1350c73c039SSebastian Grimberg   return CEED_ERROR_SUCCESS;
136fcbe8c06SSebastian Grimberg }
1370c73c039SSebastian Grimberg 
1381cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyCurlOrientedUnsignedNoTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp,
139ad70ee2cSJeremy L Thompson                                                                                    const CeedInt block_size, const CeedInt comp_stride, CeedInt start,
14094648b7dSSebastian Grimberg                                                                                    CeedInt stop, CeedInt num_elem, CeedInt elem_size,
141eda0adbcSSebastian Grimberg                                                                                    CeedInt v_offset, const CeedScalar *__restrict__ uu,
142eda0adbcSSebastian Grimberg                                                                                    CeedScalar *__restrict__ vv) {
14394648b7dSSebastian Grimberg   // Restriction with (unsigned) tridiagonal transformation
1440c73c039SSebastian Grimberg   CeedElemRestriction_Ref *impl;
145ad70ee2cSJeremy L Thompson 
1461cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
147ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
1487c1dbaffSSebastian Grimberg     CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
1497c1dbaffSSebastian Grimberg       CeedInt n = 0;
150ad70ee2cSJeremy L Thompson 
151ad70ee2cSJeremy L Thompson       CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
152ad70ee2cSJeremy L Thompson         vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
153ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] *
154ad70ee2cSJeremy L Thompson                 abs(impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size]) +
155ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + (n + 1) * block_size + e * elem_size] + k * comp_stride] *
156ad70ee2cSJeremy L Thompson                 abs(impl->curl_orients[j + (3 * n + 2) * block_size + e * 3 * elem_size]);
1577c1dbaffSSebastian Grimberg       }
1585c7e0f51SSebastian Grimberg       CeedPragmaSIMD for (n = 1; n < elem_size - 1; n++) {
159ad70ee2cSJeremy L Thompson         CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
160ad70ee2cSJeremy L Thompson           vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
161ad70ee2cSJeremy L Thompson               uu[impl->offsets[j + (n - 1) * block_size + e * elem_size] + k * comp_stride] *
162ad70ee2cSJeremy L Thompson                   abs(impl->curl_orients[j + (3 * n + 0) * block_size + e * 3 * elem_size]) +
163ad70ee2cSJeremy L Thompson               uu[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] *
164ad70ee2cSJeremy L Thompson                   abs(impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size]) +
165ad70ee2cSJeremy L Thompson               uu[impl->offsets[j + (n + 1) * block_size + e * elem_size] + k * comp_stride] *
166ad70ee2cSJeremy L Thompson                   abs(impl->curl_orients[j + (3 * n + 2) * block_size + e * 3 * elem_size]);
1677c1dbaffSSebastian Grimberg         }
1687c1dbaffSSebastian Grimberg       }
169ad70ee2cSJeremy L Thompson       CeedPragmaSIMD for (CeedInt j = 0; j < block_size; j++) {
170ad70ee2cSJeremy L Thompson         vv[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] =
171ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + (n - 1) * block_size + e * elem_size] + k * comp_stride] *
172ad70ee2cSJeremy L Thompson                 abs(impl->curl_orients[j + (3 * n + 0) * block_size + e * 3 * elem_size]) +
173ad70ee2cSJeremy L Thompson             uu[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] *
174ad70ee2cSJeremy L Thompson                 abs(impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size]);
1757c1dbaffSSebastian Grimberg       }
1767c1dbaffSSebastian Grimberg     }
1777c1dbaffSSebastian Grimberg   }
1787c1dbaffSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1797c1dbaffSSebastian Grimberg }
1807c1dbaffSSebastian Grimberg 
1811cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyStridedTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
18294648b7dSSebastian Grimberg                                                                     CeedInt start, CeedInt stop, CeedInt num_elem, CeedInt elem_size,
183eda0adbcSSebastian Grimberg                                                                     CeedInt v_offset, const CeedScalar *__restrict__ uu,
184eda0adbcSSebastian Grimberg                                                                     CeedScalar *__restrict__ vv) {
18594648b7dSSebastian Grimberg   // No offsets provided, identity restriction
186d1d35e2fSjeremylt   bool has_backend_strides;
187ad70ee2cSJeremy L Thompson 
1881cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
189d1d35e2fSjeremylt   if (has_backend_strides) {
190d1d35e2fSjeremylt     // CPU backend strides are {1, elem_size, elem_size*num_comp}
1917f90ec76Sjeremylt     // This if brach is left separate to allow better inlining
192ad70ee2cSJeremy L Thompson     for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
1932b730f8bSJeremy L Thompson       CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
1942b730f8bSJeremy L Thompson         CeedPragmaSIMD for (CeedInt n = 0; n < elem_size; n++) {
195ad70ee2cSJeremy L Thompson           CeedPragmaSIMD for (CeedInt j = 0; j < CeedIntMin(block_size, num_elem - e); j++) {
196bf9b6c6bSSebastian Grimberg             vv[n + k * elem_size + (e + j) * elem_size * num_comp] += uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset];
1972b730f8bSJeremy L Thompson           }
1982b730f8bSJeremy L Thompson         }
1992b730f8bSJeremy L Thompson       }
2002b730f8bSJeremy L Thompson     }
2017f90ec76Sjeremylt   } else {
2027f90ec76Sjeremylt     // User provided strides
2037f90ec76Sjeremylt     CeedInt strides[3];
204ad70ee2cSJeremy L Thompson 
2051cc2c60dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetStrides(rstr, &strides));
206ad70ee2cSJeremy L Thompson     for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
2072b730f8bSJeremy L Thompson       CeedPragmaSIMD for (CeedInt k = 0; k < num_comp; k++) {
2082b730f8bSJeremy L Thompson         CeedPragmaSIMD for (CeedInt n = 0; n < elem_size; n++) {
209ad70ee2cSJeremy L Thompson           CeedPragmaSIMD for (CeedInt j = 0; j < CeedIntMin(block_size, num_elem - e); j++) {
2106d5e61d4SSebastian Grimberg             vv[n * strides[0] + k * strides[1] + (e + j) * strides[2]] +=
2116d5e61d4SSebastian Grimberg                 uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset];
2122b730f8bSJeremy L Thompson           }
2132b730f8bSJeremy L Thompson         }
2142b730f8bSJeremy L Thompson       }
2152b730f8bSJeremy L Thompson     }
216523b8ea0Sjeremylt   }
21794648b7dSSebastian Grimberg   return CEED_ERROR_SUCCESS;
21894648b7dSSebastian Grimberg }
21994648b7dSSebastian Grimberg 
220eda0adbcSSebastian Grimberg static inline int CeedElemRestrictionApplyOffsetTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
22194648b7dSSebastian Grimberg                                                                    const CeedInt comp_stride, CeedInt start, CeedInt stop, CeedInt num_elem,
222eda0adbcSSebastian Grimberg                                                                    CeedInt elem_size, CeedInt v_offset, const CeedScalar *__restrict__ uu,
223eda0adbcSSebastian Grimberg                                                                    CeedScalar *__restrict__ vv) {
224fcbe8c06SSebastian Grimberg   // Default restriction with offsets
22594648b7dSSebastian Grimberg   CeedElemRestriction_Ref *impl;
226ad70ee2cSJeremy L Thompson 
2271cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
228ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
2292b730f8bSJeremy L Thompson     for (CeedInt k = 0; k < num_comp; k++) {
230ad70ee2cSJeremy L Thompson       for (CeedInt i = 0; i < elem_size * block_size; i += block_size) {
2318d94b059Sjeremylt         // Iteration bound set to discard padding elements
232ad70ee2cSJeremy L Thompson         for (CeedInt j = i; j < i + CeedIntMin(block_size, num_elem - e); j++) {
2335c7e0f51SSebastian Grimberg           CeedScalar vv_loc;
23458c07c4fSSebastian Grimberg 
2355c7e0f51SSebastian Grimberg           vv_loc = uu[elem_size * (k * block_size + e * num_comp) + j - v_offset];
2365c7e0f51SSebastian Grimberg           CeedPragmaAtomic vv[impl->offsets[j + e * elem_size] + k * comp_stride] += vv_loc;
237fcbe8c06SSebastian Grimberg         }
238fcbe8c06SSebastian Grimberg       }
239fcbe8c06SSebastian Grimberg     }
240fcbe8c06SSebastian Grimberg   }
24194648b7dSSebastian Grimberg   return CEED_ERROR_SUCCESS;
24294648b7dSSebastian Grimberg }
24394648b7dSSebastian Grimberg 
2441cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyOrientedTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
24594648b7dSSebastian Grimberg                                                                      const CeedInt comp_stride, CeedInt start, CeedInt stop, CeedInt num_elem,
246eda0adbcSSebastian Grimberg                                                                      CeedInt elem_size, CeedInt v_offset, const CeedScalar *__restrict__ uu,
247eda0adbcSSebastian Grimberg                                                                      CeedScalar *__restrict__ vv) {
248fcbe8c06SSebastian Grimberg   // Restriction with orientations
24994648b7dSSebastian Grimberg   CeedElemRestriction_Ref *impl;
250ad70ee2cSJeremy L Thompson 
2511cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
252ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
253fcbe8c06SSebastian Grimberg     for (CeedInt k = 0; k < num_comp; k++) {
254ad70ee2cSJeremy L Thompson       for (CeedInt i = 0; i < elem_size * block_size; i += block_size) {
255fcbe8c06SSebastian Grimberg         // Iteration bound set to discard padding elements
256ad70ee2cSJeremy L Thompson         for (CeedInt j = i; j < i + CeedIntMin(block_size, num_elem - e); j++) {
2575c7e0f51SSebastian Grimberg           CeedScalar vv_loc;
25858c07c4fSSebastian Grimberg 
2595c7e0f51SSebastian Grimberg           vv_loc = uu[elem_size * (k * block_size + e * num_comp) + j - v_offset] * (impl->orients[j + e * elem_size] ? -1.0 : 1.0);
2605c7e0f51SSebastian Grimberg           CeedPragmaAtomic vv[impl->offsets[j + e * elem_size] + k * comp_stride] += vv_loc;
261fcbe8c06SSebastian Grimberg         }
262fcbe8c06SSebastian Grimberg       }
263fcbe8c06SSebastian Grimberg     }
264fcbe8c06SSebastian Grimberg   }
26594648b7dSSebastian Grimberg   return CEED_ERROR_SUCCESS;
26694648b7dSSebastian Grimberg }
26794648b7dSSebastian Grimberg 
2681cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyCurlOrientedTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
26994648b7dSSebastian Grimberg                                                                          const CeedInt comp_stride, CeedInt start, CeedInt stop, CeedInt num_elem,
270eda0adbcSSebastian Grimberg                                                                          CeedInt elem_size, CeedInt v_offset, const CeedScalar *__restrict__ uu,
271eda0adbcSSebastian Grimberg                                                                          CeedScalar *__restrict__ vv) {
27277d1c127SSebastian Grimberg   // Restriction with tridiagonal transformation
27394648b7dSSebastian Grimberg   CeedElemRestriction_Ref *impl;
2745c7e0f51SSebastian Grimberg   CeedScalar               vv_loc[block_size];
275ad70ee2cSJeremy L Thompson 
2761cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
277ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
278fcbe8c06SSebastian Grimberg     for (CeedInt k = 0; k < num_comp; k++) {
279fcbe8c06SSebastian Grimberg       // Iteration bound set to discard padding elements
28058c07c4fSSebastian Grimberg       const CeedInt block_end = CeedIntMin(block_size, num_elem - e);
28158c07c4fSSebastian Grimberg       CeedInt       n         = 0;
28258c07c4fSSebastian Grimberg 
2835c7e0f51SSebastian Grimberg       CeedPragmaSIMD for (CeedInt j = 0; j < block_end; j++) {
2845c7e0f51SSebastian Grimberg         vv_loc[j] = uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] *
285ad70ee2cSJeremy L Thompson                         impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size] +
286ad70ee2cSJeremy L Thompson                     uu[e * elem_size * num_comp + (k * elem_size + n + 1) * block_size + j - v_offset] *
287ad70ee2cSJeremy L Thompson                         impl->curl_orients[j + (3 * n + 3) * block_size + e * 3 * elem_size];
2885c7e0f51SSebastian Grimberg       }
2895c7e0f51SSebastian Grimberg       for (CeedInt j = 0; j < block_end; j++) {
2905c7e0f51SSebastian Grimberg         CeedPragmaAtomic vv[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] += vv_loc[j];
2910c73c039SSebastian Grimberg       }
2920c73c039SSebastian Grimberg       for (n = 1; n < elem_size - 1; n++) {
2935c7e0f51SSebastian Grimberg         CeedPragmaSIMD for (CeedInt j = 0; j < block_end; j++) {
2945c7e0f51SSebastian Grimberg           vv_loc[j] = uu[e * elem_size * num_comp + (k * elem_size + n - 1) * block_size + j - v_offset] *
295ad70ee2cSJeremy L Thompson                           impl->curl_orients[j + (3 * n - 1) * block_size + e * 3 * elem_size] +
296ad70ee2cSJeremy L Thompson                       uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] *
297ad70ee2cSJeremy L Thompson                           impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size] +
298ad70ee2cSJeremy L Thompson                       uu[e * elem_size * num_comp + (k * elem_size + n + 1) * block_size + j - v_offset] *
299ad70ee2cSJeremy L Thompson                           impl->curl_orients[j + (3 * n + 3) * block_size + e * 3 * elem_size];
3000c73c039SSebastian Grimberg         }
301ad70ee2cSJeremy L Thompson         for (CeedInt j = 0; j < block_end; j++) {
3025c7e0f51SSebastian Grimberg           CeedPragmaAtomic vv[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] += vv_loc[j];
3035c7e0f51SSebastian Grimberg         }
3045c7e0f51SSebastian Grimberg       }
3055c7e0f51SSebastian Grimberg       CeedPragmaSIMD for (CeedInt j = 0; j < block_end; j++) {
3065c7e0f51SSebastian Grimberg         vv_loc[j] = uu[e * elem_size * num_comp + (k * elem_size + n - 1) * block_size + j - v_offset] *
307ad70ee2cSJeremy L Thompson                         impl->curl_orients[j + (3 * n - 1) * block_size + e * 3 * elem_size] +
308ad70ee2cSJeremy L Thompson                     uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] *
309ad70ee2cSJeremy L Thompson                         impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size];
3105c7e0f51SSebastian Grimberg       }
3115c7e0f51SSebastian Grimberg       for (CeedInt j = 0; j < block_end; j++) {
3125c7e0f51SSebastian Grimberg         CeedPragmaAtomic vv[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] += vv_loc[j];
31321617c04Sjeremylt       }
314b435c5a6Srezgarshakeri     }
3152b730f8bSJeremy L Thompson   }
316e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
31721617c04Sjeremylt }
31821617c04Sjeremylt 
3191cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApplyCurlOrientedUnsignedTranspose_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp,
320ad70ee2cSJeremy L Thompson                                                                                  const CeedInt block_size, const CeedInt comp_stride, CeedInt start,
32194648b7dSSebastian Grimberg                                                                                  CeedInt stop, CeedInt num_elem, CeedInt elem_size, CeedInt v_offset,
322eda0adbcSSebastian Grimberg                                                                                  const CeedScalar *__restrict__ uu, CeedScalar *__restrict__ vv) {
32394648b7dSSebastian Grimberg   // Restriction with (unsigned) tridiagonal transformation
3247c1dbaffSSebastian Grimberg   CeedElemRestriction_Ref *impl;
3255c7e0f51SSebastian Grimberg   CeedScalar               vv_loc[block_size];
326ad70ee2cSJeremy L Thompson 
3271cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
328ad70ee2cSJeremy L Thompson   for (CeedInt e = start * block_size; e < stop * block_size; e += block_size) {
3297c1dbaffSSebastian Grimberg     for (CeedInt k = 0; k < num_comp; k++) {
3307c1dbaffSSebastian Grimberg       // Iteration bound set to discard padding elements
331ad70ee2cSJeremy L Thompson       const CeedInt block_end = CeedIntMin(block_size, num_elem - e);
33258c07c4fSSebastian Grimberg       CeedInt       n         = 0;
333ad70ee2cSJeremy L Thompson 
3345c7e0f51SSebastian Grimberg       CeedPragmaSIMD for (CeedInt j = 0; j < block_end; j++) {
3355c7e0f51SSebastian Grimberg         vv_loc[j] = uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] *
336ad70ee2cSJeremy L Thompson                         abs(impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size]) +
337ad70ee2cSJeremy L Thompson                     uu[e * elem_size * num_comp + (k * elem_size + n + 1) * block_size + j - v_offset] *
338ad70ee2cSJeremy L Thompson                         abs(impl->curl_orients[j + (3 * n + 3) * block_size + e * 3 * elem_size]);
3395c7e0f51SSebastian Grimberg       }
3405c7e0f51SSebastian Grimberg       for (CeedInt j = 0; j < block_end; j++) {
3415c7e0f51SSebastian Grimberg         CeedPragmaAtomic vv[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] += vv_loc[j];
3427c1dbaffSSebastian Grimberg       }
3437c1dbaffSSebastian Grimberg       for (n = 1; n < elem_size - 1; n++) {
3445c7e0f51SSebastian Grimberg         CeedPragmaSIMD for (CeedInt j = 0; j < block_end; j++) {
3455c7e0f51SSebastian Grimberg           vv_loc[j] = uu[e * elem_size * num_comp + (k * elem_size + n - 1) * block_size + j - v_offset] *
346ad70ee2cSJeremy L Thompson                           abs(impl->curl_orients[j + (3 * n - 1) * block_size + e * 3 * elem_size]) +
347ad70ee2cSJeremy L Thompson                       uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] *
348ad70ee2cSJeremy L Thompson                           abs(impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size]) +
349ad70ee2cSJeremy L Thompson                       uu[e * elem_size * num_comp + (k * elem_size + n + 1) * block_size + j - v_offset] *
350ad70ee2cSJeremy L Thompson                           abs(impl->curl_orients[j + (3 * n + 3) * block_size + e * 3 * elem_size]);
3517c1dbaffSSebastian Grimberg         }
352ad70ee2cSJeremy L Thompson         for (CeedInt j = 0; j < block_end; j++) {
3535c7e0f51SSebastian Grimberg           CeedPragmaAtomic vv[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] += vv_loc[j];
3545c7e0f51SSebastian Grimberg         }
3555c7e0f51SSebastian Grimberg       }
3565c7e0f51SSebastian Grimberg       CeedPragmaSIMD for (CeedInt j = 0; j < block_end; j++) {
3575c7e0f51SSebastian Grimberg         vv_loc[j] = uu[e * elem_size * num_comp + (k * elem_size + n - 1) * block_size + j - v_offset] *
358ad70ee2cSJeremy L Thompson                         abs(impl->curl_orients[j + (3 * n - 1) * block_size + e * 3 * elem_size]) +
359ad70ee2cSJeremy L Thompson                     uu[e * elem_size * num_comp + (k * elem_size + n) * block_size + j - v_offset] *
360ad70ee2cSJeremy L Thompson                         abs(impl->curl_orients[j + (3 * n + 1) * block_size + e * 3 * elem_size]);
3615c7e0f51SSebastian Grimberg       }
3625c7e0f51SSebastian Grimberg       for (CeedInt j = 0; j < block_end; j++) {
3635c7e0f51SSebastian Grimberg         CeedPragmaAtomic vv[impl->offsets[j + n * block_size + e * elem_size] + k * comp_stride] += vv_loc[j];
3647c1dbaffSSebastian Grimberg       }
3657c1dbaffSSebastian Grimberg     }
3667c1dbaffSSebastian Grimberg   }
3677c1dbaffSSebastian Grimberg   return CEED_ERROR_SUCCESS;
3687c1dbaffSSebastian Grimberg }
3697c1dbaffSSebastian Grimberg 
3701249ccc5SJeremy L Thompson static inline int CeedElemRestrictionApplyAtPointsInElement_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, CeedInt start, CeedInt stop,
371eda0adbcSSebastian Grimberg                                                                      CeedTransposeMode t_mode, const CeedScalar *__restrict__ uu,
372eda0adbcSSebastian Grimberg                                                                      CeedScalar *__restrict__ vv) {
37305fa913cSJeremy L Thompson   CeedInt                  num_points, l_vec_offset, e_vec_offset = 0;
37405fa913cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
37505fa913cSJeremy L Thompson 
37605fa913cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
37705fa913cSJeremy L Thompson   for (CeedInt e = start; e < stop; e++) {
3780930e4e7SJeremy L Thompson     l_vec_offset = impl->offsets[e];
37905fa913cSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr, e, &num_points));
38005fa913cSJeremy L Thompson     if (t_mode == CEED_NOTRANSPOSE) {
38105fa913cSJeremy L Thompson       for (CeedInt i = 0; i < num_points; i++) {
3829c34f28eSJeremy L Thompson         for (CeedInt j = 0; j < num_comp; j++) vv[j * num_points + i + e_vec_offset] = uu[impl->offsets[i + l_vec_offset] * num_comp + j];
38305fa913cSJeremy L Thompson       }
38405fa913cSJeremy L Thompson     } else {
38505fa913cSJeremy L Thompson       for (CeedInt i = 0; i < num_points; i++) {
3869c34f28eSJeremy L Thompson         for (CeedInt j = 0; j < num_comp; j++) vv[impl->offsets[i + l_vec_offset] * num_comp + j] = uu[j * num_points + i + e_vec_offset];
38705fa913cSJeremy L Thompson       }
38805fa913cSJeremy L Thompson     }
38905fa913cSJeremy L Thompson     e_vec_offset += num_points * num_comp;
39005fa913cSJeremy L Thompson   }
39105fa913cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
39205fa913cSJeremy L Thompson }
39305fa913cSJeremy L Thompson 
3941cc2c60dSJeremy L Thompson static inline int CeedElemRestrictionApply_Ref_Core(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size,
395ad70ee2cSJeremy L Thompson                                                     const CeedInt comp_stride, CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs,
396ad70ee2cSJeremy L Thompson                                                     bool use_orients, CeedVector u, CeedVector v, CeedRequest *request) {
397ad70ee2cSJeremy L Thompson   CeedInt             num_elem, elem_size, v_offset;
398ad70ee2cSJeremy L Thompson   CeedRestrictionType rstr_type;
3997c1dbaffSSebastian Grimberg   const CeedScalar   *uu;
4007c1dbaffSSebastian Grimberg   CeedScalar         *vv;
401ad70ee2cSJeremy L Thompson 
4021cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
4031cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
404ad70ee2cSJeremy L Thompson   v_offset = start * block_size * elem_size * num_comp;
4051cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
40694648b7dSSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &uu));
407ad70ee2cSJeremy L Thompson 
40894648b7dSSebastian Grimberg   if (t_mode == CEED_TRANSPOSE) {
40994648b7dSSebastian Grimberg     // Sum into for transpose mode, E-vector to L-vector
41094648b7dSSebastian Grimberg     CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_HOST, &vv));
41194648b7dSSebastian Grimberg   } else {
41294648b7dSSebastian Grimberg     // Overwrite for notranspose mode, L-vector to E-vector
41394648b7dSSebastian Grimberg     CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &vv));
41494648b7dSSebastian Grimberg   }
41594648b7dSSebastian Grimberg   if (t_mode == CEED_TRANSPOSE) {
4167c1dbaffSSebastian Grimberg     // Restriction from E-vector to L-vector
4177c1dbaffSSebastian Grimberg     // Performing v += r^T * u
4187c1dbaffSSebastian Grimberg     // uu has shape [elem_size, num_comp, num_elem], row-major
4197c1dbaffSSebastian Grimberg     // vv has shape [nnodes, num_comp]
4207c1dbaffSSebastian Grimberg     // Sum into for transpose mode
4217c1dbaffSSebastian Grimberg     switch (rstr_type) {
4227c1dbaffSSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
4235d10938bSJeremy L Thompson         CeedCallBackend(
4241cc2c60dSJeremy L Thompson             CeedElemRestrictionApplyStridedTranspose_Ref_Core(rstr, num_comp, block_size, start, stop, num_elem, elem_size, v_offset, uu, vv));
42594648b7dSSebastian Grimberg         break;
42661a27d74SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
427eda0adbcSSebastian Grimberg         CeedCallBackend(CeedElemRestrictionApplyOffsetTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem, elem_size,
4285d10938bSJeremy L Thompson                                                                          v_offset, uu, vv));
42994648b7dSSebastian Grimberg         break;
4307c1dbaffSSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
43194648b7dSSebastian Grimberg         if (use_signs) {
4321cc2c60dSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApplyOrientedTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem,
4331cc2c60dSJeremy L Thompson                                                                              elem_size, v_offset, uu, vv));
43494648b7dSSebastian Grimberg         } else {
435eda0adbcSSebastian Grimberg           CeedCallBackend(CeedElemRestrictionApplyOffsetTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem, elem_size,
436eda0adbcSSebastian Grimberg                                                                            v_offset, uu, vv));
43794648b7dSSebastian Grimberg         }
43894648b7dSSebastian Grimberg         break;
43994648b7dSSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
44094648b7dSSebastian Grimberg         if (use_signs && use_orients) {
4411cc2c60dSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApplyCurlOrientedTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem,
4425d10938bSJeremy L Thompson                                                                                  elem_size, v_offset, uu, vv));
44394648b7dSSebastian Grimberg         } else if (use_orients) {
4441cc2c60dSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApplyCurlOrientedUnsignedTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop,
4451cc2c60dSJeremy L Thompson                                                                                          num_elem, elem_size, v_offset, uu, vv));
44694648b7dSSebastian Grimberg         } else {
447eda0adbcSSebastian Grimberg           CeedCallBackend(CeedElemRestrictionApplyOffsetTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem, elem_size,
448eda0adbcSSebastian Grimberg                                                                            v_offset, uu, vv));
44994648b7dSSebastian Grimberg         }
45094648b7dSSebastian Grimberg         break;
4512c7e7413SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4521249ccc5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApplyAtPointsInElement_Ref_Core(rstr, num_comp, start, stop, t_mode, uu, vv));
4532c7e7413SJeremy L Thompson         break;
45494648b7dSSebastian Grimberg     }
45594648b7dSSebastian Grimberg   } else {
45694648b7dSSebastian Grimberg     // Restriction from L-vector to E-vector
45794648b7dSSebastian Grimberg     // Perform: v = r * u
45894648b7dSSebastian Grimberg     // vv has shape [elem_size, num_comp, num_elem], row-major
45994648b7dSSebastian Grimberg     // uu has shape [nnodes, num_comp]
46094648b7dSSebastian Grimberg     // Overwrite for notranspose mode
46194648b7dSSebastian Grimberg     switch (rstr_type) {
46294648b7dSSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
4635d10938bSJeremy L Thompson         CeedCallBackend(
4641cc2c60dSJeremy L Thompson             CeedElemRestrictionApplyStridedNoTranspose_Ref_Core(rstr, num_comp, block_size, start, stop, num_elem, elem_size, v_offset, uu, vv));
46594648b7dSSebastian Grimberg         break;
46661a27d74SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
467eda0adbcSSebastian Grimberg         CeedCallBackend(CeedElemRestrictionApplyOffsetNoTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem, elem_size,
468eda0adbcSSebastian Grimberg                                                                            v_offset, uu, vv));
46994648b7dSSebastian Grimberg         break;
47094648b7dSSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
47194648b7dSSebastian Grimberg         if (use_signs) {
4721cc2c60dSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApplyOrientedNoTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem,
4731cc2c60dSJeremy L Thompson                                                                                elem_size, v_offset, uu, vv));
47494648b7dSSebastian Grimberg         } else {
475eda0adbcSSebastian Grimberg           CeedCallBackend(CeedElemRestrictionApplyOffsetNoTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem,
4761cc2c60dSJeremy L Thompson                                                                              elem_size, v_offset, uu, vv));
47794648b7dSSebastian Grimberg         }
47894648b7dSSebastian Grimberg         break;
47994648b7dSSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
48094648b7dSSebastian Grimberg         if (use_signs && use_orients) {
4811cc2c60dSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApplyCurlOrientedNoTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem,
4825d10938bSJeremy L Thompson                                                                                    elem_size, v_offset, uu, vv));
48394648b7dSSebastian Grimberg         } else if (use_orients) {
4841cc2c60dSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApplyCurlOrientedUnsignedNoTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop,
4855d10938bSJeremy L Thompson                                                                                            num_elem, elem_size, v_offset, uu, vv));
48694648b7dSSebastian Grimberg         } else {
487eda0adbcSSebastian Grimberg           CeedCallBackend(CeedElemRestrictionApplyOffsetNoTranspose_Ref_Core(rstr, num_comp, block_size, comp_stride, start, stop, num_elem,
4881cc2c60dSJeremy L Thompson                                                                              elem_size, v_offset, uu, vv));
48994648b7dSSebastian Grimberg         }
49094648b7dSSebastian Grimberg         break;
4912c7e7413SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4921249ccc5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApplyAtPointsInElement_Ref_Core(rstr, num_comp, start, stop, t_mode, uu, vv));
4932c7e7413SJeremy L Thompson         break;
49494648b7dSSebastian Grimberg     }
4957c1dbaffSSebastian Grimberg   }
4967c1dbaffSSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(u, &uu));
4977c1dbaffSSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArray(v, &vv));
4987c1dbaffSSebastian Grimberg   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
4997c1dbaffSSebastian Grimberg   return CEED_ERROR_SUCCESS;
5007c1dbaffSSebastian Grimberg }
5017c1dbaffSSebastian Grimberg 
5027c1dbaffSSebastian Grimberg //------------------------------------------------------------------------------
503f10650afSjeremylt // ElemRestriction Apply - Common Sizes
504f10650afSjeremylt //------------------------------------------------------------------------------
5051cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_110(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5067c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5077c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5081cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 1, 1, comp_stride, start, stop, t_mode, use_signs, use_orients, u, v, request);
509d979a051Sjeremylt }
510d979a051Sjeremylt 
5111cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_111(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5127c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5137c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5141cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 1, 1, 1, start, stop, t_mode, use_signs, use_orients, u, v, request);
5154d2a38eeSjeremylt }
5164d2a38eeSjeremylt 
5171cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_180(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5187c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5197c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5201cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 1, 8, comp_stride, start, stop, t_mode, use_signs, use_orients, u, v, request);
5219c36149bSjeremylt }
5229c36149bSjeremylt 
5231cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_181(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5247c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5257c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5261cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 1, 8, 1, start, stop, t_mode, use_signs, use_orients, u, v, request);
5279c36149bSjeremylt }
5289c36149bSjeremylt 
5291cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_310(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5307c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5317c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5321cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 3, 1, comp_stride, start, stop, t_mode, use_signs, use_orients, u, v, request);
533d979a051Sjeremylt }
534d979a051Sjeremylt 
5351cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_311(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5367c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5377c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5381cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 3, 1, 1, start, stop, t_mode, use_signs, use_orients, u, v, request);
539d979a051Sjeremylt }
540d979a051Sjeremylt 
5411cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_380(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5427c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5437c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5441cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 3, 8, comp_stride, start, stop, t_mode, use_signs, use_orients, u, v, request);
545d979a051Sjeremylt }
546d979a051Sjeremylt 
5471cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_381(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5487c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5497c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5501cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 3, 8, 1, start, stop, t_mode, use_signs, use_orients, u, v, request);
551d979a051Sjeremylt }
552d979a051Sjeremylt 
553bf4d1581Sjeremylt // LCOV_EXCL_START
5541cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_510(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5557c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5567c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5571cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 5, 1, comp_stride, start, stop, t_mode, use_signs, use_orients, u, v, request);
558d979a051Sjeremylt }
559bf4d1581Sjeremylt // LCOV_EXCL_STOP
560d979a051Sjeremylt 
5611cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_511(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5627c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5637c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5641cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 5, 1, 1, start, stop, t_mode, use_signs, use_orients, u, v, request);
565d979a051Sjeremylt }
566d979a051Sjeremylt 
567bf4d1581Sjeremylt // LCOV_EXCL_START
5681cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_580(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5697c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5707c1dbaffSSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5711cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 5, 8, comp_stride, start, stop, t_mode, use_signs, use_orients, u, v, request);
572d979a051Sjeremylt }
573bf4d1581Sjeremylt // LCOV_EXCL_STOP
574d979a051Sjeremylt 
5751cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref_581(CeedElemRestriction rstr, const CeedInt num_comp, const CeedInt block_size, const CeedInt comp_stride,
5767c1dbaffSSebastian Grimberg                                             CeedInt start, CeedInt stop, CeedTransposeMode t_mode, bool use_signs, bool use_orients, CeedVector u,
5770c73c039SSebastian Grimberg                                             CeedVector v, CeedRequest *request) {
5781cc2c60dSJeremy L Thompson   return CeedElemRestrictionApply_Ref_Core(rstr, 5, 8, 1, start, stop, t_mode, use_signs, use_orients, u, v, request);
5794d2a38eeSjeremylt }
5804d2a38eeSjeremylt 
581f10650afSjeremylt //------------------------------------------------------------------------------
582f10650afSjeremylt // ElemRestriction Apply
583f10650afSjeremylt //------------------------------------------------------------------------------
5841cc2c60dSJeremy L Thompson static int CeedElemRestrictionApply_Ref(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
585ad70ee2cSJeremy L Thompson   CeedInt                  num_block, block_size, num_comp, comp_stride;
586ad70ee2cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
587ad70ee2cSJeremy L Thompson 
5881cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumBlocks(rstr, &num_block));
5891cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetBlockSize(rstr, &block_size));
5901cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
5911cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
5921cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
5931cc2c60dSJeremy L Thompson   CeedCallBackend(impl->Apply(rstr, num_comp, block_size, comp_stride, 0, num_block, t_mode, true, true, u, v, request));
5945d10938bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
595f30b1135SSebastian Grimberg }
596f30b1135SSebastian Grimberg 
597f30b1135SSebastian Grimberg //------------------------------------------------------------------------------
598f30b1135SSebastian Grimberg // ElemRestriction Apply Unsigned
599f30b1135SSebastian Grimberg //------------------------------------------------------------------------------
6001cc2c60dSJeremy L Thompson static int CeedElemRestrictionApplyUnsigned_Ref(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
6011cc2c60dSJeremy L Thompson                                                 CeedRequest *request) {
602ad70ee2cSJeremy L Thompson   CeedInt                  num_block, block_size, num_comp, comp_stride;
603ad70ee2cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
604ad70ee2cSJeremy L Thompson 
6051cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumBlocks(rstr, &num_block));
6061cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetBlockSize(rstr, &block_size));
6071cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
6081cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
6091cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
6101cc2c60dSJeremy L Thompson   CeedCallBackend(impl->Apply(rstr, num_comp, block_size, comp_stride, 0, num_block, t_mode, false, true, u, v, request));
6115d10938bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6127c1dbaffSSebastian Grimberg }
6137c1dbaffSSebastian Grimberg 
6147c1dbaffSSebastian Grimberg //------------------------------------------------------------------------------
6157c1dbaffSSebastian Grimberg // ElemRestriction Apply Unoriented
6167c1dbaffSSebastian Grimberg //------------------------------------------------------------------------------
6171cc2c60dSJeremy L Thompson static int CeedElemRestrictionApplyUnoriented_Ref(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
6181cc2c60dSJeremy L Thompson                                                   CeedRequest *request) {
619ad70ee2cSJeremy L Thompson   CeedInt                  num_block, block_size, num_comp, comp_stride;
620ad70ee2cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
621ad70ee2cSJeremy L Thompson 
6221cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumBlocks(rstr, &num_block));
6231cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetBlockSize(rstr, &block_size));
6241cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
6251cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
6261cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
6271cc2c60dSJeremy L Thompson   CeedCallBackend(impl->Apply(rstr, num_comp, block_size, comp_stride, 0, num_block, t_mode, false, false, u, v, request));
6285d10938bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6299c36149bSjeremylt }
630be9261b7Sjeremylt 
631f10650afSjeremylt //------------------------------------------------------------------------------
6322c7e7413SJeremy L Thompson // ElemRestriction Apply Points
6332c7e7413SJeremy L Thompson //------------------------------------------------------------------------------
634eda0adbcSSebastian Grimberg static int CeedElemRestrictionApplyAtPointsInElement_Ref(CeedElemRestriction rstr, CeedInt elem, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
6352c7e7413SJeremy L Thompson                                                          CeedRequest *request) {
63605fa913cSJeremy L Thompson   CeedInt                  num_comp;
6372c7e7413SJeremy L Thompson   CeedElemRestriction_Ref *impl;
6382c7e7413SJeremy L Thompson 
639eda0adbcSSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
640eda0adbcSSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
641eda0adbcSSebastian Grimberg   return impl->Apply(rstr, num_comp, 0, 1, elem, elem + 1, t_mode, false, false, u, v, request);
6422c7e7413SJeremy L Thompson }
6432c7e7413SJeremy L Thompson 
6442c7e7413SJeremy L Thompson //------------------------------------------------------------------------------
645f10650afSjeremylt // ElemRestriction Apply Block
646f10650afSjeremylt //------------------------------------------------------------------------------
6471cc2c60dSJeremy L Thompson static int CeedElemRestrictionApplyBlock_Ref(CeedElemRestriction rstr, CeedInt block, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
648074cb416Sjeremylt                                              CeedRequest *request) {
649ad70ee2cSJeremy L Thompson   CeedInt                  block_size, num_comp, comp_stride;
650ad70ee2cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
651ad70ee2cSJeremy L Thompson 
6521cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetBlockSize(rstr, &block_size));
6531cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
6541cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
6551cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
6561cc2c60dSJeremy L Thompson   CeedCallBackend(impl->Apply(rstr, num_comp, block_size, comp_stride, block, block + 1, t_mode, true, true, u, v, request));
6575d10938bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6589c36149bSjeremylt }
659be9261b7Sjeremylt 
660f10650afSjeremylt //------------------------------------------------------------------------------
661bd33150aSjeremylt // ElemRestriction Get Offsets
662bd33150aSjeremylt //------------------------------------------------------------------------------
6632b730f8bSJeremy L Thompson static int CeedElemRestrictionGetOffsets_Ref(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
664bd33150aSjeremylt   Ceed                     ceed;
665ad70ee2cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
666ad70ee2cSJeremy L Thompson 
667ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
6682b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
669bd33150aSjeremylt 
6706574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
671bd33150aSjeremylt 
672bd33150aSjeremylt   *offsets = impl->offsets;
673e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
674bd33150aSjeremylt }
675bd33150aSjeremylt 
676bd33150aSjeremylt //------------------------------------------------------------------------------
67777d1c127SSebastian Grimberg // ElemRestriction Get Orientations
67877d1c127SSebastian Grimberg //------------------------------------------------------------------------------
67977d1c127SSebastian Grimberg static int CeedElemRestrictionGetOrientations_Ref(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
68077d1c127SSebastian Grimberg   Ceed                     ceed;
681ad70ee2cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
682ad70ee2cSJeremy L Thompson 
683ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
68477d1c127SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
68577d1c127SSebastian Grimberg 
686fcbe8c06SSebastian Grimberg   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
68777d1c127SSebastian Grimberg 
68877d1c127SSebastian Grimberg   *orients = impl->orients;
68977d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
69077d1c127SSebastian Grimberg }
69177d1c127SSebastian Grimberg 
69277d1c127SSebastian Grimberg //------------------------------------------------------------------------------
69377d1c127SSebastian Grimberg // ElemRestriction Get Curl-Conforming Orientations
69477d1c127SSebastian Grimberg //------------------------------------------------------------------------------
6950c73c039SSebastian Grimberg static int CeedElemRestrictionGetCurlOrientations_Ref(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
69677d1c127SSebastian Grimberg   Ceed                     ceed;
697ad70ee2cSJeremy L Thompson   CeedElemRestriction_Ref *impl;
698ad70ee2cSJeremy L Thompson 
699ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
70077d1c127SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
70177d1c127SSebastian Grimberg 
702fcbe8c06SSebastian Grimberg   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
70377d1c127SSebastian Grimberg 
70477d1c127SSebastian Grimberg   *curl_orients = impl->curl_orients;
70577d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
70677d1c127SSebastian Grimberg }
70777d1c127SSebastian Grimberg 
70877d1c127SSebastian Grimberg //------------------------------------------------------------------------------
709f10650afSjeremylt // ElemRestriction Destroy
710f10650afSjeremylt //------------------------------------------------------------------------------
7111cc2c60dSJeremy L Thompson static int CeedElemRestrictionDestroy_Ref(CeedElemRestriction rstr) {
712fe2413ffSjeremylt   CeedElemRestriction_Ref *impl;
71321617c04Sjeremylt 
7141cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
7152b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->offsets_allocated));
71677d1c127SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->orients_allocated));
71777d1c127SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->curl_orients_allocated));
7182b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
719e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72021617c04Sjeremylt }
72121617c04Sjeremylt 
722f10650afSjeremylt //------------------------------------------------------------------------------
723f10650afSjeremylt // ElemRestriction Create
724f10650afSjeremylt //------------------------------------------------------------------------------
725fcbe8c06SSebastian Grimberg int CeedElemRestrictionCreate_Ref(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
7261cc2c60dSJeremy L Thompson                                   const CeedInt8 *curl_orients, CeedElemRestriction rstr) {
727ad70ee2cSJeremy L Thompson   Ceed                     ceed;
72807d5dec1SJeremy L Thompson   CeedInt                  num_elem, elem_size, num_block, block_size, num_comp, comp_stride, num_points = 0, num_offsets;
729ad70ee2cSJeremy L Thompson   CeedRestrictionType      rstr_type;
73021617c04Sjeremylt   CeedElemRestriction_Ref *impl;
731ad70ee2cSJeremy L Thompson 
7321cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
7331cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
7341cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
7351cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumBlocks(rstr, &num_block));
7361cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetBlockSize(rstr, &block_size));
7371cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
7381cc2c60dSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
739*22eb1385SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
74021617c04Sjeremylt 
7416574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Only MemType = HOST supported");
742dce49693SSebastian Grimberg 
7432b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
744dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetData(rstr, impl));
745*22eb1385SJeremy L Thompson 
746*22eb1385SJeremy L Thompson   // Set layouts
747*22eb1385SJeremy L Thompson   {
748*22eb1385SJeremy L Thompson     bool    has_backend_strides;
749*22eb1385SJeremy L Thompson     CeedInt layout[3] = {1, elem_size, elem_size * num_comp};
750*22eb1385SJeremy L Thompson 
751dce49693SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetELayout(rstr, layout));
752*22eb1385SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_STRIDED) {
753*22eb1385SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
754*22eb1385SJeremy L Thompson       if (has_backend_strides) {
755*22eb1385SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionSetLLayout(rstr, layout));
756*22eb1385SJeremy L Thompson       }
757*22eb1385SJeremy L Thompson     }
758*22eb1385SJeremy L Thompson   }
7593661185eSjeremylt 
76092fe105eSJeremy L Thompson   // Offsets data
761fcbe8c06SSebastian Grimberg   if (rstr_type != CEED_RESTRICTION_STRIDED) {
7623661185eSjeremylt     const char *resource;
763ad70ee2cSJeremy L Thompson 
764ad70ee2cSJeremy L Thompson     // Check indices for ref or memcheck backends
76535aed383SJeremy L Thompson     {
76635aed383SJeremy L Thompson       Ceed current = ceed, parent = NULL;
76735aed383SJeremy L Thompson 
76835aed383SJeremy L Thompson       CeedCallBackend(CeedGetParent(current, &parent));
76935aed383SJeremy L Thompson       while (current != parent) {
77035aed383SJeremy L Thompson         current = parent;
77135aed383SJeremy L Thompson         CeedCallBackend(CeedGetParent(current, &parent));
77235aed383SJeremy L Thompson       }
77335aed383SJeremy L Thompson       CeedCallBackend(CeedGetResource(parent, &resource));
77435aed383SJeremy L Thompson     }
7752b730f8bSJeremy L Thompson     if (!strcmp(resource, "/cpu/self/ref/serial") || !strcmp(resource, "/cpu/self/ref/blocked") || !strcmp(resource, "/cpu/self/memcheck/serial") ||
776d1d35e2fSjeremylt         !strcmp(resource, "/cpu/self/memcheck/blocked")) {
777e79b91d9SJeremy L Thompson       CeedSize l_size;
7783661185eSjeremylt 
7791cc2c60dSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
7802b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < num_elem * elem_size; i++) {
7816574a04fSJeremy L Thompson         CeedCheck(offsets[i] >= 0 && offsets[i] + (num_comp - 1) * comp_stride < l_size, ceed, CEED_ERROR_BACKEND,
7826574a04fSJeremy L Thompson                   "Restriction offset %" CeedInt_FMT " (%" CeedInt_FMT ") out of range [0, %" CeedInt_FMT "]", i, offsets[i], l_size);
7832b730f8bSJeremy L Thompson       }
7842b730f8bSJeremy L Thompson     }
7853661185eSjeremylt 
78692fe105eSJeremy L Thompson     // Copy data
78707d5dec1SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) CeedCallBackend(CeedElemRestrictionGetNumPoints(rstr, &num_points));
78807d5dec1SJeremy L Thompson     num_offsets = rstr_type == CEED_RESTRICTION_POINTS ? (num_elem + 1 + num_points) : (num_elem * elem_size);
789d1d35e2fSjeremylt     switch (copy_mode) {
79021617c04Sjeremylt       case CEED_COPY_VALUES:
79107d5dec1SJeremy L Thompson         CeedCallBackend(CeedMalloc(num_offsets, &impl->offsets_allocated));
79207d5dec1SJeremy L Thompson         memcpy(impl->offsets_allocated, offsets, num_offsets * sizeof(offsets[0]));
793d979a051Sjeremylt         impl->offsets = impl->offsets_allocated;
79421617c04Sjeremylt         break;
79521617c04Sjeremylt       case CEED_OWN_POINTER:
796d979a051Sjeremylt         impl->offsets_allocated = (CeedInt *)offsets;
797d979a051Sjeremylt         impl->offsets           = impl->offsets_allocated;
79821617c04Sjeremylt         break;
79921617c04Sjeremylt       case CEED_USE_POINTER:
800d979a051Sjeremylt         impl->offsets = offsets;
80121617c04Sjeremylt     }
802fcbe8c06SSebastian Grimberg 
803fcbe8c06SSebastian Grimberg     // Orientation data
804fcbe8c06SSebastian Grimberg     if (rstr_type == CEED_RESTRICTION_ORIENTED) {
8050305e208SSebastian Grimberg       CeedCheck(orients != NULL, ceed, CEED_ERROR_BACKEND, "No orients array provided for oriented restriction");
806fcbe8c06SSebastian Grimberg       switch (copy_mode) {
807fcbe8c06SSebastian Grimberg         case CEED_COPY_VALUES:
80807d5dec1SJeremy L Thompson           CeedCallBackend(CeedMalloc(num_offsets, &impl->orients_allocated));
80907d5dec1SJeremy L Thompson           memcpy(impl->orients_allocated, orients, num_offsets * sizeof(orients[0]));
810fcbe8c06SSebastian Grimberg           impl->orients = impl->orients_allocated;
811fcbe8c06SSebastian Grimberg           break;
812fcbe8c06SSebastian Grimberg         case CEED_OWN_POINTER:
813fcbe8c06SSebastian Grimberg           impl->orients_allocated = (bool *)orients;
814fcbe8c06SSebastian Grimberg           impl->orients           = impl->orients_allocated;
815fcbe8c06SSebastian Grimberg           break;
816fcbe8c06SSebastian Grimberg         case CEED_USE_POINTER:
817fcbe8c06SSebastian Grimberg           impl->orients = orients;
818fcbe8c06SSebastian Grimberg       }
819fcbe8c06SSebastian Grimberg     } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
8200305e208SSebastian Grimberg       CeedCheck(curl_orients != NULL, ceed, CEED_ERROR_BACKEND, "No curl_orients array provided for oriented restriction");
821fcbe8c06SSebastian Grimberg       switch (copy_mode) {
822fcbe8c06SSebastian Grimberg         case CEED_COPY_VALUES:
82307d5dec1SJeremy L Thompson           CeedCallBackend(CeedMalloc(3 * num_offsets, &impl->curl_orients_allocated));
82407d5dec1SJeremy L Thompson           memcpy(impl->curl_orients_allocated, curl_orients, 3 * num_offsets * sizeof(curl_orients[0]));
825fcbe8c06SSebastian Grimberg           impl->curl_orients = impl->curl_orients_allocated;
826fcbe8c06SSebastian Grimberg           break;
827fcbe8c06SSebastian Grimberg         case CEED_OWN_POINTER:
8280c73c039SSebastian Grimberg           impl->curl_orients_allocated = (CeedInt8 *)curl_orients;
829fcbe8c06SSebastian Grimberg           impl->curl_orients           = impl->curl_orients_allocated;
830fcbe8c06SSebastian Grimberg           break;
831fcbe8c06SSebastian Grimberg         case CEED_USE_POINTER:
832fcbe8c06SSebastian Grimberg           impl->curl_orients = curl_orients;
833fcbe8c06SSebastian Grimberg       }
834fcbe8c06SSebastian Grimberg     }
83592fe105eSJeremy L Thompson   }
836fe2413ffSjeremylt 
837ad70ee2cSJeremy L Thompson   // Set apply function based upon num_comp, block_size, and comp_stride
838ad70ee2cSJeremy L Thompson   CeedInt index = -1;
839ad70ee2cSJeremy L Thompson 
840ad70ee2cSJeremy L Thompson   if (block_size < 10) index = 100 * num_comp + 10 * block_size + (comp_stride == 1);
841ad70ee2cSJeremy L Thompson   switch (index) {
842d979a051Sjeremylt     case 110:
843d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_110;
844d979a051Sjeremylt       break;
845d979a051Sjeremylt     case 111:
846d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_111;
847d979a051Sjeremylt       break;
848d979a051Sjeremylt     case 180:
849d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_180;
850d979a051Sjeremylt       break;
851d979a051Sjeremylt     case 181:
852d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_181;
853d979a051Sjeremylt       break;
854d979a051Sjeremylt     case 310:
855d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_310;
856d979a051Sjeremylt       break;
857d979a051Sjeremylt     case 311:
858d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_311;
859d979a051Sjeremylt       break;
860d979a051Sjeremylt     case 380:
861d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_380;
862d979a051Sjeremylt       break;
863d979a051Sjeremylt     case 381:
864d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_381;
865d979a051Sjeremylt       break;
866bf4d1581Sjeremylt     // LCOV_EXCL_START
867d979a051Sjeremylt     case 510:
868d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_510;
869d979a051Sjeremylt       break;
870bf4d1581Sjeremylt     // LCOV_EXCL_STOP
871d979a051Sjeremylt     case 511:
872d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_511;
873d979a051Sjeremylt       break;
874bf4d1581Sjeremylt     // LCOV_EXCL_START
875d979a051Sjeremylt     case 580:
876d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_580;
877d979a051Sjeremylt       break;
878bf4d1581Sjeremylt     // LCOV_EXCL_STOP
879d979a051Sjeremylt     case 581:
880d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_581;
881d979a051Sjeremylt       break;
882d979a051Sjeremylt     default:
883d979a051Sjeremylt       impl->Apply = CeedElemRestrictionApply_Ref_Core;
884d979a051Sjeremylt       break;
885d979a051Sjeremylt   }
886dce49693SSebastian Grimberg 
887dce49693SSebastian Grimberg   // Register backend functions
888dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Apply", CeedElemRestrictionApply_Ref));
889dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnsigned", CeedElemRestrictionApplyUnsigned_Ref));
890dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApplyUnoriented_Ref));
891dce49693SSebastian Grimberg   if (rstr_type == CEED_RESTRICTION_POINTS) {
892dce49693SSebastian Grimberg     CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyAtPointsInElement", CeedElemRestrictionApplyAtPointsInElement_Ref));
893dce49693SSebastian Grimberg   }
894dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyBlock", CeedElemRestrictionApplyBlock_Ref));
895dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Ref));
896dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Ref));
897dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Ref));
898dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Ref));
899e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90021617c04Sjeremylt }
901fc0567d9Srezgarshakeri 
902fc0567d9Srezgarshakeri //------------------------------------------------------------------------------
903