xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-restriction.c (revision 56c48462b0d39517237dc0770653f43cb7356d08)
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.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
10437930d1SJeremy L Thompson #include <ceed/jit-tools.h>
110d0321e0SJeremy L Thompson #include <stdbool.h>
120d0321e0SJeremy L Thompson #include <stddef.h>
1344d7a66cSJeremy L Thompson #include <string.h>
14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
190d0321e0SJeremy L Thompson 
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
21cf8cbdd6SSebastian Grimberg // Compile restriction kernels
22cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
23cf8cbdd6SSebastian Grimberg static inline int CeedElemRestrictionSetupCompile_Hip(CeedElemRestriction rstr) {
24cf8cbdd6SSebastian Grimberg   Ceed                     ceed;
25cf8cbdd6SSebastian Grimberg   bool                     is_deterministic;
26cf8cbdd6SSebastian Grimberg   CeedInt                  num_elem, num_comp, elem_size, comp_stride;
27cf8cbdd6SSebastian Grimberg   CeedRestrictionType      rstr_type;
28cf8cbdd6SSebastian Grimberg   char                    *restriction_kernel_path, *restriction_kernel_source;
29cf8cbdd6SSebastian Grimberg   CeedElemRestriction_Hip *impl;
30cf8cbdd6SSebastian Grimberg 
31cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
32cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
33cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
34cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
35cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
36cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
37cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
38cf8cbdd6SSebastian Grimberg   is_deterministic = impl->d_l_vec_indices != NULL;
39cf8cbdd6SSebastian Grimberg 
40cf8cbdd6SSebastian Grimberg   // Compile HIP kernels
41cf8cbdd6SSebastian Grimberg   switch (rstr_type) {
42cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STRIDED: {
43cf8cbdd6SSebastian Grimberg       CeedInt strides[3] = {1, num_elem * elem_size, elem_size};
44cf8cbdd6SSebastian Grimberg       bool    has_backend_strides;
45cf8cbdd6SSebastian Grimberg 
46cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
47cf8cbdd6SSebastian Grimberg       if (!has_backend_strides) {
48*56c48462SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetStrides(rstr, strides));
49cf8cbdd6SSebastian Grimberg       }
50cf8cbdd6SSebastian Grimberg 
51cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-strided.h", &restriction_kernel_path));
52cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
53cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
54cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
55cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
56cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_STRIDE_NODES", strides[0], "RSTR_STRIDE_COMP", strides[1], "RSTR_STRIDE_ELEM",
57cf8cbdd6SSebastian Grimberg                                       strides[2]));
58cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->ApplyNoTranspose));
59cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->ApplyTranspose));
60cf8cbdd6SSebastian Grimberg     } break;
61cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STANDARD: {
62cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &restriction_kernel_path));
63cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
64cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
65cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
66cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
67cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
68cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
69cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyNoTranspose));
70cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyTranspose));
71cf8cbdd6SSebastian Grimberg     } break;
72cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_ORIENTED: {
73cf8cbdd6SSebastian Grimberg       char *offset_kernel_path;
74cf8cbdd6SSebastian Grimberg 
75cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-oriented.h", &restriction_kernel_path));
76cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
77cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
78cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path));
79cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source));
80cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
81cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
82cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
83cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
84cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedNoTranspose", &impl->ApplyNoTranspose));
85cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnsignedNoTranspose));
86cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedTranspose", &impl->ApplyTranspose));
87cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnsignedTranspose));
88cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
89cf8cbdd6SSebastian Grimberg     } break;
90cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_CURL_ORIENTED: {
91cf8cbdd6SSebastian Grimberg       char *offset_kernel_path;
92cf8cbdd6SSebastian Grimberg 
93cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-curl-oriented.h", &restriction_kernel_path));
94cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
95cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
96cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path));
97cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source));
98cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
99cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
100cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
101cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
102cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedNoTranspose", &impl->ApplyNoTranspose));
103cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedNoTranspose", &impl->ApplyUnsignedNoTranspose));
104cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnorientedNoTranspose));
105cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedTranspose", &impl->ApplyTranspose));
106cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedTranspose", &impl->ApplyUnsignedTranspose));
107cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnorientedTranspose));
108cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
109cf8cbdd6SSebastian Grimberg     } break;
110cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_POINTS: {
111cf8cbdd6SSebastian Grimberg       // LCOV_EXCL_START
112cf8cbdd6SSebastian Grimberg       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
113cf8cbdd6SSebastian Grimberg       // LCOV_EXCL_STOP
114cf8cbdd6SSebastian Grimberg     } break;
115cf8cbdd6SSebastian Grimberg   }
116cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_path));
117cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_source));
118cf8cbdd6SSebastian Grimberg   return CEED_ERROR_SUCCESS;
119cf8cbdd6SSebastian Grimberg }
120cf8cbdd6SSebastian Grimberg 
121cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
122dce49693SSebastian Grimberg // Core apply restriction code
1230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
124dce49693SSebastian Grimberg static inline int CeedElemRestrictionApply_Hip_Core(CeedElemRestriction rstr, CeedTransposeMode t_mode, bool use_signs, bool use_orients,
125dce49693SSebastian Grimberg                                                     CeedVector u, CeedVector v, CeedRequest *request) {
1260d0321e0SJeremy L Thompson   Ceed                     ceed;
127dce49693SSebastian Grimberg   CeedRestrictionType      rstr_type;
1280d0321e0SJeremy L Thompson   const CeedScalar        *d_u;
1290d0321e0SJeremy L Thompson   CeedScalar              *d_v;
130b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
131b7453713SJeremy L Thompson 
132dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
133dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
134dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
135cf8cbdd6SSebastian Grimberg 
136cf8cbdd6SSebastian Grimberg   // Assemble kernel if needed
137cf8cbdd6SSebastian Grimberg   if (!impl->module) {
138cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetupCompile_Hip(rstr));
139cf8cbdd6SSebastian Grimberg   }
140b7453713SJeremy L Thompson 
141b7453713SJeremy L Thompson   // Get vectors
1422b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
143437930d1SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
1440d0321e0SJeremy L Thompson     // Sum into for transpose mode, e-vec to l-vec
1452b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
1460d0321e0SJeremy L Thompson   } else {
1470d0321e0SJeremy L Thompson     // Overwrite for notranspose mode, l-vec to e-vec
1482b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1490d0321e0SJeremy L Thompson   }
1500d0321e0SJeremy L Thompson 
1510d0321e0SJeremy L Thompson   // Restrict
152437930d1SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
1530d0321e0SJeremy L Thompson     // L-vector -> E-vector
154cf8cbdd6SSebastian Grimberg     CeedInt elem_size;
155cf8cbdd6SSebastian Grimberg 
156cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
157dce49693SSebastian Grimberg     const CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
158cf8cbdd6SSebastian Grimberg     const CeedInt grid       = CeedDivUpInt(impl->num_nodes, block_size);
15958549094SSebastian Grimberg 
160dce49693SSebastian Grimberg     switch (rstr_type) {
161dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
162cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
16358549094SSebastian Grimberg 
164cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
165dce49693SSebastian Grimberg       } break;
166dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
167cf8cbdd6SSebastian Grimberg         void *args[] = {&impl->d_ind, &d_u, &d_v};
168dce49693SSebastian Grimberg 
169cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
170dce49693SSebastian Grimberg       } break;
171dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
172dce49693SSebastian Grimberg         if (use_signs) {
173cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &impl->d_orients, &d_u, &d_v};
174dce49693SSebastian Grimberg 
175cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
176dce49693SSebastian Grimberg         } else {
177cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &d_u, &d_v};
178dce49693SSebastian Grimberg 
179cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
180dce49693SSebastian Grimberg         }
181dce49693SSebastian Grimberg       } break;
182dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
183dce49693SSebastian Grimberg         if (use_signs && use_orients) {
184cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
185dce49693SSebastian Grimberg 
186cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
187dce49693SSebastian Grimberg         } else if (use_orients) {
188cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
189dce49693SSebastian Grimberg 
190cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
191dce49693SSebastian Grimberg         } else {
192cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &d_u, &d_v};
193dce49693SSebastian Grimberg 
194cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedNoTranspose, grid, block_size, args));
195dce49693SSebastian Grimberg         }
196dce49693SSebastian Grimberg       } break;
197b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
198b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
199b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
200b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
201b3d03e38SSebastian Grimberg       } break;
2020d0321e0SJeremy L Thompson     }
2030d0321e0SJeremy L Thompson   } else {
2040d0321e0SJeremy L Thompson     // E-vector -> L-vector
205cf8cbdd6SSebastian Grimberg     const bool    is_deterministic = impl->d_l_vec_indices != NULL;
206dce49693SSebastian Grimberg     const CeedInt block_size       = 64;
207cf8cbdd6SSebastian Grimberg     const CeedInt grid             = CeedDivUpInt(impl->num_nodes, block_size);
208b7453713SJeremy L Thompson 
209dce49693SSebastian Grimberg     switch (rstr_type) {
210dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
211cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
212dce49693SSebastian Grimberg 
213cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
214dce49693SSebastian Grimberg       } break;
215dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
216cf8cbdd6SSebastian Grimberg         if (!is_deterministic) {
217cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &d_u, &d_v};
21858549094SSebastian Grimberg 
219cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2200d0321e0SJeremy L Thompson         } else {
22158549094SSebastian Grimberg           void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
22258549094SSebastian Grimberg 
223cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
22458549094SSebastian Grimberg         }
225dce49693SSebastian Grimberg       } break;
226dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
227dce49693SSebastian Grimberg         if (use_signs) {
228cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
229cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &impl->d_orients, &d_u, &d_v};
23058549094SSebastian Grimberg 
231cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
232dce49693SSebastian Grimberg           } else {
2337aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_orients, &d_u, &d_v};
2347aa91133SSebastian Grimberg 
235cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2367aa91133SSebastian Grimberg           }
2377aa91133SSebastian Grimberg         } else {
238cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
239cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &d_u, &d_v};
240dce49693SSebastian Grimberg 
241cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
242dce49693SSebastian Grimberg           } else {
243dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
244dce49693SSebastian Grimberg 
245cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
246dce49693SSebastian Grimberg           }
247dce49693SSebastian Grimberg         }
248dce49693SSebastian Grimberg       } break;
249dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
250dce49693SSebastian Grimberg         if (use_signs && use_orients) {
251cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
252cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
253dce49693SSebastian Grimberg 
254cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2557aa91133SSebastian Grimberg           } else {
2567aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
2577aa91133SSebastian Grimberg 
258cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2597aa91133SSebastian Grimberg           }
260dce49693SSebastian Grimberg         } else if (use_orients) {
261cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
262cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
263dce49693SSebastian Grimberg 
264cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
265dce49693SSebastian Grimberg           } else {
2667aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
2677aa91133SSebastian Grimberg 
268cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
2697aa91133SSebastian Grimberg           }
2707aa91133SSebastian Grimberg         } else {
271cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
272cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &d_u, &d_v};
273dce49693SSebastian Grimberg 
274cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args));
275dce49693SSebastian Grimberg           } else {
276dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
277dce49693SSebastian Grimberg 
278cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args));
279dce49693SSebastian Grimberg           }
280dce49693SSebastian Grimberg         }
281dce49693SSebastian Grimberg       } break;
282b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
283b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
284b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
285b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
286b3d03e38SSebastian Grimberg       } break;
2870d0321e0SJeremy L Thompson     }
2880d0321e0SJeremy L Thompson   }
2890d0321e0SJeremy L Thompson 
2902b730f8bSJeremy L Thompson   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
2910d0321e0SJeremy L Thompson 
2920d0321e0SJeremy L Thompson   // Restore arrays
2932b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
2942b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2950d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2960d0321e0SJeremy L Thompson }
2970d0321e0SJeremy L Thompson 
2980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
299dce49693SSebastian Grimberg // Apply restriction
300dce49693SSebastian Grimberg //------------------------------------------------------------------------------
301dce49693SSebastian Grimberg static int CeedElemRestrictionApply_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
302dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, true, true, u, v, request);
303dce49693SSebastian Grimberg }
304dce49693SSebastian Grimberg 
305dce49693SSebastian Grimberg //------------------------------------------------------------------------------
306dce49693SSebastian Grimberg // Apply unsigned restriction
307dce49693SSebastian Grimberg //------------------------------------------------------------------------------
308dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnsigned_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
309dce49693SSebastian Grimberg                                                 CeedRequest *request) {
310dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, true, u, v, request);
311dce49693SSebastian Grimberg }
312dce49693SSebastian Grimberg 
313dce49693SSebastian Grimberg //------------------------------------------------------------------------------
314dce49693SSebastian Grimberg // Apply unoriented restriction
315dce49693SSebastian Grimberg //------------------------------------------------------------------------------
316dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnoriented_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
317dce49693SSebastian Grimberg                                                   CeedRequest *request) {
318dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, false, u, v, request);
319dce49693SSebastian Grimberg }
320dce49693SSebastian Grimberg 
321dce49693SSebastian Grimberg //------------------------------------------------------------------------------
3220d0321e0SJeremy L Thompson // Get offsets
3230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
324472941f0SJeremy L Thompson static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
3250d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
3260d0321e0SJeremy L Thompson 
327b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
328472941f0SJeremy L Thompson   switch (mem_type) {
3290d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
3300d0321e0SJeremy L Thompson       *offsets = impl->h_ind;
3310d0321e0SJeremy L Thompson       break;
3320d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
3330d0321e0SJeremy L Thompson       *offsets = impl->d_ind;
3340d0321e0SJeremy L Thompson       break;
3350d0321e0SJeremy L Thompson   }
3360d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3370d0321e0SJeremy L Thompson }
3380d0321e0SJeremy L Thompson 
3390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
340dce49693SSebastian Grimberg // Get orientations
341dce49693SSebastian Grimberg //------------------------------------------------------------------------------
342dce49693SSebastian Grimberg static int CeedElemRestrictionGetOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
343dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
344dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
345dce49693SSebastian Grimberg 
346dce49693SSebastian Grimberg   switch (mem_type) {
347dce49693SSebastian Grimberg     case CEED_MEM_HOST:
348dce49693SSebastian Grimberg       *orients = impl->h_orients;
349dce49693SSebastian Grimberg       break;
350dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
351dce49693SSebastian Grimberg       *orients = impl->d_orients;
352dce49693SSebastian Grimberg       break;
353dce49693SSebastian Grimberg   }
354dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
355dce49693SSebastian Grimberg }
356dce49693SSebastian Grimberg 
357dce49693SSebastian Grimberg //------------------------------------------------------------------------------
358dce49693SSebastian Grimberg // Get curl-conforming orientations
359dce49693SSebastian Grimberg //------------------------------------------------------------------------------
360dce49693SSebastian Grimberg static int CeedElemRestrictionGetCurlOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
361dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
362dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
363dce49693SSebastian Grimberg 
364dce49693SSebastian Grimberg   switch (mem_type) {
365dce49693SSebastian Grimberg     case CEED_MEM_HOST:
366dce49693SSebastian Grimberg       *curl_orients = impl->h_curl_orients;
367dce49693SSebastian Grimberg       break;
368dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
369dce49693SSebastian Grimberg       *curl_orients = impl->d_curl_orients;
370dce49693SSebastian Grimberg       break;
371dce49693SSebastian Grimberg   }
372dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
373dce49693SSebastian Grimberg }
374dce49693SSebastian Grimberg 
375dce49693SSebastian Grimberg //------------------------------------------------------------------------------
3760d0321e0SJeremy L Thompson // Destroy restriction
3770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
378dce49693SSebastian Grimberg static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction rstr) {
3790d0321e0SJeremy L Thompson   Ceed                     ceed;
380b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
381b7453713SJeremy L Thompson 
382dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
383dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
384cf8cbdd6SSebastian Grimberg   if (impl->module) {
3852b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->module));
386cf8cbdd6SSebastian Grimberg   }
3872b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_ind_allocated));
3882b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_ind_allocated));
3892b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_offsets));
3902b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_indices));
3912b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_l_vec_indices));
392dce49693SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->h_orients_allocated));
393dce49693SSebastian Grimberg   CeedCallHip(ceed, hipFree(impl->d_orients_allocated));
394dce49693SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->h_curl_orients_allocated));
395dce49693SSebastian Grimberg   CeedCallHip(ceed, hipFree(impl->d_curl_orients_allocated));
3962b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
3970d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3980d0321e0SJeremy L Thompson }
3990d0321e0SJeremy L Thompson 
4000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4010d0321e0SJeremy L Thompson // Create transpose offsets and indices
4020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
403dce49693SSebastian Grimberg static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction rstr, const CeedInt *indices) {
4040d0321e0SJeremy L Thompson   Ceed                     ceed;
405b7453713SJeremy L Thompson   bool                    *is_node;
406e79b91d9SJeremy L Thompson   CeedSize                 l_size;
407dce49693SSebastian Grimberg   CeedInt                  num_elem, elem_size, num_comp, num_nodes = 0;
408dce49693SSebastian Grimberg   CeedInt                 *ind_to_offset, *l_vec_indices, *t_offsets, *t_indices;
409b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
410b7453713SJeremy L Thompson 
411dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
412dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
413dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
414dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
415dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
416dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
417b7453713SJeremy L Thompson   const CeedInt size_indices = num_elem * elem_size;
4180d0321e0SJeremy L Thompson 
419437930d1SJeremy L Thompson   // Count num_nodes
4202b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &is_node));
421dce49693SSebastian Grimberg 
4222b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1;
4232b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i];
424437930d1SJeremy L Thompson   impl->num_nodes = num_nodes;
4250d0321e0SJeremy L Thompson 
4260d0321e0SJeremy L Thompson   // L-vector offsets array
4272b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &ind_to_offset));
4282b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices));
429b7453713SJeremy L Thompson   for (CeedInt i = 0, j = 0; i < l_size; i++) {
430437930d1SJeremy L Thompson     if (is_node[i]) {
431437930d1SJeremy L Thompson       l_vec_indices[j] = i;
4320d0321e0SJeremy L Thompson       ind_to_offset[i] = j++;
4330d0321e0SJeremy L Thompson     }
4342b730f8bSJeremy L Thompson   }
4352b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&is_node));
4360d0321e0SJeremy L Thompson 
4370d0321e0SJeremy L Thompson   // Compute transpose offsets and indices
438437930d1SJeremy L Thompson   const CeedInt size_offsets = num_nodes + 1;
439b7453713SJeremy L Thompson 
4402b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(size_offsets, &t_offsets));
4412b730f8bSJeremy L Thompson   CeedCallBackend(CeedMalloc(size_indices, &t_indices));
4420d0321e0SJeremy L Thompson   // Count node multiplicity
4432b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
4442b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1];
4452b730f8bSJeremy L Thompson   }
4460d0321e0SJeremy L Thompson   // Convert to running sum
4472b730f8bSJeremy L Thompson   for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1];
4480d0321e0SJeremy L Thompson   // List all E-vec indices associated with L-vec node
449437930d1SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
450437930d1SJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) {
451437930d1SJeremy L Thompson       const CeedInt lid = elem_size * e + i;
4520d0321e0SJeremy L Thompson       const CeedInt gid = indices[lid];
453b7453713SJeremy L Thompson 
454437930d1SJeremy L Thompson       t_indices[t_offsets[ind_to_offset[gid]]++] = lid;
4550d0321e0SJeremy L Thompson     }
4560d0321e0SJeremy L Thompson   }
4570d0321e0SJeremy L Thompson   // Reset running sum
4582b730f8bSJeremy L Thompson   for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1];
459437930d1SJeremy L Thompson   t_offsets[0] = 0;
4600d0321e0SJeremy L Thompson 
4610d0321e0SJeremy L Thompson   // Copy data to device
4620d0321e0SJeremy L Thompson   // -- L-vector indices
4632b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt)));
4642b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice));
4650d0321e0SJeremy L Thompson   // -- Transpose offsets
4662b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt)));
4672b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice));
4680d0321e0SJeremy L Thompson   // -- Transpose indices
4692b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt)));
4702b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice));
4710d0321e0SJeremy L Thompson 
4720d0321e0SJeremy L Thompson   // Cleanup
4732b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&ind_to_offset));
4742b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&l_vec_indices));
4752b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_offsets));
4762b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_indices));
4770d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4780d0321e0SJeremy L Thompson }
4790d0321e0SJeremy L Thompson 
4800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4810d0321e0SJeremy L Thompson // Create restriction
4820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
483fcbe8c06SSebastian Grimberg int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *indices, const bool *orients,
484dce49693SSebastian Grimberg                                   const CeedInt8 *curl_orients, CeedElemRestriction rstr) {
485b7453713SJeremy L Thompson   Ceed                     ceed, ceed_parent;
486dce49693SSebastian Grimberg   bool                     is_deterministic;
487cf8cbdd6SSebastian Grimberg   CeedInt                  num_elem, elem_size;
488b7453713SJeremy L Thompson   CeedRestrictionType      rstr_type;
4890d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
490b7453713SJeremy L Thompson 
491dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
492ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
493ca735530SJeremy L Thompson   CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic));
494dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
495dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
49622eb1385SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
497dce49693SSebastian Grimberg   const CeedInt size = num_elem * elem_size;
4980d0321e0SJeremy L Thompson 
499dce49693SSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
500dce49693SSebastian Grimberg   impl->num_nodes                = size;
5010d0321e0SJeremy L Thompson   impl->h_ind                    = NULL;
5020d0321e0SJeremy L Thompson   impl->h_ind_allocated          = NULL;
5030d0321e0SJeremy L Thompson   impl->d_ind                    = NULL;
5040d0321e0SJeremy L Thompson   impl->d_ind_allocated          = NULL;
505437930d1SJeremy L Thompson   impl->d_t_indices              = NULL;
506437930d1SJeremy L Thompson   impl->d_t_offsets              = NULL;
507dce49693SSebastian Grimberg   impl->h_orients                = NULL;
508dce49693SSebastian Grimberg   impl->h_orients_allocated      = NULL;
509dce49693SSebastian Grimberg   impl->d_orients                = NULL;
510dce49693SSebastian Grimberg   impl->d_orients_allocated      = NULL;
511dce49693SSebastian Grimberg   impl->h_curl_orients           = NULL;
512dce49693SSebastian Grimberg   impl->h_curl_orients_allocated = NULL;
513dce49693SSebastian Grimberg   impl->d_curl_orients           = NULL;
514dce49693SSebastian Grimberg   impl->d_curl_orients_allocated = NULL;
515dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetData(rstr, impl));
51622eb1385SJeremy L Thompson 
51722eb1385SJeremy L Thompson   // Set layouts
51822eb1385SJeremy L Thompson   {
51922eb1385SJeremy L Thompson     bool    has_backend_strides;
52022eb1385SJeremy L Thompson     CeedInt layout[3] = {1, size, elem_size};
52122eb1385SJeremy L Thompson 
522dce49693SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetELayout(rstr, layout));
52322eb1385SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_STRIDED) {
52422eb1385SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
52522eb1385SJeremy L Thompson       if (has_backend_strides) {
52622eb1385SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionSetLLayout(rstr, layout));
52722eb1385SJeremy L Thompson       }
52822eb1385SJeremy L Thompson     }
52922eb1385SJeremy L Thompson   }
5300d0321e0SJeremy L Thompson 
531dce49693SSebastian Grimberg   // Set up device offset/orientation arrays
532dce49693SSebastian Grimberg   if (rstr_type != CEED_RESTRICTION_STRIDED) {
533472941f0SJeremy L Thompson     switch (mem_type) {
5346574a04fSJeremy L Thompson       case CEED_MEM_HOST: {
535472941f0SJeremy L Thompson         switch (copy_mode) {
5360d0321e0SJeremy L Thompson           case CEED_OWN_POINTER:
5370d0321e0SJeremy L Thompson             impl->h_ind_allocated = (CeedInt *)indices;
5380d0321e0SJeremy L Thompson             impl->h_ind           = (CeedInt *)indices;
5390d0321e0SJeremy L Thompson             break;
5400d0321e0SJeremy L Thompson           case CEED_USE_POINTER:
5410d0321e0SJeremy L Thompson             impl->h_ind = (CeedInt *)indices;
5420d0321e0SJeremy L Thompson             break;
5430d0321e0SJeremy L Thompson           case CEED_COPY_VALUES:
544dce49693SSebastian Grimberg             CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated));
545dce49693SSebastian Grimberg             memcpy(impl->h_ind_allocated, indices, size * sizeof(CeedInt));
54644d7a66cSJeremy L Thompson             impl->h_ind = impl->h_ind_allocated;
5470d0321e0SJeremy L Thompson             break;
5480d0321e0SJeremy L Thompson         }
5492b730f8bSJeremy L Thompson         CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
5500d0321e0SJeremy L Thompson         impl->d_ind_allocated = impl->d_ind;  // We own the device memory
5512b730f8bSJeremy L Thompson         CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyHostToDevice));
552dce49693SSebastian Grimberg         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, indices));
553dce49693SSebastian Grimberg       } break;
5546574a04fSJeremy L Thompson       case CEED_MEM_DEVICE: {
555472941f0SJeremy L Thompson         switch (copy_mode) {
5560d0321e0SJeremy L Thompson           case CEED_COPY_VALUES:
5572b730f8bSJeremy L Thompson             CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
5580d0321e0SJeremy L Thompson             impl->d_ind_allocated = impl->d_ind;  // We own the device memory
5592b730f8bSJeremy L Thompson             CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyDeviceToDevice));
5600d0321e0SJeremy L Thompson             break;
5610d0321e0SJeremy L Thompson           case CEED_OWN_POINTER:
5620d0321e0SJeremy L Thompson             impl->d_ind           = (CeedInt *)indices;
5630d0321e0SJeremy L Thompson             impl->d_ind_allocated = impl->d_ind;
5640d0321e0SJeremy L Thompson             break;
5650d0321e0SJeremy L Thompson           case CEED_USE_POINTER:
5660d0321e0SJeremy L Thompson             impl->d_ind = (CeedInt *)indices;
5676574a04fSJeremy L Thompson             break;
5686574a04fSJeremy L Thompson         }
569dce49693SSebastian Grimberg         CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated));
570dce49693SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(impl->h_ind_allocated, impl->d_ind, size * sizeof(CeedInt), hipMemcpyDeviceToHost));
571dce49693SSebastian Grimberg         impl->h_ind = impl->h_ind_allocated;
572dce49693SSebastian Grimberg         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, indices));
573dce49693SSebastian Grimberg       } break;
574dce49693SSebastian Grimberg     }
575dce49693SSebastian Grimberg 
576dce49693SSebastian Grimberg     // Orientation data
577dce49693SSebastian Grimberg     if (rstr_type == CEED_RESTRICTION_ORIENTED) {
578dce49693SSebastian Grimberg       switch (mem_type) {
579dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
580dce49693SSebastian Grimberg           switch (copy_mode) {
581dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
582dce49693SSebastian Grimberg               impl->h_orients_allocated = (bool *)orients;
583dce49693SSebastian Grimberg               impl->h_orients           = (bool *)orients;
584dce49693SSebastian Grimberg               break;
585dce49693SSebastian Grimberg             case CEED_USE_POINTER:
586dce49693SSebastian Grimberg               impl->h_orients = (bool *)orients;
587dce49693SSebastian Grimberg               break;
588dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
589dce49693SSebastian Grimberg               CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated));
590dce49693SSebastian Grimberg               memcpy(impl->h_orients_allocated, orients, size * sizeof(bool));
591dce49693SSebastian Grimberg               impl->h_orients = impl->h_orients_allocated;
592dce49693SSebastian Grimberg               break;
593dce49693SSebastian Grimberg           }
594dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients, size * sizeof(bool)));
595dce49693SSebastian Grimberg           impl->d_orients_allocated = impl->d_orients;  // We own the device memory
596dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->d_orients, orients, size * sizeof(bool), hipMemcpyHostToDevice));
597dce49693SSebastian Grimberg         } break;
598dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
599dce49693SSebastian Grimberg           switch (copy_mode) {
600dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
601dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients, size * sizeof(bool)));
602dce49693SSebastian Grimberg               impl->d_orients_allocated = impl->d_orients;  // We own the device memory
603dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMemcpy(impl->d_orients, orients, size * sizeof(bool), hipMemcpyDeviceToDevice));
604dce49693SSebastian Grimberg               break;
605dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
606dce49693SSebastian Grimberg               impl->d_orients           = (bool *)orients;
607dce49693SSebastian Grimberg               impl->d_orients_allocated = impl->d_orients;
608dce49693SSebastian Grimberg               break;
609dce49693SSebastian Grimberg             case CEED_USE_POINTER:
610dce49693SSebastian Grimberg               impl->d_orients = (bool *)orients;
611dce49693SSebastian Grimberg               break;
612dce49693SSebastian Grimberg           }
613dce49693SSebastian Grimberg           CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated));
614dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->h_orients_allocated, impl->d_orients, size * sizeof(bool), hipMemcpyDeviceToHost));
615dce49693SSebastian Grimberg           impl->h_orients = impl->h_orients_allocated;
616dce49693SSebastian Grimberg         } break;
617dce49693SSebastian Grimberg       }
618dce49693SSebastian Grimberg     } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
619dce49693SSebastian Grimberg       switch (mem_type) {
620dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
621dce49693SSebastian Grimberg           switch (copy_mode) {
622dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
623dce49693SSebastian Grimberg               impl->h_curl_orients_allocated = (CeedInt8 *)curl_orients;
624dce49693SSebastian Grimberg               impl->h_curl_orients           = (CeedInt8 *)curl_orients;
625dce49693SSebastian Grimberg               break;
626dce49693SSebastian Grimberg             case CEED_USE_POINTER:
627dce49693SSebastian Grimberg               impl->h_curl_orients = (CeedInt8 *)curl_orients;
628dce49693SSebastian Grimberg               break;
629dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
630dce49693SSebastian Grimberg               CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated));
631dce49693SSebastian Grimberg               memcpy(impl->h_curl_orients_allocated, curl_orients, 3 * size * sizeof(CeedInt8));
632dce49693SSebastian Grimberg               impl->h_curl_orients = impl->h_curl_orients_allocated;
633dce49693SSebastian Grimberg               break;
634dce49693SSebastian Grimberg           }
635dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8)));
636dce49693SSebastian Grimberg           impl->d_curl_orients_allocated = impl->d_curl_orients;  // We own the device memory
637dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyHostToDevice));
638dce49693SSebastian Grimberg         } break;
639dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
640dce49693SSebastian Grimberg           switch (copy_mode) {
641dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
642dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8)));
643dce49693SSebastian Grimberg               impl->d_curl_orients_allocated = impl->d_curl_orients;  // We own the device memory
644dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToDevice));
645dce49693SSebastian Grimberg               break;
646dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
647dce49693SSebastian Grimberg               impl->d_curl_orients           = (CeedInt8 *)curl_orients;
648dce49693SSebastian Grimberg               impl->d_curl_orients_allocated = impl->d_curl_orients;
649dce49693SSebastian Grimberg               break;
650dce49693SSebastian Grimberg             case CEED_USE_POINTER:
651dce49693SSebastian Grimberg               impl->d_curl_orients = (CeedInt8 *)curl_orients;
652dce49693SSebastian Grimberg               break;
653dce49693SSebastian Grimberg           }
654dce49693SSebastian Grimberg           CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated));
655dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->h_curl_orients_allocated, impl->d_curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToHost));
656dce49693SSebastian Grimberg           impl->h_curl_orients = impl->h_curl_orients_allocated;
657dce49693SSebastian Grimberg         } break;
658dce49693SSebastian Grimberg       }
659dce49693SSebastian Grimberg     }
6600d0321e0SJeremy L Thompson   }
6610d0321e0SJeremy L Thompson 
6620d0321e0SJeremy L Thompson   // Register backend functions
663dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Apply", CeedElemRestrictionApply_Hip));
664dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnsigned", CeedElemRestrictionApplyUnsigned_Hip));
665dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApplyUnoriented_Hip));
666dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Hip));
667dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Hip));
668dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Hip));
669dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Hip));
6700d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6710d0321e0SJeremy L Thompson }
6720d0321e0SJeremy L Thompson 
6730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
674