xref: /libCEED/backends/hip-ref/ceed-hip-ref-basis.c (revision 9bc663991d6482bcb1d60b1f116148f11db83fa1)
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));
90*9bc66399SJeremy 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 
128*9bc66399SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
129*9bc66399SJeremy 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));
1951c21e869SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "GradAtPoints", &data->GradAtPoints));
1961c21e869SJeremy L Thompson   }
1971c21e869SJeremy L Thompson 
1981c21e869SJeremy L Thompson   // Get read/write access to u, v
1991c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(x_ref, CEED_MEM_DEVICE, &d_x));
2001c21e869SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
2011c21e869SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
202db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
203db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
2041c21e869SJeremy L Thompson 
2051c21e869SJeremy L Thompson   // Clear v for transpose operation
206db2becc9SJeremy L Thompson   if (is_transpose && !apply_add) {
20719a04db8SJeremy L Thompson     CeedInt  num_comp, q_comp, num_nodes;
2081c21e869SJeremy L Thompson     CeedSize length;
2091c21e869SJeremy L Thompson 
21019a04db8SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
21119a04db8SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
21219a04db8SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
21319a04db8SJeremy L Thompson     length =
21419a04db8SJeremy L Thompson         (CeedSize)num_elem * (CeedSize)num_comp * (t_mode == CEED_TRANSPOSE ? (CeedSize)num_nodes : ((CeedSize)max_num_points * (CeedSize)q_comp));
2151c21e869SJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
2161c21e869SJeremy L Thompson   }
2171c21e869SJeremy L Thompson 
2181c21e869SJeremy L Thompson   // Basis action
2191c21e869SJeremy L Thompson   switch (eval_mode) {
2201c21e869SJeremy L Thompson     case CEED_EVAL_INTERP: {
221111870feSJeremy 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};
2221c21e869SJeremy L Thompson       const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
2231c21e869SJeremy L Thompson 
2241c21e869SJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->InterpAtPoints, num_elem, block_size, interp_args));
2251c21e869SJeremy L Thompson     } break;
2261c21e869SJeremy L Thompson     case CEED_EVAL_GRAD: {
227111870feSJeremy 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};
2282d10e82cSJeremy L Thompson       const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
2291c21e869SJeremy L Thompson 
2301c21e869SJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->GradAtPoints, num_elem, block_size, grad_args));
2311c21e869SJeremy L Thompson     } break;
2321c21e869SJeremy L Thompson     case CEED_EVAL_WEIGHT:
2331c21e869SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
2341c21e869SJeremy L Thompson       break;
2351c21e869SJeremy L Thompson     // LCOV_EXCL_START
2361c21e869SJeremy L Thompson     case CEED_EVAL_DIV:
2371c21e869SJeremy L Thompson     case CEED_EVAL_CURL:
2381c21e869SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
2391c21e869SJeremy L Thompson       // LCOV_EXCL_STOP
2401c21e869SJeremy L Thompson   }
2411c21e869SJeremy L Thompson 
2421c21e869SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
2431c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(x_ref, &d_x));
2441c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2451c21e869SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
2461c21e869SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
247*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
2481c21e869SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2491c21e869SJeremy L Thompson }
2501c21e869SJeremy L Thompson 
251db2becc9SJeremy L Thompson static int CeedBasisApplyAtPoints_Hip(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
252db2becc9SJeremy L Thompson                                       CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
253db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyAtPointsCore_Hip(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
254db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
255db2becc9SJeremy L Thompson }
256db2becc9SJeremy L Thompson 
257db2becc9SJeremy L Thompson static int CeedBasisApplyAddAtPoints_Hip(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
258db2becc9SJeremy L Thompson                                          CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
259db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyAtPointsCore_Hip(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
260db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
261db2becc9SJeremy L Thompson }
262db2becc9SJeremy L Thompson 
2631c21e869SJeremy L Thompson //------------------------------------------------------------------------------
2640d0321e0SJeremy L Thompson // Basis apply - non-tensor
2650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
266db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensorCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
267db2becc9SJeremy L Thompson                                            CeedVector u, CeedVector v) {
2680d0321e0SJeremy L Thompson   Ceed                    ceed;
269437930d1SJeremy L Thompson   CeedInt                 num_nodes, num_qpts;
2707bbbfca3SJeremy L Thompson   const CeedInt           is_transpose    = t_mode == CEED_TRANSPOSE;
271d075f50bSSebastian Grimberg   const int               elems_per_block = 1;
272d075f50bSSebastian Grimberg   const int               grid            = CeedDivUpInt(num_elem, elems_per_block);
2730d0321e0SJeremy L Thompson   const CeedScalar       *d_u;
2740d0321e0SJeremy L Thompson   CeedScalar             *d_v;
275b7453713SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
276b7453713SJeremy L Thompson 
277b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
278b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
279b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
280b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
281b7453713SJeremy L Thompson 
2829ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
2839ea2cfd9SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
2849ea2cfd9SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
285db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
286db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
2870d0321e0SJeremy L Thompson 
2880d0321e0SJeremy L Thompson   // Clear v for transpose operation
289db2becc9SJeremy L Thompson   if (is_transpose && !apply_add) {
2901f9221feSJeremy L Thompson     CeedSize length;
291b7453713SJeremy L Thompson 
2922b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
2932b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
2940d0321e0SJeremy L Thompson   }
2950d0321e0SJeremy L Thompson 
2960d0321e0SJeremy L Thompson   // Apply basis operation
297437930d1SJeremy L Thompson   switch (eval_mode) {
2980d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
299d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
3007bbbfca3SJeremy L Thompson       const int block_size_x  = is_transpose ? num_nodes : num_qpts;
301b2165e7aSSebastian Grimberg 
3027bbbfca3SJeremy L Thompson       if (is_transpose) {
303d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
304d075f50bSSebastian Grimberg       } else {
305b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
306d075f50bSSebastian Grimberg       }
3070d0321e0SJeremy L Thompson     } break;
3080d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
309d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
3107bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
311b2165e7aSSebastian Grimberg 
3127bbbfca3SJeremy L Thompson       if (is_transpose) {
313d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
314d075f50bSSebastian Grimberg       } else {
315d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
316d075f50bSSebastian Grimberg       }
317d075f50bSSebastian Grimberg     } break;
318d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
319d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
3207bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
321d075f50bSSebastian Grimberg 
3227bbbfca3SJeremy L Thompson       if (is_transpose) {
323d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
324d075f50bSSebastian Grimberg       } else {
325d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
326d075f50bSSebastian Grimberg       }
327d075f50bSSebastian Grimberg     } break;
328d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
329d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
3307bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
331d075f50bSSebastian Grimberg 
3327bbbfca3SJeremy L Thompson       if (is_transpose) {
333d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
334d075f50bSSebastian Grimberg       } else {
335d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
336d075f50bSSebastian Grimberg       }
3370d0321e0SJeremy L Thompson     } break;
3380d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
339097cc795SJames Wright       CeedCheck(data->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights not set", CeedEvalModes[eval_mode]);
340437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
341b2165e7aSSebastian Grimberg 
342b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
3430d0321e0SJeremy L Thompson     } break;
3449ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
3459ea2cfd9SJeremy L Thompson       break;
3460d0321e0SJeremy L Thompson   }
3470d0321e0SJeremy L Thompson 
3489ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
3492b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
3509ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
3519ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
352*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
3530d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3540d0321e0SJeremy L Thompson }
3550d0321e0SJeremy L Thompson 
356db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
357db2becc9SJeremy L Thompson                                        CeedVector v) {
358db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, false, num_elem, t_mode, eval_mode, u, v));
359db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
360db2becc9SJeremy L Thompson }
361db2becc9SJeremy L Thompson 
362db2becc9SJeremy L Thompson static int CeedBasisApplyAddNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
363db2becc9SJeremy L Thompson                                           CeedVector v) {
364db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, true, num_elem, t_mode, eval_mode, u, v));
365db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
366db2becc9SJeremy L Thompson }
367db2becc9SJeremy L Thompson 
3680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3690d0321e0SJeremy L Thompson // Destroy tensor basis
3700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3710d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
3720d0321e0SJeremy L Thompson   Ceed           ceed;
3730d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
374b7453713SJeremy L Thompson 
375b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3762b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
3772b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
3781c21e869SJeremy L Thompson   if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
379097cc795SJames Wright   if (data->d_q_weight_1d) CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
380111870feSJeremy L Thompson   CeedCallBackend(CeedFree(&data->h_points_per_elem));
381111870feSJeremy L Thompson   if (data->d_points_per_elem) CeedCallHip(ceed, hipFree(data->d_points_per_elem));
3822b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
3832b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
3841c21e869SJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_chebyshev_interp_1d));
3852b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
386*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
3870d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3880d0321e0SJeremy L Thompson }
3890d0321e0SJeremy L Thompson 
3900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3910d0321e0SJeremy L Thompson // Destroy non-tensor basis
3920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3930d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
3940d0321e0SJeremy L Thompson   Ceed                    ceed;
3950d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
396b7453713SJeremy L Thompson 
397b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3982b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
3992b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
400097cc795SJames Wright   if (data->d_q_weight) CeedCallHip(ceed, hipFree(data->d_q_weight));
4012b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
4022b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
403d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
404d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
4052b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
406*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
4070d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4080d0321e0SJeremy L Thompson }
4090d0321e0SJeremy L Thompson 
4100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4110d0321e0SJeremy L Thompson // Create tensor
4120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4132b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
4146574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
4150d0321e0SJeremy L Thompson   Ceed           ceed;
416b7453713SJeremy L Thompson   CeedInt        num_comp;
417b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
418b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
4190d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
420b7453713SJeremy L Thompson 
421b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4222b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
4230d0321e0SJeremy L Thompson 
4240d0321e0SJeremy L Thompson   // Copy data to GPU
425097cc795SJames Wright   if (q_weight_1d) {
4262b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
4272b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
428097cc795SJames Wright   }
4292b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
4302b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
4312b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
4322b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
4330d0321e0SJeremy L Thompson 
434ecc88aebSJeremy L Thompson   // Compile basis kernels
4359c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Tensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-tensor.h>\n";
4369c25dd66SJeremy L Thompson 
437b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
438eb7e6cafSJeremy 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",
439f7c9815fSJeremy L Thompson                                   Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
440b7453713SJeremy L Thompson                                   "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
441eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
442eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
443eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
444437930d1SJeremy L Thompson 
4452b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4460d0321e0SJeremy L Thompson 
447d075f50bSSebastian Grimberg   // Register backend functions
4482b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
449db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Hip));
4501c21e869SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip));
451db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Hip));
4522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
453*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
4540d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4550d0321e0SJeremy L Thompson }
4560d0321e0SJeremy L Thompson 
4570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
458d075f50bSSebastian Grimberg // Create non-tensor H^1
4590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4602b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
46151475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
4620d0321e0SJeremy L Thompson   Ceed                    ceed;
463d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
464b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
4650d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
466b7453713SJeremy L Thompson 
467b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4682b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
4690d0321e0SJeremy L Thompson 
4700d0321e0SJeremy L Thompson   // Copy basis data to GPU
471d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
472d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
473097cc795SJames Wright   if (q_weight) {
4742b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
4752b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
476097cc795SJames Wright   }
477d075f50bSSebastian Grimberg   if (interp) {
478d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
479d075f50bSSebastian Grimberg 
4802b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
4812b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
482d075f50bSSebastian Grimberg   }
483d075f50bSSebastian Grimberg   if (grad) {
484d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
485d075f50bSSebastian Grimberg 
4862b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
4872b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
488d075f50bSSebastian Grimberg   }
4890d0321e0SJeremy L Thompson 
4900d0321e0SJeremy L Thompson   // Compile basis kernels
4919c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n";
4929c25dd66SJeremy L Thompson 
493b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
494d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
495d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp));
496eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
497d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
498d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
499d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
500eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
501d075f50bSSebastian Grimberg 
502d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
503d075f50bSSebastian Grimberg 
504d075f50bSSebastian Grimberg   // Register backend functions
505d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
506db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
507d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
508*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
509d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
510d075f50bSSebastian Grimberg }
511d075f50bSSebastian Grimberg 
512d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
513d075f50bSSebastian Grimberg // Create non-tensor H(div)
514d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
515d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
516d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
517d075f50bSSebastian Grimberg   Ceed                    ceed;
518d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
519d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
520d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
521d075f50bSSebastian Grimberg 
522d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
523d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
524d075f50bSSebastian Grimberg 
525d075f50bSSebastian Grimberg   // Copy basis data to GPU
526d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
527d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
528097cc795SJames Wright   if (q_weight) {
529d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
530d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
531097cc795SJames Wright   }
532d075f50bSSebastian Grimberg   if (interp) {
533d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
534d075f50bSSebastian Grimberg 
535d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
536d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
537d075f50bSSebastian Grimberg   }
538d075f50bSSebastian Grimberg   if (div) {
539d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
540d075f50bSSebastian Grimberg 
541d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
542d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
543d075f50bSSebastian Grimberg   }
544d075f50bSSebastian Grimberg 
545d075f50bSSebastian Grimberg   // Compile basis kernels
5469c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n";
5479c25dd66SJeremy L Thompson 
548d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
549d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
550d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
551d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
552d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
553d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
554d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
555d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
556d075f50bSSebastian Grimberg 
557d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
558d075f50bSSebastian Grimberg 
559d075f50bSSebastian Grimberg   // Register backend functions
560d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
561db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
562d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
563*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
564d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
565d075f50bSSebastian Grimberg }
566d075f50bSSebastian Grimberg 
567d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
568d075f50bSSebastian Grimberg // Create non-tensor H(curl)
569d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
570d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
571d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
572d075f50bSSebastian Grimberg   Ceed                    ceed;
573d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
574d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
575d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
576d075f50bSSebastian Grimberg 
577d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
578d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
579d075f50bSSebastian Grimberg 
580d075f50bSSebastian Grimberg   // Copy basis data to GPU
581d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
582d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
583097cc795SJames Wright   if (q_weight) {
584d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
585d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
586097cc795SJames Wright   }
587d075f50bSSebastian Grimberg   if (interp) {
588d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
589d075f50bSSebastian Grimberg 
590d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
591d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
592d075f50bSSebastian Grimberg   }
593d075f50bSSebastian Grimberg   if (curl) {
594d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
595d075f50bSSebastian Grimberg 
596d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
597d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
598d075f50bSSebastian Grimberg   }
599d075f50bSSebastian Grimberg 
600d075f50bSSebastian Grimberg   // Compile basis kernels
6019c25dd66SJeremy L Thompson   const char basis_kernel_source[] = "// Nontensor basis source\n#include <ceed/jit-source/hip/hip-ref-basis-nontensor.h>\n";
6029c25dd66SJeremy L Thompson 
603d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
604d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
605d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
606d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
607d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
608d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
609d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
610d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
611d075f50bSSebastian Grimberg 
6122b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
6130d0321e0SJeremy L Thompson 
6140d0321e0SJeremy L Thompson   // Register backend functions
6152b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
616db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
6172b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
618*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
6190d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6200d0321e0SJeremy L Thompson }
6212a86cc9dSSebastian Grimberg 
6220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
623