xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-restriction.c (revision a267acd12487b2c23311fe9e8f7b0c3e959135b1)
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;
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));
34cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
35cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
36cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
37cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
38cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
39cf8cbdd6SSebastian Grimberg   is_deterministic = impl->d_l_vec_indices != NULL;
40cf8cbdd6SSebastian Grimberg 
41cf8cbdd6SSebastian Grimberg   // Compile HIP kernels
42cf8cbdd6SSebastian Grimberg   switch (rstr_type) {
43cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STRIDED: {
44cf8cbdd6SSebastian Grimberg       CeedInt strides[3] = {1, num_elem * elem_size, elem_size};
45cf8cbdd6SSebastian Grimberg       bool    has_backend_strides;
46cf8cbdd6SSebastian Grimberg 
47cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
48cf8cbdd6SSebastian Grimberg       if (!has_backend_strides) {
4956c48462SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetStrides(rstr, strides));
50cf8cbdd6SSebastian Grimberg       }
51cf8cbdd6SSebastian Grimberg 
52cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-strided.h", &restriction_kernel_path));
53cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
54cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
55cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
56cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
57cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_STRIDE_NODES", strides[0], "RSTR_STRIDE_COMP", strides[1], "RSTR_STRIDE_ELEM",
58cf8cbdd6SSebastian Grimberg                                       strides[2]));
59cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->ApplyNoTranspose));
60cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->ApplyTranspose));
61cf8cbdd6SSebastian Grimberg     } break;
62cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STANDARD: {
63cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &restriction_kernel_path));
64cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
65cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
66cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
67cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
68cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
69cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
70cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyNoTranspose));
71cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyTranspose));
72cf8cbdd6SSebastian Grimberg     } break;
73cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_ORIENTED: {
7422070f95SJeremy L Thompson       const char *offset_kernel_path;
75cf8cbdd6SSebastian Grimberg 
76cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-oriented.h", &restriction_kernel_path));
77cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
78cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
79cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path));
80cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source));
81cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
82cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
83cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
84cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
85cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedNoTranspose", &impl->ApplyNoTranspose));
86cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnsignedNoTranspose));
87cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedTranspose", &impl->ApplyTranspose));
88cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnsignedTranspose));
89cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
90cf8cbdd6SSebastian Grimberg     } break;
91cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_CURL_ORIENTED: {
9222070f95SJeremy L Thompson       const char *offset_kernel_path;
93cf8cbdd6SSebastian Grimberg 
94cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-curl-oriented.h", &restriction_kernel_path));
95cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
96cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
97cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path));
98cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source));
99cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
100cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
101cf8cbdd6SSebastian Grimberg                                       "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
102cf8cbdd6SSebastian Grimberg                                       "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
103cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedNoTranspose", &impl->ApplyNoTranspose));
104cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedNoTranspose", &impl->ApplyUnsignedNoTranspose));
105cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnorientedNoTranspose));
106cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedTranspose", &impl->ApplyTranspose));
107cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedTranspose", &impl->ApplyUnsignedTranspose));
108cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnorientedTranspose));
109cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
110cf8cbdd6SSebastian Grimberg     } break;
111cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_POINTS: {
112cf8cbdd6SSebastian Grimberg       // LCOV_EXCL_START
113cf8cbdd6SSebastian Grimberg       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
114cf8cbdd6SSebastian Grimberg       // LCOV_EXCL_STOP
115cf8cbdd6SSebastian Grimberg     } break;
116cf8cbdd6SSebastian Grimberg   }
117cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_path));
118cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_source));
119cf8cbdd6SSebastian Grimberg   return CEED_ERROR_SUCCESS;
120cf8cbdd6SSebastian Grimberg }
121cf8cbdd6SSebastian Grimberg 
122cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
123dce49693SSebastian Grimberg // Core apply restriction code
1240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
125dce49693SSebastian Grimberg static inline int CeedElemRestrictionApply_Hip_Core(CeedElemRestriction rstr, CeedTransposeMode t_mode, bool use_signs, bool use_orients,
126dce49693SSebastian Grimberg                                                     CeedVector u, CeedVector v, CeedRequest *request) {
1270d0321e0SJeremy L Thompson   Ceed                     ceed;
128dce49693SSebastian Grimberg   CeedRestrictionType      rstr_type;
1290d0321e0SJeremy L Thompson   const CeedScalar        *d_u;
1300d0321e0SJeremy L Thompson   CeedScalar              *d_v;
131b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
132b7453713SJeremy L Thompson 
133dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
134dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
135dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
136cf8cbdd6SSebastian Grimberg 
137cf8cbdd6SSebastian Grimberg   // Assemble kernel if needed
138cf8cbdd6SSebastian Grimberg   if (!impl->module) {
139cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetupCompile_Hip(rstr));
140cf8cbdd6SSebastian Grimberg   }
141b7453713SJeremy L Thompson 
142b7453713SJeremy L Thompson   // Get vectors
1432b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
144437930d1SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
1450d0321e0SJeremy L Thompson     // Sum into for transpose mode, e-vec to l-vec
1462b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
1470d0321e0SJeremy L Thompson   } else {
1480d0321e0SJeremy L Thompson     // Overwrite for notranspose mode, l-vec to e-vec
1492b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1500d0321e0SJeremy L Thompson   }
1510d0321e0SJeremy L Thompson 
1520d0321e0SJeremy L Thompson   // Restrict
153437930d1SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
1540d0321e0SJeremy L Thompson     // L-vector -> E-vector
155cf8cbdd6SSebastian Grimberg     CeedInt elem_size;
156cf8cbdd6SSebastian Grimberg 
157cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
158dce49693SSebastian Grimberg     const CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
159cf8cbdd6SSebastian Grimberg     const CeedInt grid       = CeedDivUpInt(impl->num_nodes, block_size);
16058549094SSebastian Grimberg 
161dce49693SSebastian Grimberg     switch (rstr_type) {
162dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
163cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
16458549094SSebastian Grimberg 
165cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
166dce49693SSebastian Grimberg       } break;
167dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
168*a267acd1SJeremy L Thompson         void *args[] = {&impl->d_offsets, &d_u, &d_v};
169dce49693SSebastian Grimberg 
170cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
171dce49693SSebastian Grimberg       } break;
172dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
173dce49693SSebastian Grimberg         if (use_signs) {
174*a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &impl->d_orients, &d_u, &d_v};
175dce49693SSebastian Grimberg 
176cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
177dce49693SSebastian Grimberg         } else {
178*a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &d_u, &d_v};
179dce49693SSebastian Grimberg 
180cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
181dce49693SSebastian Grimberg         }
182dce49693SSebastian Grimberg       } break;
183dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
184dce49693SSebastian Grimberg         if (use_signs && use_orients) {
185*a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
186dce49693SSebastian Grimberg 
187cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args));
188dce49693SSebastian Grimberg         } else if (use_orients) {
189*a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
190dce49693SSebastian Grimberg 
191cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
192dce49693SSebastian Grimberg         } else {
193*a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &d_u, &d_v};
194dce49693SSebastian Grimberg 
195cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedNoTranspose, grid, block_size, args));
196dce49693SSebastian Grimberg         }
197dce49693SSebastian Grimberg       } break;
198b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
199b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
200b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
201b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
202b3d03e38SSebastian Grimberg       } break;
2030d0321e0SJeremy L Thompson     }
2040d0321e0SJeremy L Thompson   } else {
2050d0321e0SJeremy L Thompson     // E-vector -> L-vector
206cf8cbdd6SSebastian Grimberg     const bool    is_deterministic = impl->d_l_vec_indices != NULL;
207dce49693SSebastian Grimberg     const CeedInt block_size       = 64;
208cf8cbdd6SSebastian Grimberg     const CeedInt grid             = CeedDivUpInt(impl->num_nodes, block_size);
209b7453713SJeremy L Thompson 
210dce49693SSebastian Grimberg     switch (rstr_type) {
211dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
212cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
213dce49693SSebastian Grimberg 
214cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
215dce49693SSebastian Grimberg       } break;
216dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
217cf8cbdd6SSebastian Grimberg         if (!is_deterministic) {
218*a267acd1SJeremy L Thompson           void *args[] = {&impl->d_offsets, &d_u, &d_v};
21958549094SSebastian Grimberg 
220cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2210d0321e0SJeremy L Thompson         } else {
22258549094SSebastian Grimberg           void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
22358549094SSebastian Grimberg 
224cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
22558549094SSebastian Grimberg         }
226dce49693SSebastian Grimberg       } break;
227dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
228dce49693SSebastian Grimberg         if (use_signs) {
229cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
230*a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &impl->d_orients, &d_u, &d_v};
23158549094SSebastian Grimberg 
232cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
233dce49693SSebastian Grimberg           } else {
2347aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_orients, &d_u, &d_v};
2357aa91133SSebastian Grimberg 
236cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2377aa91133SSebastian Grimberg           }
2387aa91133SSebastian Grimberg         } else {
239cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
240*a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &d_u, &d_v};
241dce49693SSebastian Grimberg 
242cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
243dce49693SSebastian Grimberg           } else {
244dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
245dce49693SSebastian Grimberg 
246cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
247dce49693SSebastian Grimberg           }
248dce49693SSebastian Grimberg         }
249dce49693SSebastian Grimberg       } break;
250dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
251dce49693SSebastian Grimberg         if (use_signs && use_orients) {
252cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
253*a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
254dce49693SSebastian Grimberg 
255cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2567aa91133SSebastian Grimberg           } else {
2577aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
2587aa91133SSebastian Grimberg 
259cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args));
2607aa91133SSebastian Grimberg           }
261dce49693SSebastian Grimberg         } else if (use_orients) {
262cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
263*a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &impl->d_curl_orients, &d_u, &d_v};
264dce49693SSebastian Grimberg 
265cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, 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_curl_orients, &d_u, &d_v};
2687aa91133SSebastian Grimberg 
269cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
2707aa91133SSebastian Grimberg           }
2717aa91133SSebastian Grimberg         } else {
272cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
273*a267acd1SJeremy L Thompson             void *args[] = {&impl->d_offsets, &d_u, &d_v};
274dce49693SSebastian Grimberg 
275cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedTranspose, 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->ApplyUnorientedTranspose, grid, block_size, args));
280dce49693SSebastian Grimberg           }
281dce49693SSebastian Grimberg         }
282dce49693SSebastian Grimberg       } break;
283b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
284b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
285b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
286b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
287b3d03e38SSebastian Grimberg       } break;
2880d0321e0SJeremy L Thompson     }
2890d0321e0SJeremy L Thompson   }
2900d0321e0SJeremy L Thompson 
2912b730f8bSJeremy L Thompson   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
2920d0321e0SJeremy L Thompson 
2930d0321e0SJeremy L Thompson   // Restore arrays
2942b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
2952b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2960d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2970d0321e0SJeremy L Thompson }
2980d0321e0SJeremy L Thompson 
2990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
300dce49693SSebastian Grimberg // Apply restriction
301dce49693SSebastian Grimberg //------------------------------------------------------------------------------
302dce49693SSebastian Grimberg static int CeedElemRestrictionApply_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
303dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, true, true, u, v, request);
304dce49693SSebastian Grimberg }
305dce49693SSebastian Grimberg 
306dce49693SSebastian Grimberg //------------------------------------------------------------------------------
307dce49693SSebastian Grimberg // Apply unsigned restriction
308dce49693SSebastian Grimberg //------------------------------------------------------------------------------
309dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnsigned_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
310dce49693SSebastian Grimberg                                                 CeedRequest *request) {
311dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, true, u, v, request);
312dce49693SSebastian Grimberg }
313dce49693SSebastian Grimberg 
314dce49693SSebastian Grimberg //------------------------------------------------------------------------------
315dce49693SSebastian Grimberg // Apply unoriented restriction
316dce49693SSebastian Grimberg //------------------------------------------------------------------------------
317dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnoriented_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
318dce49693SSebastian Grimberg                                                   CeedRequest *request) {
319dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, false, u, v, request);
320dce49693SSebastian Grimberg }
321dce49693SSebastian Grimberg 
322dce49693SSebastian Grimberg //------------------------------------------------------------------------------
3230d0321e0SJeremy L Thompson // Get offsets
3240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
325472941f0SJeremy L Thompson static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
3260d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
3270d0321e0SJeremy L Thompson 
328b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
329472941f0SJeremy L Thompson   switch (mem_type) {
3300d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
331*a267acd1SJeremy L Thompson       *offsets = impl->h_offsets;
3320d0321e0SJeremy L Thompson       break;
3330d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
334*a267acd1SJeremy L Thompson       *offsets = impl->d_offsets;
3350d0321e0SJeremy L Thompson       break;
3360d0321e0SJeremy L Thompson   }
3370d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3380d0321e0SJeremy L Thompson }
3390d0321e0SJeremy L Thompson 
3400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
341dce49693SSebastian Grimberg // Get orientations
342dce49693SSebastian Grimberg //------------------------------------------------------------------------------
343dce49693SSebastian Grimberg static int CeedElemRestrictionGetOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
344dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
345dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
346dce49693SSebastian Grimberg 
347dce49693SSebastian Grimberg   switch (mem_type) {
348dce49693SSebastian Grimberg     case CEED_MEM_HOST:
349dce49693SSebastian Grimberg       *orients = impl->h_orients;
350dce49693SSebastian Grimberg       break;
351dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
352dce49693SSebastian Grimberg       *orients = impl->d_orients;
353dce49693SSebastian Grimberg       break;
354dce49693SSebastian Grimberg   }
355dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
356dce49693SSebastian Grimberg }
357dce49693SSebastian Grimberg 
358dce49693SSebastian Grimberg //------------------------------------------------------------------------------
359dce49693SSebastian Grimberg // Get curl-conforming orientations
360dce49693SSebastian Grimberg //------------------------------------------------------------------------------
361dce49693SSebastian Grimberg static int CeedElemRestrictionGetCurlOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
362dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
363dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
364dce49693SSebastian Grimberg 
365dce49693SSebastian Grimberg   switch (mem_type) {
366dce49693SSebastian Grimberg     case CEED_MEM_HOST:
367dce49693SSebastian Grimberg       *curl_orients = impl->h_curl_orients;
368dce49693SSebastian Grimberg       break;
369dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
370dce49693SSebastian Grimberg       *curl_orients = impl->d_curl_orients;
371dce49693SSebastian Grimberg       break;
372dce49693SSebastian Grimberg   }
373dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
374dce49693SSebastian Grimberg }
375dce49693SSebastian Grimberg 
376dce49693SSebastian Grimberg //------------------------------------------------------------------------------
3770d0321e0SJeremy L Thompson // Destroy restriction
3780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
379dce49693SSebastian Grimberg static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction rstr) {
3800d0321e0SJeremy L Thompson   Ceed                     ceed;
381b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
382b7453713SJeremy L Thompson 
383dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
384dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
385cf8cbdd6SSebastian Grimberg   if (impl->module) {
3862b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->module));
387cf8cbdd6SSebastian Grimberg   }
388*a267acd1SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_offsets_owned));
389*a267acd1SJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_offsets_owned));
3902b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_offsets));
3912b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_indices));
3922b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_l_vec_indices));
393*a267acd1SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_orients_owned));
394*a267acd1SJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_orients_owned));
395*a267acd1SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_curl_orients_owned));
396*a267acd1SJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_curl_orients_owned));
3972b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
3980d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3990d0321e0SJeremy L Thompson }
4000d0321e0SJeremy L Thompson 
4010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4020d0321e0SJeremy L Thompson // Create transpose offsets and indices
4030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
404dce49693SSebastian Grimberg static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction rstr, const CeedInt *indices) {
4050d0321e0SJeremy L Thompson   Ceed                     ceed;
406b7453713SJeremy L Thompson   bool                    *is_node;
407e79b91d9SJeremy L Thompson   CeedSize                 l_size;
408dce49693SSebastian Grimberg   CeedInt                  num_elem, elem_size, num_comp, num_nodes = 0;
409dce49693SSebastian Grimberg   CeedInt                 *ind_to_offset, *l_vec_indices, *t_offsets, *t_indices;
410b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
411b7453713SJeremy L Thompson 
412dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
413dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
414dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
415dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
416dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
417dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
418b7453713SJeremy L Thompson   const CeedInt size_indices = num_elem * elem_size;
4190d0321e0SJeremy L Thompson 
420437930d1SJeremy L Thompson   // Count num_nodes
4212b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &is_node));
422dce49693SSebastian Grimberg 
4232b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1;
4242b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i];
425437930d1SJeremy L Thompson   impl->num_nodes = num_nodes;
4260d0321e0SJeremy L Thompson 
4270d0321e0SJeremy L Thompson   // L-vector offsets array
4282b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &ind_to_offset));
4292b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices));
430b7453713SJeremy L Thompson   for (CeedInt i = 0, j = 0; i < l_size; i++) {
431437930d1SJeremy L Thompson     if (is_node[i]) {
432437930d1SJeremy L Thompson       l_vec_indices[j] = i;
4330d0321e0SJeremy L Thompson       ind_to_offset[i] = j++;
4340d0321e0SJeremy L Thompson     }
4352b730f8bSJeremy L Thompson   }
4362b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&is_node));
4370d0321e0SJeremy L Thompson 
4380d0321e0SJeremy L Thompson   // Compute transpose offsets and indices
439437930d1SJeremy L Thompson   const CeedInt size_offsets = num_nodes + 1;
440b7453713SJeremy L Thompson 
4412b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(size_offsets, &t_offsets));
4422b730f8bSJeremy L Thompson   CeedCallBackend(CeedMalloc(size_indices, &t_indices));
4430d0321e0SJeremy L Thompson   // Count node multiplicity
4442b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
4452b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1];
4462b730f8bSJeremy L Thompson   }
4470d0321e0SJeremy L Thompson   // Convert to running sum
4482b730f8bSJeremy L Thompson   for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1];
4490d0321e0SJeremy L Thompson   // List all E-vec indices associated with L-vec node
450437930d1SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
451437930d1SJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) {
452437930d1SJeremy L Thompson       const CeedInt lid = elem_size * e + i;
4530d0321e0SJeremy L Thompson       const CeedInt gid = indices[lid];
454b7453713SJeremy L Thompson 
455437930d1SJeremy L Thompson       t_indices[t_offsets[ind_to_offset[gid]]++] = lid;
4560d0321e0SJeremy L Thompson     }
4570d0321e0SJeremy L Thompson   }
4580d0321e0SJeremy L Thompson   // Reset running sum
4592b730f8bSJeremy L Thompson   for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1];
460437930d1SJeremy L Thompson   t_offsets[0] = 0;
4610d0321e0SJeremy L Thompson 
4620d0321e0SJeremy L Thompson   // Copy data to device
4630d0321e0SJeremy L Thompson   // -- L-vector indices
4642b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt)));
4652b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice));
4660d0321e0SJeremy L Thompson   // -- Transpose offsets
4672b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt)));
4682b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice));
4690d0321e0SJeremy L Thompson   // -- Transpose indices
4702b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt)));
4712b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice));
4720d0321e0SJeremy L Thompson 
4730d0321e0SJeremy L Thompson   // Cleanup
4742b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&ind_to_offset));
4752b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&l_vec_indices));
4762b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_offsets));
4772b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_indices));
4780d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4790d0321e0SJeremy L Thompson }
4800d0321e0SJeremy L Thompson 
4810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4820d0321e0SJeremy L Thompson // Create restriction
4830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
484*a267acd1SJeremy L Thompson int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
485dce49693SSebastian Grimberg                                   const CeedInt8 *curl_orients, CeedElemRestriction rstr) {
486b7453713SJeremy L Thompson   Ceed                     ceed, ceed_parent;
487dce49693SSebastian Grimberg   bool                     is_deterministic;
488cf8cbdd6SSebastian Grimberg   CeedInt                  num_elem, elem_size;
489b7453713SJeremy L Thompson   CeedRestrictionType      rstr_type;
4900d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
491b7453713SJeremy L Thompson 
492dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
493ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
494ca735530SJeremy L Thompson   CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic));
495dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
496dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
49722eb1385SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
498dce49693SSebastian Grimberg   const CeedInt size = num_elem * elem_size;
4990d0321e0SJeremy L Thompson 
500dce49693SSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
501dce49693SSebastian Grimberg   impl->num_nodes = size;
502dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetData(rstr, impl));
50322eb1385SJeremy L Thompson 
50422eb1385SJeremy L Thompson   // Set layouts
50522eb1385SJeremy L Thompson   {
50622eb1385SJeremy L Thompson     bool    has_backend_strides;
50722eb1385SJeremy L Thompson     CeedInt layout[3] = {1, size, elem_size};
50822eb1385SJeremy L Thompson 
509dce49693SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetELayout(rstr, layout));
51022eb1385SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_STRIDED) {
51122eb1385SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
51222eb1385SJeremy L Thompson       if (has_backend_strides) {
51322eb1385SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionSetLLayout(rstr, layout));
51422eb1385SJeremy L Thompson       }
51522eb1385SJeremy L Thompson     }
51622eb1385SJeremy L Thompson   }
5170d0321e0SJeremy L Thompson 
518dce49693SSebastian Grimberg   // Set up device offset/orientation arrays
519dce49693SSebastian Grimberg   if (rstr_type != CEED_RESTRICTION_STRIDED) {
520472941f0SJeremy L Thompson     switch (mem_type) {
5216574a04fSJeremy L Thompson       case CEED_MEM_HOST: {
522472941f0SJeremy L Thompson         switch (copy_mode) {
523*a267acd1SJeremy L Thompson           case CEED_COPY_VALUES:
524*a267acd1SJeremy L Thompson             CeedCallBackend(CeedMalloc(size, &impl->h_offsets_owned));
525*a267acd1SJeremy L Thompson             memcpy(impl->h_offsets_owned, offsets, size * sizeof(CeedInt));
526*a267acd1SJeremy L Thompson             impl->h_offsets_borrowed = NULL;
527*a267acd1SJeremy L Thompson             impl->h_offsets          = impl->h_offsets_owned;
528*a267acd1SJeremy L Thompson             break;
5290d0321e0SJeremy L Thompson           case CEED_OWN_POINTER:
530*a267acd1SJeremy L Thompson             impl->h_offsets_owned    = (CeedInt *)offsets;
531*a267acd1SJeremy L Thompson             impl->h_offsets_borrowed = NULL;
532*a267acd1SJeremy L Thompson             impl->h_offsets          = impl->h_offsets_owned;
5330d0321e0SJeremy L Thompson             break;
5340d0321e0SJeremy L Thompson           case CEED_USE_POINTER:
535*a267acd1SJeremy L Thompson             impl->h_offsets_owned    = NULL;
536*a267acd1SJeremy L Thompson             impl->h_offsets_borrowed = (CeedInt *)offsets;
537*a267acd1SJeremy L Thompson             impl->h_offsets          = impl->h_offsets_borrowed;
5380d0321e0SJeremy L Thompson             break;
5390d0321e0SJeremy L Thompson         }
540*a267acd1SJeremy L Thompson         CeedCallHip(ceed, hipMalloc((void **)&impl->d_offsets_owned, size * sizeof(CeedInt)));
541*a267acd1SJeremy L Thompson         CeedCallHip(ceed, hipMemcpy(impl->d_offsets_owned, impl->h_offsets, size * sizeof(CeedInt), hipMemcpyHostToDevice));
542*a267acd1SJeremy L Thompson         impl->d_offsets = impl->d_offsets_owned;
543*a267acd1SJeremy L Thompson         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, offsets));
544dce49693SSebastian Grimberg       } break;
5456574a04fSJeremy L Thompson       case CEED_MEM_DEVICE: {
546472941f0SJeremy L Thompson         switch (copy_mode) {
5470d0321e0SJeremy L Thompson           case CEED_COPY_VALUES:
548*a267acd1SJeremy L Thompson             CeedCallHip(ceed, hipMalloc((void **)&impl->d_offsets_owned, size * sizeof(CeedInt)));
549*a267acd1SJeremy L Thompson             CeedCallHip(ceed, hipMemcpy(impl->d_offsets_owned, offsets, size * sizeof(CeedInt), hipMemcpyDeviceToDevice));
550*a267acd1SJeremy L Thompson             impl->d_offsets_borrowed = NULL;
551*a267acd1SJeremy L Thompson             impl->d_offsets          = impl->d_offsets_owned;
5520d0321e0SJeremy L Thompson             break;
5530d0321e0SJeremy L Thompson           case CEED_OWN_POINTER:
554*a267acd1SJeremy L Thompson             impl->d_offsets_owned    = (CeedInt *)offsets;
555*a267acd1SJeremy L Thompson             impl->d_offsets_borrowed = NULL;
556*a267acd1SJeremy L Thompson             impl->d_offsets          = impl->d_offsets_owned;
5570d0321e0SJeremy L Thompson             break;
5580d0321e0SJeremy L Thompson           case CEED_USE_POINTER:
559*a267acd1SJeremy L Thompson             impl->d_offsets_owned    = NULL;
560*a267acd1SJeremy L Thompson             impl->d_offsets_borrowed = (CeedInt *)offsets;
561*a267acd1SJeremy L Thompson             impl->d_offsets          = impl->d_offsets_borrowed;
5626574a04fSJeremy L Thompson             break;
5636574a04fSJeremy L Thompson         }
564*a267acd1SJeremy L Thompson         CeedCallBackend(CeedMalloc(size, &impl->h_offsets_owned));
565*a267acd1SJeremy L Thompson         CeedCallHip(ceed, hipMemcpy(impl->h_offsets_owned, impl->d_offsets, size * sizeof(CeedInt), hipMemcpyDeviceToHost));
566*a267acd1SJeremy L Thompson         impl->h_offsets = impl->h_offsets_owned;
567*a267acd1SJeremy L Thompson         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, offsets));
568dce49693SSebastian Grimberg       } break;
569dce49693SSebastian Grimberg     }
570dce49693SSebastian Grimberg 
571dce49693SSebastian Grimberg     // Orientation data
572dce49693SSebastian Grimberg     if (rstr_type == CEED_RESTRICTION_ORIENTED) {
573dce49693SSebastian Grimberg       switch (mem_type) {
574dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
575dce49693SSebastian Grimberg           switch (copy_mode) {
576*a267acd1SJeremy L Thompson             case CEED_COPY_VALUES:
577*a267acd1SJeremy L Thompson               CeedCallBackend(CeedMalloc(size, &impl->h_orients_owned));
578*a267acd1SJeremy L Thompson               memcpy(impl->h_orients_owned, orients, size * sizeof(bool));
579*a267acd1SJeremy L Thompson               impl->h_orients_borrowed = NULL;
580*a267acd1SJeremy L Thompson               impl->h_orients          = impl->h_orients_owned;
581*a267acd1SJeremy L Thompson               break;
582dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
583*a267acd1SJeremy L Thompson               impl->h_orients_owned    = (bool *)orients;
584*a267acd1SJeremy L Thompson               impl->h_orients_borrowed = NULL;
585*a267acd1SJeremy L Thompson               impl->h_orients          = impl->h_orients_owned;
586dce49693SSebastian Grimberg               break;
587dce49693SSebastian Grimberg             case CEED_USE_POINTER:
588*a267acd1SJeremy L Thompson               impl->h_orients_owned    = NULL;
589*a267acd1SJeremy L Thompson               impl->h_orients_borrowed = (bool *)orients;
590*a267acd1SJeremy L Thompson               impl->h_orients          = impl->h_orients_borrowed;
591dce49693SSebastian Grimberg               break;
592dce49693SSebastian Grimberg           }
593*a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients_owned, size * sizeof(bool)));
594*a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(impl->d_orients_owned, impl->h_orients, size * sizeof(bool), hipMemcpyHostToDevice));
595*a267acd1SJeremy L Thompson           impl->d_orients = impl->d_orients_owned;
596dce49693SSebastian Grimberg         } break;
597dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
598dce49693SSebastian Grimberg           switch (copy_mode) {
599dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
600*a267acd1SJeremy L Thompson               CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients_owned, size * sizeof(bool)));
601*a267acd1SJeremy L Thompson               CeedCallHip(ceed, hipMemcpy(impl->d_orients_owned, orients, size * sizeof(bool), hipMemcpyDeviceToDevice));
602*a267acd1SJeremy L Thompson               impl->d_orients_borrowed = NULL;
603*a267acd1SJeremy L Thompson               impl->d_orients          = impl->d_orients_owned;
604dce49693SSebastian Grimberg               break;
605dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
606*a267acd1SJeremy L Thompson               impl->d_orients_owned    = (bool *)orients;
607*a267acd1SJeremy L Thompson               impl->d_orients_borrowed = NULL;
608*a267acd1SJeremy L Thompson               impl->d_orients          = impl->d_orients_owned;
609dce49693SSebastian Grimberg               break;
610dce49693SSebastian Grimberg             case CEED_USE_POINTER:
611*a267acd1SJeremy L Thompson               impl->d_orients_owned    = NULL;
612*a267acd1SJeremy L Thompson               impl->d_orients_borrowed = (bool *)orients;
613*a267acd1SJeremy L Thompson               impl->d_orients          = impl->d_orients_borrowed;
614dce49693SSebastian Grimberg               break;
615dce49693SSebastian Grimberg           }
616*a267acd1SJeremy L Thompson           CeedCallBackend(CeedMalloc(size, &impl->h_orients_owned));
617*a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(impl->h_orients_owned, impl->d_orients, size * sizeof(bool), hipMemcpyDeviceToHost));
618*a267acd1SJeremy L Thompson           impl->h_orients = impl->h_orients_owned;
619dce49693SSebastian Grimberg         } break;
620dce49693SSebastian Grimberg       }
621dce49693SSebastian Grimberg     } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
622dce49693SSebastian Grimberg       switch (mem_type) {
623dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
624dce49693SSebastian Grimberg           switch (copy_mode) {
625*a267acd1SJeremy L Thompson             case CEED_COPY_VALUES:
626*a267acd1SJeremy L Thompson               CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_owned));
627*a267acd1SJeremy L Thompson               memcpy(impl->h_curl_orients_owned, curl_orients, 3 * size * sizeof(CeedInt8));
628*a267acd1SJeremy L Thompson               impl->h_curl_orients_borrowed = NULL;
629*a267acd1SJeremy L Thompson               impl->h_curl_orients          = impl->h_curl_orients_owned;
630*a267acd1SJeremy L Thompson               break;
631dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
632*a267acd1SJeremy L Thompson               impl->h_curl_orients_owned    = (CeedInt8 *)curl_orients;
633*a267acd1SJeremy L Thompson               impl->h_curl_orients_borrowed = NULL;
634*a267acd1SJeremy L Thompson               impl->h_curl_orients          = impl->h_curl_orients_owned;
635dce49693SSebastian Grimberg               break;
636dce49693SSebastian Grimberg             case CEED_USE_POINTER:
637*a267acd1SJeremy L Thompson               impl->h_curl_orients_owned    = NULL;
638*a267acd1SJeremy L Thompson               impl->h_curl_orients_borrowed = (CeedInt8 *)curl_orients;
639*a267acd1SJeremy L Thompson               impl->h_curl_orients          = impl->h_curl_orients_borrowed;
640dce49693SSebastian Grimberg               break;
641dce49693SSebastian Grimberg           }
642*a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients_owned, 3 * size * sizeof(CeedInt8)));
643*a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients_owned, impl->h_curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyHostToDevice));
644*a267acd1SJeremy L Thompson           impl->d_curl_orients = impl->d_curl_orients_owned;
645dce49693SSebastian Grimberg         } break;
646dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
647dce49693SSebastian Grimberg           switch (copy_mode) {
648dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
649*a267acd1SJeremy L Thompson               CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients_owned, 3 * size * sizeof(CeedInt8)));
650*a267acd1SJeremy L Thompson               CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients_owned, curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToDevice));
651*a267acd1SJeremy L Thompson               impl->d_curl_orients_borrowed = NULL;
652*a267acd1SJeremy L Thompson               impl->d_curl_orients          = impl->d_curl_orients_owned;
653dce49693SSebastian Grimberg               break;
654dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
655*a267acd1SJeremy L Thompson               impl->d_curl_orients_owned    = (CeedInt8 *)curl_orients;
656*a267acd1SJeremy L Thompson               impl->d_curl_orients_borrowed = NULL;
657*a267acd1SJeremy L Thompson               impl->d_curl_orients          = impl->d_curl_orients_owned;
658dce49693SSebastian Grimberg               break;
659dce49693SSebastian Grimberg             case CEED_USE_POINTER:
660*a267acd1SJeremy L Thompson               impl->d_curl_orients_owned    = NULL;
661*a267acd1SJeremy L Thompson               impl->d_curl_orients_borrowed = (CeedInt8 *)curl_orients;
662*a267acd1SJeremy L Thompson               impl->d_curl_orients          = impl->d_curl_orients_borrowed;
663dce49693SSebastian Grimberg               break;
664dce49693SSebastian Grimberg           }
665*a267acd1SJeremy L Thompson           CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_owned));
666*a267acd1SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(impl->h_curl_orients_owned, impl->d_curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToHost));
667*a267acd1SJeremy L Thompson           impl->h_curl_orients = impl->h_curl_orients_owned;
668dce49693SSebastian Grimberg         } break;
669dce49693SSebastian Grimberg       }
670dce49693SSebastian Grimberg     }
6710d0321e0SJeremy L Thompson   }
6720d0321e0SJeremy L Thompson 
6730d0321e0SJeremy L Thompson   // Register backend functions
674dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Apply", CeedElemRestrictionApply_Hip));
675dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnsigned", CeedElemRestrictionApplyUnsigned_Hip));
676dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApplyUnoriented_Hip));
677dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Hip));
678dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Hip));
679dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Hip));
680dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Hip));
6810d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6820d0321e0SJeremy L Thompson }
6830d0321e0SJeremy L Thompson 
6840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
685