xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-restriction.c (revision 7aa91133a7af23b7649782744c8ef18519e34736)
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 //------------------------------------------------------------------------------
21dce49693SSebastian Grimberg // Core apply restriction code
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
23dce49693SSebastian Grimberg static inline int CeedElemRestrictionApply_Hip_Core(CeedElemRestriction rstr, CeedTransposeMode t_mode, bool use_signs, bool use_orients,
24dce49693SSebastian Grimberg                                                     CeedVector u, CeedVector v, CeedRequest *request) {
250d0321e0SJeremy L Thompson   Ceed                     ceed;
26437930d1SJeremy L Thompson   CeedInt                  num_elem, elem_size;
27dce49693SSebastian Grimberg   CeedRestrictionType      rstr_type;
280d0321e0SJeremy L Thompson   const CeedScalar        *d_u;
290d0321e0SJeremy L Thompson   CeedScalar              *d_v;
30b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
31b7453713SJeremy L Thompson   hipFunction_t            kernel;
32b7453713SJeremy L Thompson 
33dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
34dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
35dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
36dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
37dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
38b7453713SJeremy L Thompson   const CeedInt num_nodes = impl->num_nodes;
39b7453713SJeremy L Thompson 
40b7453713SJeremy L Thompson   // Get vectors
412b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
42437930d1SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
430d0321e0SJeremy L Thompson     // Sum into for transpose mode, e-vec to l-vec
442b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
450d0321e0SJeremy L Thompson   } else {
460d0321e0SJeremy L Thompson     // Overwrite for notranspose mode, l-vec to e-vec
472b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
480d0321e0SJeremy L Thompson   }
490d0321e0SJeremy L Thompson 
500d0321e0SJeremy L Thompson   // Restrict
51437930d1SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
520d0321e0SJeremy L Thompson     // L-vector -> E-vector
53dce49693SSebastian Grimberg     const CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
54dce49693SSebastian Grimberg     const CeedInt grid       = CeedDivUpInt(num_nodes, block_size);
5558549094SSebastian Grimberg 
56dce49693SSebastian Grimberg     switch (rstr_type) {
57dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
58437930d1SJeremy L Thompson         kernel       = impl->StridedNoTranspose;
59437930d1SJeremy L Thompson         void *args[] = {&num_elem, &d_u, &d_v};
6058549094SSebastian Grimberg 
61dce49693SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
62dce49693SSebastian Grimberg       } break;
63dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
64dce49693SSebastian Grimberg         kernel       = impl->OffsetNoTranspose;
65dce49693SSebastian Grimberg         void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
66dce49693SSebastian Grimberg 
67dce49693SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
68dce49693SSebastian Grimberg       } break;
69dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
70dce49693SSebastian Grimberg         if (use_signs) {
71dce49693SSebastian Grimberg           kernel       = impl->OrientedNoTranspose;
72dce49693SSebastian Grimberg           void *args[] = {&num_elem, &impl->d_ind, &impl->d_orients, &d_u, &d_v};
73dce49693SSebastian Grimberg 
74dce49693SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
75dce49693SSebastian Grimberg         } else {
76dce49693SSebastian Grimberg           kernel       = impl->OffsetNoTranspose;
77dce49693SSebastian Grimberg           void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
78dce49693SSebastian Grimberg 
79dce49693SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
80dce49693SSebastian Grimberg         }
81dce49693SSebastian Grimberg       } break;
82dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
83dce49693SSebastian Grimberg         if (use_signs && use_orients) {
84dce49693SSebastian Grimberg           kernel       = impl->CurlOrientedNoTranspose;
85dce49693SSebastian Grimberg           void *args[] = {&num_elem, &impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
86dce49693SSebastian Grimberg 
87dce49693SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
88dce49693SSebastian Grimberg         } else if (use_orients) {
89dce49693SSebastian Grimberg           kernel       = impl->CurlOrientedUnsignedNoTranspose;
90dce49693SSebastian Grimberg           void *args[] = {&num_elem, &impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
91dce49693SSebastian Grimberg 
92dce49693SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
93dce49693SSebastian Grimberg         } else {
94dce49693SSebastian Grimberg           kernel       = impl->OffsetNoTranspose;
95dce49693SSebastian Grimberg           void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
96dce49693SSebastian Grimberg 
97dce49693SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
98dce49693SSebastian Grimberg         }
99dce49693SSebastian Grimberg       } break;
100b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
101b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
102b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
103b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
104b3d03e38SSebastian Grimberg       } break;
1050d0321e0SJeremy L Thompson     }
1060d0321e0SJeremy L Thompson   } else {
1070d0321e0SJeremy L Thompson     // E-vector -> L-vector
108dce49693SSebastian Grimberg     const CeedInt block_size = 64;
109dce49693SSebastian Grimberg     const CeedInt grid       = CeedDivUpInt(num_nodes, block_size);
110b7453713SJeremy L Thompson 
111dce49693SSebastian Grimberg     switch (rstr_type) {
112dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
113dce49693SSebastian Grimberg         kernel       = impl->StridedTranspose;
114dce49693SSebastian Grimberg         void *args[] = {&num_elem, &d_u, &d_v};
115dce49693SSebastian Grimberg 
116dce49693SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
117dce49693SSebastian Grimberg       } break;
118dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
11958549094SSebastian Grimberg         if (impl->OffsetTranspose) {
120437930d1SJeremy L Thompson           kernel       = impl->OffsetTranspose;
12158549094SSebastian Grimberg           void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
12258549094SSebastian Grimberg 
123dce49693SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
1240d0321e0SJeremy L Thompson         } else {
12558549094SSebastian Grimberg           kernel       = impl->OffsetTransposeDet;
12658549094SSebastian Grimberg           void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
12758549094SSebastian Grimberg 
128dce49693SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
12958549094SSebastian Grimberg         }
130dce49693SSebastian Grimberg       } break;
131dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
132dce49693SSebastian Grimberg         if (use_signs) {
133*7aa91133SSebastian Grimberg           if (impl->OrientedTranspose) {
134dce49693SSebastian Grimberg             kernel       = impl->OrientedTranspose;
135dce49693SSebastian Grimberg             void *args[] = {&num_elem, &impl->d_ind, &impl->d_orients, &d_u, &d_v};
13658549094SSebastian Grimberg 
137dce49693SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
138dce49693SSebastian Grimberg           } else {
139*7aa91133SSebastian Grimberg             kernel       = impl->OrientedTransposeDet;
140*7aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_orients, &d_u, &d_v};
141*7aa91133SSebastian Grimberg 
142*7aa91133SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
143*7aa91133SSebastian Grimberg           }
144*7aa91133SSebastian Grimberg         } else {
145dce49693SSebastian Grimberg           if (impl->OffsetTranspose) {
146dce49693SSebastian Grimberg             kernel       = impl->OffsetTranspose;
147dce49693SSebastian Grimberg             void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
148dce49693SSebastian Grimberg 
149dce49693SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
150dce49693SSebastian Grimberg           } else {
151dce49693SSebastian Grimberg             kernel       = impl->OffsetTransposeDet;
152dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
153dce49693SSebastian Grimberg 
154dce49693SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
155dce49693SSebastian Grimberg           }
156dce49693SSebastian Grimberg         }
157dce49693SSebastian Grimberg       } break;
158dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
159dce49693SSebastian Grimberg         if (use_signs && use_orients) {
160*7aa91133SSebastian Grimberg           if (impl->CurlOrientedTranspose) {
161dce49693SSebastian Grimberg             kernel       = impl->CurlOrientedTranspose;
162dce49693SSebastian Grimberg             void *args[] = {&num_elem, &impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
163dce49693SSebastian Grimberg 
164dce49693SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
165*7aa91133SSebastian Grimberg           } else {
166*7aa91133SSebastian Grimberg             kernel       = impl->CurlOrientedTransposeDet;
167*7aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
168*7aa91133SSebastian Grimberg 
169*7aa91133SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
170*7aa91133SSebastian Grimberg           }
171dce49693SSebastian Grimberg         } else if (use_orients) {
172*7aa91133SSebastian Grimberg           if (impl->CurlOrientedUnsignedTranspose) {
173dce49693SSebastian Grimberg             kernel       = impl->CurlOrientedUnsignedTranspose;
174dce49693SSebastian Grimberg             void *args[] = {&num_elem, &impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
175dce49693SSebastian Grimberg 
176dce49693SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
177dce49693SSebastian Grimberg           } else {
178*7aa91133SSebastian Grimberg             kernel       = impl->CurlOrientedUnsignedTransposeDet;
179*7aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
180*7aa91133SSebastian Grimberg 
181*7aa91133SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
182*7aa91133SSebastian Grimberg           }
183*7aa91133SSebastian Grimberg         } else {
184dce49693SSebastian Grimberg           if (impl->OffsetTranspose) {
185dce49693SSebastian Grimberg             kernel       = impl->OffsetTranspose;
186dce49693SSebastian Grimberg             void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
187dce49693SSebastian Grimberg 
188dce49693SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
189dce49693SSebastian Grimberg           } else {
190dce49693SSebastian Grimberg             kernel       = impl->OffsetTransposeDet;
191dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
192dce49693SSebastian Grimberg 
193dce49693SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, grid, block_size, args));
194dce49693SSebastian Grimberg           }
195dce49693SSebastian Grimberg         }
196dce49693SSebastian Grimberg       } break;
197b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
198b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
199b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
200b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
201b3d03e38SSebastian Grimberg       } break;
2020d0321e0SJeremy L Thompson     }
2030d0321e0SJeremy L Thompson   }
2040d0321e0SJeremy L Thompson 
2052b730f8bSJeremy L Thompson   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
2060d0321e0SJeremy L Thompson 
2070d0321e0SJeremy L Thompson   // Restore arrays
2082b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
2092b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2100d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2110d0321e0SJeremy L Thompson }
2120d0321e0SJeremy L Thompson 
2130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
214dce49693SSebastian Grimberg // Apply restriction
215dce49693SSebastian Grimberg //------------------------------------------------------------------------------
216dce49693SSebastian Grimberg static int CeedElemRestrictionApply_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
217dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, true, true, u, v, request);
218dce49693SSebastian Grimberg }
219dce49693SSebastian Grimberg 
220dce49693SSebastian Grimberg //------------------------------------------------------------------------------
221dce49693SSebastian Grimberg // Apply unsigned restriction
222dce49693SSebastian Grimberg //------------------------------------------------------------------------------
223dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnsigned_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
224dce49693SSebastian Grimberg                                                 CeedRequest *request) {
225dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, true, u, v, request);
226dce49693SSebastian Grimberg }
227dce49693SSebastian Grimberg 
228dce49693SSebastian Grimberg //------------------------------------------------------------------------------
229dce49693SSebastian Grimberg // Apply unoriented restriction
230dce49693SSebastian Grimberg //------------------------------------------------------------------------------
231dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnoriented_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
232dce49693SSebastian Grimberg                                                   CeedRequest *request) {
233dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, false, u, v, request);
234dce49693SSebastian Grimberg }
235dce49693SSebastian Grimberg 
236dce49693SSebastian Grimberg //------------------------------------------------------------------------------
2370d0321e0SJeremy L Thompson // Get offsets
2380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
239472941f0SJeremy L Thompson static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
2400d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
2410d0321e0SJeremy L Thompson 
242b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
243472941f0SJeremy L Thompson   switch (mem_type) {
2440d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
2450d0321e0SJeremy L Thompson       *offsets = impl->h_ind;
2460d0321e0SJeremy L Thompson       break;
2470d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
2480d0321e0SJeremy L Thompson       *offsets = impl->d_ind;
2490d0321e0SJeremy L Thompson       break;
2500d0321e0SJeremy L Thompson   }
2510d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2520d0321e0SJeremy L Thompson }
2530d0321e0SJeremy L Thompson 
2540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
255dce49693SSebastian Grimberg // Get orientations
256dce49693SSebastian Grimberg //------------------------------------------------------------------------------
257dce49693SSebastian Grimberg static int CeedElemRestrictionGetOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
258dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
259dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
260dce49693SSebastian Grimberg 
261dce49693SSebastian Grimberg   switch (mem_type) {
262dce49693SSebastian Grimberg     case CEED_MEM_HOST:
263dce49693SSebastian Grimberg       *orients = impl->h_orients;
264dce49693SSebastian Grimberg       break;
265dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
266dce49693SSebastian Grimberg       *orients = impl->d_orients;
267dce49693SSebastian Grimberg       break;
268dce49693SSebastian Grimberg   }
269dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
270dce49693SSebastian Grimberg }
271dce49693SSebastian Grimberg 
272dce49693SSebastian Grimberg //------------------------------------------------------------------------------
273dce49693SSebastian Grimberg // Get curl-conforming orientations
274dce49693SSebastian Grimberg //------------------------------------------------------------------------------
275dce49693SSebastian Grimberg static int CeedElemRestrictionGetCurlOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
276dce49693SSebastian Grimberg   CeedElemRestriction_Hip *impl;
277dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
278dce49693SSebastian Grimberg 
279dce49693SSebastian Grimberg   switch (mem_type) {
280dce49693SSebastian Grimberg     case CEED_MEM_HOST:
281dce49693SSebastian Grimberg       *curl_orients = impl->h_curl_orients;
282dce49693SSebastian Grimberg       break;
283dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
284dce49693SSebastian Grimberg       *curl_orients = impl->d_curl_orients;
285dce49693SSebastian Grimberg       break;
286dce49693SSebastian Grimberg   }
287dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
288dce49693SSebastian Grimberg }
289dce49693SSebastian Grimberg 
290dce49693SSebastian Grimberg //------------------------------------------------------------------------------
2910d0321e0SJeremy L Thompson // Destroy restriction
2920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
293dce49693SSebastian Grimberg static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction rstr) {
2940d0321e0SJeremy L Thompson   Ceed                     ceed;
295b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
296b7453713SJeremy L Thompson 
297dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
298dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
2992b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(impl->module));
3002b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_ind_allocated));
3012b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_ind_allocated));
3022b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_offsets));
3032b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_indices));
3042b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_l_vec_indices));
305dce49693SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->h_orients_allocated));
306dce49693SSebastian Grimberg   CeedCallHip(ceed, hipFree(impl->d_orients_allocated));
307dce49693SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->h_curl_orients_allocated));
308dce49693SSebastian Grimberg   CeedCallHip(ceed, hipFree(impl->d_curl_orients_allocated));
3092b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
3100d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3110d0321e0SJeremy L Thompson }
3120d0321e0SJeremy L Thompson 
3130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3140d0321e0SJeremy L Thompson // Create transpose offsets and indices
3150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
316dce49693SSebastian Grimberg static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction rstr, const CeedInt *indices) {
3170d0321e0SJeremy L Thompson   Ceed                     ceed;
318b7453713SJeremy L Thompson   bool                    *is_node;
319e79b91d9SJeremy L Thompson   CeedSize                 l_size;
320dce49693SSebastian Grimberg   CeedInt                  num_elem, elem_size, num_comp, num_nodes = 0;
321dce49693SSebastian Grimberg   CeedInt                 *ind_to_offset, *l_vec_indices, *t_offsets, *t_indices;
322b7453713SJeremy L Thompson   CeedElemRestriction_Hip *impl;
323b7453713SJeremy L Thompson 
324dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
325dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
326dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
327dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
328dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
329dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
330b7453713SJeremy L Thompson   const CeedInt size_indices = num_elem * elem_size;
3310d0321e0SJeremy L Thompson 
332437930d1SJeremy L Thompson   // Count num_nodes
3332b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &is_node));
334dce49693SSebastian Grimberg 
3352b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1;
3362b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i];
337437930d1SJeremy L Thompson   impl->num_nodes = num_nodes;
3380d0321e0SJeremy L Thompson 
3390d0321e0SJeremy L Thompson   // L-vector offsets array
3402b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &ind_to_offset));
3412b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices));
342b7453713SJeremy L Thompson   for (CeedInt i = 0, j = 0; i < l_size; i++) {
343437930d1SJeremy L Thompson     if (is_node[i]) {
344437930d1SJeremy L Thompson       l_vec_indices[j] = i;
3450d0321e0SJeremy L Thompson       ind_to_offset[i] = j++;
3460d0321e0SJeremy L Thompson     }
3472b730f8bSJeremy L Thompson   }
3482b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&is_node));
3490d0321e0SJeremy L Thompson 
3500d0321e0SJeremy L Thompson   // Compute transpose offsets and indices
351437930d1SJeremy L Thompson   const CeedInt size_offsets = num_nodes + 1;
352b7453713SJeremy L Thompson 
3532b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(size_offsets, &t_offsets));
3542b730f8bSJeremy L Thompson   CeedCallBackend(CeedMalloc(size_indices, &t_indices));
3550d0321e0SJeremy L Thompson   // Count node multiplicity
3562b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
3572b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1];
3582b730f8bSJeremy L Thompson   }
3590d0321e0SJeremy L Thompson   // Convert to running sum
3602b730f8bSJeremy L Thompson   for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1];
3610d0321e0SJeremy L Thompson   // List all E-vec indices associated with L-vec node
362437930d1SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
363437930d1SJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) {
364437930d1SJeremy L Thompson       const CeedInt lid = elem_size * e + i;
3650d0321e0SJeremy L Thompson       const CeedInt gid = indices[lid];
366b7453713SJeremy L Thompson 
367437930d1SJeremy L Thompson       t_indices[t_offsets[ind_to_offset[gid]]++] = lid;
3680d0321e0SJeremy L Thompson     }
3690d0321e0SJeremy L Thompson   }
3700d0321e0SJeremy L Thompson   // Reset running sum
3712b730f8bSJeremy L Thompson   for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1];
372437930d1SJeremy L Thompson   t_offsets[0] = 0;
3730d0321e0SJeremy L Thompson 
3740d0321e0SJeremy L Thompson   // Copy data to device
3750d0321e0SJeremy L Thompson   // -- L-vector indices
3762b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt)));
3772b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice));
3780d0321e0SJeremy L Thompson   // -- Transpose offsets
3792b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt)));
3802b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice));
3810d0321e0SJeremy L Thompson   // -- Transpose indices
3822b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt)));
3832b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice));
3840d0321e0SJeremy L Thompson 
3850d0321e0SJeremy L Thompson   // Cleanup
3862b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&ind_to_offset));
3872b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&l_vec_indices));
3882b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_offsets));
3892b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_indices));
3900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3910d0321e0SJeremy L Thompson }
3920d0321e0SJeremy L Thompson 
3930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3940d0321e0SJeremy L Thompson // Create restriction
3950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
396fcbe8c06SSebastian Grimberg int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *indices, const bool *orients,
397dce49693SSebastian Grimberg                                   const CeedInt8 *curl_orients, CeedElemRestriction rstr) {
398b7453713SJeremy L Thompson   Ceed                     ceed, ceed_parent;
399dce49693SSebastian Grimberg   bool                     is_deterministic;
400b7453713SJeremy L Thompson   CeedInt                  num_elem, num_comp, elem_size, comp_stride = 1;
401b7453713SJeremy L Thompson   CeedRestrictionType      rstr_type;
402dce49693SSebastian Grimberg   char                    *restriction_kernel_path, *restriction_kernel_source;
4030d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
404b7453713SJeremy L Thompson 
405dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
406ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
407ca735530SJeremy L Thompson   CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic));
408dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
409dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
410dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
411dce49693SSebastian Grimberg   const CeedInt size       = num_elem * elem_size;
412437930d1SJeremy L Thompson   CeedInt       strides[3] = {1, size, elem_size};
413b7453713SJeremy L Thompson   CeedInt       layout[3]  = {1, elem_size * num_elem, elem_size};
4140d0321e0SJeremy L Thompson 
4150d0321e0SJeremy L Thompson   // Stride data
416dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
417dce49693SSebastian Grimberg   if (rstr_type == CEED_RESTRICTION_STRIDED) {
418437930d1SJeremy L Thompson     bool has_backend_strides;
419b7453713SJeremy L Thompson 
420dce49693SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
421437930d1SJeremy L Thompson     if (!has_backend_strides) {
422dce49693SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetStrides(rstr, &strides));
4230d0321e0SJeremy L Thompson     }
4240d0321e0SJeremy L Thompson   } else {
425dce49693SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
4260d0321e0SJeremy L Thompson   }
4270d0321e0SJeremy L Thompson 
428dce49693SSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
429dce49693SSebastian Grimberg   impl->num_nodes                = size;
4300d0321e0SJeremy L Thompson   impl->h_ind                    = NULL;
4310d0321e0SJeremy L Thompson   impl->h_ind_allocated          = NULL;
4320d0321e0SJeremy L Thompson   impl->d_ind                    = NULL;
4330d0321e0SJeremy L Thompson   impl->d_ind_allocated          = NULL;
434437930d1SJeremy L Thompson   impl->d_t_indices              = NULL;
435437930d1SJeremy L Thompson   impl->d_t_offsets              = NULL;
436dce49693SSebastian Grimberg   impl->h_orients                = NULL;
437dce49693SSebastian Grimberg   impl->h_orients_allocated      = NULL;
438dce49693SSebastian Grimberg   impl->d_orients                = NULL;
439dce49693SSebastian Grimberg   impl->d_orients_allocated      = NULL;
440dce49693SSebastian Grimberg   impl->h_curl_orients           = NULL;
441dce49693SSebastian Grimberg   impl->h_curl_orients_allocated = NULL;
442dce49693SSebastian Grimberg   impl->d_curl_orients           = NULL;
443dce49693SSebastian Grimberg   impl->d_curl_orients_allocated = NULL;
444dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetData(rstr, impl));
445dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetELayout(rstr, layout));
4460d0321e0SJeremy L Thompson 
447dce49693SSebastian Grimberg   // Set up device offset/orientation arrays
448dce49693SSebastian Grimberg   if (rstr_type != CEED_RESTRICTION_STRIDED) {
449472941f0SJeremy L Thompson     switch (mem_type) {
4506574a04fSJeremy L Thompson       case CEED_MEM_HOST: {
451472941f0SJeremy L Thompson         switch (copy_mode) {
4520d0321e0SJeremy L Thompson           case CEED_OWN_POINTER:
4530d0321e0SJeremy L Thompson             impl->h_ind_allocated = (CeedInt *)indices;
4540d0321e0SJeremy L Thompson             impl->h_ind           = (CeedInt *)indices;
4550d0321e0SJeremy L Thompson             break;
4560d0321e0SJeremy L Thompson           case CEED_USE_POINTER:
4570d0321e0SJeremy L Thompson             impl->h_ind = (CeedInt *)indices;
4580d0321e0SJeremy L Thompson             break;
4590d0321e0SJeremy L Thompson           case CEED_COPY_VALUES:
460dce49693SSebastian Grimberg             CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated));
461dce49693SSebastian Grimberg             memcpy(impl->h_ind_allocated, indices, size * sizeof(CeedInt));
46244d7a66cSJeremy L Thompson             impl->h_ind = impl->h_ind_allocated;
4630d0321e0SJeremy L Thompson             break;
4640d0321e0SJeremy L Thompson         }
4652b730f8bSJeremy L Thompson         CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
4660d0321e0SJeremy L Thompson         impl->d_ind_allocated = impl->d_ind;  // We own the device memory
4672b730f8bSJeremy L Thompson         CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyHostToDevice));
468dce49693SSebastian Grimberg         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, indices));
469dce49693SSebastian Grimberg       } break;
4706574a04fSJeremy L Thompson       case CEED_MEM_DEVICE: {
471472941f0SJeremy L Thompson         switch (copy_mode) {
4720d0321e0SJeremy L Thompson           case CEED_COPY_VALUES:
4732b730f8bSJeremy L Thompson             CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
4740d0321e0SJeremy L Thompson             impl->d_ind_allocated = impl->d_ind;  // We own the device memory
4752b730f8bSJeremy L Thompson             CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyDeviceToDevice));
4760d0321e0SJeremy L Thompson             break;
4770d0321e0SJeremy L Thompson           case CEED_OWN_POINTER:
4780d0321e0SJeremy L Thompson             impl->d_ind           = (CeedInt *)indices;
4790d0321e0SJeremy L Thompson             impl->d_ind_allocated = impl->d_ind;
4800d0321e0SJeremy L Thompson             break;
4810d0321e0SJeremy L Thompson           case CEED_USE_POINTER:
4820d0321e0SJeremy L Thompson             impl->d_ind = (CeedInt *)indices;
4836574a04fSJeremy L Thompson             break;
4846574a04fSJeremy L Thompson         }
485dce49693SSebastian Grimberg         CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated));
486dce49693SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(impl->h_ind_allocated, impl->d_ind, size * sizeof(CeedInt), hipMemcpyDeviceToHost));
487dce49693SSebastian Grimberg         impl->h_ind = impl->h_ind_allocated;
488dce49693SSebastian Grimberg         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, indices));
489dce49693SSebastian Grimberg       } break;
490dce49693SSebastian Grimberg     }
491dce49693SSebastian Grimberg 
492dce49693SSebastian Grimberg     // Orientation data
493dce49693SSebastian Grimberg     if (rstr_type == CEED_RESTRICTION_ORIENTED) {
494dce49693SSebastian Grimberg       switch (mem_type) {
495dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
496dce49693SSebastian Grimberg           switch (copy_mode) {
497dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
498dce49693SSebastian Grimberg               impl->h_orients_allocated = (bool *)orients;
499dce49693SSebastian Grimberg               impl->h_orients           = (bool *)orients;
500dce49693SSebastian Grimberg               break;
501dce49693SSebastian Grimberg             case CEED_USE_POINTER:
502dce49693SSebastian Grimberg               impl->h_orients = (bool *)orients;
503dce49693SSebastian Grimberg               break;
504dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
505dce49693SSebastian Grimberg               CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated));
506dce49693SSebastian Grimberg               memcpy(impl->h_orients_allocated, orients, size * sizeof(bool));
507dce49693SSebastian Grimberg               impl->h_orients = impl->h_orients_allocated;
508dce49693SSebastian Grimberg               break;
509dce49693SSebastian Grimberg           }
510dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients, size * sizeof(bool)));
511dce49693SSebastian Grimberg           impl->d_orients_allocated = impl->d_orients;  // We own the device memory
512dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->d_orients, orients, size * sizeof(bool), hipMemcpyHostToDevice));
513dce49693SSebastian Grimberg         } break;
514dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
515dce49693SSebastian Grimberg           switch (copy_mode) {
516dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
517dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients, size * sizeof(bool)));
518dce49693SSebastian Grimberg               impl->d_orients_allocated = impl->d_orients;  // We own the device memory
519dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMemcpy(impl->d_orients, orients, size * sizeof(bool), hipMemcpyDeviceToDevice));
520dce49693SSebastian Grimberg               break;
521dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
522dce49693SSebastian Grimberg               impl->d_orients           = (bool *)orients;
523dce49693SSebastian Grimberg               impl->d_orients_allocated = impl->d_orients;
524dce49693SSebastian Grimberg               break;
525dce49693SSebastian Grimberg             case CEED_USE_POINTER:
526dce49693SSebastian Grimberg               impl->d_orients = (bool *)orients;
527dce49693SSebastian Grimberg               break;
528dce49693SSebastian Grimberg           }
529dce49693SSebastian Grimberg           CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated));
530dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->h_orients_allocated, impl->d_orients, size * sizeof(bool), hipMemcpyDeviceToHost));
531dce49693SSebastian Grimberg           impl->h_orients = impl->h_orients_allocated;
532dce49693SSebastian Grimberg         } break;
533dce49693SSebastian Grimberg       }
534dce49693SSebastian Grimberg     } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
535dce49693SSebastian Grimberg       switch (mem_type) {
536dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
537dce49693SSebastian Grimberg           switch (copy_mode) {
538dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
539dce49693SSebastian Grimberg               impl->h_curl_orients_allocated = (CeedInt8 *)curl_orients;
540dce49693SSebastian Grimberg               impl->h_curl_orients           = (CeedInt8 *)curl_orients;
541dce49693SSebastian Grimberg               break;
542dce49693SSebastian Grimberg             case CEED_USE_POINTER:
543dce49693SSebastian Grimberg               impl->h_curl_orients = (CeedInt8 *)curl_orients;
544dce49693SSebastian Grimberg               break;
545dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
546dce49693SSebastian Grimberg               CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated));
547dce49693SSebastian Grimberg               memcpy(impl->h_curl_orients_allocated, curl_orients, 3 * size * sizeof(CeedInt8));
548dce49693SSebastian Grimberg               impl->h_curl_orients = impl->h_curl_orients_allocated;
549dce49693SSebastian Grimberg               break;
550dce49693SSebastian Grimberg           }
551dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8)));
552dce49693SSebastian Grimberg           impl->d_curl_orients_allocated = impl->d_curl_orients;  // We own the device memory
553dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyHostToDevice));
554dce49693SSebastian Grimberg         } break;
555dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
556dce49693SSebastian Grimberg           switch (copy_mode) {
557dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
558dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8)));
559dce49693SSebastian Grimberg               impl->d_curl_orients_allocated = impl->d_curl_orients;  // We own the device memory
560dce49693SSebastian Grimberg               CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToDevice));
561dce49693SSebastian Grimberg               break;
562dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
563dce49693SSebastian Grimberg               impl->d_curl_orients           = (CeedInt8 *)curl_orients;
564dce49693SSebastian Grimberg               impl->d_curl_orients_allocated = impl->d_curl_orients;
565dce49693SSebastian Grimberg               break;
566dce49693SSebastian Grimberg             case CEED_USE_POINTER:
567dce49693SSebastian Grimberg               impl->d_curl_orients = (CeedInt8 *)curl_orients;
568dce49693SSebastian Grimberg               break;
569dce49693SSebastian Grimberg           }
570dce49693SSebastian Grimberg           CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated));
571dce49693SSebastian Grimberg           CeedCallHip(ceed, hipMemcpy(impl->h_curl_orients_allocated, impl->d_curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToHost));
572dce49693SSebastian Grimberg           impl->h_curl_orients = impl->h_curl_orients_allocated;
573dce49693SSebastian Grimberg         } break;
574dce49693SSebastian Grimberg       }
575dce49693SSebastian Grimberg     }
5760d0321e0SJeremy L Thompson   }
5770d0321e0SJeremy L Thompson 
5780d0321e0SJeremy L Thompson   // Compile HIP kernels
5792b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction.h", &restriction_kernel_path));
58023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
5812b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
58223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
583dce49693SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 8, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
584dce49693SSebastian Grimberg                                   "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride, "RSTR_STRIDE_NODES",
585dce49693SSebastian Grimberg                                   strides[0], "RSTR_STRIDE_COMP", strides[1], "RSTR_STRIDE_ELEM", strides[2]));
586eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->StridedNoTranspose));
587eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->StridedTranspose));
58858549094SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->OffsetNoTranspose));
589dce49693SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedNoTranspose", &impl->OrientedNoTranspose));
590dce49693SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedNoTranspose", &impl->CurlOrientedNoTranspose));
591dce49693SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedNoTranspose", &impl->CurlOrientedUnsignedNoTranspose));
592*7aa91133SSebastian Grimberg   if (!is_deterministic) {
593*7aa91133SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->OffsetTranspose));
594*7aa91133SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedTranspose", &impl->OrientedTranspose));
595dce49693SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedTranspose", &impl->CurlOrientedTranspose));
596dce49693SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedTranspose", &impl->CurlOrientedUnsignedTranspose));
597*7aa91133SSebastian Grimberg   } else {
598*7aa91133SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTransposeDet", &impl->OffsetTransposeDet));
599*7aa91133SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedTransposeDet", &impl->OrientedTransposeDet));
600*7aa91133SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedTransposeDet", &impl->CurlOrientedTransposeDet));
601*7aa91133SSebastian Grimberg     CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedTransposeDet", &impl->CurlOrientedUnsignedTransposeDet));
602*7aa91133SSebastian Grimberg   }
6032b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&restriction_kernel_path));
6042b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&restriction_kernel_source));
6050d0321e0SJeremy L Thompson 
6060d0321e0SJeremy L Thompson   // Register backend functions
607dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Apply", CeedElemRestrictionApply_Hip));
608dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnsigned", CeedElemRestrictionApplyUnsigned_Hip));
609dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApplyUnoriented_Hip));
610dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Hip));
611dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Hip));
612dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Hip));
613dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Hip));
6140d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6150d0321e0SJeremy L Thompson }
6160d0321e0SJeremy L Thompson 
6170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
618