xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-basis.c (revision 9e511c80210b4447ceabc14bc1a44d55934e0fbf)
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) {
421f9221feSJeremy L Thompson     CeedSize length;
43b7453713SJeremy L Thompson 
442b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
452b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
460d0321e0SJeremy L Thompson   }
47b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
48b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
490d0321e0SJeremy L Thompson 
500d0321e0SJeremy L Thompson   // Basis action
51437930d1SJeremy L Thompson   switch (eval_mode) {
520d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
537bbbfca3SJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &d_u, &d_v};
54b2165e7aSSebastian Grimberg       const CeedInt block_size    = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
550d0321e0SJeremy L Thompson 
56eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->Interp, num_elem, block_size, interp_args));
570d0321e0SJeremy L Thompson     } break;
580d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
597bbbfca3SJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &data->d_grad_1d, &d_u, &d_v};
60b2165e7aSSebastian Grimberg       const CeedInt block_size  = max_block_size;
610d0321e0SJeremy L Thompson 
62eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->Grad, num_elem, block_size, grad_args));
630d0321e0SJeremy L Thompson     } break;
640d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
65097cc795SJames Wright       CeedCheck(data->d_q_weight_1d, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights_1d not set", CeedEvalModes[eval_mode]);
66437930d1SJeremy L Thompson       void     *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v};
67b2165e7aSSebastian Grimberg       const int block_size_x  = Q_1d;
68b2165e7aSSebastian Grimberg       const int block_size_y  = dim >= 2 ? Q_1d : 1;
690d0321e0SJeremy L Thompson 
70b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, num_elem, block_size_x, block_size_y, 1, weight_args));
710d0321e0SJeremy L Thompson     } break;
729ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
739ea2cfd9SJeremy L Thompson       break;
740d0321e0SJeremy L Thompson     // LCOV_EXCL_START
750d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
760d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
77bcbe1c99SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
780d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
790d0321e0SJeremy L Thompson   }
800d0321e0SJeremy L Thompson 
819ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
822b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
839ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
849ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
850d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
860d0321e0SJeremy L Thompson }
870d0321e0SJeremy L Thompson 
88db2becc9SJeremy L Thompson static int CeedBasisApply_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
89db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyCore_Hip(basis, false, num_elem, t_mode, eval_mode, u, v));
90db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
91db2becc9SJeremy L Thompson }
92db2becc9SJeremy L Thompson 
93db2becc9SJeremy L Thompson static int CeedBasisApplyAdd_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
94db2becc9SJeremy L Thompson                                  CeedVector v) {
95db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyCore_Hip(basis, true, num_elem, t_mode, eval_mode, u, v));
96db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
97db2becc9SJeremy L Thompson }
98db2becc9SJeremy L Thompson 
990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1001c21e869SJeremy L Thompson // Basis apply - tensor AtPoints
1011c21e869SJeremy L Thompson //------------------------------------------------------------------------------
102db2becc9SJeremy L Thompson static int CeedBasisApplyAtPointsCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, const CeedInt *num_points,
103db2becc9SJeremy L Thompson                                           CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
1041c21e869SJeremy L Thompson   Ceed              ceed;
1051c21e869SJeremy L Thompson   CeedInt           Q_1d, dim, max_num_points = num_points[0];
1061c21e869SJeremy L Thompson   const CeedInt     is_transpose   = t_mode == CEED_TRANSPOSE;
1071c21e869SJeremy L Thompson   const int         max_block_size = 32;
1081c21e869SJeremy L Thompson   const CeedScalar *d_x, *d_u;
1091c21e869SJeremy L Thompson   CeedScalar       *d_v;
1101c21e869SJeremy L Thompson   CeedBasis_Hip    *data;
1111c21e869SJeremy L Thompson 
1121c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1131c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1141c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1151c21e869SJeremy L Thompson   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
1161c21e869SJeremy L Thompson 
1171c21e869SJeremy L Thompson   // Weight handled separately
1181c21e869SJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
1195a5594ffSJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(v, 1.0));
1201c21e869SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1211c21e869SJeremy L Thompson   }
1221c21e869SJeremy L Thompson 
123111870feSJeremy L Thompson   // Check padded to uniform number of points per elem
124111870feSJeremy L Thompson   for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]);
125111870feSJeremy L Thompson   {
126111870feSJeremy L Thompson     CeedInt  num_comp, q_comp;
127111870feSJeremy L Thompson     CeedSize len, len_required;
128111870feSJeremy L Thompson 
129111870feSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
130111870feSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
131111870feSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(is_transpose ? u : v, &len));
132111870feSJeremy L Thompson     len_required = (CeedSize)num_comp * (CeedSize)q_comp * (CeedSize)num_elem * (CeedSize)max_num_points;
133111870feSJeremy L Thompson     CeedCheck(len >= len_required, ceed, CEED_ERROR_BACKEND,
134111870feSJeremy L Thompson               "Vector at points must be padded to the same number of points in each element for BasisApplyAtPoints on GPU backends."
135111870feSJeremy L Thompson               " Found %" CeedSize_FMT ", Required %" CeedSize_FMT,
136111870feSJeremy L Thompson               len, len_required);
137111870feSJeremy L Thompson   }
138111870feSJeremy L Thompson 
139111870feSJeremy L Thompson   // Move num_points array to device
140111870feSJeremy L Thompson   if (is_transpose) {
141111870feSJeremy L Thompson     const CeedInt num_bytes = num_elem * sizeof(CeedInt);
142111870feSJeremy L Thompson 
143111870feSJeremy L Thompson     if (num_elem != data->num_elem_at_points) {
144111870feSJeremy L Thompson       data->num_elem_at_points = num_elem;
145111870feSJeremy L Thompson 
146111870feSJeremy L Thompson       if (data->d_points_per_elem) CeedCallHip(ceed, hipFree(data->d_points_per_elem));
147111870feSJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&data->d_points_per_elem, num_bytes));
148111870feSJeremy L Thompson       CeedCallBackend(CeedFree(&data->h_points_per_elem));
149111870feSJeremy L Thompson       CeedCallBackend(CeedCalloc(num_elem, &data->h_points_per_elem));
150111870feSJeremy L Thompson     }
151*9e511c80SJeremy L Thompson     if (memcmp(data->h_points_per_elem, num_points, num_bytes)) {
152111870feSJeremy L Thompson       memcpy(data->h_points_per_elem, num_points, num_bytes);
153111870feSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(data->d_points_per_elem, num_points, num_bytes, hipMemcpyHostToDevice));
154111870feSJeremy L Thompson     }
155111870feSJeremy L Thompson   }
156111870feSJeremy L Thompson 
1571c21e869SJeremy L Thompson   // Build kernels if needed
1581c21e869SJeremy L Thompson   if (data->num_points != max_num_points) {
1591c21e869SJeremy L Thompson     CeedInt P_1d;
1601c21e869SJeremy L Thompson 
1611c21e869SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
1621c21e869SJeremy L Thompson     data->num_points = max_num_points;
1631c21e869SJeremy L Thompson 
1641c21e869SJeremy L Thompson     // -- Create interp matrix to Chebyshev coefficients
1651c21e869SJeremy L Thompson     if (!data->d_chebyshev_interp_1d) {
1661c21e869SJeremy L Thompson       CeedSize    interp_bytes;
1671c21e869SJeremy L Thompson       CeedScalar *chebyshev_interp_1d;
1681c21e869SJeremy L Thompson 
1691c21e869SJeremy L Thompson       interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
1701c21e869SJeremy L Thompson       CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
1715a5594ffSJeremy L Thompson       CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
1721c21e869SJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&data->d_chebyshev_interp_1d, interp_bytes));
1731c21e869SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
1741c21e869SJeremy L Thompson       CeedCallBackend(CeedFree(&chebyshev_interp_1d));
1751c21e869SJeremy L Thompson     }
1761c21e869SJeremy L Thompson 
1771c21e869SJeremy L Thompson     // -- Compile kernels
1781c21e869SJeremy L Thompson     char       *basis_kernel_source;
1791c21e869SJeremy L Thompson     const char *basis_kernel_path;
1801c21e869SJeremy L Thompson     CeedInt     num_comp;
1811c21e869SJeremy L Thompson 
1821c21e869SJeremy L Thompson     if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
1831c21e869SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
1841c21e869SJeremy L Thompson     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor-at-points.h", &basis_kernel_path));
1851c21e869SJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
1861c21e869SJeremy L Thompson     CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
1871c21e869SJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
1881c21e869SJeremy L Thompson     CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->moduleAtPoints, 9, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "BASIS_BUF_LEN",
189f7c9815fSJeremy L Thompson                                     Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
1901c21e869SJeremy L Thompson                                     "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim), "BASIS_NUM_PTS",
191f7c9815fSJeremy L Thompson                                     max_num_points, "POINTS_BUFF_LEN", CeedIntPow(Q_1d, dim - 1)));
1921c21e869SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "InterpAtPoints", &data->InterpAtPoints));
1931c21e869SJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "GradAtPoints", &data->GradAtPoints));
1941c21e869SJeremy L Thompson     CeedCallBackend(CeedFree(&basis_kernel_path));
1951c21e869SJeremy L Thompson     CeedCallBackend(CeedFree(&basis_kernel_source));
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) {
2071c21e869SJeremy L Thompson     CeedSize length;
2081c21e869SJeremy L Thompson 
2091c21e869SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
2101c21e869SJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
2111c21e869SJeremy L Thompson   }
2121c21e869SJeremy L Thompson 
2131c21e869SJeremy L Thompson   // Basis action
2141c21e869SJeremy L Thompson   switch (eval_mode) {
2151c21e869SJeremy L Thompson     case CEED_EVAL_INTERP: {
216111870feSJeremy 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};
2171c21e869SJeremy L Thompson       const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
2181c21e869SJeremy L Thompson 
2191c21e869SJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->InterpAtPoints, num_elem, block_size, interp_args));
2201c21e869SJeremy L Thompson     } break;
2211c21e869SJeremy L Thompson     case CEED_EVAL_GRAD: {
222111870feSJeremy 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};
2232d10e82cSJeremy L Thompson       const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
2241c21e869SJeremy L Thompson 
2251c21e869SJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->GradAtPoints, num_elem, block_size, grad_args));
2261c21e869SJeremy L Thompson     } break;
2271c21e869SJeremy L Thompson     case CEED_EVAL_WEIGHT:
2281c21e869SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
2291c21e869SJeremy L Thompson       break;
2301c21e869SJeremy L Thompson     // LCOV_EXCL_START
2311c21e869SJeremy L Thompson     case CEED_EVAL_DIV:
2321c21e869SJeremy L Thompson     case CEED_EVAL_CURL:
2331c21e869SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
2341c21e869SJeremy L Thompson       // LCOV_EXCL_STOP
2351c21e869SJeremy L Thompson   }
2361c21e869SJeremy L Thompson 
2371c21e869SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
2381c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(x_ref, &d_x));
2391c21e869SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2401c21e869SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
2411c21e869SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
2421c21e869SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2431c21e869SJeremy L Thompson }
2441c21e869SJeremy L Thompson 
245db2becc9SJeremy L Thompson static int CeedBasisApplyAtPoints_Hip(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
246db2becc9SJeremy L Thompson                                       CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
247db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyAtPointsCore_Hip(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
248db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
249db2becc9SJeremy L Thompson }
250db2becc9SJeremy L Thompson 
251db2becc9SJeremy L Thompson static int CeedBasisApplyAddAtPoints_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, true, 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 
2571c21e869SJeremy L Thompson //------------------------------------------------------------------------------
2580d0321e0SJeremy L Thompson // Basis apply - non-tensor
2590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
260db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensorCore_Hip(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
261db2becc9SJeremy L Thompson                                            CeedVector u, CeedVector v) {
2620d0321e0SJeremy L Thompson   Ceed                    ceed;
263437930d1SJeremy L Thompson   CeedInt                 num_nodes, num_qpts;
2647bbbfca3SJeremy L Thompson   const CeedInt           is_transpose    = t_mode == CEED_TRANSPOSE;
265d075f50bSSebastian Grimberg   const int               elems_per_block = 1;
266d075f50bSSebastian Grimberg   const int               grid            = CeedDivUpInt(num_elem, elems_per_block);
2670d0321e0SJeremy L Thompson   const CeedScalar       *d_u;
2680d0321e0SJeremy L Thompson   CeedScalar             *d_v;
269b7453713SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
270b7453713SJeremy L Thompson 
271b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
272b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
273b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
274b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
275b7453713SJeremy L Thompson 
2769ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
2779ea2cfd9SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
2789ea2cfd9SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
279db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
280db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
2810d0321e0SJeremy L Thompson 
2820d0321e0SJeremy L Thompson   // Clear v for transpose operation
283db2becc9SJeremy L Thompson   if (is_transpose && !apply_add) {
2841f9221feSJeremy L Thompson     CeedSize length;
285b7453713SJeremy L Thompson 
2862b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
2872b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
2880d0321e0SJeremy L Thompson   }
2890d0321e0SJeremy L Thompson 
2900d0321e0SJeremy L Thompson   // Apply basis operation
291437930d1SJeremy L Thompson   switch (eval_mode) {
2920d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
293d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
2947bbbfca3SJeremy L Thompson       const int block_size_x  = is_transpose ? num_nodes : num_qpts;
295b2165e7aSSebastian Grimberg 
2967bbbfca3SJeremy L Thompson       if (is_transpose) {
297d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
298d075f50bSSebastian Grimberg       } else {
299b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
300d075f50bSSebastian Grimberg       }
3010d0321e0SJeremy L Thompson     } break;
3020d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
303d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
3047bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
305b2165e7aSSebastian Grimberg 
3067bbbfca3SJeremy L Thompson       if (is_transpose) {
307d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
308d075f50bSSebastian Grimberg       } else {
309d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
310d075f50bSSebastian Grimberg       }
311d075f50bSSebastian Grimberg     } break;
312d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
313d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
3147bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
315d075f50bSSebastian Grimberg 
3167bbbfca3SJeremy L Thompson       if (is_transpose) {
317d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
318d075f50bSSebastian Grimberg       } else {
319d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
320d075f50bSSebastian Grimberg       }
321d075f50bSSebastian Grimberg     } break;
322d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
323d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
3247bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
325d075f50bSSebastian Grimberg 
3267bbbfca3SJeremy L Thompson       if (is_transpose) {
327d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
328d075f50bSSebastian Grimberg       } else {
329d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
330d075f50bSSebastian Grimberg       }
3310d0321e0SJeremy L Thompson     } break;
3320d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
333097cc795SJames Wright       CeedCheck(data->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights not set", CeedEvalModes[eval_mode]);
334437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
335b2165e7aSSebastian Grimberg 
336b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
3370d0321e0SJeremy L Thompson     } break;
3389ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
3399ea2cfd9SJeremy L Thompson       break;
3400d0321e0SJeremy L Thompson   }
3410d0321e0SJeremy L Thompson 
3429ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
3432b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
3449ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
3459ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
3460d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3470d0321e0SJeremy L Thompson }
3480d0321e0SJeremy L Thompson 
349db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
350db2becc9SJeremy L Thompson                                        CeedVector v) {
351db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, false, num_elem, t_mode, eval_mode, u, v));
352db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
353db2becc9SJeremy L Thompson }
354db2becc9SJeremy L Thompson 
355db2becc9SJeremy L Thompson static int CeedBasisApplyAddNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
356db2becc9SJeremy L Thompson                                           CeedVector v) {
357db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Hip(basis, true, num_elem, t_mode, eval_mode, u, v));
358db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
359db2becc9SJeremy L Thompson }
360db2becc9SJeremy L Thompson 
3610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3620d0321e0SJeremy L Thompson // Destroy tensor basis
3630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3640d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
3650d0321e0SJeremy L Thompson   Ceed           ceed;
3660d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
367b7453713SJeremy L Thompson 
368b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3692b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
3702b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
3711c21e869SJeremy L Thompson   if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
372097cc795SJames Wright   if (data->d_q_weight_1d) CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
373111870feSJeremy L Thompson   CeedCallBackend(CeedFree(&data->h_points_per_elem));
374111870feSJeremy L Thompson   if (data->d_points_per_elem) CeedCallHip(ceed, hipFree(data->d_points_per_elem));
3752b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
3762b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
3771c21e869SJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_chebyshev_interp_1d));
3782b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
3790d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3800d0321e0SJeremy L Thompson }
3810d0321e0SJeremy L Thompson 
3820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3830d0321e0SJeremy L Thompson // Destroy non-tensor basis
3840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3850d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
3860d0321e0SJeremy L Thompson   Ceed                    ceed;
3870d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
388b7453713SJeremy L Thompson 
389b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
3902b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
3912b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
392097cc795SJames Wright   if (data->d_q_weight) CeedCallHip(ceed, hipFree(data->d_q_weight));
3932b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
3942b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
395d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
396d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
3972b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
3980d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3990d0321e0SJeremy L Thompson }
4000d0321e0SJeremy L Thompson 
4010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4020d0321e0SJeremy L Thompson // Create tensor
4030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4042b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
4056574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
4060d0321e0SJeremy L Thompson   Ceed           ceed;
40722070f95SJeremy L Thompson   char          *basis_kernel_source;
40822070f95SJeremy L Thompson   const char    *basis_kernel_path;
409b7453713SJeremy L Thompson   CeedInt        num_comp;
410b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
411b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
4120d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
413b7453713SJeremy L Thompson 
414b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4152b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
4160d0321e0SJeremy L Thompson 
4170d0321e0SJeremy L Thompson   // Copy data to GPU
418097cc795SJames Wright   if (q_weight_1d) {
4192b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
4202b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
421097cc795SJames Wright   }
4222b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
4232b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
4242b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
4252b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
4260d0321e0SJeremy L Thompson 
427ecc88aebSJeremy L Thompson   // Compile basis kernels
428b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
4292b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor.h", &basis_kernel_path));
43023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
4312b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
43223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
433eb7e6cafSJeremy 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",
434f7c9815fSJeremy L Thompson                                   Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
435b7453713SJeremy L Thompson                                   "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
436eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
437eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
438eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
4392b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
4402b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
441437930d1SJeremy L Thompson 
4422b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4430d0321e0SJeremy L Thompson 
444d075f50bSSebastian Grimberg   // Register backend functions
4452b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
446db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Hip));
4471c21e869SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip));
448db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Hip));
4492b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
4500d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4510d0321e0SJeremy L Thompson }
4520d0321e0SJeremy L Thompson 
4530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
454d075f50bSSebastian Grimberg // Create non-tensor H^1
4550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4562b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
45751475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
4580d0321e0SJeremy L Thompson   Ceed                    ceed;
45922070f95SJeremy L Thompson   char                   *basis_kernel_source;
46022070f95SJeremy L Thompson   const char             *basis_kernel_path;
461d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
462b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
4630d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
464b7453713SJeremy L Thompson 
465b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4662b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
4670d0321e0SJeremy L Thompson 
4680d0321e0SJeremy L Thompson   // Copy basis data to GPU
469d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
470d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
471097cc795SJames Wright   if (q_weight) {
4722b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
4732b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
474097cc795SJames Wright   }
475d075f50bSSebastian Grimberg   if (interp) {
476d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
477d075f50bSSebastian Grimberg 
4782b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
4792b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
480d075f50bSSebastian Grimberg   }
481d075f50bSSebastian Grimberg   if (grad) {
482d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
483d075f50bSSebastian Grimberg 
4842b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
4852b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
486d075f50bSSebastian Grimberg   }
4870d0321e0SJeremy L Thompson 
4880d0321e0SJeremy L Thompson   // Compile basis kernels
489b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
4902b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
49123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
4922b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
49323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
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));
5012b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
5022b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
503d075f50bSSebastian Grimberg 
504d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
505d075f50bSSebastian Grimberg 
506d075f50bSSebastian Grimberg   // Register backend functions
507d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
508db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
509d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
510d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
511d075f50bSSebastian Grimberg }
512d075f50bSSebastian Grimberg 
513d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
514d075f50bSSebastian Grimberg // Create non-tensor H(div)
515d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
516d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
517d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
518d075f50bSSebastian Grimberg   Ceed                    ceed;
51922070f95SJeremy L Thompson   char                   *basis_kernel_source;
52022070f95SJeremy L Thompson   const char             *basis_kernel_path;
521d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
522d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
523d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
524d075f50bSSebastian Grimberg 
525d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
526d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
527d075f50bSSebastian Grimberg 
528d075f50bSSebastian Grimberg   // Copy basis data to GPU
529d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
530d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
531097cc795SJames Wright   if (q_weight) {
532d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
533d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
534097cc795SJames Wright   }
535d075f50bSSebastian Grimberg   if (interp) {
536d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
537d075f50bSSebastian Grimberg 
538d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
539d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
540d075f50bSSebastian Grimberg   }
541d075f50bSSebastian Grimberg   if (div) {
542d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
543d075f50bSSebastian Grimberg 
544d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
545d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
546d075f50bSSebastian Grimberg   }
547d075f50bSSebastian Grimberg 
548d075f50bSSebastian Grimberg   // Compile basis kernels
549d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
550d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
551d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
552d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
553d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
554d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
555d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
556d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
557d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
558d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
559d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
560d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
561d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
562d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
563d075f50bSSebastian Grimberg 
564d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
565d075f50bSSebastian Grimberg 
566d075f50bSSebastian Grimberg   // Register backend functions
567d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
568db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
569d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
570d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
571d075f50bSSebastian Grimberg }
572d075f50bSSebastian Grimberg 
573d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
574d075f50bSSebastian Grimberg // Create non-tensor H(curl)
575d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
576d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
577d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
578d075f50bSSebastian Grimberg   Ceed                    ceed;
57922070f95SJeremy L Thompson   char                   *basis_kernel_source;
58022070f95SJeremy L Thompson   const char             *basis_kernel_path;
581d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
582d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
583d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
584d075f50bSSebastian Grimberg 
585d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
586d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
587d075f50bSSebastian Grimberg 
588d075f50bSSebastian Grimberg   // Copy basis data to GPU
589d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
590d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
591097cc795SJames Wright   if (q_weight) {
592d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
593d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
594097cc795SJames Wright   }
595d075f50bSSebastian Grimberg   if (interp) {
596d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
597d075f50bSSebastian Grimberg 
598d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
599d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
600d075f50bSSebastian Grimberg   }
601d075f50bSSebastian Grimberg   if (curl) {
602d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
603d075f50bSSebastian Grimberg 
604d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
605d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
606d075f50bSSebastian Grimberg   }
607d075f50bSSebastian Grimberg 
608d075f50bSSebastian Grimberg   // Compile basis kernels
609d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
610d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
611d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
612d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
613d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
614d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
615d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
616d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
617d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
618d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
619d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
620d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
621d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
622d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
623d075f50bSSebastian Grimberg 
6242b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
6250d0321e0SJeremy L Thompson 
6260d0321e0SJeremy L Thompson   // Register backend functions
6272b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
628db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip));
6292b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
6300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6310d0321e0SJeremy L Thompson }
6322a86cc9dSSebastian Grimberg 
6330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
634