xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-restriction.c (revision 2e88d319acd75658c85aee6ae8a14c6e519e582d)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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;
2622070f95SJeremy L Thompson   char                    *restriction_kernel_source;
2722070f95SJeremy L Thompson   const char              *restriction_kernel_path;
28cf8cbdd6SSebastian Grimberg   CeedInt                  num_elem, num_comp, elem_size, comp_stride;
29cf8cbdd6SSebastian Grimberg   CeedRestrictionType      rstr_type;
30cf8cbdd6SSebastian Grimberg   CeedElemRestriction_Hip *impl;
31cf8cbdd6SSebastian Grimberg 
32cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
33cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
34fe960054SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
35cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
36cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
37cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
38fe960054SJeremy L Thompson   if (rstr_type == CEED_RESTRICTION_POINTS) {
39fe960054SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr, &elem_size));
40fe960054SJeremy L Thompson   } else {
41fe960054SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
42fe960054SJeremy L Thompson   }
43cf8cbdd6SSebastian Grimberg   is_deterministic = impl->d_l_vec_indices != NULL;
44cf8cbdd6SSebastian Grimberg 
45cf8cbdd6SSebastian Grimberg   // Compile HIP kernels
46cf8cbdd6SSebastian Grimberg   switch (rstr_type) {
47cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STRIDED: {
48cf8cbdd6SSebastian Grimberg       bool    has_backend_strides;
49509d4af6SJeremy L Thompson       CeedInt strides[3] = {1, num_elem * elem_size, elem_size};
50cf8cbdd6SSebastian Grimberg 
51cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
52cf8cbdd6SSebastian Grimberg       if (!has_backend_strides) {
5356c48462SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetStrides(rstr, strides));
54cf8cbdd6SSebastian Grimberg       }
55cf8cbdd6SSebastian Grimberg 
56cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-strided.h", &restriction_kernel_path));
57cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
58cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
59cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
60cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
61cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_STRIDE_NODES", strides[0], "RSTR_STRIDE_COMP", strides[1], "RSTR_STRIDE_ELEM",
62cf8cbdd6SSebastian Grimberg                                       strides[2]));
63cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->ApplyNoTranspose));
64cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->ApplyTranspose));
65cf8cbdd6SSebastian Grimberg     } break;
660b63de31SJeremy L Thompson     case CEED_RESTRICTION_POINTS: {
670b63de31SJeremy L Thompson       const char *offset_kernel_path;
680b63de31SJeremy L Thompson       char      **file_paths     = NULL;
690b63de31SJeremy L Thompson       CeedInt     num_file_paths = 0;
700b63de31SJeremy L Thompson 
710b63de31SJeremy L Thompson       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-at-points.h", &restriction_kernel_path));
720b63de31SJeremy L Thompson       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
730b63de31SJeremy L Thompson       CeedCallBackend(CeedLoadSourceAndInitializeBuffer(ceed, restriction_kernel_path, &num_file_paths, &file_paths, &restriction_kernel_source));
740b63de31SJeremy L Thompson       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path));
750b63de31SJeremy L Thompson       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &num_file_paths, &file_paths, &restriction_kernel_source));
760b63de31SJeremy L Thompson       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
770b63de31SJeremy L Thompson       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
780b63de31SJeremy L Thompson                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
790b63de31SJeremy L Thompson                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
800b63de31SJeremy L Thompson       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyNoTranspose));
810b63de31SJeremy L Thompson       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "AtPointsTranspose", &impl->ApplyTranspose));
820b63de31SJeremy L Thompson     } break;
83cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STANDARD: {
84cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &restriction_kernel_path));
85cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
86cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
87cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
88cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
89cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
90cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
91cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyNoTranspose));
92cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyTranspose));
93cf8cbdd6SSebastian Grimberg     } break;
94cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_ORIENTED: {
9522070f95SJeremy L Thompson       const char *offset_kernel_path;
96509d4af6SJeremy L Thompson       char      **file_paths     = NULL;
97509d4af6SJeremy L Thompson       CeedInt     num_file_paths = 0;
98cf8cbdd6SSebastian Grimberg 
99cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-oriented.h", &restriction_kernel_path));
100cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
101509d4af6SJeremy L Thompson       CeedCallBackend(CeedLoadSourceAndInitializeBuffer(ceed, restriction_kernel_path, &num_file_paths, &file_paths, &restriction_kernel_source));
102cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path));
103509d4af6SJeremy L Thompson       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &num_file_paths, &file_paths, &restriction_kernel_source));
104cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
105cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
106cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
107cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
108cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedNoTranspose", &impl->ApplyNoTranspose));
109cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnsignedNoTranspose));
110cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedTranspose", &impl->ApplyTranspose));
111cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnsignedTranspose));
112509d4af6SJeremy L Thompson       // Cleanup
113cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
1145a5594ffSJeremy L Thompson       for (CeedInt i = 0; i < num_file_paths; i++) CeedCallBackend(CeedFree(&file_paths[i]));
1155a5594ffSJeremy L Thompson       CeedCallBackend(CeedFree(&file_paths));
116cf8cbdd6SSebastian Grimberg     } break;
117cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_CURL_ORIENTED: {
11822070f95SJeremy L Thompson       const char *offset_kernel_path;
119509d4af6SJeremy L Thompson       char      **file_paths     = NULL;
120509d4af6SJeremy L Thompson       CeedInt     num_file_paths = 0;
121cf8cbdd6SSebastian Grimberg 
122cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-curl-oriented.h", &restriction_kernel_path));
123cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
124509d4af6SJeremy L Thompson       CeedCallBackend(CeedLoadSourceAndInitializeBuffer(ceed, restriction_kernel_path, &num_file_paths, &file_paths, &restriction_kernel_source));
125cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path));
126509d4af6SJeremy L Thompson       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &num_file_paths, &file_paths, &restriction_kernel_source));
127cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
128cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
129cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
130cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
131cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedNoTranspose", &impl->ApplyNoTranspose));
132cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedNoTranspose", &impl->ApplyUnsignedNoTranspose));
133cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnorientedNoTranspose));
134cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedTranspose", &impl->ApplyTranspose));
135cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedTranspose", &impl->ApplyUnsignedTranspose));
136cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnorientedTranspose));
137509d4af6SJeremy L Thompson       // Cleanup
138cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
1395a5594ffSJeremy L Thompson       for (CeedInt i = 0; i < num_file_paths; i++) CeedCallBackend(CeedFree(&file_paths[i]));
1405a5594ffSJeremy L Thompson       CeedCallBackend(CeedFree(&file_paths));
141cf8cbdd6SSebastian Grimberg     } break;
142cf8cbdd6SSebastian Grimberg   }
143cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_path));
144cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_source));
145cf8cbdd6SSebastian Grimberg   return CEED_ERROR_SUCCESS;
146cf8cbdd6SSebastian Grimberg }
147cf8cbdd6SSebastian Grimberg 
148cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
149dce49693SSebastian Grimberg // Core apply restriction code
1500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
151dce49693SSebastian Grimberg static inline int CeedElemRestrictionApply_Hip_Core(CeedElemRestriction rstr, CeedTransposeMode t_mode, bool use_signs, bool use_orients,
152dce49693SSebastian Grimberg                                                     CeedVector u, CeedVector v, CeedRequest *request) {
1530d0321e0SJeremy L Thompson   Ceed                     ceed;
154dce49693SSebastian Grimberg   CeedRestrictionType      rstr_type;
1550d0321e0SJeremy L Thompson   const CeedScalar        *d_u;
1560d0321e0SJeremy L Thompson   CeedScalar              *d_v;
157b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
158b7453713SJeremy L Thompson 
159dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
160dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
161dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
162cf8cbdd6SSebastian Grimberg 
163cf8cbdd6SSebastian Grimberg   // Assemble kernel if needed
164cf8cbdd6SSebastian Grimberg   if (!impl->module) {
165cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetupCompile_Hip(rstr));
166cf8cbdd6SSebastian Grimberg   }
167b7453713SJeremy L Thompson 
168b7453713SJeremy L Thompson   // Get vectors
1692b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
170437930d1SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
1710d0321e0SJeremy L Thompson     // Sum into for transpose mode, e-vec to l-vec
1722b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
1730d0321e0SJeremy L Thompson   } else {
1740d0321e0SJeremy L Thompson     // Overwrite for notranspose mode, l-vec to e-vec
1752b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1760d0321e0SJeremy L Thompson   }
1770d0321e0SJeremy L Thompson 
1780d0321e0SJeremy L Thompson   // Restrict
179437930d1SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
1800d0321e0SJeremy L Thompson     // L-vector -> E-vector
181cf8cbdd6SSebastian Grimberg     CeedInt elem_size;
182cf8cbdd6SSebastian Grimberg 
183cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
184dce49693SSebastian Grimberg     const CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
185cf8cbdd6SSebastian Grimberg     const CeedInt grid       = CeedDivUpInt(impl->num_nodes, block_size);
18658549094SSebastian Grimberg 
187dce49693SSebastian Grimberg     switch (rstr_type) {
188dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
189cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
19058549094SSebastian Grimberg 
191cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
192dce49693SSebastian Grimberg       } break;
193fe960054SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
194dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
195a267acd1SJeremy L Thompson         void *args[] = {&impl->d_offsets, &d_u, &d_v};
196dce49693SSebastian Grimberg 
197cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
198dce49693SSebastian Grimberg       } break;
199dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
200dce49693SSebastian Grimberg         if (use_signs) {
201a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &impl->d_orients, &d_u, &d_v};
202dce49693SSebastian Grimberg 
203cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
204dce49693SSebastian Grimberg         } else {
205a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &d_u, &d_v};
206dce49693SSebastian Grimberg 
207cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
208dce49693SSebastian Grimberg         }
209dce49693SSebastian Grimberg       } break;
210dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
211dce49693SSebastian Grimberg         if (use_signs && use_orients) {
212a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
213dce49693SSebastian Grimberg 
214cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
215dce49693SSebastian Grimberg         } else if (use_orients) {
216a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
217dce49693SSebastian Grimberg 
218cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
219dce49693SSebastian Grimberg         } else {
220a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &d_u, &d_v};
221dce49693SSebastian Grimberg 
222cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedNoTranspose, grid, block_size, args));
223dce49693SSebastian Grimberg         }
224dce49693SSebastian Grimberg       } break;
2250d0321e0SJeremy L Thompson     }
2260d0321e0SJeremy L Thompson   } else {
2270d0321e0SJeremy L Thompson     // E-vector -> L-vector
228cf8cbdd6SSebastian Grimberg     const bool    is_deterministic = impl->d_l_vec_indices != NULL;
229dce49693SSebastian Grimberg     const CeedInt block_size       = 64;
230cf8cbdd6SSebastian Grimberg     const CeedInt grid             = CeedDivUpInt(impl->num_nodes, block_size);
231b7453713SJeremy L Thompson 
232dce49693SSebastian Grimberg     switch (rstr_type) {
233dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
234cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
235dce49693SSebastian Grimberg 
236cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
237dce49693SSebastian Grimberg       } break;
2380b63de31SJeremy L Thompson       case CEED_RESTRICTION_POINTS: {
2390b63de31SJeremy L Thompson         if (!is_deterministic) {
2400b63de31SJeremy L Thompson           void *args[] = {&impl->d_offsets, &impl->d_points_per_elem, &d_u, &d_v};
2410b63de31SJeremy L Thompson 
2420b63de31SJeremy L Thompson           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2430b63de31SJeremy L Thompson         } else {
2440b63de31SJeremy L Thompson           void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_points_per_elem, &impl->d_t_offsets, &d_u, &d_v};
2450b63de31SJeremy L Thompson 
2460b63de31SJeremy L Thompson           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2470b63de31SJeremy L Thompson         }
2480b63de31SJeremy L Thompson       } break;
249dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
250cf8cbdd6SSebastian Grimberg         if (!is_deterministic) {
251a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &d_u, &d_v};
25258549094SSebastian Grimberg 
253cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2540d0321e0SJeremy L Thompson         } else {
25558549094SSebastian Grimberg           void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
25658549094SSebastian Grimberg 
257cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
25858549094SSebastian Grimberg         }
259dce49693SSebastian Grimberg       } break;
260dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
261dce49693SSebastian Grimberg         if (use_signs) {
262cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
263a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &impl->d_orients, &d_u, &d_v};
26458549094SSebastian Grimberg 
265cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
266dce49693SSebastian Grimberg           } else {
2677aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_orients, &d_u, &d_v};
2687aa91133SSebastian Grimberg 
269cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2707aa91133SSebastian Grimberg           }
2717aa91133SSebastian Grimberg         } else {
272cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
273a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &d_u, &d_v};
274dce49693SSebastian Grimberg 
275cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
276dce49693SSebastian Grimberg           } else {
277dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
278dce49693SSebastian Grimberg 
279cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
280dce49693SSebastian Grimberg           }
281dce49693SSebastian Grimberg         }
282dce49693SSebastian Grimberg       } break;
283dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
284dce49693SSebastian Grimberg         if (use_signs && use_orients) {
285cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
286a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
287dce49693SSebastian Grimberg 
288cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2897aa91133SSebastian Grimberg           } else {
2907aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
2917aa91133SSebastian Grimberg 
292cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2937aa91133SSebastian Grimberg           }
294dce49693SSebastian Grimberg         } else if (use_orients) {
295cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
296a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
297dce49693SSebastian Grimberg 
298cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
299dce49693SSebastian Grimberg           } else {
3007aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
3017aa91133SSebastian Grimberg 
302cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
3037aa91133SSebastian Grimberg           }
3047aa91133SSebastian Grimberg         } else {
305cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
306a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &d_u, &d_v};
307dce49693SSebastian Grimberg 
308cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args));
309dce49693SSebastian Grimberg           } else {
310dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
311dce49693SSebastian Grimberg 
312cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args));
313dce49693SSebastian Grimberg           }
314dce49693SSebastian Grimberg         }
315dce49693SSebastian Grimberg       } break;
3160d0321e0SJeremy L Thompson     }
3170d0321e0SJeremy L Thompson   }
3180d0321e0SJeremy L Thompson 
3192b730f8bSJeremy L Thompson   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
3200d0321e0SJeremy L Thompson 
3210d0321e0SJeremy L Thompson   // Restore arrays
3222b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
3232b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
3240d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3250d0321e0SJeremy L Thompson }
3260d0321e0SJeremy L Thompson 
3270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
328dce49693SSebastian Grimberg // Apply restriction
329dce49693SSebastian Grimberg //------------------------------------------------------------------------------
330dce49693SSebastian Grimberg static int CeedElemRestrictionApply_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
331dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, true, true, u, v, request);
332dce49693SSebastian Grimberg }
333dce49693SSebastian Grimberg 
334dce49693SSebastian Grimberg //------------------------------------------------------------------------------
335dce49693SSebastian Grimberg // Apply unsigned restriction
336dce49693SSebastian Grimberg //------------------------------------------------------------------------------
337dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnsigned_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
338dce49693SSebastian Grimberg                                                 CeedRequest *request) {
339dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, true, u, v, request);
340dce49693SSebastian Grimberg }
341dce49693SSebastian Grimberg 
342dce49693SSebastian Grimberg //------------------------------------------------------------------------------
343dce49693SSebastian Grimberg // Apply unoriented restriction
344dce49693SSebastian Grimberg //------------------------------------------------------------------------------
345dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnoriented_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
346dce49693SSebastian Grimberg                                                   CeedRequest *request) {
347dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, false, u, v, request);
348dce49693SSebastian Grimberg }
349dce49693SSebastian Grimberg 
350dce49693SSebastian Grimberg //------------------------------------------------------------------------------
3510d0321e0SJeremy L Thompson // Get offsets
3520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
353472941f0SJeremy L Thompson static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
3540d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
355fe960054SJeremy L Thompson   CeedRestrictionType      rstr_type;
3560d0321e0SJeremy L Thompson 
357b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
358fe960054SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
359472941f0SJeremy L Thompson   switch (mem_type) {
3600d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
361fe960054SJeremy L Thompson       *offsets = rstr_type == CEED_RESTRICTION_POINTS ? impl->h_offsets_at_points : impl->h_offsets;
3620d0321e0SJeremy L Thompson       break;
3630d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
364fe960054SJeremy L Thompson       *offsets = rstr_type == CEED_RESTRICTION_POINTS ? impl->d_offsets_at_points : impl->d_offsets;
3650d0321e0SJeremy L Thompson       break;
3660d0321e0SJeremy L Thompson   }
3670d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3680d0321e0SJeremy L Thompson }
3690d0321e0SJeremy L Thompson 
3700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
371dce49693SSebastian Grimberg // Get orientations
372dce49693SSebastian Grimberg //------------------------------------------------------------------------------
373dce49693SSebastian Grimberg static int CeedElemRestrictionGetOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
374dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
375dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
376dce49693SSebastian Grimberg 
377dce49693SSebastian Grimberg   switch (mem_type) {
378dce49693SSebastian Grimberg     case CEED_MEM_HOST:
379dce49693SSebastian Grimberg       *orients = impl->h_orients;
380dce49693SSebastian Grimberg       break;
381dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
382dce49693SSebastian Grimberg       *orients = impl->d_orients;
383dce49693SSebastian Grimberg       break;
384dce49693SSebastian Grimberg   }
385dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
386dce49693SSebastian Grimberg }
387dce49693SSebastian Grimberg 
388dce49693SSebastian Grimberg //------------------------------------------------------------------------------
389dce49693SSebastian Grimberg // Get curl-conforming orientations
390dce49693SSebastian Grimberg //------------------------------------------------------------------------------
391dce49693SSebastian Grimberg static int CeedElemRestrictionGetCurlOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
392dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
393dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
394dce49693SSebastian Grimberg 
395dce49693SSebastian Grimberg   switch (mem_type) {
396dce49693SSebastian Grimberg     case CEED_MEM_HOST:
397dce49693SSebastian Grimberg       *curl_orients = impl->h_curl_orients;
398dce49693SSebastian Grimberg       break;
399dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
400dce49693SSebastian Grimberg       *curl_orients = impl->d_curl_orients;
401dce49693SSebastian Grimberg       break;
402dce49693SSebastian Grimberg   }
403dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
404dce49693SSebastian Grimberg }
405dce49693SSebastian Grimberg 
406dce49693SSebastian Grimberg //------------------------------------------------------------------------------
407fe960054SJeremy L Thompson // Get offset for padded AtPoints E-layout
408fe960054SJeremy L Thompson //------------------------------------------------------------------------------
409fe960054SJeremy L Thompson static int CeedElemRestrictionGetAtPointsElementOffset_Hip(CeedElemRestriction rstr, CeedInt elem, CeedSize *elem_offset) {
410fe960054SJeremy L Thompson   CeedInt layout[3];
411fe960054SJeremy L Thompson 
412fe960054SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetELayout(rstr, layout));
413fe960054SJeremy L Thompson   *elem_offset = 0 * layout[0] + 0 * layout[1] + elem * layout[2];
414fe960054SJeremy L Thompson   return CEED_ERROR_SUCCESS;
415fe960054SJeremy L Thompson }
416fe960054SJeremy L Thompson 
417fe960054SJeremy L Thompson //------------------------------------------------------------------------------
4180d0321e0SJeremy L Thompson // Destroy restriction
4190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
420dce49693SSebastian Grimberg static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction rstr) {
4210d0321e0SJeremy L Thompson   Ceed                     ceed;
422b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
423b7453713SJeremy L Thompson 
424dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
425dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
426cf8cbdd6SSebastian Grimberg   if (impl->module) {
4272b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->module));
428cf8cbdd6SSebastian Grimberg   }
429a267acd1SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_offsets_owned));
430f5d1e504SJeremy L Thompson   CeedCallHip(ceed, hipFree((CeedInt *)impl->d_offsets_owned));
431081aa29dSJeremy L Thompson   CeedCallHip(ceed, hipFree((CeedInt *)impl->d_t_offsets));
432081aa29dSJeremy L Thompson   CeedCallHip(ceed, hipFree((CeedInt *)impl->d_t_indices));
433081aa29dSJeremy L Thompson   CeedCallHip(ceed, hipFree((CeedInt *)impl->d_l_vec_indices));
434a267acd1SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_orients_owned));
435f5d1e504SJeremy L Thompson   CeedCallHip(ceed, hipFree((bool *)impl->d_orients_owned));
436a267acd1SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_curl_orients_owned));
437f5d1e504SJeremy L Thompson   CeedCallHip(ceed, hipFree((CeedInt8 *)impl->d_curl_orients_owned));
438fe960054SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_offsets_at_points_owned));
439fe960054SJeremy L Thompson   CeedCallHip(ceed, hipFree((CeedInt8 *)impl->d_offsets_at_points_owned));
4400b63de31SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_points_per_elem_owned));
4410b63de31SJeremy L Thompson   CeedCallHip(ceed, hipFree((CeedInt *)impl->d_points_per_elem_owned));
4422b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
4430d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4440d0321e0SJeremy L Thompson }
4450d0321e0SJeremy L Thompson 
4460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4470d0321e0SJeremy L Thompson // Create transpose offsets and indices
4480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
449fe960054SJeremy L Thompson static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction rstr, const CeedInt elem_size, const CeedInt *indices) {
4500d0321e0SJeremy L Thompson   Ceed                     ceed;
451b7453713SJeremy L Thompson   bool                    *is_node;
452e79b91d9SJeremy L Thompson   CeedSize                 l_size;
453fe960054SJeremy L Thompson   CeedInt                  num_elem, num_comp, num_nodes = 0;
454dce49693SSebastian Grimberg   CeedInt                 *ind_to_offset, *l_vec_indices, *t_offsets, *t_indices;
455fe960054SJeremy L Thompson   CeedRestrictionType      rstr_type;
456b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
457b7453713SJeremy L Thompson 
458dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
459dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
460dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
461fe960054SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
462dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
463dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
464b7453713SJeremy L Thompson   const CeedInt size_indices = num_elem * elem_size;
4650d0321e0SJeremy L Thompson 
466437930d1SJeremy L Thompson   // Count num_nodes
4672b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &is_node));
468dce49693SSebastian Grimberg 
4692b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1;
4702b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i];
471437930d1SJeremy L Thompson   impl->num_nodes = num_nodes;
4720d0321e0SJeremy L Thompson 
4730d0321e0SJeremy L Thompson   // L-vector offsets array
4742b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &ind_to_offset));
4752b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices));
476b7453713SJeremy L Thompson   for (CeedInt i = 0, j = 0; i < l_size; i++) {
477437930d1SJeremy L Thompson     if (is_node[i]) {
478437930d1SJeremy L Thompson       l_vec_indices[j] = i;
4790d0321e0SJeremy L Thompson       ind_to_offset[i] = j++;
4800d0321e0SJeremy L Thompson     }
4812b730f8bSJeremy L Thompson   }
4822b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&is_node));
4830d0321e0SJeremy L Thompson 
4840d0321e0SJeremy L Thompson   // Compute transpose offsets and indices
485437930d1SJeremy L Thompson   const CeedInt size_offsets = num_nodes + 1;
486b7453713SJeremy L Thompson 
4872b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(size_offsets, &t_offsets));
4882b730f8bSJeremy L Thompson   CeedCallBackend(CeedMalloc(size_indices, &t_indices));
4890d0321e0SJeremy L Thompson   // Count node multiplicity
4902b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
4912b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1];
4922b730f8bSJeremy L Thompson   }
4930d0321e0SJeremy L Thompson   // Convert to running sum
4942b730f8bSJeremy L Thompson   for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1];
4950d0321e0SJeremy L Thompson   // List all E-vec indices associated with L-vec node
496437930d1SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
497437930d1SJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) {
498437930d1SJeremy L Thompson       const CeedInt lid = elem_size * e + i;
4990d0321e0SJeremy L Thompson       const CeedInt gid = indices[lid];
500b7453713SJeremy L Thompson 
501437930d1SJeremy L Thompson       t_indices[t_offsets[ind_to_offset[gid]]++] = lid;
5020d0321e0SJeremy L Thompson     }
5030d0321e0SJeremy L Thompson   }
5040d0321e0SJeremy L Thompson   // Reset running sum
5052b730f8bSJeremy L Thompson   for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1];
506437930d1SJeremy L Thompson   t_offsets[0] = 0;
5070d0321e0SJeremy L Thompson 
5080d0321e0SJeremy L Thompson   // Copy data to device
5090d0321e0SJeremy L Thompson   // -- L-vector indices
5102b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt)));
511081aa29dSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy((CeedInt *)impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice));
5120d0321e0SJeremy L Thompson   // -- Transpose offsets
5132b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt)));
514081aa29dSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy((CeedInt *)impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice));
5150d0321e0SJeremy L Thompson   // -- Transpose indices
5162b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt)));
517081aa29dSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy((CeedInt *)impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice));
5180d0321e0SJeremy L Thompson 
5190d0321e0SJeremy L Thompson   // Cleanup
5202b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&ind_to_offset));
5212b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&l_vec_indices));
5222b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_offsets));
5232b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_indices));
5240d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5250d0321e0SJeremy L Thompson }
5260d0321e0SJeremy L Thompson 
5270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5280d0321e0SJeremy L Thompson // Create restriction
5290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
530a267acd1SJeremy L Thompson int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
531dce49693SSebastian Grimberg                                   const CeedInt8 *curl_orients, CeedElemRestriction rstr) {
532b7453713SJeremy L Thompson   Ceed                     ceed, ceed_parent;
533dce49693SSebastian Grimberg   bool                     is_deterministic;
534ff1bc20eSJeremy L Thompson   CeedInt                  num_elem, num_comp, elem_size;
535b7453713SJeremy L Thompson   CeedRestrictionType      rstr_type;
5360d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
537b7453713SJeremy L Thompson 
538dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
539ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
540ca735530SJeremy L Thompson   CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic));
541dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
542ff1bc20eSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
543dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
54422eb1385SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
545fe960054SJeremy L Thompson   // Use max number of points as elem size for AtPoints restrictions
546fe960054SJeremy L Thompson   if (rstr_type == CEED_RESTRICTION_POINTS) {
547fe960054SJeremy L Thompson     CeedInt max_points = 0;
548fe960054SJeremy L Thompson 
549fe960054SJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
550fe960054SJeremy L Thompson       max_points = CeedIntMax(max_points, offsets[i + 1] - offsets[i]);
551fe960054SJeremy L Thompson     }
552fe960054SJeremy L Thompson     elem_size = max_points;
553fe960054SJeremy L Thompson   }
554dce49693SSebastian Grimberg   const CeedInt size = num_elem * elem_size;
5550d0321e0SJeremy L Thompson 
556dce49693SSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
557dce49693SSebastian Grimberg   impl->num_nodes = size;
558dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetData(rstr, impl));
55922eb1385SJeremy L Thompson 
56022eb1385SJeremy L Thompson   // Set layouts
56122eb1385SJeremy L Thompson   {
56222eb1385SJeremy L Thompson     bool    has_backend_strides;
56322eb1385SJeremy L Thompson     CeedInt layout[3] = {1, size, elem_size};
56422eb1385SJeremy L Thompson 
565dce49693SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetELayout(rstr, layout));
56622eb1385SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_STRIDED) {
56722eb1385SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
56822eb1385SJeremy L Thompson       if (has_backend_strides) {
56922eb1385SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionSetLLayout(rstr, layout));
57022eb1385SJeremy L Thompson       }
57122eb1385SJeremy L Thompson     }
57222eb1385SJeremy L Thompson   }
5730d0321e0SJeremy L Thompson 
574fe960054SJeremy L Thompson   // Pad AtPoints indices
575fe960054SJeremy L Thompson   if (rstr_type == CEED_RESTRICTION_POINTS) {
576fe960054SJeremy L Thompson     CeedSize offsets_len = elem_size * num_elem, at_points_size = num_elem + 1;
5770b63de31SJeremy L Thompson     CeedInt  max_points = elem_size, *offsets_padded, *points_per_elem;
578fe960054SJeremy L Thompson 
579fe960054SJeremy L Thompson     CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "only MemType Host supported when creating AtPoints restriction");
580fe960054SJeremy L Thompson     CeedCallBackend(CeedMalloc(offsets_len, &offsets_padded));
5810b63de31SJeremy L Thompson     CeedCallBackend(CeedMalloc(num_elem, &points_per_elem));
582fe960054SJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
583fe960054SJeremy L Thompson       CeedInt num_points = offsets[i + 1] - offsets[i];
584fe960054SJeremy L Thompson 
5850b63de31SJeremy L Thompson       points_per_elem[i] = num_points;
586fe960054SJeremy L Thompson       at_points_size += num_points;
587fe960054SJeremy L Thompson       // -- Copy all points in element
588fe960054SJeremy L Thompson       for (CeedInt j = 0; j < num_points; j++) {
5898be297eeSJeremy L Thompson         offsets_padded[i * max_points + j] = offsets[offsets[i] + j] * num_comp;
590fe960054SJeremy L Thompson       }
591fe960054SJeremy L Thompson       // -- Replicate out last point in element
592fe960054SJeremy L Thompson       for (CeedInt j = num_points; j < max_points; j++) {
5938be297eeSJeremy L Thompson         offsets_padded[i * max_points + j] = offsets[offsets[i] + num_points - 1] * num_comp;
594fe960054SJeremy L Thompson       }
595fe960054SJeremy L Thompson     }
596fe960054SJeremy L Thompson     CeedCallBackend(CeedSetHostCeedIntArray(offsets, copy_mode, at_points_size, &impl->h_offsets_at_points_owned, &impl->h_offsets_at_points_borrowed,
597fe960054SJeremy L Thompson                                             &impl->h_offsets_at_points));
598fe960054SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&impl->d_offsets_at_points_owned, at_points_size * sizeof(CeedInt)));
599fe960054SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy((CeedInt **)impl->d_offsets_at_points_owned, impl->h_offsets_at_points, at_points_size * sizeof(CeedInt),
600fe960054SJeremy L Thompson                                 hipMemcpyHostToDevice));
601fe960054SJeremy L Thompson     impl->d_offsets_at_points = (CeedInt *)impl->d_offsets_at_points_owned;
602ff1bc20eSJeremy L Thompson 
603fe960054SJeremy L Thompson     // -- Use padded offsets for the rest of the setup
604fe960054SJeremy L Thompson     offsets   = (const CeedInt *)offsets_padded;
605fe960054SJeremy L Thompson     copy_mode = CEED_OWN_POINTER;
606*2e88d319SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionSetAtPointsEVectorSize(rstr, elem_size * num_elem * num_comp));
6070b63de31SJeremy L Thompson 
6080b63de31SJeremy L Thompson     // -- Points per element
6090b63de31SJeremy L Thompson     CeedCallBackend(CeedSetHostCeedIntArray(points_per_elem, CEED_OWN_POINTER, num_elem, &impl->h_points_per_elem_owned,
6100b63de31SJeremy L Thompson                                             &impl->h_points_per_elem_borrowed, &impl->h_points_per_elem));
6110b63de31SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&impl->d_points_per_elem_owned, num_elem * sizeof(CeedInt)));
6120b63de31SJeremy L Thompson     CeedCallHip(ceed,
6130b63de31SJeremy L Thompson                 hipMemcpy((CeedInt **)impl->d_points_per_elem_owned, impl->h_points_per_elem, num_elem * sizeof(CeedInt), hipMemcpyHostToDevice));
6140b63de31SJeremy L Thompson     impl->d_points_per_elem = (CeedInt *)impl->d_points_per_elem_owned;
615fe960054SJeremy L Thompson   }
616fe960054SJeremy L Thompson 
617dce49693SSebastian Grimberg   // Set up device offset/orientation arrays
618dce49693SSebastian Grimberg   if (rstr_type != CEED_RESTRICTION_STRIDED) {
619472941f0SJeremy L Thompson     switch (mem_type) {
6206574a04fSJeremy L Thompson       case CEED_MEM_HOST: {
621f5d1e504SJeremy L Thompson         CeedCallBackend(CeedSetHostCeedIntArray(offsets, copy_mode, size, &impl->h_offsets_owned, &impl->h_offsets_borrowed, &impl->h_offsets));
622a267acd1SJeremy L Thompson         CeedCallHip(ceed, hipMalloc((void **)&impl->d_offsets_owned, size * sizeof(CeedInt)));
623f5d1e504SJeremy L Thompson         CeedCallHip(ceed, hipMemcpy((CeedInt **)impl->d_offsets_owned, impl->h_offsets, size * sizeof(CeedInt), hipMemcpyHostToDevice));
624f5d1e504SJeremy L Thompson         impl->d_offsets = (CeedInt *)impl->d_offsets_owned;
625fe960054SJeremy L Thompson         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, elem_size, offsets));
626dce49693SSebastian Grimberg       } break;
6276574a04fSJeremy L Thompson       case CEED_MEM_DEVICE: {
628f5d1e504SJeremy L Thompson         CeedCallBackend(CeedSetDeviceCeedIntArray_Hip(ceed, offsets, copy_mode, size, &impl->d_offsets_owned, &impl->d_offsets_borrowed,
629f5d1e504SJeremy L Thompson                                                       (const CeedInt **)&impl->d_offsets));
630a267acd1SJeremy L Thompson         CeedCallBackend(CeedMalloc(size, &impl->h_offsets_owned));
631f5d1e504SJeremy L Thompson         CeedCallHip(ceed, hipMemcpy((CeedInt **)impl->h_offsets_owned, impl->d_offsets, size * sizeof(CeedInt), hipMemcpyDeviceToHost));
632a267acd1SJeremy L Thompson         impl->h_offsets = impl->h_offsets_owned;
633fe960054SJeremy L Thompson         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, elem_size, offsets));
634dce49693SSebastian Grimberg       } break;
635dce49693SSebastian Grimberg     }
636dce49693SSebastian Grimberg 
637dce49693SSebastian Grimberg     // Orientation data
638dce49693SSebastian Grimberg     if (rstr_type == CEED_RESTRICTION_ORIENTED) {
639dce49693SSebastian Grimberg       switch (mem_type) {
640dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
641f5d1e504SJeremy L Thompson           CeedCallBackend(CeedSetHostBoolArray(orients, copy_mode, size, &impl->h_orients_owned, &impl->h_orients_borrowed, &impl->h_orients));
642a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients_owned, size * sizeof(bool)));
643f5d1e504SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy((bool *)impl->d_orients_owned, impl->h_orients, size * sizeof(bool), hipMemcpyHostToDevice));
644a267acd1SJeremy L Thompson           impl->d_orients = impl->d_orients_owned;
645dce49693SSebastian Grimberg         } break;
646dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
647f5d1e504SJeremy L Thompson           CeedCallBackend(CeedSetDeviceBoolArray_Hip(ceed, orients, copy_mode, size, &impl->d_orients_owned, &impl->d_orients_borrowed,
648f5d1e504SJeremy L Thompson                                                      (const bool **)&impl->d_orients));
649a267acd1SJeremy L Thompson           CeedCallBackend(CeedMalloc(size, &impl->h_orients_owned));
650f5d1e504SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy((bool *)impl->h_orients_owned, impl->d_orients, size * sizeof(bool), hipMemcpyDeviceToHost));
651a267acd1SJeremy L Thompson           impl->h_orients = impl->h_orients_owned;
652dce49693SSebastian Grimberg         } break;
653dce49693SSebastian Grimberg       }
654dce49693SSebastian Grimberg     } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
655dce49693SSebastian Grimberg       switch (mem_type) {
656dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
657f5d1e504SJeremy L Thompson           CeedCallBackend(CeedSetHostCeedInt8Array(curl_orients, copy_mode, 3 * size, &impl->h_curl_orients_owned, &impl->h_curl_orients_borrowed,
658f5d1e504SJeremy L Thompson                                                    &impl->h_curl_orients));
659a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients_owned, 3 * size * sizeof(CeedInt8)));
660f5d1e504SJeremy L Thompson           CeedCallHip(ceed,
661f5d1e504SJeremy L Thompson                       hipMemcpy((CeedInt8 *)impl->d_curl_orients_owned, impl->h_curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyHostToDevice));
662a267acd1SJeremy L Thompson           impl->d_curl_orients = impl->d_curl_orients_owned;
663dce49693SSebastian Grimberg         } break;
664dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
665f5d1e504SJeremy L Thompson           CeedCallBackend(CeedSetDeviceCeedInt8Array_Hip(ceed, curl_orients, copy_mode, 3 * size, &impl->d_curl_orients_owned,
666f5d1e504SJeremy L Thompson                                                          &impl->d_curl_orients_borrowed, (const CeedInt8 **)&impl->d_curl_orients));
667a267acd1SJeremy L Thompson           CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_owned));
668f5d1e504SJeremy L Thompson           CeedCallHip(ceed,
669f5d1e504SJeremy L Thompson                       hipMemcpy((CeedInt8 *)impl->h_curl_orients_owned, impl->d_curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToHost));
670a267acd1SJeremy L Thompson           impl->h_curl_orients = impl->h_curl_orients_owned;
671dce49693SSebastian Grimberg         } break;
672dce49693SSebastian Grimberg       }
673dce49693SSebastian Grimberg     }
6740d0321e0SJeremy L Thompson   }
6750d0321e0SJeremy L Thompson 
6760d0321e0SJeremy L Thompson   // Register backend functions
677dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Apply", CeedElemRestrictionApply_Hip));
678dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnsigned", CeedElemRestrictionApplyUnsigned_Hip));
679dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApplyUnoriented_Hip));
680dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Hip));
681dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Hip));
682dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Hip));
683fe960054SJeremy L Thompson   if (rstr_type == CEED_RESTRICTION_POINTS) {
684fe960054SJeremy L Thompson     CeedCallBackend(
685fe960054SJeremy L Thompson         CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetAtPointsElementOffset", CeedElemRestrictionGetAtPointsElementOffset_Hip));
686fe960054SJeremy L Thompson   }
687dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Hip));
6880d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6890d0321e0SJeremy L Thompson }
6900d0321e0SJeremy L Thompson 
6910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
692