xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-basis.c (revision 81ae61599cc0e14ccce523d66e38bf75c6f7903c)
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));
909bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
910d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
920d0321e0SJeremy L Thompson }
930d0321e0SJeremy L Thompson 
94db2becc9SJeremy L Thompson static int CeedBasisApply_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
95db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyCore_Hip(basis, false, num_elem, t_mode, eval_mode, u, v));
96db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
97db2becc9SJeremy L Thompson }
98db2becc9SJeremy L Thompson 
99db2becc9SJeremy L Thompson static int CeedBasisApplyAdd_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
100db2becc9SJeremy L Thompson                                  CeedVector v) {
101db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyCore_Hip(basis, true, num_elem, t_mode, eval_mode, u, v));
102db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
103db2becc9SJeremy L Thompson }
104db2becc9SJeremy L Thompson 
1050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1061c21e869SJeremy L Thompson // Basis apply - tensor AtPoints
1071c21e869SJeremy L Thompson //------------------------------------------------------------------------------
108db2becc9SJeremy L Thompson static int CeedBasisApplyAtPointsCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, const CeedInt *num_points,
109db2becc9SJeremy L Thompson                                           CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
1101c21e869SJeremy L Thompson   Ceed              ceed;
1111c21e869SJeremy L Thompson   CeedInt           Q_1d, dim, max_num_points = num_points[0];
1121c21e869SJeremy L Thompson   const CeedInt     is_transpose   = t_mode == CEED_TRANSPOSE;
1131c21e869SJeremy L Thompson   const int         max_block_size = 32;
1141c21e869SJeremy L Thompson   const CeedScalar *d_x, *d_u;
1151c21e869SJeremy L Thompson   CeedScalar       *d_v;
1161c21e869SJeremy L Thompson   CeedBasis_Hip    *data;
1171c21e869SJeremy L Thompson 
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 
1289bc66399SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1299bc66399SJeremy L Thompson 
130111870feSJeremy L Thompson   // Check padded to uniform number of points per elem
131111870feSJeremy L Thompson   for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]);
132111870feSJeremy L Thompson   {
133111870feSJeremy L Thompson     CeedInt  num_comp, q_comp;
134111870feSJeremy L Thompson     CeedSize len, len_required;
135111870feSJeremy L Thompson 
136111870feSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
137111870feSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
138111870feSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(is_transpose ? u : v, &len));
139111870feSJeremy L Thompson     len_required = (CeedSize)num_comp * (CeedSize)q_comp * (CeedSize)num_elem * (CeedSize)max_num_points;
140111870feSJeremy L Thompson     CeedCheck(len >= len_required, ceed, CEED_ERROR_BACKEND,
141111870feSJeremy L Thompson               "Vector at points must be padded to the same number of points in each element for BasisApplyAtPoints on GPU backends."
142111870feSJeremy L Thompson               " Found %" CeedSize_FMT ", Required %" CeedSize_FMT,
143111870feSJeremy L Thompson               len, len_required);
144111870feSJeremy L Thompson   }
145111870feSJeremy L Thompson 
146111870feSJeremy L Thompson   // Move num_points array to device
147111870feSJeremy L Thompson   if (is_transpose) {
148111870feSJeremy L Thompson     const CeedInt num_bytes = num_elem * sizeof(CeedInt);
149111870feSJeremy L Thompson 
150111870feSJeremy L Thompson     if (num_elem != data->num_elem_at_points) {
151111870feSJeremy L Thompson       data->num_elem_at_points = num_elem;
152111870feSJeremy L Thompson 
153111870feSJeremy L Thompson       if (data->d_points_per_elem) CeedCallHip(ceed, hipFree(data->d_points_per_elem));
154111870feSJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&data->d_points_per_elem, num_bytes));
155111870feSJeremy L Thompson       CeedCallBackend(CeedFree(&data->h_points_per_elem));
156111870feSJeremy L Thompson       CeedCallBackend(CeedCalloc(num_elem, &data->h_points_per_elem));
157111870feSJeremy L Thompson     }
1589e511c80SJeremy L Thompson     if (memcmp(data->h_points_per_elem, num_points, num_bytes)) {
159111870feSJeremy L Thompson       memcpy(data->h_points_per_elem, num_points, num_bytes);
160111870feSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(data->d_points_per_elem, num_points, num_bytes, hipMemcpyHostToDevice));
161111870feSJeremy L Thompson     }
162111870feSJeremy L Thompson   }
163111870feSJeremy L Thompson 
1641c21e869SJeremy L Thompson   // Build kernels if needed
1651c21e869SJeremy L Thompson   if (data->num_points != max_num_points) {
1661c21e869SJeremy L Thompson     CeedInt P_1d;
1671c21e869SJeremy L Thompson 
1681c21e869SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
1691c21e869SJeremy L Thompson     data->num_points = max_num_points;
1701c21e869SJeremy L Thompson 
1711c21e869SJeremy L Thompson     // -- Create interp matrix to Chebyshev coefficients
1721c21e869SJeremy L Thompson     if (!data->d_chebyshev_interp_1d) {
1731c21e869SJeremy L Thompson       CeedSize    interp_bytes;
1741c21e869SJeremy L Thompson       CeedScalar *chebyshev_interp_1d;
1751c21e869SJeremy L Thompson 
1761c21e869SJeremy L Thompson       interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
1771c21e869SJeremy L Thompson       CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
1785a5594ffSJeremy L Thompson       CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
1791c21e869SJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&data->d_chebyshev_interp_1d, interp_bytes));
1801c21e869SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
1811c21e869SJeremy L Thompson       CeedCallBackend(CeedFree(&chebyshev_interp_1d));
1821c21e869SJeremy L Thompson     }
1831c21e869SJeremy L Thompson 
1841c21e869SJeremy L Thompson     // -- Compile kernels
1859c25dd66SJeremy L Thompson     const char basis_kernel_source[] = "// AtPoints basis source\n#include <ceed/jit-source/hip/hip-ref-basis-tensor-at-points.h>\n";
1861c21e869SJeremy L Thompson     CeedInt    num_comp;
1871c21e869SJeremy L Thompson 
1881c21e869SJeremy L Thompson     if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
1891c21e869SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
1901c21e869SJeremy 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",
191f7c9815fSJeremy L Thompson                                     Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
1921c21e869SJeremy L Thompson                                     "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim), "BASIS_NUM_PTS",
193f7c9815fSJeremy L Thompson                                     max_num_points, "POINTS_BUFF_LEN", CeedIntPow(Q_1d, dim - 1)));
1941c21e869SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "InterpAtPoints", &data->InterpAtPoints));
195*81ae6159SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "InterpTransposeAtPoints", &data->InterpTransposeAtPoints));
1961c21e869SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "GradAtPoints", &data->GradAtPoints));
197*81ae6159SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "GradTransposeAtPoints", &data->GradTransposeAtPoints));
1981c21e869SJeremy L Thompson   }
1991c21e869SJeremy L Thompson 
2001c21e869SJeremy L Thompson   // Get read/write access to u, v
2011c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(x_ref, CEED_MEM_DEVICE, &d_x));
2021c21e869SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
2031c21e869SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
204db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
205db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
2061c21e869SJeremy L Thompson 
2071c21e869SJeremy L Thompson   // Clear v for transpose operation
208db2becc9SJeremy L Thompson   if (is_transpose && !apply_add) {
20919a04db8SJeremy L Thompson     CeedInt  num_comp, q_comp, num_nodes;
2101c21e869SJeremy L Thompson     CeedSize length;
2111c21e869SJeremy L Thompson 
21219a04db8SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
21319a04db8SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
21419a04db8SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
21519a04db8SJeremy L Thompson     length =
21619a04db8SJeremy L Thompson         (CeedSize)num_elem * (CeedSize)num_comp * (t_mode == CEED_TRANSPOSE ? (CeedSize)num_nodes : ((CeedSize)max_num_points * (CeedSize)q_comp));
2171c21e869SJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
2181c21e869SJeremy L Thompson   }
2191c21e869SJeremy L Thompson 
2201c21e869SJeremy L Thompson   // Basis action
2211c21e869SJeremy L Thompson   switch (eval_mode) {
2221c21e869SJeremy L Thompson     case CEED_EVAL_INTERP: {
223*81ae6159SJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, &data->d_chebyshev_interp_1d, &data->d_points_per_elem, &d_x, &d_u, &d_v};
2241c21e869SJeremy L Thompson       const CeedInt block_size    = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
2251c21e869SJeremy L Thompson 
226*81ae6159SJeremy L Thompson       CeedCallBackend(
227*81ae6159SJeremy L Thompson           CeedRunKernel_Hip(ceed, is_transpose ? data->InterpTransposeAtPoints : data->InterpAtPoints, num_elem, block_size, interp_args));
2281c21e869SJeremy L Thompson     } break;
2291c21e869SJeremy L Thompson     case CEED_EVAL_GRAD: {
230*81ae6159SJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, &data->d_chebyshev_interp_1d, &data->d_points_per_elem, &d_x, &d_u, &d_v};
2312d10e82cSJeremy L Thompson       const CeedInt block_size  = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
2321c21e869SJeremy L Thompson 
233*81ae6159SJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, is_transpose ? data->GradTransposeAtPoints : data->GradAtPoints, num_elem, block_size, grad_args));
2341c21e869SJeremy L Thompson     } break;
2351c21e869SJeremy L Thompson     case CEED_EVAL_WEIGHT:
2361c21e869SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
2371c21e869SJeremy L Thompson       break;
2381c21e869SJeremy L Thompson     // LCOV_EXCL_START
2391c21e869SJeremy L Thompson     case CEED_EVAL_DIV:
2401c21e869SJeremy L Thompson     case CEED_EVAL_CURL:
2411c21e869SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
2421c21e869SJeremy L Thompson       // LCOV_EXCL_STOP
2431c21e869SJeremy L Thompson   }
2441c21e869SJeremy L Thompson 
2451c21e869SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
2461c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(x_ref, &d_x));
2471c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2481c21e869SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
2491c21e869SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
2509bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
2511c21e869SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2521c21e869SJeremy L Thompson }
2531c21e869SJeremy L Thompson 
254db2becc9SJeremy L Thompson static int CeedBasisApplyAtPoints_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, false, 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 
260db2becc9SJeremy L Thompson static int CeedBasisApplyAddAtPoints_Hip(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
261db2becc9SJeremy L Thompson                                          CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
262db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyAtPointsCore_Hip(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
263db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
264db2becc9SJeremy L Thompson }
265db2becc9SJeremy L Thompson 
2661c21e869SJeremy L Thompson //------------------------------------------------------------------------------
2670d0321e0SJeremy L Thompson // Basis apply - non-tensor
2680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
269db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensorCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
270db2becc9SJeremy L Thompson                                            CeedVector u, CeedVector v) {
2710d0321e0SJeremy L Thompson   Ceed                    ceed;
272437930d1SJeremy L Thompson   CeedInt                 num_nodes, num_qpts;
2737bbbfca3SJeremy L Thompson   const CeedInt           is_transpose    = t_mode == CEED_TRANSPOSE;
274d075f50bSSebastian Grimberg   const int               elems_per_block = 1;
275d075f50bSSebastian Grimberg   const int               grid            = CeedDivUpInt(num_elem, elems_per_block);
2760d0321e0SJeremy L Thompson   const CeedScalar       *d_u;
2770d0321e0SJeremy L Thompson   CeedScalar             *d_v;
278b7453713SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
279b7453713SJeremy L Thompson 
280b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
281b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
282b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
283b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
284b7453713SJeremy L Thompson 
2859ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
2869ea2cfd9SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
2879ea2cfd9SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
288db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
289db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
2900d0321e0SJeremy L Thompson 
2910d0321e0SJeremy L Thompson   // Clear v for transpose operation
292db2becc9SJeremy L Thompson   if (is_transpose && !apply_add) {
2931f9221feSJeremy L Thompson     CeedSize length;
294b7453713SJeremy L Thompson 
2952b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
2962b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
2970d0321e0SJeremy L Thompson   }
2980d0321e0SJeremy L Thompson 
2990d0321e0SJeremy L Thompson   // Apply basis operation
300437930d1SJeremy L Thompson   switch (eval_mode) {
3010d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
302d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
3037bbbfca3SJeremy L Thompson       const int block_size_x  = is_transpose ? num_nodes : num_qpts;
304b2165e7aSSebastian Grimberg 
3057bbbfca3SJeremy L Thompson       if (is_transpose) {
306d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
307d075f50bSSebastian Grimberg       } else {
308b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
309d075f50bSSebastian Grimberg       }
3100d0321e0SJeremy L Thompson     } break;
3110d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
312d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
3137bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
314b2165e7aSSebastian Grimberg 
3157bbbfca3SJeremy L Thompson       if (is_transpose) {
316d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
317d075f50bSSebastian Grimberg       } else {
318d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
319d075f50bSSebastian Grimberg       }
320d075f50bSSebastian Grimberg     } break;
321d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
322d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
3237bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
324d075f50bSSebastian Grimberg 
3257bbbfca3SJeremy L Thompson       if (is_transpose) {
326d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
327d075f50bSSebastian Grimberg       } else {
328d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
329d075f50bSSebastian Grimberg       }
330d075f50bSSebastian Grimberg     } break;
331d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
332d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
3337bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
334d075f50bSSebastian Grimberg 
3357bbbfca3SJeremy L Thompson       if (is_transpose) {
336d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
337d075f50bSSebastian Grimberg       } else {
338d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
339d075f50bSSebastian Grimberg       }
3400d0321e0SJeremy L Thompson     } break;
3410d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
342097cc795SJames Wright       CeedCheck(data->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights not set", CeedEvalModes[eval_mode]);
343437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
344b2165e7aSSebastian Grimberg 
345b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
3460d0321e0SJeremy L Thompson     } break;
3479ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
3489ea2cfd9SJeremy L Thompson       break;
3490d0321e0SJeremy L Thompson   }
3500d0321e0SJeremy L Thompson 
3519ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
3522b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
3539ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
3549ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
3559bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
3560d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3570d0321e0SJeremy L Thompson }
3580d0321e0SJeremy L Thompson 
359db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
360db2becc9SJeremy L Thompson                                        CeedVector v) {
361db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, false, num_elem, t_mode, eval_mode, u, v));
362db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
363db2becc9SJeremy L Thompson }
364db2becc9SJeremy L Thompson 
365db2becc9SJeremy L Thompson static int CeedBasisApplyAddNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
366db2becc9SJeremy L Thompson                                           CeedVector v) {
367db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, true, num_elem, t_mode, eval_mode, u, v));
368db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
369db2becc9SJeremy L Thompson }
370db2becc9SJeremy L Thompson 
3710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3720d0321e0SJeremy L Thompson // Destroy tensor basis
3730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3740d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
3750d0321e0SJeremy L Thompson   Ceed           ceed;
3760d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
377b7453713SJeremy L Thompson 
378b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3792b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
3802b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
3811c21e869SJeremy L Thompson   if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
382097cc795SJames Wright   if (data->d_q_weight_1d) CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
383111870feSJeremy L Thompson   CeedCallBackend(CeedFree(&data->h_points_per_elem));
384111870feSJeremy L Thompson   if (data->d_points_per_elem) CeedCallHip(ceed, hipFree(data->d_points_per_elem));
3852b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
3862b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
3871c21e869SJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_chebyshev_interp_1d));
3882b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
3899bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
3900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3910d0321e0SJeremy L Thompson }
3920d0321e0SJeremy L Thompson 
3930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3940d0321e0SJeremy L Thompson // Destroy non-tensor basis
3950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3960d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
3970d0321e0SJeremy L Thompson   Ceed                    ceed;
3980d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
399b7453713SJeremy L Thompson 
400b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4012b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
4022b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
403097cc795SJames Wright   if (data->d_q_weight) CeedCallHip(ceed, hipFree(data->d_q_weight));
4042b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
4052b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
406d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
407d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
4082b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
4099bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
4100d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4110d0321e0SJeremy L Thompson }
4120d0321e0SJeremy L Thompson 
4130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4140d0321e0SJeremy L Thompson // Create tensor
4150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4162b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
4176574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
4180d0321e0SJeremy L Thompson   Ceed           ceed;
419b7453713SJeremy L Thompson   CeedInt        num_comp;
420b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
421b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
4220d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
423b7453713SJeremy L Thompson 
424b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4252b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
4260d0321e0SJeremy L Thompson 
4270d0321e0SJeremy L Thompson   // Copy data to GPU
428097cc795SJames Wright   if (q_weight_1d) {
4292b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
4302b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
431097cc795SJames Wright   }
4322b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
4332b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
4342b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
4352b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
4360d0321e0SJeremy L Thompson 
437ecc88aebSJeremy L Thompson   // Compile basis kernels
4389c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Tensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-tensor.h>\n";
4399c25dd66SJeremy L Thompson 
440b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
441eb7e6cafSJeremy 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",
442f7c9815fSJeremy L Thompson                                   Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
443b7453713SJeremy L Thompson                                   "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
444eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
445eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
446eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
447437930d1SJeremy L Thompson 
4482b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4490d0321e0SJeremy L Thompson 
450d075f50bSSebastian Grimberg   // Register backend functions
4512b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
452db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Hip));
4531c21e869SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip));
454db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Hip));
4552b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
4569bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
4570d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4580d0321e0SJeremy L Thompson }
4590d0321e0SJeremy L Thompson 
4600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
461d075f50bSSebastian Grimberg // Create non-tensor H^1
4620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4632b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
46451475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
4650d0321e0SJeremy L Thompson   Ceed                    ceed;
466d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
467b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
4680d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
469b7453713SJeremy L Thompson 
470b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4712b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
4720d0321e0SJeremy L Thompson 
4730d0321e0SJeremy L Thompson   // Copy basis data to GPU
474d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
475d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
476097cc795SJames Wright   if (q_weight) {
4772b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
4782b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
479097cc795SJames Wright   }
480d075f50bSSebastian Grimberg   if (interp) {
481d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
482d075f50bSSebastian Grimberg 
4832b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
4842b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
485d075f50bSSebastian Grimberg   }
486d075f50bSSebastian Grimberg   if (grad) {
487d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
488d075f50bSSebastian Grimberg 
4892b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
4902b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
491d075f50bSSebastian Grimberg   }
4920d0321e0SJeremy L Thompson 
4930d0321e0SJeremy L Thompson   // Compile basis kernels
4949c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n";
4959c25dd66SJeremy L Thompson 
496b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
497d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
498d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp));
499eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
500d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
501d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
502d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
503eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
504d075f50bSSebastian Grimberg 
505d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
506d075f50bSSebastian Grimberg 
507d075f50bSSebastian Grimberg   // Register backend functions
508d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
509db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
510d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
5119bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
512d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
513d075f50bSSebastian Grimberg }
514d075f50bSSebastian Grimberg 
515d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
516d075f50bSSebastian Grimberg // Create non-tensor H(div)
517d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
518d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
519d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
520d075f50bSSebastian Grimberg   Ceed                    ceed;
521d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
522d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
523d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
524d075f50bSSebastian Grimberg 
525d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
526d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
527d075f50bSSebastian Grimberg 
528d075f50bSSebastian Grimberg   // Copy basis data to GPU
529d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
530d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
531097cc795SJames Wright   if (q_weight) {
532d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
533d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
534097cc795SJames Wright   }
535d075f50bSSebastian Grimberg   if (interp) {
536d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
537d075f50bSSebastian Grimberg 
538d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
539d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
540d075f50bSSebastian Grimberg   }
541d075f50bSSebastian Grimberg   if (div) {
542d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
543d075f50bSSebastian Grimberg 
544d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
545d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
546d075f50bSSebastian Grimberg   }
547d075f50bSSebastian Grimberg 
548d075f50bSSebastian Grimberg   // Compile basis kernels
5499c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n";
5509c25dd66SJeremy L Thompson 
551d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
552d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
553d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
554d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
555d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
556d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
557d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
558d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
559d075f50bSSebastian Grimberg 
560d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
561d075f50bSSebastian Grimberg 
562d075f50bSSebastian Grimberg   // Register backend functions
563d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
564db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
565d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
5669bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
567d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
568d075f50bSSebastian Grimberg }
569d075f50bSSebastian Grimberg 
570d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
571d075f50bSSebastian Grimberg // Create non-tensor H(curl)
572d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
573d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
574d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
575d075f50bSSebastian Grimberg   Ceed                    ceed;
576d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
577d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
578d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
579d075f50bSSebastian Grimberg 
580d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
581d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
582d075f50bSSebastian Grimberg 
583d075f50bSSebastian Grimberg   // Copy basis data to GPU
584d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
585d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
586097cc795SJames Wright   if (q_weight) {
587d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
588d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
589097cc795SJames Wright   }
590d075f50bSSebastian Grimberg   if (interp) {
591d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
592d075f50bSSebastian Grimberg 
593d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
594d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
595d075f50bSSebastian Grimberg   }
596d075f50bSSebastian Grimberg   if (curl) {
597d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
598d075f50bSSebastian Grimberg 
599d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
600d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
601d075f50bSSebastian Grimberg   }
602d075f50bSSebastian Grimberg 
603d075f50bSSebastian Grimberg   // Compile basis kernels
6049c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n";
6059c25dd66SJeremy L Thompson 
606d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
607d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
608d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
609d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
610d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
611d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
612d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
613d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
614d075f50bSSebastian Grimberg 
6152b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
6160d0321e0SJeremy L Thompson 
6170d0321e0SJeremy L Thompson   // Register backend functions
6182b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
619db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
6202b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
6219bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
6220d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6230d0321e0SJeremy L Thompson }
6242a86cc9dSSebastian Grimberg 
6250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
626