15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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> 11111870feSJeremy L Thompson #include <string.h> 120d0321e0SJeremy L Thompson #include <hip/hip_runtime.h> 132b730f8bSJeremy L Thompson 1449aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h" 150d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h" 162b730f8bSJeremy L Thompson #include "ceed-hip-ref.h" 170d0321e0SJeremy L Thompson 180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 190d0321e0SJeremy L Thompson // Basis apply - tensor 200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 21db2becc9SJeremy L Thompson static int CeedBasisApplyCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 22db2becc9SJeremy L Thompson CeedVector u, CeedVector v) { 230d0321e0SJeremy L Thompson Ceed ceed; 24b7453713SJeremy L Thompson CeedInt Q_1d, dim; 257bbbfca3SJeremy L Thompson const CeedInt is_transpose = t_mode == CEED_TRANSPOSE; 26437930d1SJeremy L Thompson const int max_block_size = 64; 270d0321e0SJeremy L Thompson const CeedScalar *d_u; 280d0321e0SJeremy L Thompson CeedScalar *d_v; 29b7453713SJeremy L Thompson CeedBasis_Hip *data; 30b7453713SJeremy L Thompson 31b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 32b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 33b7453713SJeremy L Thompson 349ea2cfd9SJeremy L Thompson // Get read/write access to u, v 356574a04fSJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 366574a04fSJeremy L Thompson else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 37db2becc9SJeremy L Thompson if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 38db2becc9SJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 390d0321e0SJeremy L Thompson 400d0321e0SJeremy L Thompson // Clear v for transpose operation 41db2becc9SJeremy L Thompson if (is_transpose && !apply_add) { 4219a04db8SJeremy L Thompson CeedInt num_comp, q_comp, num_nodes, num_qpts; 431f9221feSJeremy L Thompson CeedSize length; 44b7453713SJeremy L Thompson 4519a04db8SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 4619a04db8SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 4719a04db8SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes)); 4819a04db8SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 4919a04db8SJeremy L Thompson length = (CeedSize)num_elem * (CeedSize)num_comp * (t_mode == CEED_TRANSPOSE ? (CeedSize)num_nodes : ((CeedSize)num_qpts * (CeedSize)q_comp)); 502b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar))); 510d0321e0SJeremy L Thompson } 52b2165e7aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 53b2165e7aSSebastian Grimberg CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 540d0321e0SJeremy L Thompson 550d0321e0SJeremy L Thompson // Basis action 56437930d1SJeremy L Thompson switch (eval_mode) { 570d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: { 587bbbfca3SJeremy L Thompson void *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &d_u, &d_v}; 59b2165e7aSSebastian Grimberg const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size); 600d0321e0SJeremy L Thompson 61eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Hip(ceed, data->Interp, num_elem, block_size, interp_args)); 620d0321e0SJeremy L Thompson } break; 630d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: { 647bbbfca3SJeremy L Thompson void *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &data->d_grad_1d, &d_u, &d_v}; 65b2165e7aSSebastian Grimberg const CeedInt block_size = max_block_size; 660d0321e0SJeremy L Thompson 67eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Hip(ceed, data->Grad, num_elem, block_size, grad_args)); 680d0321e0SJeremy L Thompson } break; 690d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 70097cc795SJames Wright CeedCheck(data->d_q_weight_1d, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights_1d not set", CeedEvalModes[eval_mode]); 71437930d1SJeremy L Thompson void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v}; 72b2165e7aSSebastian Grimberg const int block_size_x = Q_1d; 73b2165e7aSSebastian Grimberg const int block_size_y = dim >= 2 ? Q_1d : 1; 740d0321e0SJeremy L Thompson 75b2165e7aSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, num_elem, block_size_x, block_size_y, 1, weight_args)); 760d0321e0SJeremy L Thompson } break; 779ea2cfd9SJeremy L Thompson case CEED_EVAL_NONE: /* handled separately below */ 789ea2cfd9SJeremy L Thompson break; 790d0321e0SJeremy L Thompson // LCOV_EXCL_START 800d0321e0SJeremy L Thompson case CEED_EVAL_DIV: 810d0321e0SJeremy L Thompson case CEED_EVAL_CURL: 82bcbe1c99SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); 830d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 840d0321e0SJeremy L Thompson } 850d0321e0SJeremy L Thompson 869ea2cfd9SJeremy L Thompson // Restore vectors, cover CEED_EVAL_NONE 872b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 889ea2cfd9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); 899ea2cfd9SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 900d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 910d0321e0SJeremy L Thompson } 920d0321e0SJeremy L Thompson 93db2becc9SJeremy L Thompson static int CeedBasisApply_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 94db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyCore_Hip(basis, false, num_elem, t_mode, eval_mode, u, v)); 95db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 96db2becc9SJeremy L Thompson } 97db2becc9SJeremy L Thompson 98db2becc9SJeremy L Thompson static int CeedBasisApplyAdd_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 99db2becc9SJeremy L Thompson CeedVector v) { 100db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyCore_Hip(basis, true, num_elem, t_mode, eval_mode, u, v)); 101db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 102db2becc9SJeremy L Thompson } 103db2becc9SJeremy L Thompson 1040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1051c21e869SJeremy L Thompson // Basis apply - tensor AtPoints 1061c21e869SJeremy L Thompson //------------------------------------------------------------------------------ 107db2becc9SJeremy L Thompson static int CeedBasisApplyAtPointsCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, const CeedInt *num_points, 108db2becc9SJeremy L Thompson CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 1091c21e869SJeremy L Thompson Ceed ceed; 1101c21e869SJeremy L Thompson CeedInt Q_1d, dim, max_num_points = num_points[0]; 1111c21e869SJeremy L Thompson const CeedInt is_transpose = t_mode == CEED_TRANSPOSE; 1121c21e869SJeremy L Thompson const int max_block_size = 32; 1131c21e869SJeremy L Thompson const CeedScalar *d_x, *d_u; 1141c21e869SJeremy L Thompson CeedScalar *d_v; 1151c21e869SJeremy L Thompson CeedBasis_Hip *data; 1161c21e869SJeremy L Thompson 1171c21e869SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 1181c21e869SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 1191c21e869SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1201c21e869SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 1211c21e869SJeremy L Thompson 1221c21e869SJeremy L Thompson // Weight handled separately 1231c21e869SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 1245a5594ffSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(v, 1.0)); 1251c21e869SJeremy L Thompson return CEED_ERROR_SUCCESS; 1261c21e869SJeremy L Thompson } 1271c21e869SJeremy L Thompson 128111870feSJeremy L Thompson // Check padded to uniform number of points per elem 129111870feSJeremy L Thompson for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]); 130111870feSJeremy L Thompson { 131111870feSJeremy L Thompson CeedInt num_comp, q_comp; 132111870feSJeremy L Thompson CeedSize len, len_required; 133111870feSJeremy L Thompson 134111870feSJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 135111870feSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 136111870feSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(is_transpose ? u : v, &len)); 137111870feSJeremy L Thompson len_required = (CeedSize)num_comp * (CeedSize)q_comp * (CeedSize)num_elem * (CeedSize)max_num_points; 138111870feSJeremy L Thompson CeedCheck(len >= len_required, ceed, CEED_ERROR_BACKEND, 139111870feSJeremy L Thompson "Vector at points must be padded to the same number of points in each element for BasisApplyAtPoints on GPU backends." 140111870feSJeremy L Thompson " Found %" CeedSize_FMT ", Required %" CeedSize_FMT, 141111870feSJeremy L Thompson len, len_required); 142111870feSJeremy L Thompson } 143111870feSJeremy L Thompson 144111870feSJeremy L Thompson // Move num_points array to device 145111870feSJeremy L Thompson if (is_transpose) { 146111870feSJeremy L Thompson const CeedInt num_bytes = num_elem * sizeof(CeedInt); 147111870feSJeremy L Thompson 148111870feSJeremy L Thompson if (num_elem != data->num_elem_at_points) { 149111870feSJeremy L Thompson data->num_elem_at_points = num_elem; 150111870feSJeremy L Thompson 151111870feSJeremy L Thompson if (data->d_points_per_elem) CeedCallHip(ceed, hipFree(data->d_points_per_elem)); 152111870feSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_points_per_elem, num_bytes)); 153111870feSJeremy L Thompson CeedCallBackend(CeedFree(&data->h_points_per_elem)); 154111870feSJeremy L Thompson CeedCallBackend(CeedCalloc(num_elem, &data->h_points_per_elem)); 155111870feSJeremy L Thompson } 1569e511c80SJeremy L Thompson if (memcmp(data->h_points_per_elem, num_points, num_bytes)) { 157111870feSJeremy L Thompson memcpy(data->h_points_per_elem, num_points, num_bytes); 158111870feSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_points_per_elem, num_points, num_bytes, hipMemcpyHostToDevice)); 159111870feSJeremy L Thompson } 160111870feSJeremy L Thompson } 161111870feSJeremy L Thompson 1621c21e869SJeremy L Thompson // Build kernels if needed 1631c21e869SJeremy L Thompson if (data->num_points != max_num_points) { 1641c21e869SJeremy L Thompson CeedInt P_1d; 1651c21e869SJeremy L Thompson 1661c21e869SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 1671c21e869SJeremy L Thompson data->num_points = max_num_points; 1681c21e869SJeremy L Thompson 1691c21e869SJeremy L Thompson // -- Create interp matrix to Chebyshev coefficients 1701c21e869SJeremy L Thompson if (!data->d_chebyshev_interp_1d) { 1711c21e869SJeremy L Thompson CeedSize interp_bytes; 1721c21e869SJeremy L Thompson CeedScalar *chebyshev_interp_1d; 1731c21e869SJeremy L Thompson 1741c21e869SJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 1751c21e869SJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 1765a5594ffSJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 1771c21e869SJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_chebyshev_interp_1d, interp_bytes)); 1781c21e869SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice)); 1791c21e869SJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 1801c21e869SJeremy L Thompson } 1811c21e869SJeremy L Thompson 1821c21e869SJeremy L Thompson // -- Compile kernels 183*9c25dd66SJeremy L Thompson const char basis_kernel_source[] = "// AtPoints basis source\n#include <ceed/jit-source/hip/hip-ref-basis-tensor-at-points.h>\n"; 1841c21e869SJeremy L Thompson CeedInt num_comp; 1851c21e869SJeremy L Thompson 1861c21e869SJeremy L Thompson if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints)); 1871c21e869SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 1881c21e869SJeremy L Thompson CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->moduleAtPoints, 9, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "BASIS_BUF_LEN", 189f7c9815fSJeremy L Thompson Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp, 1901c21e869SJeremy L Thompson "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim), "BASIS_NUM_PTS", 191f7c9815fSJeremy L Thompson max_num_points, "POINTS_BUFF_LEN", CeedIntPow(Q_1d, dim - 1))); 1921c21e869SJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "InterpAtPoints", &data->InterpAtPoints)); 1931c21e869SJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "GradAtPoints", &data->GradAtPoints)); 1941c21e869SJeremy L Thompson } 1951c21e869SJeremy L Thompson 1961c21e869SJeremy L Thompson // Get read/write access to u, v 1971c21e869SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(x_ref, CEED_MEM_DEVICE, &d_x)); 1981c21e869SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 1991c21e869SJeremy L Thompson else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 200db2becc9SJeremy L Thompson if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 201db2becc9SJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 2021c21e869SJeremy L Thompson 2031c21e869SJeremy L Thompson // Clear v for transpose operation 204db2becc9SJeremy L Thompson if (is_transpose && !apply_add) { 20519a04db8SJeremy L Thompson CeedInt num_comp, q_comp, num_nodes; 2061c21e869SJeremy L Thompson CeedSize length; 2071c21e869SJeremy L Thompson 20819a04db8SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 20919a04db8SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 21019a04db8SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes)); 21119a04db8SJeremy L Thompson length = 21219a04db8SJeremy L Thompson (CeedSize)num_elem * (CeedSize)num_comp * (t_mode == CEED_TRANSPOSE ? (CeedSize)num_nodes : ((CeedSize)max_num_points * (CeedSize)q_comp)); 2131c21e869SJeremy L Thompson CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar))); 2141c21e869SJeremy L Thompson } 2151c21e869SJeremy L Thompson 2161c21e869SJeremy L Thompson // Basis action 2171c21e869SJeremy L Thompson switch (eval_mode) { 2181c21e869SJeremy L Thompson case CEED_EVAL_INTERP: { 219111870feSJeremy L Thompson void *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &data->d_points_per_elem, &d_x, &d_u, &d_v}; 2201c21e869SJeremy L Thompson const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size); 2211c21e869SJeremy L Thompson 2221c21e869SJeremy L Thompson CeedCallBackend(CeedRunKernel_Hip(ceed, data->InterpAtPoints, num_elem, block_size, interp_args)); 2231c21e869SJeremy L Thompson } break; 2241c21e869SJeremy L Thompson case CEED_EVAL_GRAD: { 225111870feSJeremy L Thompson void *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &data->d_points_per_elem, &d_x, &d_u, &d_v}; 2262d10e82cSJeremy L Thompson const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size); 2271c21e869SJeremy L Thompson 2281c21e869SJeremy L Thompson CeedCallBackend(CeedRunKernel_Hip(ceed, data->GradAtPoints, num_elem, block_size, grad_args)); 2291c21e869SJeremy L Thompson } break; 2301c21e869SJeremy L Thompson case CEED_EVAL_WEIGHT: 2311c21e869SJeremy L Thompson case CEED_EVAL_NONE: /* handled separately below */ 2321c21e869SJeremy L Thompson break; 2331c21e869SJeremy L Thompson // LCOV_EXCL_START 2341c21e869SJeremy L Thompson case CEED_EVAL_DIV: 2351c21e869SJeremy L Thompson case CEED_EVAL_CURL: 2361c21e869SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); 2371c21e869SJeremy L Thompson // LCOV_EXCL_STOP 2381c21e869SJeremy L Thompson } 2391c21e869SJeremy L Thompson 2401c21e869SJeremy L Thompson // Restore vectors, cover CEED_EVAL_NONE 2411c21e869SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(x_ref, &d_x)); 2421c21e869SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 2431c21e869SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); 2441c21e869SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 2451c21e869SJeremy L Thompson return CEED_ERROR_SUCCESS; 2461c21e869SJeremy L Thompson } 2471c21e869SJeremy L Thompson 248db2becc9SJeremy L Thompson static int CeedBasisApplyAtPoints_Hip(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 249db2becc9SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 250db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPointsCore_Hip(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 251db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 252db2becc9SJeremy L Thompson } 253db2becc9SJeremy L Thompson 254db2becc9SJeremy L Thompson static int CeedBasisApplyAddAtPoints_Hip(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 255db2becc9SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 256db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPointsCore_Hip(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 257db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 258db2becc9SJeremy L Thompson } 259db2becc9SJeremy L Thompson 2601c21e869SJeremy L Thompson //------------------------------------------------------------------------------ 2610d0321e0SJeremy L Thompson // Basis apply - non-tensor 2620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 263db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensorCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 264db2becc9SJeremy L Thompson CeedVector u, CeedVector v) { 2650d0321e0SJeremy L Thompson Ceed ceed; 266437930d1SJeremy L Thompson CeedInt num_nodes, num_qpts; 2677bbbfca3SJeremy L Thompson const CeedInt is_transpose = t_mode == CEED_TRANSPOSE; 268d075f50bSSebastian Grimberg const int elems_per_block = 1; 269d075f50bSSebastian Grimberg const int grid = CeedDivUpInt(num_elem, elems_per_block); 2700d0321e0SJeremy L Thompson const CeedScalar *d_u; 2710d0321e0SJeremy L Thompson CeedScalar *d_v; 272b7453713SJeremy L Thompson CeedBasisNonTensor_Hip *data; 273b7453713SJeremy L Thompson 274b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 275b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 276b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 277b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes)); 278b7453713SJeremy L Thompson 2799ea2cfd9SJeremy L Thompson // Get read/write access to u, v 2809ea2cfd9SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 2819ea2cfd9SJeremy L Thompson else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 282db2becc9SJeremy L Thompson if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 283db2becc9SJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 2840d0321e0SJeremy L Thompson 2850d0321e0SJeremy L Thompson // Clear v for transpose operation 286db2becc9SJeremy L Thompson if (is_transpose && !apply_add) { 2871f9221feSJeremy L Thompson CeedSize length; 288b7453713SJeremy L Thompson 2892b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(v, &length)); 2902b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar))); 2910d0321e0SJeremy L Thompson } 2920d0321e0SJeremy L Thompson 2930d0321e0SJeremy L Thompson // Apply basis operation 294437930d1SJeremy L Thompson switch (eval_mode) { 2950d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: { 296d075f50bSSebastian Grimberg void *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v}; 2977bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 298b2165e7aSSebastian Grimberg 2997bbbfca3SJeremy L Thompson if (is_transpose) { 300d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args)); 301d075f50bSSebastian Grimberg } else { 302b2165e7aSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args)); 303d075f50bSSebastian Grimberg } 3040d0321e0SJeremy L Thompson } break; 3050d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: { 306d075f50bSSebastian Grimberg void *grad_args[] = {(void *)&num_elem, &data->d_grad, &d_u, &d_v}; 3077bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 308b2165e7aSSebastian Grimberg 3097bbbfca3SJeremy L Thompson if (is_transpose) { 310d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args)); 311d075f50bSSebastian Grimberg } else { 312d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args)); 313d075f50bSSebastian Grimberg } 314d075f50bSSebastian Grimberg } break; 315d075f50bSSebastian Grimberg case CEED_EVAL_DIV: { 316d075f50bSSebastian Grimberg void *div_args[] = {(void *)&num_elem, &data->d_div, &d_u, &d_v}; 3177bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 318d075f50bSSebastian Grimberg 3197bbbfca3SJeremy L Thompson if (is_transpose) { 320d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args)); 321d075f50bSSebastian Grimberg } else { 322d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args)); 323d075f50bSSebastian Grimberg } 324d075f50bSSebastian Grimberg } break; 325d075f50bSSebastian Grimberg case CEED_EVAL_CURL: { 326d075f50bSSebastian Grimberg void *curl_args[] = {(void *)&num_elem, &data->d_curl, &d_u, &d_v}; 3277bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 328d075f50bSSebastian Grimberg 3297bbbfca3SJeremy L Thompson if (is_transpose) { 330d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args)); 331d075f50bSSebastian Grimberg } else { 332d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args)); 333d075f50bSSebastian Grimberg } 3340d0321e0SJeremy L Thompson } break; 3350d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 336097cc795SJames Wright CeedCheck(data->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights not set", CeedEvalModes[eval_mode]); 337437930d1SJeremy L Thompson void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v}; 338b2165e7aSSebastian Grimberg 339b2165e7aSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args)); 3400d0321e0SJeremy L Thompson } break; 3419ea2cfd9SJeremy L Thompson case CEED_EVAL_NONE: /* handled separately below */ 3429ea2cfd9SJeremy L Thompson break; 3430d0321e0SJeremy L Thompson } 3440d0321e0SJeremy L Thompson 3459ea2cfd9SJeremy L Thompson // Restore vectors, cover CEED_EVAL_NONE 3462b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 3479ea2cfd9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); 3489ea2cfd9SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 3490d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3500d0321e0SJeremy L Thompson } 3510d0321e0SJeremy L Thompson 352db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 353db2becc9SJeremy L Thompson CeedVector v) { 354db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, false, num_elem, t_mode, eval_mode, u, v)); 355db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 356db2becc9SJeremy L Thompson } 357db2becc9SJeremy L Thompson 358db2becc9SJeremy L Thompson static int CeedBasisApplyAddNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 359db2becc9SJeremy L Thompson CeedVector v) { 360db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, true, num_elem, t_mode, eval_mode, u, v)); 361db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 362db2becc9SJeremy L Thompson } 363db2becc9SJeremy L Thompson 3640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3650d0321e0SJeremy L Thompson // Destroy tensor basis 3660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3670d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) { 3680d0321e0SJeremy L Thompson Ceed ceed; 3690d0321e0SJeremy L Thompson CeedBasis_Hip *data; 370b7453713SJeremy L Thompson 371b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 3722b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 3732b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleUnload(data->module)); 3741c21e869SJeremy L Thompson if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints)); 375097cc795SJames Wright if (data->d_q_weight_1d) CeedCallHip(ceed, hipFree(data->d_q_weight_1d)); 376111870feSJeremy L Thompson CeedCallBackend(CeedFree(&data->h_points_per_elem)); 377111870feSJeremy L Thompson if (data->d_points_per_elem) CeedCallHip(ceed, hipFree(data->d_points_per_elem)); 3782b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(data->d_interp_1d)); 3792b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(data->d_grad_1d)); 3801c21e869SJeremy L Thompson CeedCallHip(ceed, hipFree(data->d_chebyshev_interp_1d)); 3812b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 3820d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3830d0321e0SJeremy L Thompson } 3840d0321e0SJeremy L Thompson 3850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3860d0321e0SJeremy L Thompson // Destroy non-tensor basis 3870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3880d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) { 3890d0321e0SJeremy L Thompson Ceed ceed; 3900d0321e0SJeremy L Thompson CeedBasisNonTensor_Hip *data; 391b7453713SJeremy L Thompson 392b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 3932b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 3942b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleUnload(data->module)); 395097cc795SJames Wright if (data->d_q_weight) CeedCallHip(ceed, hipFree(data->d_q_weight)); 3962b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(data->d_interp)); 3972b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(data->d_grad)); 398d075f50bSSebastian Grimberg CeedCallHip(ceed, hipFree(data->d_div)); 399d075f50bSSebastian Grimberg CeedCallHip(ceed, hipFree(data->d_curl)); 4002b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 4010d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4020d0321e0SJeremy L Thompson } 4030d0321e0SJeremy L Thompson 4040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4050d0321e0SJeremy L Thompson // Create tensor 4060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4072b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d, 4086574a04fSJeremy L Thompson const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) { 4090d0321e0SJeremy L Thompson Ceed ceed; 410b7453713SJeremy L Thompson CeedInt num_comp; 411b7453713SJeremy L Thompson const CeedInt q_bytes = Q_1d * sizeof(CeedScalar); 412b7453713SJeremy L Thompson const CeedInt interp_bytes = q_bytes * P_1d; 4130d0321e0SJeremy L Thompson CeedBasis_Hip *data; 414b7453713SJeremy L Thompson 415b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 4162b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &data)); 4170d0321e0SJeremy L Thompson 4180d0321e0SJeremy L Thompson // Copy data to GPU 419097cc795SJames Wright if (q_weight_1d) { 4202b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes)); 4212b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice)); 422097cc795SJames Wright } 4232b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes)); 4242b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice)); 4252b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes)); 4262b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice)); 4270d0321e0SJeremy L Thompson 428ecc88aebSJeremy L Thompson // Compile basis kernels 429*9c25dd66SJeremy L Thompson const char basis_kernel_source[] = "// Tensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-tensor.h>\n"; 430*9c25dd66SJeremy L Thompson 431b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 432eb7e6cafSJeremy L Thompson CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 7, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "BASIS_BUF_LEN", 433f7c9815fSJeremy L Thompson Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp, 434b7453713SJeremy L Thompson "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim))); 435eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp)); 436eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad)); 437eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight)); 438437930d1SJeremy L Thompson 4392b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisSetData(basis, data)); 4400d0321e0SJeremy L Thompson 441d075f50bSSebastian Grimberg // Register backend functions 4422b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip)); 443db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Hip)); 4441c21e869SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip)); 445db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Hip)); 4462b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip)); 4470d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4480d0321e0SJeremy L Thompson } 4490d0321e0SJeremy L Thompson 4500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 451d075f50bSSebastian Grimberg // Create non-tensor H^1 4520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4532b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad, 45451475c7cSJeremy L Thompson const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 4550d0321e0SJeremy L Thompson Ceed ceed; 456d075f50bSSebastian Grimberg CeedInt num_comp, q_comp_interp, q_comp_grad; 457b7453713SJeremy L Thompson const CeedInt q_bytes = num_qpts * sizeof(CeedScalar); 4580d0321e0SJeremy L Thompson CeedBasisNonTensor_Hip *data; 459b7453713SJeremy L Thompson 460b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 4612b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &data)); 4620d0321e0SJeremy L Thompson 4630d0321e0SJeremy L Thompson // Copy basis data to GPU 464d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 465d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad)); 466097cc795SJames Wright if (q_weight) { 4672b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes)); 4682b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice)); 469097cc795SJames Wright } 470d075f50bSSebastian Grimberg if (interp) { 471d075f50bSSebastian Grimberg const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp; 472d075f50bSSebastian Grimberg 4732b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes)); 4742b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice)); 475d075f50bSSebastian Grimberg } 476d075f50bSSebastian Grimberg if (grad) { 477d075f50bSSebastian Grimberg const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad; 478d075f50bSSebastian Grimberg 4792b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes)); 4802b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice)); 481d075f50bSSebastian Grimberg } 4820d0321e0SJeremy L Thompson 4830d0321e0SJeremy L Thompson // Compile basis kernels 484*9c25dd66SJeremy L Thompson const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n"; 485*9c25dd66SJeremy L Thompson 486b7453713SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 487d075f50bSSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP", 488d075f50bSSebastian Grimberg q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp)); 489eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp)); 490d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose)); 491d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv)); 492d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose)); 493eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight)); 494d075f50bSSebastian Grimberg 495d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisSetData(basis, data)); 496d075f50bSSebastian Grimberg 497d075f50bSSebastian Grimberg // Register backend functions 498d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip)); 499db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip)); 500d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip)); 501d075f50bSSebastian Grimberg return CEED_ERROR_SUCCESS; 502d075f50bSSebastian Grimberg } 503d075f50bSSebastian Grimberg 504d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 505d075f50bSSebastian Grimberg // Create non-tensor H(div) 506d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 507d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div, 508d075f50bSSebastian Grimberg const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 509d075f50bSSebastian Grimberg Ceed ceed; 510d075f50bSSebastian Grimberg CeedInt num_comp, q_comp_interp, q_comp_div; 511d075f50bSSebastian Grimberg const CeedInt q_bytes = num_qpts * sizeof(CeedScalar); 512d075f50bSSebastian Grimberg CeedBasisNonTensor_Hip *data; 513d075f50bSSebastian Grimberg 514d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 515d075f50bSSebastian Grimberg CeedCallBackend(CeedCalloc(1, &data)); 516d075f50bSSebastian Grimberg 517d075f50bSSebastian Grimberg // Copy basis data to GPU 518d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 519d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div)); 520097cc795SJames Wright if (q_weight) { 521d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes)); 522d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice)); 523097cc795SJames Wright } 524d075f50bSSebastian Grimberg if (interp) { 525d075f50bSSebastian Grimberg const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp; 526d075f50bSSebastian Grimberg 527d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes)); 528d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice)); 529d075f50bSSebastian Grimberg } 530d075f50bSSebastian Grimberg if (div) { 531d075f50bSSebastian Grimberg const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div; 532d075f50bSSebastian Grimberg 533d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes)); 534d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice)); 535d075f50bSSebastian Grimberg } 536d075f50bSSebastian Grimberg 537d075f50bSSebastian Grimberg // Compile basis kernels 538*9c25dd66SJeremy L Thompson const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n"; 539*9c25dd66SJeremy L Thompson 540d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 541d075f50bSSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP", 542d075f50bSSebastian Grimberg q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp)); 543d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp)); 544d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose)); 545d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv)); 546d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose)); 547d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight)); 548d075f50bSSebastian Grimberg 549d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisSetData(basis, data)); 550d075f50bSSebastian Grimberg 551d075f50bSSebastian Grimberg // Register backend functions 552d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip)); 553db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip)); 554d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip)); 555d075f50bSSebastian Grimberg return CEED_ERROR_SUCCESS; 556d075f50bSSebastian Grimberg } 557d075f50bSSebastian Grimberg 558d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 559d075f50bSSebastian Grimberg // Create non-tensor H(curl) 560d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 561d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 562d075f50bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 563d075f50bSSebastian Grimberg Ceed ceed; 564d075f50bSSebastian Grimberg CeedInt num_comp, q_comp_interp, q_comp_curl; 565d075f50bSSebastian Grimberg const CeedInt q_bytes = num_qpts * sizeof(CeedScalar); 566d075f50bSSebastian Grimberg CeedBasisNonTensor_Hip *data; 567d075f50bSSebastian Grimberg 568d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 569d075f50bSSebastian Grimberg CeedCallBackend(CeedCalloc(1, &data)); 570d075f50bSSebastian Grimberg 571d075f50bSSebastian Grimberg // Copy basis data to GPU 572d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 573d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl)); 574097cc795SJames Wright if (q_weight) { 575d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes)); 576d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice)); 577097cc795SJames Wright } 578d075f50bSSebastian Grimberg if (interp) { 579d075f50bSSebastian Grimberg const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp; 580d075f50bSSebastian Grimberg 581d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes)); 582d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice)); 583d075f50bSSebastian Grimberg } 584d075f50bSSebastian Grimberg if (curl) { 585d075f50bSSebastian Grimberg const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl; 586d075f50bSSebastian Grimberg 587d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes)); 588d075f50bSSebastian Grimberg CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice)); 589d075f50bSSebastian Grimberg } 590d075f50bSSebastian Grimberg 591d075f50bSSebastian Grimberg // Compile basis kernels 592*9c25dd66SJeremy L Thompson const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n"; 593*9c25dd66SJeremy L Thompson 594d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 595d075f50bSSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP", 596d075f50bSSebastian Grimberg q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp)); 597d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp)); 598d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose)); 599d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv)); 600d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose)); 601d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight)); 602d075f50bSSebastian Grimberg 6032b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisSetData(basis, data)); 6040d0321e0SJeremy L Thompson 6050d0321e0SJeremy L Thompson // Register backend functions 6062b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip)); 607db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip)); 6082b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip)); 6090d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6100d0321e0SJeremy L Thompson } 6112a86cc9dSSebastian Grimberg 6120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 613