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 num_nodes = impl->num_nodes; 31437930d1SJeremy L Thompson CeedInt num_elem, elem_size; 32437930d1SJeremy L Thompson CeedElemRestrictionGetNumElements(r, &num_elem); 332b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size)); 340d0321e0SJeremy L Thompson hipFunction_t kernel; 350d0321e0SJeremy L Thompson 360d0321e0SJeremy L Thompson // Get vectors 370d0321e0SJeremy L Thompson const CeedScalar *d_u; 380d0321e0SJeremy L Thompson CeedScalar *d_v; 392b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 40437930d1SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 410d0321e0SJeremy L Thompson // Sum into for transpose mode, e-vec to l-vec 422b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 430d0321e0SJeremy L Thompson } else { 440d0321e0SJeremy L Thompson // Overwrite for notranspose mode, l-vec to e-vec 452b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 460d0321e0SJeremy L Thompson } 470d0321e0SJeremy L Thompson 480d0321e0SJeremy L Thompson // Restrict 49437930d1SJeremy L Thompson if (t_mode == CEED_NOTRANSPOSE) { 500d0321e0SJeremy L Thompson // L-vector -> E-vector 510d0321e0SJeremy L Thompson if (impl->d_ind) { 520d0321e0SJeremy L Thompson // -- Offsets provided 53437930d1SJeremy L Thompson kernel = impl->OffsetNoTranspose; 54437930d1SJeremy L Thompson void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v}; 55437930d1SJeremy L Thompson CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256; 5658549094SSebastian Grimberg 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; 6358549094SSebastian Grimberg 64eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args)); 650d0321e0SJeremy L Thompson } 660d0321e0SJeremy L Thompson } else { 670d0321e0SJeremy L Thompson // E-vector -> L-vector 680d0321e0SJeremy L Thompson if (impl->d_ind) { 690d0321e0SJeremy L Thompson // -- Offsets provided 7058549094SSebastian Grimberg CeedInt block_size = 64; 7158549094SSebastian Grimberg if (impl->OffsetTranspose) { 72437930d1SJeremy L Thompson kernel = impl->OffsetTranspose; 7358549094SSebastian Grimberg void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v}; 7458549094SSebastian Grimberg 75eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args)); 760d0321e0SJeremy L Thompson } else { 7758549094SSebastian Grimberg kernel = impl->OffsetTransposeDet; 7858549094SSebastian Grimberg void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v}; 7958549094SSebastian Grimberg 8058549094SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args)); 8158549094SSebastian Grimberg } 8258549094SSebastian Grimberg } else { 830d0321e0SJeremy L Thompson // -- Strided restriction 84437930d1SJeremy L Thompson kernel = impl->StridedTranspose; 85437930d1SJeremy L Thompson void *args[] = {&num_elem, &d_u, &d_v}; 8658549094SSebastian Grimberg CeedInt block_size = 64; 8758549094SSebastian Grimberg 88eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args)); 890d0321e0SJeremy L Thompson } 900d0321e0SJeremy L Thompson } 910d0321e0SJeremy L Thompson 922b730f8bSJeremy L Thompson if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL; 930d0321e0SJeremy L Thompson 940d0321e0SJeremy L Thompson // Restore arrays 952b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 962b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 970d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 980d0321e0SJeremy L Thompson } 990d0321e0SJeremy L Thompson 1000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1010d0321e0SJeremy L Thompson // Get offsets 1020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 103472941f0SJeremy L Thompson static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) { 1040d0321e0SJeremy L Thompson CeedElemRestriction_Hip *impl; 1052b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 1060d0321e0SJeremy L Thompson 107472941f0SJeremy L Thompson switch (mem_type) { 1080d0321e0SJeremy L Thompson case CEED_MEM_HOST: 1090d0321e0SJeremy L Thompson *offsets = impl->h_ind; 1100d0321e0SJeremy L Thompson break; 1110d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 1120d0321e0SJeremy L Thompson *offsets = impl->d_ind; 1130d0321e0SJeremy L Thompson break; 1140d0321e0SJeremy L Thompson } 1150d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1160d0321e0SJeremy L Thompson } 1170d0321e0SJeremy L Thompson 1180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1190d0321e0SJeremy L Thompson // Destroy restriction 1200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1210d0321e0SJeremy L Thompson static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction r) { 1220d0321e0SJeremy L Thompson CeedElemRestriction_Hip *impl; 1232b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(r, &impl)); 1240d0321e0SJeremy L Thompson 1250d0321e0SJeremy L Thompson Ceed ceed; 1262b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed)); 1272b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleUnload(impl->module)); 1282b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_ind_allocated)); 1292b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_ind_allocated)); 1302b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_t_offsets)); 1312b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_t_indices)); 1322b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_l_vec_indices)); 1332b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 134437930d1SJeremy L Thompson 1350d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1360d0321e0SJeremy L Thompson } 1370d0321e0SJeremy L Thompson 1380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1390d0321e0SJeremy L Thompson // Create transpose offsets and indices 1400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1412b730f8bSJeremy L Thompson static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction r, const CeedInt *indices) { 1420d0321e0SJeremy L Thompson Ceed ceed; 1432b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed)); 1440d0321e0SJeremy L Thompson CeedElemRestriction_Hip *impl; 1452b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(r, &impl)); 146e79b91d9SJeremy L Thompson CeedSize l_size; 147e79b91d9SJeremy L Thompson CeedInt num_elem, elem_size, num_comp; 1482b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(r, &num_elem)); 1492b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size)); 1502b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(r, &l_size)); 1512b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(r, &num_comp)); 1520d0321e0SJeremy L Thompson 153437930d1SJeremy L Thompson // Count num_nodes 154437930d1SJeremy L Thompson bool *is_node; 1552b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(l_size, &is_node)); 156437930d1SJeremy L Thompson const CeedInt size_indices = num_elem * elem_size; 1572b730f8bSJeremy L Thompson for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1; 158437930d1SJeremy L Thompson CeedInt num_nodes = 0; 1592b730f8bSJeremy L Thompson for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i]; 160437930d1SJeremy L Thompson impl->num_nodes = num_nodes; 1610d0321e0SJeremy L Thompson 1620d0321e0SJeremy L Thompson // L-vector offsets array 163437930d1SJeremy L Thompson CeedInt *ind_to_offset, *l_vec_indices; 1642b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(l_size, &ind_to_offset)); 1652b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices)); 1660d0321e0SJeremy L Thompson CeedInt j = 0; 1672b730f8bSJeremy L Thompson for (CeedInt i = 0; i < l_size; i++) { 168437930d1SJeremy L Thompson if (is_node[i]) { 169437930d1SJeremy L Thompson l_vec_indices[j] = i; 1700d0321e0SJeremy L Thompson ind_to_offset[i] = j++; 1710d0321e0SJeremy L Thompson } 1722b730f8bSJeremy L Thompson } 1732b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&is_node)); 1740d0321e0SJeremy L Thompson 1750d0321e0SJeremy L Thompson // Compute transpose offsets and indices 176437930d1SJeremy L Thompson const CeedInt size_offsets = num_nodes + 1; 177437930d1SJeremy L Thompson CeedInt *t_offsets; 1782b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(size_offsets, &t_offsets)); 179437930d1SJeremy L Thompson CeedInt *t_indices; 1802b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(size_indices, &t_indices)); 1810d0321e0SJeremy L Thompson // Count node multiplicity 1822b730f8bSJeremy L Thompson for (CeedInt e = 0; e < num_elem; ++e) { 1832b730f8bSJeremy L Thompson for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1]; 1842b730f8bSJeremy L Thompson } 1850d0321e0SJeremy L Thompson // Convert to running sum 1862b730f8bSJeremy L Thompson for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1]; 1870d0321e0SJeremy L Thompson // List all E-vec indices associated with L-vec node 188437930d1SJeremy L Thompson for (CeedInt e = 0; e < num_elem; ++e) { 189437930d1SJeremy L Thompson for (CeedInt i = 0; i < elem_size; ++i) { 190437930d1SJeremy L Thompson const CeedInt lid = elem_size * e + i; 1910d0321e0SJeremy L Thompson const CeedInt gid = indices[lid]; 192437930d1SJeremy L Thompson t_indices[t_offsets[ind_to_offset[gid]]++] = lid; 1930d0321e0SJeremy L Thompson } 1940d0321e0SJeremy L Thompson } 1950d0321e0SJeremy L Thompson // Reset running sum 1962b730f8bSJeremy L Thompson for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1]; 197437930d1SJeremy L Thompson t_offsets[0] = 0; 1980d0321e0SJeremy L Thompson 1990d0321e0SJeremy L Thompson // Copy data to device 2000d0321e0SJeremy L Thompson // -- L-vector indices 2012b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt))); 2022b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice)); 2030d0321e0SJeremy L Thompson // -- Transpose offsets 2042b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt))); 2052b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice)); 2060d0321e0SJeremy L Thompson // -- Transpose indices 2072b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt))); 2082b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice)); 2090d0321e0SJeremy L Thompson 2100d0321e0SJeremy L Thompson // Cleanup 2112b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&ind_to_offset)); 2122b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&l_vec_indices)); 2132b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&t_offsets)); 2142b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&t_indices)); 215437930d1SJeremy L Thompson 2160d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2170d0321e0SJeremy L Thompson } 2180d0321e0SJeremy L Thompson 2190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2200d0321e0SJeremy L Thompson // Create restriction 2210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 222fcbe8c06SSebastian Grimberg int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *indices, const bool *orients, 2230c73c039SSebastian Grimberg const CeedInt8 *curl_orients, CeedElemRestriction r) { 2240d0321e0SJeremy L Thompson Ceed ceed; 2252b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed)); 2260d0321e0SJeremy L Thompson CeedElemRestriction_Hip *impl; 2272b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 228*ca735530SJeremy L Thompson Ceed ceed_parent; 229*ca735530SJeremy L Thompson CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); 23058549094SSebastian Grimberg bool is_deterministic; 231*ca735530SJeremy L Thompson CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic)); 232437930d1SJeremy L Thompson CeedInt num_elem, num_comp, elem_size; 2332b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(r, &num_elem)); 2342b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(r, &num_comp)); 2352b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size)); 236437930d1SJeremy L Thompson CeedInt size = num_elem * elem_size; 237437930d1SJeremy L Thompson CeedInt strides[3] = {1, size, elem_size}; 238437930d1SJeremy L Thompson CeedInt comp_stride = 1; 2390d0321e0SJeremy L Thompson 24000125730SSebastian Grimberg CeedRestrictionType rstr_type; 24100125730SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(r, &rstr_type)); 24200125730SSebastian Grimberg CeedCheck(rstr_type != CEED_RESTRICTION_ORIENTED && rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_BACKEND, 24300125730SSebastian Grimberg "Backend does not implement CeedElemRestrictionCreateOriented or CeedElemRestrictionCreateCurlOriented"); 24400125730SSebastian Grimberg 2450d0321e0SJeremy L Thompson // Stride data 246437930d1SJeremy L Thompson bool is_strided; 2472b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(r, &is_strided)); 248437930d1SJeremy L Thompson if (is_strided) { 249437930d1SJeremy L Thompson bool has_backend_strides; 2502b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(r, &has_backend_strides)); 251437930d1SJeremy L Thompson if (!has_backend_strides) { 2522b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(r, &strides)); 2530d0321e0SJeremy L Thompson } 2540d0321e0SJeremy L Thompson } else { 2552b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(r, &comp_stride)); 2560d0321e0SJeremy L Thompson } 2570d0321e0SJeremy L Thompson 2580d0321e0SJeremy L Thompson impl->h_ind = NULL; 2590d0321e0SJeremy L Thompson impl->h_ind_allocated = NULL; 2600d0321e0SJeremy L Thompson impl->d_ind = NULL; 2610d0321e0SJeremy L Thompson impl->d_ind_allocated = NULL; 262437930d1SJeremy L Thompson impl->d_t_indices = NULL; 263437930d1SJeremy L Thompson impl->d_t_offsets = NULL; 264437930d1SJeremy L Thompson impl->num_nodes = size; 2652b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionSetData(r, impl)); 266437930d1SJeremy L Thompson CeedInt layout[3] = {1, elem_size * num_elem, elem_size}; 2672b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionSetELayout(r, layout)); 2680d0321e0SJeremy L Thompson 2690d0321e0SJeremy L Thompson // Set up device indices/offset arrays 270472941f0SJeremy L Thompson switch (mem_type) { 2716574a04fSJeremy L Thompson case CEED_MEM_HOST: { 272472941f0SJeremy L Thompson switch (copy_mode) { 2730d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 2740d0321e0SJeremy L Thompson impl->h_ind_allocated = (CeedInt *)indices; 2750d0321e0SJeremy L Thompson impl->h_ind = (CeedInt *)indices; 2760d0321e0SJeremy L Thompson break; 2770d0321e0SJeremy L Thompson case CEED_USE_POINTER: 2780d0321e0SJeremy L Thompson impl->h_ind = (CeedInt *)indices; 2790d0321e0SJeremy L Thompson break; 2800d0321e0SJeremy L Thompson case CEED_COPY_VALUES: 28144d7a66cSJeremy L Thompson if (indices != NULL) { 2822b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(elem_size * num_elem, &impl->h_ind_allocated)); 28344d7a66cSJeremy L Thompson memcpy(impl->h_ind_allocated, indices, elem_size * num_elem * sizeof(CeedInt)); 28444d7a66cSJeremy L Thompson impl->h_ind = impl->h_ind_allocated; 28544d7a66cSJeremy L Thompson } 2860d0321e0SJeremy L Thompson break; 2870d0321e0SJeremy L Thompson } 2880d0321e0SJeremy L Thompson if (indices != NULL) { 2892b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt))); 2900d0321e0SJeremy L Thompson impl->d_ind_allocated = impl->d_ind; // We own the device memory 2912b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyHostToDevice)); 29258549094SSebastian Grimberg if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(r, indices)); 2930d0321e0SJeremy L Thompson } 2946574a04fSJeremy L Thompson break; 2956574a04fSJeremy L Thompson } 2966574a04fSJeremy L Thompson case CEED_MEM_DEVICE: { 297472941f0SJeremy L Thompson switch (copy_mode) { 2980d0321e0SJeremy L Thompson case CEED_COPY_VALUES: 2990d0321e0SJeremy L Thompson if (indices != NULL) { 3002b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt))); 3010d0321e0SJeremy L Thompson impl->d_ind_allocated = impl->d_ind; // We own the device memory 3022b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyDeviceToDevice)); 3030d0321e0SJeremy L Thompson } 3040d0321e0SJeremy L Thompson break; 3050d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 3060d0321e0SJeremy L Thompson impl->d_ind = (CeedInt *)indices; 3070d0321e0SJeremy L Thompson impl->d_ind_allocated = impl->d_ind; 3080d0321e0SJeremy L Thompson break; 3090d0321e0SJeremy L Thompson case CEED_USE_POINTER: 3100d0321e0SJeremy L Thompson impl->d_ind = (CeedInt *)indices; 3110d0321e0SJeremy L Thompson } 3120d0321e0SJeremy L Thompson if (indices != NULL) { 3132b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(elem_size * num_elem, &impl->h_ind_allocated)); 3142b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->h_ind_allocated, impl->d_ind, elem_size * num_elem * sizeof(CeedInt), hipMemcpyDeviceToHost)); 31544d7a66cSJeremy L Thompson impl->h_ind = impl->h_ind_allocated; 31658549094SSebastian Grimberg if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(r, indices)); 3170d0321e0SJeremy L Thompson } 3186574a04fSJeremy L Thompson break; 3196574a04fSJeremy L Thompson } 3200d0321e0SJeremy L Thompson // LCOV_EXCL_START 3216574a04fSJeremy L Thompson default: 3222b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Only MemType = HOST or DEVICE supported"); 3230d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 3240d0321e0SJeremy L Thompson } 3250d0321e0SJeremy L Thompson 3260d0321e0SJeremy L Thompson // Compile HIP kernels 327437930d1SJeremy L Thompson CeedInt num_nodes = impl->num_nodes; 328437930d1SJeremy L Thompson char *restriction_kernel_path, *restriction_kernel_source; 3292b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction.h", &restriction_kernel_path)); 33023d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n"); 3312b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source)); 33223d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n"); 333eb7e6cafSJeremy L Thompson CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 8, "RESTR_ELEM_SIZE", elem_size, "RESTR_NUM_ELEM", num_elem, 3342b730f8bSJeremy L Thompson "RESTR_NUM_COMP", num_comp, "RESTR_NUM_NODES", num_nodes, "RESTR_COMP_STRIDE", comp_stride, "RESTR_STRIDE_NODES", 3352b730f8bSJeremy L Thompson strides[0], "RESTR_STRIDE_COMP", strides[1], "RESTR_STRIDE_ELEM", strides[2])); 336eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->StridedNoTranspose)); 337eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->StridedTranspose)); 33858549094SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->OffsetNoTranspose)); 33958549094SSebastian Grimberg if (!is_deterministic) { 340eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->OffsetTranspose)); 34158549094SSebastian Grimberg } else { 34258549094SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTransposeDet", &impl->OffsetTransposeDet)); 34358549094SSebastian Grimberg } 3442b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&restriction_kernel_path)); 3452b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&restriction_kernel_source)); 3460d0321e0SJeremy L Thompson 3470d0321e0SJeremy L Thompson // Register backend functions 3482b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "Apply", CeedElemRestrictionApply_Hip)); 349b17517eeSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "ApplyUnsigned", CeedElemRestrictionApply_Hip)); 3507c1dbaffSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "ApplyUnoriented", CeedElemRestrictionApply_Hip)); 3512b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "GetOffsets", CeedElemRestrictionGetOffsets_Hip)); 3522b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "Destroy", CeedElemRestrictionDestroy_Hip)); 3530d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3540d0321e0SJeremy L Thompson } 3550d0321e0SJeremy L Thompson 3560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 357