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 //------------------------------------------------------------------------------ 21*cf8cbdd6SSebastian Grimberg // Compile restriction kernels 22*cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------ 23*cf8cbdd6SSebastian Grimberg static inline int CeedElemRestrictionSetupCompile_Hip(CeedElemRestriction rstr) { 24*cf8cbdd6SSebastian Grimberg Ceed ceed; 25*cf8cbdd6SSebastian Grimberg bool is_deterministic; 26*cf8cbdd6SSebastian Grimberg CeedInt num_elem, num_comp, elem_size, comp_stride; 27*cf8cbdd6SSebastian Grimberg CeedRestrictionType rstr_type; 28*cf8cbdd6SSebastian Grimberg char *restriction_kernel_path, *restriction_kernel_source; 29*cf8cbdd6SSebastian Grimberg CeedElemRestriction_Hip *impl; 30*cf8cbdd6SSebastian Grimberg 31*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 32*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); 33*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 34*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp)); 35*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 36*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride)); 37*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type)); 38*cf8cbdd6SSebastian Grimberg is_deterministic = impl->d_l_vec_indices != NULL; 39*cf8cbdd6SSebastian Grimberg 40*cf8cbdd6SSebastian Grimberg // Compile HIP kernels 41*cf8cbdd6SSebastian Grimberg switch (rstr_type) { 42*cf8cbdd6SSebastian Grimberg case CEED_RESTRICTION_STRIDED: { 43*cf8cbdd6SSebastian Grimberg CeedInt strides[3] = {1, num_elem * elem_size, elem_size}; 44*cf8cbdd6SSebastian Grimberg bool has_backend_strides; 45*cf8cbdd6SSebastian Grimberg 46*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides)); 47*cf8cbdd6SSebastian Grimberg if (!has_backend_strides) { 48*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetStrides(rstr, &strides)); 49*cf8cbdd6SSebastian Grimberg } 50*cf8cbdd6SSebastian Grimberg 51*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-strided.h", &restriction_kernel_path)); 52*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n"); 53*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source)); 54*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n"); 55*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem, 56*cf8cbdd6SSebastian Grimberg "RSTR_NUM_COMP", num_comp, "RSTR_STRIDE_NODES", strides[0], "RSTR_STRIDE_COMP", strides[1], "RSTR_STRIDE_ELEM", 57*cf8cbdd6SSebastian Grimberg strides[2])); 58*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->ApplyNoTranspose)); 59*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->ApplyTranspose)); 60*cf8cbdd6SSebastian Grimberg } break; 61*cf8cbdd6SSebastian Grimberg case CEED_RESTRICTION_STANDARD: { 62*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &restriction_kernel_path)); 63*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n"); 64*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source)); 65*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n"); 66*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem, 67*cf8cbdd6SSebastian Grimberg "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride, 68*cf8cbdd6SSebastian Grimberg "USE_DETERMINISTIC", is_deterministic ? 1 : 0)); 69*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyNoTranspose)); 70*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyTranspose)); 71*cf8cbdd6SSebastian Grimberg } break; 72*cf8cbdd6SSebastian Grimberg case CEED_RESTRICTION_ORIENTED: { 73*cf8cbdd6SSebastian Grimberg char *offset_kernel_path; 74*cf8cbdd6SSebastian Grimberg 75*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-oriented.h", &restriction_kernel_path)); 76*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n"); 77*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source)); 78*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path)); 79*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source)); 80*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n"); 81*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem, 82*cf8cbdd6SSebastian Grimberg "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride, 83*cf8cbdd6SSebastian Grimberg "USE_DETERMINISTIC", is_deterministic ? 1 : 0)); 84*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedNoTranspose", &impl->ApplyNoTranspose)); 85*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnsignedNoTranspose)); 86*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OrientedTranspose", &impl->ApplyTranspose)); 87*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnsignedTranspose)); 88*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedFree(&offset_kernel_path)); 89*cf8cbdd6SSebastian Grimberg } break; 90*cf8cbdd6SSebastian Grimberg case CEED_RESTRICTION_CURL_ORIENTED: { 91*cf8cbdd6SSebastian Grimberg char *offset_kernel_path; 92*cf8cbdd6SSebastian Grimberg 93*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-curl-oriented.h", &restriction_kernel_path)); 94*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n"); 95*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source)); 96*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction-offset.h", &offset_kernel_path)); 97*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source)); 98*cf8cbdd6SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n"); 99*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem, 100*cf8cbdd6SSebastian Grimberg "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride, 101*cf8cbdd6SSebastian Grimberg "USE_DETERMINISTIC", is_deterministic ? 1 : 0)); 102*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedNoTranspose", &impl->ApplyNoTranspose)); 103*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedNoTranspose", &impl->ApplyUnsignedNoTranspose)); 104*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnorientedNoTranspose)); 105*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedTranspose", &impl->ApplyTranspose)); 106*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "CurlOrientedUnsignedTranspose", &impl->ApplyUnsignedTranspose)); 107*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnorientedTranspose)); 108*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedFree(&offset_kernel_path)); 109*cf8cbdd6SSebastian Grimberg } break; 110*cf8cbdd6SSebastian Grimberg case CEED_RESTRICTION_POINTS: { 111*cf8cbdd6SSebastian Grimberg // LCOV_EXCL_START 112*cf8cbdd6SSebastian Grimberg return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints"); 113*cf8cbdd6SSebastian Grimberg // LCOV_EXCL_STOP 114*cf8cbdd6SSebastian Grimberg } break; 115*cf8cbdd6SSebastian Grimberg } 116*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedFree(&restriction_kernel_path)); 117*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedFree(&restriction_kernel_source)); 118*cf8cbdd6SSebastian Grimberg return CEED_ERROR_SUCCESS; 119*cf8cbdd6SSebastian Grimberg } 120*cf8cbdd6SSebastian Grimberg 121*cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------ 122dce49693SSebastian Grimberg // Core apply restriction code 1230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 124dce49693SSebastian Grimberg static inline int CeedElemRestrictionApply_Hip_Core(CeedElemRestriction rstr, CeedTransposeMode t_mode, bool use_signs, bool use_orients, 125dce49693SSebastian Grimberg CeedVector u, CeedVector v, CeedRequest *request) { 1260d0321e0SJeremy L Thompson Ceed ceed; 127dce49693SSebastian Grimberg CeedRestrictionType rstr_type; 1280d0321e0SJeremy L Thompson const CeedScalar *d_u; 1290d0321e0SJeremy L Thompson CeedScalar *d_v; 130b7453713SJeremy L Thompson CeedElemRestriction_Hip *impl; 131b7453713SJeremy L Thompson 132dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 133dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); 134dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type)); 135*cf8cbdd6SSebastian Grimberg 136*cf8cbdd6SSebastian Grimberg // Assemble kernel if needed 137*cf8cbdd6SSebastian Grimberg if (!impl->module) { 138*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionSetupCompile_Hip(rstr)); 139*cf8cbdd6SSebastian Grimberg } 140b7453713SJeremy L Thompson 141b7453713SJeremy L Thompson // Get vectors 1422b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 143437930d1SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 1440d0321e0SJeremy L Thompson // Sum into for transpose mode, e-vec to l-vec 1452b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 1460d0321e0SJeremy L Thompson } else { 1470d0321e0SJeremy L Thompson // Overwrite for notranspose mode, l-vec to e-vec 1482b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 1490d0321e0SJeremy L Thompson } 1500d0321e0SJeremy L Thompson 1510d0321e0SJeremy L Thompson // Restrict 152437930d1SJeremy L Thompson if (t_mode == CEED_NOTRANSPOSE) { 1530d0321e0SJeremy L Thompson // L-vector -> E-vector 154*cf8cbdd6SSebastian Grimberg CeedInt elem_size; 155*cf8cbdd6SSebastian Grimberg 156*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 157dce49693SSebastian Grimberg const CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256; 158*cf8cbdd6SSebastian Grimberg const CeedInt grid = CeedDivUpInt(impl->num_nodes, block_size); 15958549094SSebastian Grimberg 160dce49693SSebastian Grimberg switch (rstr_type) { 161dce49693SSebastian Grimberg case CEED_RESTRICTION_STRIDED: { 162*cf8cbdd6SSebastian Grimberg void *args[] = {&d_u, &d_v}; 16358549094SSebastian Grimberg 164*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args)); 165dce49693SSebastian Grimberg } break; 166dce49693SSebastian Grimberg case CEED_RESTRICTION_STANDARD: { 167*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &d_u, &d_v}; 168dce49693SSebastian Grimberg 169*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args)); 170dce49693SSebastian Grimberg } break; 171dce49693SSebastian Grimberg case CEED_RESTRICTION_ORIENTED: { 172dce49693SSebastian Grimberg if (use_signs) { 173*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &impl->d_orients, &d_u, &d_v}; 174dce49693SSebastian Grimberg 175*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args)); 176dce49693SSebastian Grimberg } else { 177*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &d_u, &d_v}; 178dce49693SSebastian Grimberg 179*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args)); 180dce49693SSebastian Grimberg } 181dce49693SSebastian Grimberg } break; 182dce49693SSebastian Grimberg case CEED_RESTRICTION_CURL_ORIENTED: { 183dce49693SSebastian Grimberg if (use_signs && use_orients) { 184*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v}; 185dce49693SSebastian Grimberg 186*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyNoTranspose, grid, block_size, args)); 187dce49693SSebastian Grimberg } else if (use_orients) { 188*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v}; 189dce49693SSebastian Grimberg 190*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args)); 191dce49693SSebastian Grimberg } else { 192*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &d_u, &d_v}; 193dce49693SSebastian Grimberg 194*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedNoTranspose, grid, block_size, args)); 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 } else { 2040d0321e0SJeremy L Thompson // E-vector -> L-vector 205*cf8cbdd6SSebastian Grimberg const bool is_deterministic = impl->d_l_vec_indices != NULL; 206dce49693SSebastian Grimberg const CeedInt block_size = 64; 207*cf8cbdd6SSebastian Grimberg const CeedInt grid = CeedDivUpInt(impl->num_nodes, block_size); 208b7453713SJeremy L Thompson 209dce49693SSebastian Grimberg switch (rstr_type) { 210dce49693SSebastian Grimberg case CEED_RESTRICTION_STRIDED: { 211*cf8cbdd6SSebastian Grimberg void *args[] = {&d_u, &d_v}; 212dce49693SSebastian Grimberg 213*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args)); 214dce49693SSebastian Grimberg } break; 215dce49693SSebastian Grimberg case CEED_RESTRICTION_STANDARD: { 216*cf8cbdd6SSebastian Grimberg if (!is_deterministic) { 217*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &d_u, &d_v}; 21858549094SSebastian Grimberg 219*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args)); 2200d0321e0SJeremy L Thompson } else { 22158549094SSebastian Grimberg void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v}; 22258549094SSebastian Grimberg 223*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args)); 22458549094SSebastian Grimberg } 225dce49693SSebastian Grimberg } break; 226dce49693SSebastian Grimberg case CEED_RESTRICTION_ORIENTED: { 227dce49693SSebastian Grimberg if (use_signs) { 228*cf8cbdd6SSebastian Grimberg if (!is_deterministic) { 229*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &impl->d_orients, &d_u, &d_v}; 23058549094SSebastian Grimberg 231*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args)); 232dce49693SSebastian Grimberg } else { 2337aa91133SSebastian Grimberg void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_orients, &d_u, &d_v}; 2347aa91133SSebastian Grimberg 235*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args)); 2367aa91133SSebastian Grimberg } 2377aa91133SSebastian Grimberg } else { 238*cf8cbdd6SSebastian Grimberg if (!is_deterministic) { 239*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &d_u, &d_v}; 240dce49693SSebastian Grimberg 241*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args)); 242dce49693SSebastian Grimberg } else { 243dce49693SSebastian Grimberg void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v}; 244dce49693SSebastian Grimberg 245*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args)); 246dce49693SSebastian Grimberg } 247dce49693SSebastian Grimberg } 248dce49693SSebastian Grimberg } break; 249dce49693SSebastian Grimberg case CEED_RESTRICTION_CURL_ORIENTED: { 250dce49693SSebastian Grimberg if (use_signs && use_orients) { 251*cf8cbdd6SSebastian Grimberg if (!is_deterministic) { 252*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v}; 253dce49693SSebastian Grimberg 254*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args)); 2557aa91133SSebastian Grimberg } else { 2567aa91133SSebastian Grimberg void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v}; 2577aa91133SSebastian Grimberg 258*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyTranspose, grid, block_size, args)); 2597aa91133SSebastian Grimberg } 260dce49693SSebastian Grimberg } else if (use_orients) { 261*cf8cbdd6SSebastian Grimberg if (!is_deterministic) { 262*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v}; 263dce49693SSebastian Grimberg 264*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args)); 265dce49693SSebastian Grimberg } else { 2667aa91133SSebastian Grimberg void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v}; 2677aa91133SSebastian Grimberg 268*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args)); 2697aa91133SSebastian Grimberg } 2707aa91133SSebastian Grimberg } else { 271*cf8cbdd6SSebastian Grimberg if (!is_deterministic) { 272*cf8cbdd6SSebastian Grimberg void *args[] = {&impl->d_ind, &d_u, &d_v}; 273dce49693SSebastian Grimberg 274*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args)); 275dce49693SSebastian Grimberg } else { 276dce49693SSebastian Grimberg void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v}; 277dce49693SSebastian Grimberg 278*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedRunKernel_Hip(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args)); 279dce49693SSebastian Grimberg } 280dce49693SSebastian Grimberg } 281dce49693SSebastian Grimberg } break; 282b3d03e38SSebastian Grimberg case CEED_RESTRICTION_POINTS: { 283b3d03e38SSebastian Grimberg // LCOV_EXCL_START 284b3d03e38SSebastian Grimberg return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints"); 285b3d03e38SSebastian Grimberg // LCOV_EXCL_STOP 286b3d03e38SSebastian Grimberg } break; 2870d0321e0SJeremy L Thompson } 2880d0321e0SJeremy L Thompson } 2890d0321e0SJeremy L Thompson 2902b730f8bSJeremy L Thompson if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL; 2910d0321e0SJeremy L Thompson 2920d0321e0SJeremy L Thompson // Restore arrays 2932b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 2942b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 2950d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2960d0321e0SJeremy L Thompson } 2970d0321e0SJeremy L Thompson 2980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 299dce49693SSebastian Grimberg // Apply restriction 300dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 301dce49693SSebastian Grimberg static int CeedElemRestrictionApply_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) { 302dce49693SSebastian Grimberg return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, true, true, u, v, request); 303dce49693SSebastian Grimberg } 304dce49693SSebastian Grimberg 305dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 306dce49693SSebastian Grimberg // Apply unsigned restriction 307dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 308dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnsigned_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, 309dce49693SSebastian Grimberg CeedRequest *request) { 310dce49693SSebastian Grimberg return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, true, u, v, request); 311dce49693SSebastian Grimberg } 312dce49693SSebastian Grimberg 313dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 314dce49693SSebastian Grimberg // Apply unoriented restriction 315dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 316dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnoriented_Hip(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, 317dce49693SSebastian Grimberg CeedRequest *request) { 318dce49693SSebastian Grimberg return CeedElemRestrictionApply_Hip_Core(rstr, t_mode, false, false, u, v, request); 319dce49693SSebastian Grimberg } 320dce49693SSebastian Grimberg 321dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 3220d0321e0SJeremy L Thompson // Get offsets 3230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 324472941f0SJeremy L Thompson static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) { 3250d0321e0SJeremy L Thompson CeedElemRestriction_Hip *impl; 3260d0321e0SJeremy L Thompson 327b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 328472941f0SJeremy L Thompson switch (mem_type) { 3290d0321e0SJeremy L Thompson case CEED_MEM_HOST: 3300d0321e0SJeremy L Thompson *offsets = impl->h_ind; 3310d0321e0SJeremy L Thompson break; 3320d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 3330d0321e0SJeremy L Thompson *offsets = impl->d_ind; 3340d0321e0SJeremy L Thompson break; 3350d0321e0SJeremy L Thompson } 3360d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3370d0321e0SJeremy L Thompson } 3380d0321e0SJeremy L Thompson 3390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 340dce49693SSebastian Grimberg // Get orientations 341dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 342dce49693SSebastian Grimberg static int CeedElemRestrictionGetOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) { 343dce49693SSebastian Grimberg CeedElemRestriction_Hip *impl; 344dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 345dce49693SSebastian Grimberg 346dce49693SSebastian Grimberg switch (mem_type) { 347dce49693SSebastian Grimberg case CEED_MEM_HOST: 348dce49693SSebastian Grimberg *orients = impl->h_orients; 349dce49693SSebastian Grimberg break; 350dce49693SSebastian Grimberg case CEED_MEM_DEVICE: 351dce49693SSebastian Grimberg *orients = impl->d_orients; 352dce49693SSebastian Grimberg break; 353dce49693SSebastian Grimberg } 354dce49693SSebastian Grimberg return CEED_ERROR_SUCCESS; 355dce49693SSebastian Grimberg } 356dce49693SSebastian Grimberg 357dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 358dce49693SSebastian Grimberg // Get curl-conforming orientations 359dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 360dce49693SSebastian Grimberg static int CeedElemRestrictionGetCurlOrientations_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) { 361dce49693SSebastian Grimberg CeedElemRestriction_Hip *impl; 362dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 363dce49693SSebastian Grimberg 364dce49693SSebastian Grimberg switch (mem_type) { 365dce49693SSebastian Grimberg case CEED_MEM_HOST: 366dce49693SSebastian Grimberg *curl_orients = impl->h_curl_orients; 367dce49693SSebastian Grimberg break; 368dce49693SSebastian Grimberg case CEED_MEM_DEVICE: 369dce49693SSebastian Grimberg *curl_orients = impl->d_curl_orients; 370dce49693SSebastian Grimberg break; 371dce49693SSebastian Grimberg } 372dce49693SSebastian Grimberg return CEED_ERROR_SUCCESS; 373dce49693SSebastian Grimberg } 374dce49693SSebastian Grimberg 375dce49693SSebastian Grimberg //------------------------------------------------------------------------------ 3760d0321e0SJeremy L Thompson // Destroy restriction 3770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 378dce49693SSebastian Grimberg static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction rstr) { 3790d0321e0SJeremy L Thompson Ceed ceed; 380b7453713SJeremy L Thompson CeedElemRestriction_Hip *impl; 381b7453713SJeremy L Thompson 382dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 383dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); 384*cf8cbdd6SSebastian Grimberg if (impl->module) { 3852b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleUnload(impl->module)); 386*cf8cbdd6SSebastian Grimberg } 3872b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_ind_allocated)); 3882b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_ind_allocated)); 3892b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_t_offsets)); 3902b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_t_indices)); 3912b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_l_vec_indices)); 392dce49693SSebastian Grimberg CeedCallBackend(CeedFree(&impl->h_orients_allocated)); 393dce49693SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->d_orients_allocated)); 394dce49693SSebastian Grimberg CeedCallBackend(CeedFree(&impl->h_curl_orients_allocated)); 395dce49693SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->d_curl_orients_allocated)); 3962b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 3970d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3980d0321e0SJeremy L Thompson } 3990d0321e0SJeremy L Thompson 4000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4010d0321e0SJeremy L Thompson // Create transpose offsets and indices 4020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 403dce49693SSebastian Grimberg static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction rstr, const CeedInt *indices) { 4040d0321e0SJeremy L Thompson Ceed ceed; 405b7453713SJeremy L Thompson bool *is_node; 406e79b91d9SJeremy L Thompson CeedSize l_size; 407dce49693SSebastian Grimberg CeedInt num_elem, elem_size, num_comp, num_nodes = 0; 408dce49693SSebastian Grimberg CeedInt *ind_to_offset, *l_vec_indices, *t_offsets, *t_indices; 409b7453713SJeremy L Thompson CeedElemRestriction_Hip *impl; 410b7453713SJeremy L Thompson 411dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); 412dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); 413dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 414dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 415dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 416dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp)); 417b7453713SJeremy L Thompson const CeedInt size_indices = num_elem * elem_size; 4180d0321e0SJeremy L Thompson 419437930d1SJeremy L Thompson // Count num_nodes 4202b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(l_size, &is_node)); 421dce49693SSebastian Grimberg 4222b730f8bSJeremy L Thompson for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1; 4232b730f8bSJeremy L Thompson for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i]; 424437930d1SJeremy L Thompson impl->num_nodes = num_nodes; 4250d0321e0SJeremy L Thompson 4260d0321e0SJeremy L Thompson // L-vector offsets array 4272b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(l_size, &ind_to_offset)); 4282b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices)); 429b7453713SJeremy L Thompson for (CeedInt i = 0, j = 0; i < l_size; i++) { 430437930d1SJeremy L Thompson if (is_node[i]) { 431437930d1SJeremy L Thompson l_vec_indices[j] = i; 4320d0321e0SJeremy L Thompson ind_to_offset[i] = j++; 4330d0321e0SJeremy L Thompson } 4342b730f8bSJeremy L Thompson } 4352b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&is_node)); 4360d0321e0SJeremy L Thompson 4370d0321e0SJeremy L Thompson // Compute transpose offsets and indices 438437930d1SJeremy L Thompson const CeedInt size_offsets = num_nodes + 1; 439b7453713SJeremy L Thompson 4402b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(size_offsets, &t_offsets)); 4412b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(size_indices, &t_indices)); 4420d0321e0SJeremy L Thompson // Count node multiplicity 4432b730f8bSJeremy L Thompson for (CeedInt e = 0; e < num_elem; ++e) { 4442b730f8bSJeremy L Thompson for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1]; 4452b730f8bSJeremy L Thompson } 4460d0321e0SJeremy L Thompson // Convert to running sum 4472b730f8bSJeremy L Thompson for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1]; 4480d0321e0SJeremy L Thompson // List all E-vec indices associated with L-vec node 449437930d1SJeremy L Thompson for (CeedInt e = 0; e < num_elem; ++e) { 450437930d1SJeremy L Thompson for (CeedInt i = 0; i < elem_size; ++i) { 451437930d1SJeremy L Thompson const CeedInt lid = elem_size * e + i; 4520d0321e0SJeremy L Thompson const CeedInt gid = indices[lid]; 453b7453713SJeremy L Thompson 454437930d1SJeremy L Thompson t_indices[t_offsets[ind_to_offset[gid]]++] = lid; 4550d0321e0SJeremy L Thompson } 4560d0321e0SJeremy L Thompson } 4570d0321e0SJeremy L Thompson // Reset running sum 4582b730f8bSJeremy L Thompson for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1]; 459437930d1SJeremy L Thompson t_offsets[0] = 0; 4600d0321e0SJeremy L Thompson 4610d0321e0SJeremy L Thompson // Copy data to device 4620d0321e0SJeremy L Thompson // -- L-vector indices 4632b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt))); 4642b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice)); 4650d0321e0SJeremy L Thompson // -- Transpose offsets 4662b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt))); 4672b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice)); 4680d0321e0SJeremy L Thompson // -- Transpose indices 4692b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt))); 4702b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice)); 4710d0321e0SJeremy L Thompson 4720d0321e0SJeremy L Thompson // Cleanup 4732b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&ind_to_offset)); 4742b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&l_vec_indices)); 4752b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&t_offsets)); 4762b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&t_indices)); 4770d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4780d0321e0SJeremy L Thompson } 4790d0321e0SJeremy L Thompson 4800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4810d0321e0SJeremy L Thompson // Create restriction 4820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 483fcbe8c06SSebastian Grimberg int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *indices, const bool *orients, 484dce49693SSebastian Grimberg const CeedInt8 *curl_orients, CeedElemRestriction rstr) { 485b7453713SJeremy L Thompson Ceed ceed, ceed_parent; 486dce49693SSebastian Grimberg bool is_deterministic; 487*cf8cbdd6SSebastian Grimberg CeedInt num_elem, elem_size; 488b7453713SJeremy L Thompson CeedRestrictionType rstr_type; 4890d0321e0SJeremy L Thompson CeedElemRestriction_Hip *impl; 490b7453713SJeremy L Thompson 491dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); 492ca735530SJeremy L Thompson CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); 493ca735530SJeremy L Thompson CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic)); 494dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 495dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 496dce49693SSebastian Grimberg const CeedInt size = num_elem * elem_size; 497*cf8cbdd6SSebastian Grimberg CeedInt layout[3] = {1, size, elem_size}; 4980d0321e0SJeremy L Thompson 499dce49693SSebastian Grimberg CeedCallBackend(CeedCalloc(1, &impl)); 500dce49693SSebastian Grimberg impl->num_nodes = size; 5010d0321e0SJeremy L Thompson impl->h_ind = NULL; 5020d0321e0SJeremy L Thompson impl->h_ind_allocated = NULL; 5030d0321e0SJeremy L Thompson impl->d_ind = NULL; 5040d0321e0SJeremy L Thompson impl->d_ind_allocated = NULL; 505437930d1SJeremy L Thompson impl->d_t_indices = NULL; 506437930d1SJeremy L Thompson impl->d_t_offsets = NULL; 507dce49693SSebastian Grimberg impl->h_orients = NULL; 508dce49693SSebastian Grimberg impl->h_orients_allocated = NULL; 509dce49693SSebastian Grimberg impl->d_orients = NULL; 510dce49693SSebastian Grimberg impl->d_orients_allocated = NULL; 511dce49693SSebastian Grimberg impl->h_curl_orients = NULL; 512dce49693SSebastian Grimberg impl->h_curl_orients_allocated = NULL; 513dce49693SSebastian Grimberg impl->d_curl_orients = NULL; 514dce49693SSebastian Grimberg impl->d_curl_orients_allocated = NULL; 515dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionSetData(rstr, impl)); 516dce49693SSebastian Grimberg CeedCallBackend(CeedElemRestrictionSetELayout(rstr, layout)); 5170d0321e0SJeremy L Thompson 518dce49693SSebastian Grimberg // Set up device offset/orientation arrays 519*cf8cbdd6SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type)); 520dce49693SSebastian Grimberg if (rstr_type != CEED_RESTRICTION_STRIDED) { 521472941f0SJeremy L Thompson switch (mem_type) { 5226574a04fSJeremy L Thompson case CEED_MEM_HOST: { 523472941f0SJeremy L Thompson switch (copy_mode) { 5240d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 5250d0321e0SJeremy L Thompson impl->h_ind_allocated = (CeedInt *)indices; 5260d0321e0SJeremy L Thompson impl->h_ind = (CeedInt *)indices; 5270d0321e0SJeremy L Thompson break; 5280d0321e0SJeremy L Thompson case CEED_USE_POINTER: 5290d0321e0SJeremy L Thompson impl->h_ind = (CeedInt *)indices; 5300d0321e0SJeremy L Thompson break; 5310d0321e0SJeremy L Thompson case CEED_COPY_VALUES: 532dce49693SSebastian Grimberg CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated)); 533dce49693SSebastian Grimberg memcpy(impl->h_ind_allocated, indices, size * sizeof(CeedInt)); 53444d7a66cSJeremy L Thompson impl->h_ind = impl->h_ind_allocated; 5350d0321e0SJeremy L Thompson break; 5360d0321e0SJeremy L Thompson } 5372b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt))); 5380d0321e0SJeremy L Thompson impl->d_ind_allocated = impl->d_ind; // We own the device memory 5392b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyHostToDevice)); 540dce49693SSebastian Grimberg if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, indices)); 541dce49693SSebastian Grimberg } break; 5426574a04fSJeremy L Thompson case CEED_MEM_DEVICE: { 543472941f0SJeremy L Thompson switch (copy_mode) { 5440d0321e0SJeremy L Thompson case CEED_COPY_VALUES: 5452b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt))); 5460d0321e0SJeremy L Thompson impl->d_ind_allocated = impl->d_ind; // We own the device memory 5472b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyDeviceToDevice)); 5480d0321e0SJeremy L Thompson break; 5490d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 5500d0321e0SJeremy L Thompson impl->d_ind = (CeedInt *)indices; 5510d0321e0SJeremy L Thompson impl->d_ind_allocated = impl->d_ind; 5520d0321e0SJeremy L Thompson break; 5530d0321e0SJeremy L Thompson case CEED_USE_POINTER: 5540d0321e0SJeremy L Thompson impl->d_ind = (CeedInt *)indices; 5556574a04fSJeremy L Thompson break; 5566574a04fSJeremy L Thompson } 557dce49693SSebastian Grimberg CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated)); 558dce49693SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(impl->h_ind_allocated, impl->d_ind, size * sizeof(CeedInt), hipMemcpyDeviceToHost)); 559dce49693SSebastian Grimberg impl->h_ind = impl->h_ind_allocated; 560dce49693SSebastian Grimberg if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(rstr, indices)); 561dce49693SSebastian Grimberg } break; 562dce49693SSebastian Grimberg } 563dce49693SSebastian Grimberg 564dce49693SSebastian Grimberg // Orientation data 565dce49693SSebastian Grimberg if (rstr_type == CEED_RESTRICTION_ORIENTED) { 566dce49693SSebastian Grimberg switch (mem_type) { 567dce49693SSebastian Grimberg case CEED_MEM_HOST: { 568dce49693SSebastian Grimberg switch (copy_mode) { 569dce49693SSebastian Grimberg case CEED_OWN_POINTER: 570dce49693SSebastian Grimberg impl->h_orients_allocated = (bool *)orients; 571dce49693SSebastian Grimberg impl->h_orients = (bool *)orients; 572dce49693SSebastian Grimberg break; 573dce49693SSebastian Grimberg case CEED_USE_POINTER: 574dce49693SSebastian Grimberg impl->h_orients = (bool *)orients; 575dce49693SSebastian Grimberg break; 576dce49693SSebastian Grimberg case CEED_COPY_VALUES: 577dce49693SSebastian Grimberg CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated)); 578dce49693SSebastian Grimberg memcpy(impl->h_orients_allocated, orients, size * sizeof(bool)); 579dce49693SSebastian Grimberg impl->h_orients = impl->h_orients_allocated; 580dce49693SSebastian Grimberg break; 581dce49693SSebastian Grimberg } 582dce49693SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients, size * sizeof(bool))); 583dce49693SSebastian Grimberg impl->d_orients_allocated = impl->d_orients; // We own the device memory 584dce49693SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(impl->d_orients, orients, size * sizeof(bool), hipMemcpyHostToDevice)); 585dce49693SSebastian Grimberg } break; 586dce49693SSebastian Grimberg case CEED_MEM_DEVICE: { 587dce49693SSebastian Grimberg switch (copy_mode) { 588dce49693SSebastian Grimberg case CEED_COPY_VALUES: 589dce49693SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&impl->d_orients, size * sizeof(bool))); 590dce49693SSebastian Grimberg impl->d_orients_allocated = impl->d_orients; // We own the device memory 591dce49693SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(impl->d_orients, orients, size * sizeof(bool), hipMemcpyDeviceToDevice)); 592dce49693SSebastian Grimberg break; 593dce49693SSebastian Grimberg case CEED_OWN_POINTER: 594dce49693SSebastian Grimberg impl->d_orients = (bool *)orients; 595dce49693SSebastian Grimberg impl->d_orients_allocated = impl->d_orients; 596dce49693SSebastian Grimberg break; 597dce49693SSebastian Grimberg case CEED_USE_POINTER: 598dce49693SSebastian Grimberg impl->d_orients = (bool *)orients; 599dce49693SSebastian Grimberg break; 600dce49693SSebastian Grimberg } 601dce49693SSebastian Grimberg CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated)); 602dce49693SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(impl->h_orients_allocated, impl->d_orients, size * sizeof(bool), hipMemcpyDeviceToHost)); 603dce49693SSebastian Grimberg impl->h_orients = impl->h_orients_allocated; 604dce49693SSebastian Grimberg } break; 605dce49693SSebastian Grimberg } 606dce49693SSebastian Grimberg } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) { 607dce49693SSebastian Grimberg switch (mem_type) { 608dce49693SSebastian Grimberg case CEED_MEM_HOST: { 609dce49693SSebastian Grimberg switch (copy_mode) { 610dce49693SSebastian Grimberg case CEED_OWN_POINTER: 611dce49693SSebastian Grimberg impl->h_curl_orients_allocated = (CeedInt8 *)curl_orients; 612dce49693SSebastian Grimberg impl->h_curl_orients = (CeedInt8 *)curl_orients; 613dce49693SSebastian Grimberg break; 614dce49693SSebastian Grimberg case CEED_USE_POINTER: 615dce49693SSebastian Grimberg impl->h_curl_orients = (CeedInt8 *)curl_orients; 616dce49693SSebastian Grimberg break; 617dce49693SSebastian Grimberg case CEED_COPY_VALUES: 618dce49693SSebastian Grimberg CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated)); 619dce49693SSebastian Grimberg memcpy(impl->h_curl_orients_allocated, curl_orients, 3 * size * sizeof(CeedInt8)); 620dce49693SSebastian Grimberg impl->h_curl_orients = impl->h_curl_orients_allocated; 621dce49693SSebastian Grimberg break; 622dce49693SSebastian Grimberg } 623dce49693SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8))); 624dce49693SSebastian Grimberg impl->d_curl_orients_allocated = impl->d_curl_orients; // We own the device memory 625dce49693SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyHostToDevice)); 626dce49693SSebastian Grimberg } break; 627dce49693SSebastian Grimberg case CEED_MEM_DEVICE: { 628dce49693SSebastian Grimberg switch (copy_mode) { 629dce49693SSebastian Grimberg case CEED_COPY_VALUES: 630dce49693SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8))); 631dce49693SSebastian Grimberg impl->d_curl_orients_allocated = impl->d_curl_orients; // We own the device memory 632dce49693SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToDevice)); 633dce49693SSebastian Grimberg break; 634dce49693SSebastian Grimberg case CEED_OWN_POINTER: 635dce49693SSebastian Grimberg impl->d_curl_orients = (CeedInt8 *)curl_orients; 636dce49693SSebastian Grimberg impl->d_curl_orients_allocated = impl->d_curl_orients; 637dce49693SSebastian Grimberg break; 638dce49693SSebastian Grimberg case CEED_USE_POINTER: 639dce49693SSebastian Grimberg impl->d_curl_orients = (CeedInt8 *)curl_orients; 640dce49693SSebastian Grimberg break; 641dce49693SSebastian Grimberg } 642dce49693SSebastian Grimberg CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated)); 643dce49693SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(impl->h_curl_orients_allocated, impl->d_curl_orients, 3 * size * sizeof(CeedInt8), hipMemcpyDeviceToHost)); 644dce49693SSebastian Grimberg impl->h_curl_orients = impl->h_curl_orients_allocated; 645dce49693SSebastian Grimberg } break; 646dce49693SSebastian Grimberg } 647dce49693SSebastian Grimberg } 6480d0321e0SJeremy L Thompson } 6490d0321e0SJeremy L Thompson 6500d0321e0SJeremy L Thompson // Register backend functions 651dce49693SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Apply", CeedElemRestrictionApply_Hip)); 652dce49693SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnsigned", CeedElemRestrictionApplyUnsigned_Hip)); 653dce49693SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApplyUnoriented_Hip)); 654dce49693SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Hip)); 655dce49693SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Hip)); 656dce49693SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Hip)); 657dce49693SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Hip)); 6580d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6590d0321e0SJeremy L Thompson } 6600d0321e0SJeremy L Thompson 6610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 662