xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-restriction.c (revision 7c1dbaff56f8afaddfc574209b09713e25514e81)
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 //------------------------------------------------------------------------------
210d0321e0SJeremy L Thompson // Apply restriction
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
232b730f8bSJeremy L Thompson static int CeedElemRestrictionApply_Hip(CeedElemRestriction r, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
240d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
252b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(r, &impl));
260d0321e0SJeremy L Thompson   Ceed ceed;
272b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
280d0321e0SJeremy L Thompson   Ceed_Hip *data;
292b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
30437930d1SJeremy L Thompson   const CeedInt block_size = 64;
31437930d1SJeremy L Thompson   const CeedInt num_nodes  = impl->num_nodes;
32437930d1SJeremy L Thompson   CeedInt       num_elem, elem_size;
33437930d1SJeremy L Thompson   CeedElemRestrictionGetNumElements(r, &num_elem);
342b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size));
350d0321e0SJeremy L Thompson   hipFunction_t kernel;
360d0321e0SJeremy L Thompson 
370d0321e0SJeremy L Thompson   // Get vectors
380d0321e0SJeremy L Thompson   const CeedScalar *d_u;
390d0321e0SJeremy L Thompson   CeedScalar       *d_v;
402b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
41437930d1SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
420d0321e0SJeremy L Thompson     // Sum into for transpose mode, e-vec to l-vec
432b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
440d0321e0SJeremy L Thompson   } else {
450d0321e0SJeremy L Thompson     // Overwrite for notranspose mode, l-vec to e-vec
462b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
470d0321e0SJeremy L Thompson   }
480d0321e0SJeremy L Thompson 
490d0321e0SJeremy L Thompson   // Restrict
50437930d1SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
510d0321e0SJeremy L Thompson     // L-vector -> E-vector
520d0321e0SJeremy L Thompson     if (impl->d_ind) {
530d0321e0SJeremy L Thompson       // -- Offsets provided
54437930d1SJeremy L Thompson       kernel             = impl->OffsetNoTranspose;
55437930d1SJeremy L Thompson       void   *args[]     = {&num_elem, &impl->d_ind, &d_u, &d_v};
56437930d1SJeremy L Thompson       CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
57eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
580d0321e0SJeremy L Thompson     } else {
590d0321e0SJeremy L Thompson       // -- Strided restriction
60437930d1SJeremy L Thompson       kernel             = impl->StridedNoTranspose;
61437930d1SJeremy L Thompson       void   *args[]     = {&num_elem, &d_u, &d_v};
62437930d1SJeremy L Thompson       CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
63eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
640d0321e0SJeremy L Thompson     }
650d0321e0SJeremy L Thompson   } else {
660d0321e0SJeremy L Thompson     // E-vector -> L-vector
670d0321e0SJeremy L Thompson     if (impl->d_ind) {
680d0321e0SJeremy L Thompson       // -- Offsets provided
69437930d1SJeremy L Thompson       kernel       = impl->OffsetTranspose;
702b730f8bSJeremy L Thompson       void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
71eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
720d0321e0SJeremy L Thompson     } else {
730d0321e0SJeremy L Thompson       // -- Strided restriction
74437930d1SJeremy L Thompson       kernel       = impl->StridedTranspose;
75437930d1SJeremy L Thompson       void *args[] = {&num_elem, &d_u, &d_v};
76eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
770d0321e0SJeremy L Thompson     }
780d0321e0SJeremy L Thompson   }
790d0321e0SJeremy L Thompson 
802b730f8bSJeremy L Thompson   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
810d0321e0SJeremy L Thompson 
820d0321e0SJeremy L Thompson   // Restore arrays
832b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
842b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
850d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
860d0321e0SJeremy L Thompson }
870d0321e0SJeremy L Thompson 
880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
890d0321e0SJeremy L Thompson // Get offsets
900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
91472941f0SJeremy L Thompson static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
920d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
932b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
940d0321e0SJeremy L Thompson 
95472941f0SJeremy L Thompson   switch (mem_type) {
960d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
970d0321e0SJeremy L Thompson       *offsets = impl->h_ind;
980d0321e0SJeremy L Thompson       break;
990d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1000d0321e0SJeremy L Thompson       *offsets = impl->d_ind;
1010d0321e0SJeremy L Thompson       break;
1020d0321e0SJeremy L Thompson   }
1030d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1040d0321e0SJeremy L Thompson }
1050d0321e0SJeremy L Thompson 
1060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1070d0321e0SJeremy L Thompson // Destroy restriction
1080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1090d0321e0SJeremy L Thompson static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction r) {
1100d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
1112b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(r, &impl));
1120d0321e0SJeremy L Thompson 
1130d0321e0SJeremy L Thompson   Ceed ceed;
1142b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
1152b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(impl->module));
1162b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_ind_allocated));
1172b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_ind_allocated));
1182b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_offsets));
1192b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_t_indices));
1202b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_l_vec_indices));
1212b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
122437930d1SJeremy L Thompson 
1230d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1240d0321e0SJeremy L Thompson }
1250d0321e0SJeremy L Thompson 
1260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1270d0321e0SJeremy L Thompson // Create transpose offsets and indices
1280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1292b730f8bSJeremy L Thompson static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction r, const CeedInt *indices) {
1300d0321e0SJeremy L Thompson   Ceed ceed;
1312b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
1320d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
1332b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(r, &impl));
134e79b91d9SJeremy L Thompson   CeedSize l_size;
135e79b91d9SJeremy L Thompson   CeedInt  num_elem, elem_size, num_comp;
1362b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(r, &num_elem));
1372b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size));
1382b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetLVectorSize(r, &l_size));
1392b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(r, &num_comp));
1400d0321e0SJeremy L Thompson 
141437930d1SJeremy L Thompson   // Count num_nodes
142437930d1SJeremy L Thompson   bool *is_node;
1432b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &is_node));
144437930d1SJeremy L Thompson   const CeedInt size_indices = num_elem * elem_size;
1452b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1;
146437930d1SJeremy L Thompson   CeedInt num_nodes = 0;
1472b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i];
148437930d1SJeremy L Thompson   impl->num_nodes = num_nodes;
1490d0321e0SJeremy L Thompson 
1500d0321e0SJeremy L Thompson   // L-vector offsets array
151437930d1SJeremy L Thompson   CeedInt *ind_to_offset, *l_vec_indices;
1522b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(l_size, &ind_to_offset));
1532b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices));
1540d0321e0SJeremy L Thompson   CeedInt j = 0;
1552b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < l_size; i++) {
156437930d1SJeremy L Thompson     if (is_node[i]) {
157437930d1SJeremy L Thompson       l_vec_indices[j] = i;
1580d0321e0SJeremy L Thompson       ind_to_offset[i] = j++;
1590d0321e0SJeremy L Thompson     }
1602b730f8bSJeremy L Thompson   }
1612b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&is_node));
1620d0321e0SJeremy L Thompson 
1630d0321e0SJeremy L Thompson   // Compute transpose offsets and indices
164437930d1SJeremy L Thompson   const CeedInt size_offsets = num_nodes + 1;
165437930d1SJeremy L Thompson   CeedInt      *t_offsets;
1662b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(size_offsets, &t_offsets));
167437930d1SJeremy L Thompson   CeedInt *t_indices;
1682b730f8bSJeremy L Thompson   CeedCallBackend(CeedMalloc(size_indices, &t_indices));
1690d0321e0SJeremy L Thompson   // Count node multiplicity
1702b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
1712b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1];
1722b730f8bSJeremy L Thompson   }
1730d0321e0SJeremy L Thompson   // Convert to running sum
1742b730f8bSJeremy L Thompson   for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1];
1750d0321e0SJeremy L Thompson   // List all E-vec indices associated with L-vec node
176437930d1SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; ++e) {
177437930d1SJeremy L Thompson     for (CeedInt i = 0; i < elem_size; ++i) {
178437930d1SJeremy L Thompson       const CeedInt lid                          = elem_size * e + i;
1790d0321e0SJeremy L Thompson       const CeedInt gid                          = indices[lid];
180437930d1SJeremy L Thompson       t_indices[t_offsets[ind_to_offset[gid]]++] = lid;
1810d0321e0SJeremy L Thompson     }
1820d0321e0SJeremy L Thompson   }
1830d0321e0SJeremy L Thompson   // Reset running sum
1842b730f8bSJeremy L Thompson   for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1];
185437930d1SJeremy L Thompson   t_offsets[0] = 0;
1860d0321e0SJeremy L Thompson 
1870d0321e0SJeremy L Thompson   // Copy data to device
1880d0321e0SJeremy L Thompson   // -- L-vector indices
1892b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt)));
1902b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice));
1910d0321e0SJeremy L Thompson   // -- Transpose offsets
1922b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt)));
1932b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice));
1940d0321e0SJeremy L Thompson   // -- Transpose indices
1952b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt)));
1962b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice));
1970d0321e0SJeremy L Thompson 
1980d0321e0SJeremy L Thompson   // Cleanup
1992b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&ind_to_offset));
2002b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&l_vec_indices));
2012b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_offsets));
2022b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&t_indices));
203437930d1SJeremy L Thompson 
2040d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2050d0321e0SJeremy L Thompson }
2060d0321e0SJeremy L Thompson 
2070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2080d0321e0SJeremy L Thompson // Create restriction
2090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
210fcbe8c06SSebastian Grimberg int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *indices, const bool *orients,
2110c73c039SSebastian Grimberg                                   const CeedInt8 *curl_orients, CeedElemRestriction r) {
2120d0321e0SJeremy L Thompson   Ceed ceed;
2132b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
2140d0321e0SJeremy L Thompson   CeedElemRestriction_Hip *impl;
2152b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
216437930d1SJeremy L Thompson   CeedInt num_elem, num_comp, elem_size;
2172b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(r, &num_elem));
2182b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(r, &num_comp));
2192b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size));
220437930d1SJeremy L Thompson   CeedInt size        = num_elem * elem_size;
221437930d1SJeremy L Thompson   CeedInt strides[3]  = {1, size, elem_size};
222437930d1SJeremy L Thompson   CeedInt comp_stride = 1;
2230d0321e0SJeremy L Thompson 
2240d0321e0SJeremy L Thompson   // Stride data
225437930d1SJeremy L Thompson   bool is_strided;
2262b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionIsStrided(r, &is_strided));
227437930d1SJeremy L Thompson   if (is_strided) {
228437930d1SJeremy L Thompson     bool has_backend_strides;
2292b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionHasBackendStrides(r, &has_backend_strides));
230437930d1SJeremy L Thompson     if (!has_backend_strides) {
2312b730f8bSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetStrides(r, &strides));
2320d0321e0SJeremy L Thompson     }
2330d0321e0SJeremy L Thompson   } else {
2342b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(r, &comp_stride));
2350d0321e0SJeremy L Thompson   }
2360d0321e0SJeremy L Thompson 
2370d0321e0SJeremy L Thompson   impl->h_ind           = NULL;
2380d0321e0SJeremy L Thompson   impl->h_ind_allocated = NULL;
2390d0321e0SJeremy L Thompson   impl->d_ind           = NULL;
2400d0321e0SJeremy L Thompson   impl->d_ind_allocated = NULL;
241437930d1SJeremy L Thompson   impl->d_t_indices     = NULL;
242437930d1SJeremy L Thompson   impl->d_t_offsets     = NULL;
243437930d1SJeremy L Thompson   impl->num_nodes       = size;
2442b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionSetData(r, impl));
245437930d1SJeremy L Thompson   CeedInt layout[3] = {1, elem_size * num_elem, elem_size};
2462b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionSetELayout(r, layout));
2470d0321e0SJeremy L Thompson 
2480d0321e0SJeremy L Thompson   // Set up device indices/offset arrays
249472941f0SJeremy L Thompson   switch (mem_type) {
2506574a04fSJeremy L Thompson     case CEED_MEM_HOST: {
251472941f0SJeremy L Thompson       switch (copy_mode) {
2520d0321e0SJeremy L Thompson         case CEED_OWN_POINTER:
2530d0321e0SJeremy L Thompson           impl->h_ind_allocated = (CeedInt *)indices;
2540d0321e0SJeremy L Thompson           impl->h_ind           = (CeedInt *)indices;
2550d0321e0SJeremy L Thompson           break;
2560d0321e0SJeremy L Thompson         case CEED_USE_POINTER:
2570d0321e0SJeremy L Thompson           impl->h_ind = (CeedInt *)indices;
2580d0321e0SJeremy L Thompson           break;
2590d0321e0SJeremy L Thompson         case CEED_COPY_VALUES:
26044d7a66cSJeremy L Thompson           if (indices != NULL) {
2612b730f8bSJeremy L Thompson             CeedCallBackend(CeedMalloc(elem_size * num_elem, &impl->h_ind_allocated));
26244d7a66cSJeremy L Thompson             memcpy(impl->h_ind_allocated, indices, elem_size * num_elem * sizeof(CeedInt));
26344d7a66cSJeremy L Thompson             impl->h_ind = impl->h_ind_allocated;
26444d7a66cSJeremy L Thompson           }
2650d0321e0SJeremy L Thompson           break;
2660d0321e0SJeremy L Thompson       }
2670d0321e0SJeremy L Thompson       if (indices != NULL) {
2682b730f8bSJeremy L Thompson         CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
2690d0321e0SJeremy L Thompson         impl->d_ind_allocated = impl->d_ind;  // We own the device memory
2702b730f8bSJeremy L Thompson         CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyHostToDevice));
2712b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionOffset_Hip(r, indices));
2720d0321e0SJeremy L Thompson       }
2736574a04fSJeremy L Thompson       break;
2746574a04fSJeremy L Thompson     }
2756574a04fSJeremy L Thompson     case CEED_MEM_DEVICE: {
276472941f0SJeremy L Thompson       switch (copy_mode) {
2770d0321e0SJeremy L Thompson         case CEED_COPY_VALUES:
2780d0321e0SJeremy L Thompson           if (indices != NULL) {
2792b730f8bSJeremy L Thompson             CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
2800d0321e0SJeremy L Thompson             impl->d_ind_allocated = impl->d_ind;  // We own the device memory
2812b730f8bSJeremy L Thompson             CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyDeviceToDevice));
2820d0321e0SJeremy L Thompson           }
2830d0321e0SJeremy L Thompson           break;
2840d0321e0SJeremy L Thompson         case CEED_OWN_POINTER:
2850d0321e0SJeremy L Thompson           impl->d_ind           = (CeedInt *)indices;
2860d0321e0SJeremy L Thompson           impl->d_ind_allocated = impl->d_ind;
2870d0321e0SJeremy L Thompson           break;
2880d0321e0SJeremy L Thompson         case CEED_USE_POINTER:
2890d0321e0SJeremy L Thompson           impl->d_ind = (CeedInt *)indices;
2900d0321e0SJeremy L Thompson       }
2910d0321e0SJeremy L Thompson       if (indices != NULL) {
2922b730f8bSJeremy L Thompson         CeedCallBackend(CeedMalloc(elem_size * num_elem, &impl->h_ind_allocated));
2932b730f8bSJeremy L Thompson         CeedCallHip(ceed, hipMemcpy(impl->h_ind_allocated, impl->d_ind, elem_size * num_elem * sizeof(CeedInt), hipMemcpyDeviceToHost));
29444d7a66cSJeremy L Thompson         impl->h_ind = impl->h_ind_allocated;
2952b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionOffset_Hip(r, indices));
2960d0321e0SJeremy L Thompson       }
2976574a04fSJeremy L Thompson       break;
2986574a04fSJeremy L Thompson     }
2990d0321e0SJeremy L Thompson     // LCOV_EXCL_START
3006574a04fSJeremy L Thompson     default:
3012b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "Only MemType = HOST or DEVICE supported");
3020d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
3030d0321e0SJeremy L Thompson   }
3040d0321e0SJeremy L Thompson 
3050d0321e0SJeremy L Thompson   // Compile HIP kernels
306437930d1SJeremy L Thompson   CeedInt num_nodes = impl->num_nodes;
307437930d1SJeremy L Thompson   char   *restriction_kernel_path, *restriction_kernel_source;
3082b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction.h", &restriction_kernel_path));
30923d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
3102b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
31123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
312eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 8, "RESTR_ELEM_SIZE", elem_size, "RESTR_NUM_ELEM", num_elem,
3132b730f8bSJeremy L Thompson                                   "RESTR_NUM_COMP", num_comp, "RESTR_NUM_NODES", num_nodes, "RESTR_COMP_STRIDE", comp_stride, "RESTR_STRIDE_NODES",
3142b730f8bSJeremy L Thompson                                   strides[0], "RESTR_STRIDE_COMP", strides[1], "RESTR_STRIDE_ELEM", strides[2]));
315eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->StridedNoTranspose));
316eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->OffsetNoTranspose));
317eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->StridedTranspose));
318eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->OffsetTranspose));
3192b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&restriction_kernel_path));
3202b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&restriction_kernel_source));
3210d0321e0SJeremy L Thompson 
3220d0321e0SJeremy L Thompson   // Register backend functions
3232b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "Apply", CeedElemRestrictionApply_Hip));
324b17517eeSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "ApplyUnsigned", CeedElemRestrictionApply_Hip));
325*7c1dbaffSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "ApplyUnoriented", CeedElemRestrictionApply_Hip));
3262b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "GetOffsets", CeedElemRestrictionGetOffsets_Hip));
3272b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "Destroy", CeedElemRestrictionDestroy_Hip));
3280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3290d0321e0SJeremy L Thompson }
3300d0321e0SJeremy L Thompson 
3310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
332