xref: /libCEED/backends/hip-ref/ceed-hip-ref-basis.c (revision f7c9815f6bdd2aac16e9c7ed574a2a61404669c3)
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>
110d0321e0SJeremy L Thompson #include <hip/hip_runtime.h>
122b730f8bSJeremy L Thompson 
1349aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
140d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
152b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
160d0321e0SJeremy L Thompson 
170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180d0321e0SJeremy L Thompson // Basis apply - tensor
190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
202b730f8bSJeremy L Thompson int CeedBasisApply_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
210d0321e0SJeremy L Thompson   Ceed              ceed;
22b7453713SJeremy L Thompson   CeedInt           Q_1d, dim;
237bbbfca3SJeremy L Thompson   const CeedInt     is_transpose   = t_mode == CEED_TRANSPOSE;
24437930d1SJeremy L Thompson   const int         max_block_size = 64;
250d0321e0SJeremy L Thompson   const CeedScalar *d_u;
260d0321e0SJeremy L Thompson   CeedScalar       *d_v;
27b7453713SJeremy L Thompson   CeedBasis_Hip    *data;
28b7453713SJeremy L Thompson 
29b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
30b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
31b7453713SJeremy L Thompson 
329ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
336574a04fSJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
346574a04fSJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
352b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
360d0321e0SJeremy L Thompson 
370d0321e0SJeremy L Thompson   // Clear v for transpose operation
387bbbfca3SJeremy L Thompson   if (is_transpose) {
391f9221feSJeremy L Thompson     CeedSize length;
40b7453713SJeremy L Thompson 
412b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
422b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
430d0321e0SJeremy L Thompson   }
44b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
45b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
460d0321e0SJeremy L Thompson 
470d0321e0SJeremy L Thompson   // Basis action
48437930d1SJeremy L Thompson   switch (eval_mode) {
490d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
507bbbfca3SJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &d_u, &d_v};
51b2165e7aSSebastian Grimberg       const CeedInt block_size    = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
520d0321e0SJeremy L Thompson 
53eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->Interp, num_elem, block_size, interp_args));
540d0321e0SJeremy L Thompson     } break;
550d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
567bbbfca3SJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &data->d_grad_1d, &d_u, &d_v};
57b2165e7aSSebastian Grimberg       const CeedInt block_size  = max_block_size;
580d0321e0SJeremy L Thompson 
59eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->Grad, num_elem, block_size, grad_args));
600d0321e0SJeremy L Thompson     } break;
610d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
62097cc795SJames Wright       CeedCheck(data->d_q_weight_1d, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights_1d not set", CeedEvalModes[eval_mode]);
63437930d1SJeremy L Thompson       void     *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v};
64b2165e7aSSebastian Grimberg       const int block_size_x  = Q_1d;
65b2165e7aSSebastian Grimberg       const int block_size_y  = dim >= 2 ? Q_1d : 1;
660d0321e0SJeremy L Thompson 
67b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, num_elem, block_size_x, block_size_y, 1, weight_args));
680d0321e0SJeremy L Thompson     } break;
699ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
709ea2cfd9SJeremy L Thompson       break;
710d0321e0SJeremy L Thompson     // LCOV_EXCL_START
720d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
730d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
74bcbe1c99SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
750d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
760d0321e0SJeremy L Thompson   }
770d0321e0SJeremy L Thompson 
789ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
792b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
809ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
819ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
820d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
830d0321e0SJeremy L Thompson }
840d0321e0SJeremy L Thompson 
850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
861c21e869SJeremy L Thompson // Basis apply - tensor AtPoints
871c21e869SJeremy L Thompson //------------------------------------------------------------------------------
881c21e869SJeremy L Thompson int CeedBasisApplyAtPoints_Hip(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
891c21e869SJeremy L Thompson                                CeedVector x_ref, CeedVector u, CeedVector v) {
901c21e869SJeremy L Thompson   Ceed              ceed;
911c21e869SJeremy L Thompson   CeedInt           Q_1d, dim, max_num_points = num_points[0];
921c21e869SJeremy L Thompson   const CeedInt     is_transpose   = t_mode == CEED_TRANSPOSE;
931c21e869SJeremy L Thompson   const int         max_block_size = 32;
941c21e869SJeremy L Thompson   const CeedScalar *d_x, *d_u;
951c21e869SJeremy L Thompson   CeedScalar       *d_v;
961c21e869SJeremy L Thompson   CeedBasis_Hip    *data;
971c21e869SJeremy L Thompson 
981c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
991c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1001c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1011c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
1021c21e869SJeremy L Thompson 
1031c21e869SJeremy L Thompson   // Check uniform number of points per elem
1041c21e869SJeremy L Thompson   for (CeedInt i = 1; i < num_elem; i++) {
1051c21e869SJeremy L Thompson     CeedCheck(max_num_points == num_points[i], ceed, CEED_ERROR_BACKEND,
1061c21e869SJeremy L Thompson               "BasisApplyAtPoints only supported for the same number of points in each element");
1071c21e869SJeremy L Thompson   }
1081c21e869SJeremy L Thompson 
1091c21e869SJeremy L Thompson   // Weight handled separately
1101c21e869SJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
1111c21e869SJeremy L Thompson     CeedCall(CeedVectorSetValue(v, 1.0));
1121c21e869SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1131c21e869SJeremy L Thompson   }
1141c21e869SJeremy L Thompson 
1151c21e869SJeremy L Thompson   // Build kernels if needed
1161c21e869SJeremy L Thompson   if (data->num_points != max_num_points) {
1171c21e869SJeremy L Thompson     CeedInt P_1d;
1181c21e869SJeremy L Thompson 
1191c21e869SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
1201c21e869SJeremy L Thompson     data->num_points = max_num_points;
1211c21e869SJeremy L Thompson 
1221c21e869SJeremy L Thompson     // -- Create interp matrix to Chebyshev coefficients
1231c21e869SJeremy L Thompson     if (!data->d_chebyshev_interp_1d) {
1241c21e869SJeremy L Thompson       CeedSize    interp_bytes;
1251c21e869SJeremy L Thompson       CeedScalar *chebyshev_interp_1d;
1261c21e869SJeremy L Thompson 
1271c21e869SJeremy L Thompson       interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
1281c21e869SJeremy L Thompson       CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
1291c21e869SJeremy L Thompson       CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
1301c21e869SJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&data->d_chebyshev_interp_1d, interp_bytes));
1311c21e869SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
1321c21e869SJeremy L Thompson       CeedCallBackend(CeedFree(&chebyshev_interp_1d));
1331c21e869SJeremy L Thompson     }
1341c21e869SJeremy L Thompson 
1351c21e869SJeremy L Thompson     // -- Compile kernels
1361c21e869SJeremy L Thompson     char       *basis_kernel_source;
1371c21e869SJeremy L Thompson     const char *basis_kernel_path;
1381c21e869SJeremy L Thompson     CeedInt     num_comp;
1391c21e869SJeremy L Thompson 
1401c21e869SJeremy L Thompson     if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
1411c21e869SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
1421c21e869SJeremy L Thompson     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor-at-points.h", &basis_kernel_path));
1431c21e869SJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
1441c21e869SJeremy L Thompson     CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
1451c21e869SJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
1461c21e869SJeremy 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",
147*f7c9815fSJeremy L Thompson                                     Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
1481c21e869SJeremy L Thompson                                     "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim), "BASIS_NUM_PTS",
149*f7c9815fSJeremy L Thompson                                     max_num_points, "POINTS_BUFF_LEN", CeedIntPow(Q_1d, dim - 1)));
1501c21e869SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "InterpAtPoints", &data->InterpAtPoints));
1511c21e869SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "GradAtPoints", &data->GradAtPoints));
1521c21e869SJeremy L Thompson     CeedCallBackend(CeedFree(&basis_kernel_path));
1531c21e869SJeremy L Thompson     CeedCallBackend(CeedFree(&basis_kernel_source));
1541c21e869SJeremy L Thompson   }
1551c21e869SJeremy L Thompson 
1561c21e869SJeremy L Thompson   // Get read/write access to u, v
1571c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(x_ref, CEED_MEM_DEVICE, &d_x));
1581c21e869SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
1591c21e869SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
1601c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1611c21e869SJeremy L Thompson 
1621c21e869SJeremy L Thompson   // Clear v for transpose operation
1631c21e869SJeremy L Thompson   if (is_transpose) {
1641c21e869SJeremy L Thompson     CeedSize length;
1651c21e869SJeremy L Thompson 
1661c21e869SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
1671c21e869SJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
1681c21e869SJeremy L Thompson   }
1691c21e869SJeremy L Thompson 
1701c21e869SJeremy L Thompson   // Basis action
1711c21e869SJeremy L Thompson   switch (eval_mode) {
1721c21e869SJeremy L Thompson     case CEED_EVAL_INTERP: {
1731c21e869SJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &d_x, &d_u, &d_v};
1741c21e869SJeremy L Thompson       const CeedInt block_size    = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
1751c21e869SJeremy L Thompson 
1761c21e869SJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->InterpAtPoints, num_elem, block_size, interp_args));
1771c21e869SJeremy L Thompson     } break;
1781c21e869SJeremy L Thompson     case CEED_EVAL_GRAD: {
1791c21e869SJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &d_x, &d_u, &d_v};
1802d10e82cSJeremy L Thompson       const CeedInt block_size  = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
1811c21e869SJeremy L Thompson 
1821c21e869SJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->GradAtPoints, num_elem, block_size, grad_args));
1831c21e869SJeremy L Thompson     } break;
1841c21e869SJeremy L Thompson     case CEED_EVAL_WEIGHT:
1851c21e869SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
1861c21e869SJeremy L Thompson       break;
1871c21e869SJeremy L Thompson     // LCOV_EXCL_START
1881c21e869SJeremy L Thompson     case CEED_EVAL_DIV:
1891c21e869SJeremy L Thompson     case CEED_EVAL_CURL:
1901c21e869SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
1911c21e869SJeremy L Thompson       // LCOV_EXCL_STOP
1921c21e869SJeremy L Thompson   }
1931c21e869SJeremy L Thompson 
1941c21e869SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
1951c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(x_ref, &d_x));
1961c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
1971c21e869SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
1981c21e869SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
1991c21e869SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2001c21e869SJeremy L Thompson }
2011c21e869SJeremy L Thompson 
2021c21e869SJeremy L Thompson //------------------------------------------------------------------------------
2030d0321e0SJeremy L Thompson // Basis apply - non-tensor
2040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2052b730f8bSJeremy L Thompson int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
2062b730f8bSJeremy L Thompson                                 CeedVector v) {
2070d0321e0SJeremy L Thompson   Ceed                    ceed;
208437930d1SJeremy L Thompson   CeedInt                 num_nodes, num_qpts;
2097bbbfca3SJeremy L Thompson   const CeedInt           is_transpose    = t_mode == CEED_TRANSPOSE;
210d075f50bSSebastian Grimberg   const int               elems_per_block = 1;
211d075f50bSSebastian Grimberg   const int               grid            = CeedDivUpInt(num_elem, elems_per_block);
2120d0321e0SJeremy L Thompson   const CeedScalar       *d_u;
2130d0321e0SJeremy L Thompson   CeedScalar             *d_v;
214b7453713SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
215b7453713SJeremy L Thompson 
216b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
217b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
218b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
219b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
220b7453713SJeremy L Thompson 
2219ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
2229ea2cfd9SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
2239ea2cfd9SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
2242b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
2250d0321e0SJeremy L Thompson 
2260d0321e0SJeremy L Thompson   // Clear v for transpose operation
2277bbbfca3SJeremy L Thompson   if (is_transpose) {
2281f9221feSJeremy L Thompson     CeedSize length;
229b7453713SJeremy L Thompson 
2302b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
2312b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
2320d0321e0SJeremy L Thompson   }
2330d0321e0SJeremy L Thompson 
2340d0321e0SJeremy L Thompson   // Apply basis operation
235437930d1SJeremy L Thompson   switch (eval_mode) {
2360d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
237d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
2387bbbfca3SJeremy L Thompson       const int block_size_x  = is_transpose ? num_nodes : num_qpts;
239b2165e7aSSebastian Grimberg 
2407bbbfca3SJeremy L Thompson       if (is_transpose) {
241d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
242d075f50bSSebastian Grimberg       } else {
243b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
244d075f50bSSebastian Grimberg       }
2450d0321e0SJeremy L Thompson     } break;
2460d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
247d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
2487bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
249b2165e7aSSebastian Grimberg 
2507bbbfca3SJeremy L Thompson       if (is_transpose) {
251d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
252d075f50bSSebastian Grimberg       } else {
253d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
254d075f50bSSebastian Grimberg       }
255d075f50bSSebastian Grimberg     } break;
256d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
257d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
2587bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
259d075f50bSSebastian Grimberg 
2607bbbfca3SJeremy L Thompson       if (is_transpose) {
261d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
262d075f50bSSebastian Grimberg       } else {
263d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
264d075f50bSSebastian Grimberg       }
265d075f50bSSebastian Grimberg     } break;
266d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
267d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
2687bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
269d075f50bSSebastian Grimberg 
2707bbbfca3SJeremy L Thompson       if (is_transpose) {
271d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
272d075f50bSSebastian Grimberg       } else {
273d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
274d075f50bSSebastian Grimberg       }
2750d0321e0SJeremy L Thompson     } break;
2760d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
277097cc795SJames Wright       CeedCheck(data->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights not set", CeedEvalModes[eval_mode]);
278437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
279b2165e7aSSebastian Grimberg 
280b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
2810d0321e0SJeremy L Thompson     } break;
2829ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
2839ea2cfd9SJeremy L Thompson       break;
2840d0321e0SJeremy L Thompson   }
2850d0321e0SJeremy L Thompson 
2869ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
2872b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2889ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
2899ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
2900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2910d0321e0SJeremy L Thompson }
2920d0321e0SJeremy L Thompson 
2930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2940d0321e0SJeremy L Thompson // Destroy tensor basis
2950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2960d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
2970d0321e0SJeremy L Thompson   Ceed           ceed;
2980d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
299b7453713SJeremy L Thompson 
300b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3012b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
3022b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
3031c21e869SJeremy L Thompson   if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
304097cc795SJames Wright   if (data->d_q_weight_1d) CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
3052b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
3062b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
3071c21e869SJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_chebyshev_interp_1d));
3082b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
3090d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3100d0321e0SJeremy L Thompson }
3110d0321e0SJeremy L Thompson 
3120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3130d0321e0SJeremy L Thompson // Destroy non-tensor basis
3140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3150d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
3160d0321e0SJeremy L Thompson   Ceed                    ceed;
3170d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
318b7453713SJeremy L Thompson 
319b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3202b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
3212b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
322097cc795SJames Wright   if (data->d_q_weight) CeedCallHip(ceed, hipFree(data->d_q_weight));
3232b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
3242b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
325d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
326d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
3272b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
3280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3290d0321e0SJeremy L Thompson }
3300d0321e0SJeremy L Thompson 
3310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3320d0321e0SJeremy L Thompson // Create tensor
3330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3342b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
3356574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
3360d0321e0SJeremy L Thompson   Ceed           ceed;
33722070f95SJeremy L Thompson   char          *basis_kernel_source;
33822070f95SJeremy L Thompson   const char    *basis_kernel_path;
339b7453713SJeremy L Thompson   CeedInt        num_comp;
340b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
341b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
3420d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
343b7453713SJeremy L Thompson 
344b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3452b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
3460d0321e0SJeremy L Thompson 
3470d0321e0SJeremy L Thompson   // Copy data to GPU
348097cc795SJames Wright   if (q_weight_1d) {
3492b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
3502b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
351097cc795SJames Wright   }
3522b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
3532b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
3542b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
3552b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
3560d0321e0SJeremy L Thompson 
357ecc88aebSJeremy L Thompson   // Compile basis kernels
358b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
3592b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor.h", &basis_kernel_path));
36023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
3612b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
36223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
363eb7e6cafSJeremy 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",
364*f7c9815fSJeremy L Thompson                                   Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
365b7453713SJeremy L Thompson                                   "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
366eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
367eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
368eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
3692b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
3702b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
371437930d1SJeremy L Thompson 
3722b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
3730d0321e0SJeremy L Thompson 
374d075f50bSSebastian Grimberg   // Register backend functions
3752b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
3761c21e869SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip));
3772b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
3780d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3790d0321e0SJeremy L Thompson }
3800d0321e0SJeremy L Thompson 
3810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
382d075f50bSSebastian Grimberg // Create non-tensor H^1
3830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3842b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
38551475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
3860d0321e0SJeremy L Thompson   Ceed                    ceed;
38722070f95SJeremy L Thompson   char                   *basis_kernel_source;
38822070f95SJeremy L Thompson   const char             *basis_kernel_path;
389d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
390b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
3910d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
392b7453713SJeremy L Thompson 
393b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3942b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
3950d0321e0SJeremy L Thompson 
3960d0321e0SJeremy L Thompson   // Copy basis data to GPU
397d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
398d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
399097cc795SJames Wright   if (q_weight) {
4002b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
4012b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
402097cc795SJames Wright   }
403d075f50bSSebastian Grimberg   if (interp) {
404d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
405d075f50bSSebastian Grimberg 
4062b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
4072b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
408d075f50bSSebastian Grimberg   }
409d075f50bSSebastian Grimberg   if (grad) {
410d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
411d075f50bSSebastian Grimberg 
4122b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
4132b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
414d075f50bSSebastian Grimberg   }
4150d0321e0SJeremy L Thompson 
4160d0321e0SJeremy L Thompson   // Compile basis kernels
417b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
4182b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
41923d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
4202b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
42123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
422d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
423d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp));
424eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
425d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
426d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
427d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
428eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
4292b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
4302b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
431d075f50bSSebastian Grimberg 
432d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
433d075f50bSSebastian Grimberg 
434d075f50bSSebastian Grimberg   // Register backend functions
435d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
436d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
437d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
438d075f50bSSebastian Grimberg }
439d075f50bSSebastian Grimberg 
440d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
441d075f50bSSebastian Grimberg // Create non-tensor H(div)
442d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
443d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
444d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
445d075f50bSSebastian Grimberg   Ceed                    ceed;
44622070f95SJeremy L Thompson   char                   *basis_kernel_source;
44722070f95SJeremy L Thompson   const char             *basis_kernel_path;
448d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
449d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
450d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
451d075f50bSSebastian Grimberg 
452d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
453d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
454d075f50bSSebastian Grimberg 
455d075f50bSSebastian Grimberg   // Copy basis data to GPU
456d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
457d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
458097cc795SJames Wright   if (q_weight) {
459d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
460d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
461097cc795SJames Wright   }
462d075f50bSSebastian Grimberg   if (interp) {
463d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
464d075f50bSSebastian Grimberg 
465d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
466d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
467d075f50bSSebastian Grimberg   }
468d075f50bSSebastian Grimberg   if (div) {
469d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
470d075f50bSSebastian Grimberg 
471d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
472d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
473d075f50bSSebastian Grimberg   }
474d075f50bSSebastian Grimberg 
475d075f50bSSebastian Grimberg   // Compile basis kernels
476d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
477d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
478d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
479d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
480d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
481d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
482d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
483d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
484d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
485d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
486d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
487d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
488d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
489d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
490d075f50bSSebastian Grimberg 
491d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
492d075f50bSSebastian Grimberg 
493d075f50bSSebastian Grimberg   // Register backend functions
494d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
495d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
496d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
497d075f50bSSebastian Grimberg }
498d075f50bSSebastian Grimberg 
499d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
500d075f50bSSebastian Grimberg // Create non-tensor H(curl)
501d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
502d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
503d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
504d075f50bSSebastian Grimberg   Ceed                    ceed;
50522070f95SJeremy L Thompson   char                   *basis_kernel_source;
50622070f95SJeremy L Thompson   const char             *basis_kernel_path;
507d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
508d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
509d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
510d075f50bSSebastian Grimberg 
511d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
512d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
513d075f50bSSebastian Grimberg 
514d075f50bSSebastian Grimberg   // Copy basis data to GPU
515d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
516d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
517097cc795SJames Wright   if (q_weight) {
518d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
519d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
520097cc795SJames Wright   }
521d075f50bSSebastian Grimberg   if (interp) {
522d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
523d075f50bSSebastian Grimberg 
524d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
525d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
526d075f50bSSebastian Grimberg   }
527d075f50bSSebastian Grimberg   if (curl) {
528d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
529d075f50bSSebastian Grimberg 
530d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
531d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
532d075f50bSSebastian Grimberg   }
533d075f50bSSebastian Grimberg 
534d075f50bSSebastian Grimberg   // Compile basis kernels
535d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
536d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
537d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
538d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
539d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
540d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
541d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
542d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
543d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
544d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
545d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
546d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
547d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
548d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
549d075f50bSSebastian Grimberg 
5502b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
5510d0321e0SJeremy L Thompson 
5520d0321e0SJeremy L Thompson   // Register backend functions
5532b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
5542b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
5550d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5560d0321e0SJeremy L Thompson }
5572a86cc9dSSebastian Grimberg 
5580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
559